2014-04-03 18:05:49 +00:00
|
|
|
{-# LANGUAGE InstanceSigs, MultiParamTypeClasses, FlexibleInstances #-}
|
2014-03-09 12:51:44 +00:00
|
|
|
|
2014-04-23 11:08:18 +00:00
|
|
|
module UI.UIClasses where
|
2014-03-09 12:51:44 +00:00
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
import Control.Lens ((^.))
|
|
|
|
import Control.Monad
|
|
|
|
--import Control.Monad.IO.Class -- MonadIO
|
|
|
|
import Control.Monad.RWS.Strict (get)
|
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
|
|
|
|
import qualified Types as T
|
|
|
|
import UI.UIBaseData
|
|
|
|
|
|
|
|
class GUIAnyMap m w where
|
|
|
|
guiAnyMap :: (w -> b) -> GUIAny m -> b
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
class (Monad m) => GUIWidget m uiw where
|
2014-03-09 12:51:44 +00:00
|
|
|
-- |The 'getBoundary' function gives the outer extents of the 'UIWidget'.
|
|
|
|
-- The bounding box wholly contains all children components.
|
2014-04-24 21:42:05 +00:00
|
|
|
getBoundary :: uiw -> m (ScreenUnit, ScreenUnit, ScreenUnit, ScreenUnit) -- ^@(x, y, width, height)@ in pixels (screen coordinates)
|
2014-03-09 12:51:44 +00:00
|
|
|
|
|
|
|
-- |The 'getChildren' function returns all children associated with this widget.
|
|
|
|
--
|
|
|
|
-- All children must be wholly inside the parent's bounding box specified by 'getBoundary'.
|
2014-04-23 11:08:18 +00:00
|
|
|
getChildren :: uiw -> m [UIId]
|
2014-04-24 21:42:05 +00:00
|
|
|
getChildren _ = return []
|
2014-03-09 12:51:44 +00:00
|
|
|
|
2014-04-26 17:16:53 +00:00
|
|
|
-- |The function 'isInside' tests whether a point is inside the widget itself.
|
2014-04-03 18:05:49 +00:00
|
|
|
-- A screen position may be inside the bounding box of a widget but not considered part of the
|
|
|
|
-- component.
|
|
|
|
--
|
|
|
|
-- The default implementations tests if the point is within the rectangle specified by the
|
|
|
|
-- 'getBoundary' function.
|
2014-04-26 17:16:53 +00:00
|
|
|
isInside :: ScreenUnit -- ^screen x coordinate
|
2014-04-03 18:05:49 +00:00
|
|
|
-> ScreenUnit -- ^screen y coordinate
|
2014-03-09 12:51:44 +00:00
|
|
|
-> uiw -- ^the parent widget
|
2014-04-23 11:08:18 +00:00
|
|
|
-> m Bool
|
2014-04-26 17:16:53 +00:00
|
|
|
isInside x' y' wg = do
|
2014-04-24 21:42:05 +00:00
|
|
|
(x, y, w, h) <- getBoundary wg
|
|
|
|
return $ (x' - x <= w) && (x' - x >= 0) && (y' - y <= h) && (y' - y >= 0)
|
2014-03-09 12:51:44 +00:00
|
|
|
|
|
|
|
-- |The 'getPriority' function returns the priority score of a 'GUIWidget'.
|
|
|
|
-- A widget with a high score is more in the front than a low scored widget.
|
2014-04-23 11:08:18 +00:00
|
|
|
getPriority :: uiw -> m Int
|
2014-04-24 21:42:05 +00:00
|
|
|
getPriority _ = return 0
|
2014-04-03 18:05:49 +00:00
|
|
|
|
|
|
|
-- |The 'getShorthand' function returns a descriptive 'String' mainly for debuggin prupose.
|
|
|
|
-- The shorthand should be unique for each instance.
|
2014-04-23 11:08:18 +00:00
|
|
|
getShorthand :: uiw -> m String
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-04 13:47:16 +00:00
|
|
|
-- |A 'GUIClickable' represents a widget with a 'UIButtonState'.
|
|
|
|
--
|
|
|
|
-- Minimal complete definition: 'getButtonState' and either 'updateButtonState' or 'setButtonState'.
|
|
|
|
class GUIClickable w where
|
|
|
|
updateButtonState :: (UIButtonState -> UIButtonState) -> w -> w
|
|
|
|
updateButtonState f w = setButtonState (f $ getButtonState w) w
|
|
|
|
setButtonState :: UIButtonState -> w -> w
|
|
|
|
setButtonState s = updateButtonState (\_ -> s)
|
|
|
|
getButtonState :: w -> UIButtonState
|
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
class Monad m => MouseHandler a m w where
|
2014-04-03 18:05:49 +00:00
|
|
|
-- |The function 'onMousePressed' is called when the primary button is pressed
|
|
|
|
-- while inside a screen coordinate within the widget ('isInside').
|
|
|
|
onMousePressed :: ScreenUnit -- ^screen x coordinate
|
|
|
|
-> ScreenUnit -- ^screen y coordinate
|
|
|
|
-> w -- ^widget the event is invoked on
|
2014-04-23 11:08:18 +00:00
|
|
|
-> a -> m (w, a) -- ^widget after the event and the altered handler
|
2014-04-03 18:05:49 +00:00
|
|
|
onMousePressed _ _ wg a = return (wg, a)
|
|
|
|
|
|
|
|
-- |The function 'onMouseReleased' is called when the primary button is released
|
|
|
|
-- while the pressing event occured within the widget ('isInside').
|
|
|
|
--
|
|
|
|
-- Thus, the mouse is either within the widget or outside while still dragging.
|
|
|
|
onMouseReleased :: ScreenUnit -- ^screen x coordinate
|
|
|
|
-> ScreenUnit -- ^screen x coordinate
|
|
|
|
-> w -- ^wdiget the event is invoked on
|
2014-04-23 11:08:18 +00:00
|
|
|
-> a -> m (w, a) -- ^widget after the event and the altered handler
|
2014-04-03 18:05:49 +00:00
|
|
|
onMouseReleased _ _ wg a = return (wg, a)
|
|
|
|
|
|
|
|
-- |The function 'onMousePressed' is called when the secondary button is pressed
|
|
|
|
-- while inside a screen coordinate within the widget ('isInside').
|
|
|
|
onMousePressedAlt :: ScreenUnit -- ^screen x coordinate
|
|
|
|
-> ScreenUnit -- ^screen y coordinate
|
|
|
|
-> w -- ^widget the event is invoked on
|
2014-04-23 11:08:18 +00:00
|
|
|
-> a -> m (w, a) -- ^widget after the event and the altered handler
|
2014-04-03 18:05:49 +00:00
|
|
|
onMousePressedAlt _ _ wg a = return (wg, a)
|
|
|
|
|
|
|
|
-- |The function 'onMouseReleased' is called when the secondary button is released
|
|
|
|
-- while the pressing event occured within the widget ('isInside').
|
|
|
|
--
|
|
|
|
-- Thus, the mouse is either within the widget or outside while still dragging.
|
|
|
|
onMouseReleasedAlt :: ScreenUnit -- ^screen x coordinate
|
|
|
|
-> ScreenUnit -- ^screen x coordinate
|
|
|
|
-> w -- ^wdiget the event is invoked on
|
2014-04-23 11:08:18 +00:00
|
|
|
-> a -> m (w, a) -- ^widget after the event and the altered handler
|
2014-04-03 18:05:49 +00:00
|
|
|
onMouseReleasedAlt _ _ wg a = return (wg, a)
|
|
|
|
|
|
|
|
-- |The function 'onMouseMove' is invoked when the mouse is moved inside the
|
|
|
|
-- widget's space ('isInside').
|
|
|
|
onMouseMove :: ScreenUnit -- ^screen x coordinate
|
|
|
|
-> ScreenUnit -- ^screen y coordinate
|
|
|
|
-> w -- ^widget the event is invoked on
|
2014-04-23 11:08:18 +00:00
|
|
|
-> a -> m (w, a) -- ^widget after the event and the altered handler
|
2014-04-03 18:05:49 +00:00
|
|
|
onMouseMove _ _ wg a = return (wg, a)
|
|
|
|
|
|
|
|
-- |The function 'onMouseMove' is invoked when the mouse enters the
|
|
|
|
-- widget's space ('isInside').
|
|
|
|
onMouseEnter :: ScreenUnit -- ^screen x coordinate
|
|
|
|
-> ScreenUnit -- ^screen y coordinate
|
|
|
|
-> w -- ^widget the event is invoked on
|
2014-04-23 11:08:18 +00:00
|
|
|
-> a -> m (w, a) -- ^widget after the event and the altered handler
|
2014-04-03 18:05:49 +00:00
|
|
|
onMouseEnter _ _ wg a = return (wg, a)
|
|
|
|
|
|
|
|
-- |The function 'onMouseMove' is invoked when the mouse leaves the
|
|
|
|
-- widget's space ('isInside').
|
|
|
|
onMouseLeave :: ScreenUnit -- ^screen x coordinate
|
|
|
|
-> ScreenUnit -- ^screen y coordinate
|
|
|
|
-> w -- ^widget the event is invoked on
|
2014-04-23 11:08:18 +00:00
|
|
|
-> a -> m (w, a) -- ^widget after the event and the altered handler
|
2014-04-03 18:05:49 +00:00
|
|
|
onMouseLeave _ _ wg a = return (wg, a)
|
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
instance (MouseHandler h m w) => MouseHandler (MouseHandlerSwitch h) m w where
|
2014-04-03 18:05:49 +00:00
|
|
|
onMousePressed x y w (MouseHandlerSwitch h) = do
|
|
|
|
(w', h') <- onMousePressedAlt x y w h
|
|
|
|
return (w', MouseHandlerSwitch h')
|
|
|
|
onMouseReleased x y w (MouseHandlerSwitch h) = do
|
|
|
|
(w', h') <- onMouseReleasedAlt x y w h
|
|
|
|
return (w', MouseHandlerSwitch h')
|
|
|
|
onMousePressedAlt x y w (MouseHandlerSwitch h) = do
|
|
|
|
(w', h') <- onMousePressed x y w h
|
|
|
|
return (w', MouseHandlerSwitch h')
|
|
|
|
onMouseReleasedAlt x y w (MouseHandlerSwitch h) = do
|
|
|
|
(w', h') <- onMouseReleased x y w h
|
|
|
|
return (w', MouseHandlerSwitch h')
|
|
|
|
onMouseMove x y w (MouseHandlerSwitch h) = do
|
|
|
|
(w', h') <- onMouseMove x y w h
|
|
|
|
return (w', MouseHandlerSwitch h')
|
|
|
|
onMouseEnter x y w (MouseHandlerSwitch h) = do
|
|
|
|
(w', h') <- onMouseEnter x y w h
|
|
|
|
return (w', MouseHandlerSwitch h')
|
|
|
|
onMouseLeave x y w (MouseHandlerSwitch h) = do
|
|
|
|
(w', h') <- onMouseLeave x y w h
|
|
|
|
return (w', MouseHandlerSwitch h')
|
2014-03-09 12:51:44 +00:00
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
instance (Monad m, GUIClickable w) => MouseHandler (ButtonHandler m w) m w where
|
2014-04-04 13:47:16 +00:00
|
|
|
-- |Change 'UIButtonState's '_buttonstateIsFiring' to @True@.
|
2014-04-24 21:42:05 +00:00
|
|
|
onMousePressed _ _ wg h =
|
2014-04-04 13:47:16 +00:00
|
|
|
return (updateButtonState (\s -> s {_buttonstateIsFiring = True}) wg, h)
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-04 13:47:16 +00:00
|
|
|
-- |Change 'UIButtonState's '_buttonstateIsFiring' to @False@ and
|
2014-04-03 18:05:49 +00:00
|
|
|
-- call 'action' if inside the widget or
|
2014-04-04 13:47:16 +00:00
|
|
|
-- set '_buttonstateIsDeferred' to false otherwise.
|
|
|
|
onMouseReleased x y wg h@(ButtonHandler action) = if _buttonstateIsFiring $ getButtonState wg
|
2014-04-03 18:05:49 +00:00
|
|
|
then do
|
2014-04-04 13:47:16 +00:00
|
|
|
wg' <- action wg x y
|
|
|
|
return (updateButtonState (\s -> s {_buttonstateIsFiring = False}) wg', h)
|
|
|
|
else return (updateButtonState (\s -> s {_buttonstateIsDeferred = False}) wg, h)
|
2014-04-03 18:05:49 +00:00
|
|
|
|
|
|
|
-- |Do nothing.
|
|
|
|
onMouseMove _ _ wg h = return (wg, h)
|
|
|
|
|
2014-04-04 13:47:16 +00:00
|
|
|
-- |Set 'UIButtonState's '_buttonstateIsReady' to @True@ and
|
2014-04-03 18:05:49 +00:00
|
|
|
-- update dragging state (only drag if inside widget).
|
2014-04-04 13:47:16 +00:00
|
|
|
-- In detail, change 'UIButtonState's '_buttonstateIsDeferred' to '_buttonstateIsFiring's current value
|
|
|
|
-- and set '_buttonstateIsFiring' to @False@.
|
|
|
|
onMouseEnter _ _ wg h = return
|
|
|
|
(updateButtonState (\s -> s { _buttonstateIsFiring = _buttonstateIsDeferred s
|
|
|
|
, _buttonstateIsDeferred = False
|
|
|
|
, _buttonstateIsReady = True
|
|
|
|
}) wg
|
|
|
|
, h)
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-04 13:47:16 +00:00
|
|
|
-- |Set 'UIButtonState's 'buttonstateIsReady' to @False@ and
|
2014-04-03 18:05:49 +00:00
|
|
|
-- update dragging state (only drag if inside widget).
|
2014-04-04 13:47:16 +00:00
|
|
|
-- In detail, change 'UIButtonState's '_buttonstateIsFiring' to '_buttonstateIsDeferred's current value
|
|
|
|
-- and set '_buttonstateIsDeferred's' to @False@.
|
|
|
|
onMouseLeave _ _ wg h = return
|
|
|
|
(updateButtonState (\s -> s { _buttonstateIsFiring = False
|
|
|
|
, _buttonstateIsDeferred = _buttonstateIsFiring s
|
|
|
|
, _buttonstateIsReady = False
|
|
|
|
}) wg
|
|
|
|
, h)
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
instance (Monad m) => GUIAnyMap m (GUIAny m) where
|
2014-04-03 18:05:49 +00:00
|
|
|
guiAnyMap f w = f w
|
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
instance GUIWidget T.Pioneers (GUIAny T.Pioneers) where
|
2014-04-03 18:05:49 +00:00
|
|
|
getBoundary (GUIAnyC w) = getBoundary w
|
|
|
|
getBoundary (GUIAnyP w) = getBoundary w
|
2014-04-04 13:47:16 +00:00
|
|
|
getBoundary (GUIAnyB w _) = getBoundary w
|
2014-04-03 18:05:49 +00:00
|
|
|
getChildren (GUIAnyC w) = getChildren w
|
|
|
|
getChildren (GUIAnyP w) = getChildren w
|
2014-04-04 13:47:16 +00:00
|
|
|
getChildren (GUIAnyB w _) = getChildren w
|
2014-04-26 17:16:53 +00:00
|
|
|
isInside x y (GUIAnyC w) = (isInside x y) w
|
|
|
|
isInside x y (GUIAnyP w) = (isInside x y) w
|
|
|
|
isInside x y (GUIAnyB w _) = (isInside x y) w
|
2014-04-03 18:05:49 +00:00
|
|
|
getPriority (GUIAnyC w) = getPriority w
|
|
|
|
getPriority (GUIAnyP w) = getPriority w
|
2014-04-04 13:47:16 +00:00
|
|
|
getPriority (GUIAnyB w _) = getPriority w
|
2014-04-24 21:42:05 +00:00
|
|
|
getShorthand (GUIAnyC w) = do { str <- getShorthand w; return $ "A" ++ str }
|
|
|
|
getShorthand (GUIAnyP w) = do { str <- getShorthand w; return $ "A" ++ str }
|
|
|
|
getShorthand (GUIAnyB w _) = do { str <- getShorthand w; return $ "A" ++ str }
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
instance (Monad m) => GUIAnyMap m GUIContainer where
|
2014-04-03 18:05:49 +00:00
|
|
|
guiAnyMap f (GUIAnyC c) = f c
|
|
|
|
guiAnyMap _ _ = error "invalid types in guiAnyMap"
|
2014-04-24 21:42:05 +00:00
|
|
|
instance (Monad m) => GUIWidget m GUIContainer where
|
2014-04-23 11:08:18 +00:00
|
|
|
getBoundary :: GUIContainer -> m (ScreenUnit, ScreenUnit, ScreenUnit ,ScreenUnit)
|
2014-04-24 21:42:05 +00:00
|
|
|
getBoundary cnt = return (_uiScreenX cnt, _uiScreenY cnt, _uiWidth cnt, _uiHeight cnt)
|
|
|
|
getChildren cnt = return $ _uiChildren cnt
|
|
|
|
getPriority cnt = return $ _uiPriority cnt
|
2014-04-23 11:08:18 +00:00
|
|
|
getShorthand _ = return $ "CNT"
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
instance GUIAnyMap m GUIPanel where
|
2014-04-03 18:05:49 +00:00
|
|
|
guiAnyMap f (GUIAnyP p) = f p
|
|
|
|
guiAnyMap _ _ = error "invalid types in guiAnyMap"
|
2014-04-24 21:42:05 +00:00
|
|
|
instance GUIWidget T.Pioneers GUIPanel where
|
|
|
|
getBoundary pnl = do
|
|
|
|
state <- get
|
|
|
|
let hmap = state ^. T.ui . T.uiMap
|
|
|
|
case _uiChildren $ _panelContainer pnl of
|
2014-04-03 18:05:49 +00:00
|
|
|
[] -> getBoundary $ _panelContainer pnl
|
2014-04-24 21:42:05 +00:00
|
|
|
cs -> do
|
|
|
|
let widgets = catMaybes $ map (flip Map.lookup hmap) cs
|
|
|
|
foldl' (liftM2 determineSize) (getBoundary $ _panelContainer pnl) $ map getBoundary widgets
|
2014-04-03 18:05:49 +00:00
|
|
|
where
|
|
|
|
determineSize :: (ScreenUnit, ScreenUnit, ScreenUnit ,ScreenUnit) -> (ScreenUnit, ScreenUnit, ScreenUnit ,ScreenUnit) -> (ScreenUnit, ScreenUnit, ScreenUnit ,ScreenUnit)
|
|
|
|
determineSize (x, y, w, h) (x', y', w', h') =
|
|
|
|
let x'' = if x' < x then x' else x
|
|
|
|
y'' = if y' < y then y' else y
|
|
|
|
w'' = if x' + w' > x + w then x' + w' - x'' else x + w - x''
|
|
|
|
h'' = if y' + h' > y + h then y' + h' - y'' else y + h - y''
|
|
|
|
in (x'', y'', w'', h'')
|
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
getChildren pnl = getChildren $ _panelContainer pnl
|
|
|
|
getPriority pnl = getPriority $ _panelContainer pnl
|
2014-04-23 11:08:18 +00:00
|
|
|
getShorthand _ = return $ "PNL"
|
2014-04-03 18:05:49 +00:00
|
|
|
|
2014-04-24 21:42:05 +00:00
|
|
|
instance (Monad m) => GUIAnyMap m GUIButton where
|
2014-04-04 13:47:16 +00:00
|
|
|
guiAnyMap f (GUIAnyB btn _) = f btn
|
2014-04-03 18:05:49 +00:00
|
|
|
guiAnyMap _ _ = error "invalid types in guiAnyMap"
|
2014-04-04 13:47:16 +00:00
|
|
|
instance GUIClickable GUIButton where
|
2014-04-24 21:42:05 +00:00
|
|
|
getButtonState = _uiButtonState
|
|
|
|
updateButtonState f btn = btn {_uiButtonState = f $ _uiButtonState btn}
|
|
|
|
instance (Monad m) => GUIWidget m GUIButton where
|
|
|
|
getBoundary btn = return (_uiScreenXB btn, _uiScreenYB btn, _uiWidthB btn, _uiHeightB btn)
|
2014-04-23 11:08:18 +00:00
|
|
|
getChildren _ = return []
|
2014-04-24 21:42:05 +00:00
|
|
|
getPriority btn = return $ _uiPriorityB btn
|
2014-04-23 11:08:18 +00:00
|
|
|
getShorthand _ = return "BTN"
|