{- | Getting the window size geometry. -}
module Dodge.Base.Window
    where
import Dodge.Data
import Dodge.Config.Data
import Geometry

-- | A box covering the screen in world coordinates
screenPolygon :: World -> [Point2]
screenPolygon w = [tr,tl,bl,br]
  where 
    scRot  = rotateV (_cameraRot w)
    scZoom p | _cameraZoom w /= 0 = (1/_cameraZoom w) *.* p
             | otherwise = p
    scTran p = p +.+ _cameraCenter w
    tr = scTran $ scRot $ scZoom (V2 ( halfWidth w) ( halfHeight w)) 
    tl = scTran $ scRot $ scZoom (V2 (-halfWidth w) ( halfHeight w)) 
    br = scTran $ scRot $ scZoom (V2 ( halfWidth w) (-halfHeight w))
    bl = scTran $ scRot $ scZoom (V2 (-halfWidth w) (-halfHeight w))

-- | A box covering the screen in world coordinates, with a x and y border
screenPolygonBord 
    :: Float    -- ^ X border
    -> Float    -- ^ Y border
    -> World
    -> [Point2]
screenPolygonBord xbord ybord w = [tr,tl,bl,br]
  where 
    hw = halfWidth  w - xbord
    hh = halfHeight w - ybord
    scZoom p | _cameraZoom w /= 0   = (1/_cameraZoom w) *.* p
             | otherwise            = p
    theTransform = (+.+ _cameraCenter w) . rotateV (_cameraRot w) . scZoom
    tr = theTransform (V2   hw    hh ) 
    tl = theTransform (V2 (-hw)   hh ) 
    br = theTransform (V2   hw  (-hh))
    bl = theTransform (V2 (-hw) (-hh))

halfWidth,halfHeight :: World -> Float
halfWidth w = getWindowX w / 2
halfHeight w = getWindowY w / 2
getWindowX ,getWindowY :: World -> Float 
getWindowX = _windowX . _config
getWindowY = _windowY . _config

-- | A box of the size of the screen in screen centered coordinates
screenBox 
    :: World
    -> [Point2]
screenBox w = rectNSEW hh (-hh) hw (-hw)
  where
    hw = halfWidth w
    hh = halfHeight w
