33
44 +-------------------------------------------------------------+
55 | OneLoneCoder Pixel Game Engine Extension |
6- | Transformed View v1.08 |
6+ | Transformed View v1.09 |
77 +-------------------------------------------------------------+
88
99 NOTE: UNDER ACTIVE DEVELOPMENT - THERE ARE BUGS/GLITCHES
1818 License (OLC-3)
1919 ~~~~~~~~~~~~~~~
2020
21- Copyright 2018 - 2022 OneLoneCoder.com
21+ Copyright 2018 - 2024 OneLoneCoder.com
2222
2323 Redistribution and use in source and binary forms, with or without
2424 modification, are permitted provided that the following conditions
5959
6060 Author
6161 ~~~~~~
62- David Barr, aka javidx9, ©OneLoneCoder 2019, 2020, 2021, 2022
62+ David Barr, aka javidx9, ©OneLoneCoder 2019, 2020, 2021, 2022, 2023, 2024
6363
6464 Revisions:
6565 1.00: Initial Release
7474 1.07: +DrawRectDecal()
7575 +GetPGE()
7676 1.08: +DrawPolygonDecal() with tint overload, akin to PGE
77+ 1.09: +SetScaleExtents() - Sets range that world scale can exist within
78+ +EnableScaleClamp() - Applies a range that scaling is clamped to
79+ These are both useful for having zoom clamped between a min and max
80+ without weird panning artefacts occuring
7781*/
7882
7983#pragma once
@@ -116,6 +120,9 @@ namespace olc
116120 virtual bool IsPointVisible (const olc::vf2d& vPos) const ;
117121 virtual bool IsRectVisible (const olc::vf2d& vPos, const olc::vf2d& vSize) const ;
118122 virtual void HandlePanAndZoom (const int nMouseButton = 2 , const float fZoomRate = 0 .1f , const bool bPan = true , const bool bZoom = true );
123+ void SetScaleExtents (const olc::vf2d& vScaleMin, const olc::vf2d& vScaleMax);
124+ void EnableScaleClamp (const bool bEnable);
125+
119126 protected:
120127 olc::vf2d m_vWorldOffset = { 0 .0f , 0 .0f };
121128 olc::vf2d m_vWorldScale = { 1 .0f , 1 .0f };
@@ -124,6 +131,9 @@ namespace olc
124131 bool m_bPanning = false ;
125132 olc::vf2d m_vStartPan = { 0 .0f , 0 .0f };
126133 olc::vi2d m_vViewArea;
134+ bool m_bZoomClamp = false ;
135+ olc::vf2d m_vMaxScale = { 0 .0f , 0 .0f };
136+ olc::vf2d m_vMinScale = { 0 .0f , 0 .0f };
127137
128138 public: // Hopefully, these should look familiar!
129139 // Plots a single point
@@ -253,6 +263,7 @@ namespace olc
253263 void TransformedView::SetWorldScale (const olc::vf2d& vScale)
254264 {
255265 m_vWorldScale = vScale;
266+ if (m_bZoomClamp) m_vWorldScale = m_vWorldScale.clamp (m_vMinScale, m_vMaxScale);
256267 }
257268
258269 void TransformedView::SetViewArea (const olc::vi2d& vViewArea)
@@ -275,10 +286,22 @@ namespace olc
275286 return GetWorldBR () - GetWorldTL ();
276287 }
277288
289+ void TransformedView::SetScaleExtents (const olc::vf2d& vScaleMin, const olc::vf2d& vScaleMax)
290+ {
291+ m_vMaxScale = vScaleMax;
292+ m_vMinScale = vScaleMin;
293+ }
294+
295+ void TransformedView::EnableScaleClamp (const bool bEnable)
296+ {
297+ m_bZoomClamp = bEnable;
298+ }
299+
278300 void TransformedView::ZoomAtScreenPos (const float fDeltaZoom , const olc::vi2d& vPos)
279301 {
280302 olc::vf2d vOffsetBeforeZoom = ScreenToWorld (vPos);
281303 m_vWorldScale *= fDeltaZoom ;
304+ if (m_bZoomClamp) m_vWorldScale = m_vWorldScale.clamp (m_vMinScale, m_vMaxScale);
282305 olc::vf2d vOffsetAfterZoom = ScreenToWorld (vPos);
283306 m_vWorldOffset += vOffsetBeforeZoom - vOffsetAfterZoom;
284307 }
@@ -287,6 +310,7 @@ namespace olc
287310 {
288311 olc::vf2d vOffsetBeforeZoom = ScreenToWorld (vPos);
289312 m_vWorldScale = { fZoom , fZoom };
313+ if (m_bZoomClamp) m_vWorldScale = m_vWorldScale.clamp (m_vMinScale, m_vMaxScale);
290314 olc::vf2d vOffsetAfterZoom = ScreenToWorld (vPos);
291315 m_vWorldOffset += vOffsetBeforeZoom - vOffsetAfterZoom;
292316 }
0 commit comments