Files
loop/src/Dodge/Base/Coordinate.hs
T

55 lines
2.4 KiB
Haskell

module Dodge.Base.Coordinate where
import Dodge.Base.WinScale
import Dodge.Data.Universe
import Geometry
import Control.Lens
-- | Transform coordinates from world position to screen coordinates.
worldPosToScreenNorm :: Configuration -> World -> Point2 -> Point2
worldPosToScreenNorm cfig w = doWindowScale cfig . doRotate . doZoom . doTranslate
where
doTranslate p = p -.- (w ^. cWorld . lWorld . cwCam . cwcCenter)
doZoom p = (w ^. cWorld . lWorld . cwCam . cwcZoom) *.* p
doRotate p = rotateV (negate (w ^. cWorld . lWorld . cwCam . cwcRot)) p
{- | Transform world coordinates to scaled screen coordinates.
- These have to be scaled according to the size of the window to get actual screen positions.
- This allows for line thicknesses etc to correspond to pixel sizes.
-}
worldPosToScreen :: World -> Point2 -> Point2
worldPosToScreen w =
rotateV (negate $ w ^. cWorld . lWorld . cwCam . cwcRot)
. ((w ^. cWorld . lWorld . cwCam . cwcZoom) *.*)
. (-.- (w ^. cWorld . lWorld . cwCam . cwcCenter))
{- | Transform coordinates from the map position to screen
coordinates.
-}
cartePosToScreen :: Configuration -> World -> Point2 -> Point2
cartePosToScreen cfig w = doWindowScale cfig . doRotate . doZoom . doTranslate
where
doTranslate p = p -.- (w ^. cWorld . lWorld . hud . carteCenter) -- _carteCenter (_hud (_cWorld w))
--doZoom p = _carteZoom (_hud (_cWorld w)) *.* p
doZoom p = (w ^. cWorld . lWorld . hud . carteZoom) *.* p
--doRotate p = rotateV (negate $ _carteRot (_hud (_cWorld w))) p
doRotate p = rotateV (negate $ w ^. cWorld . lWorld . hud . carteRot) p
crToMousePosOffset :: Creature -> World -> (Point2, Float)
crToMousePosOffset cr w = (mouseWorldPos w -.- _crPos cr, 0)
-- | The mouse position in world coordinates.
mouseWorldPos :: World -> Point2
mouseWorldPos w =
(w ^. cWorld . lWorld . cwCam . cwcCenter)
+.+ (1 / (w ^. cWorld . lWorld . cwCam . cwcZoom)) *.* rotateV (w ^. cWorld . lWorld . cwCam . cwcRot) (_mousePos w)
-- | The mouse position in map coordinates
mouseCartePos :: World -> Point2
mouseCartePos w = (w ^. cWorld . lWorld . hud . carteCenter)
+.+
(1 / (w ^. cWorld . lWorld . hud . carteZoom))
*.* rotateV (w ^. cWorld . lWorld . hud . carteRot) (_mousePos w)
-- _carteCenter (_hud (_cWorld w))
-- +.+ (1 / _carteZoom (_hud (_cWorld w))) *.* rotateV (_carteRot (_hud (_cWorld w))) (_mousePos w)