2017-03-05 15:39:37 +00:00
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
|
|
|
|
|
|
|
module Affection.MouseInteractable where
|
|
|
|
|
|
|
|
import Affection.Types
|
|
|
|
|
2017-03-05 15:41:18 +00:00
|
|
|
import qualified SDL
|
|
|
|
|
2017-03-05 15:39:37 +00:00
|
|
|
-- class MouseHoverable a us where
|
|
|
|
-- onHover :: a -> Affection us ()
|
|
|
|
|
|
|
|
-- | Define a mouse clickable object
|
|
|
|
class MouseClickable a us where
|
|
|
|
onClick
|
|
|
|
:: a -- The object
|
|
|
|
-> SDL.MouseButton -- The clicked button
|
|
|
|
-> (Int, Int) -- The coordinates of the click
|
|
|
|
-> SDL.InputMotion -- The 'SDL.InputMotion' of the click
|
|
|
|
-> Int -- The number of clicks
|
2017-11-27 22:30:11 +00:00
|
|
|
-> Affection us msg ()
|
2017-03-06 16:48:12 +00:00
|
|
|
|
|
|
|
-- | A helper function that checks wether provided clickables have been clicked.
|
|
|
|
-- This function does not consume provided events, but passes them on.
|
|
|
|
handleMouseClicks
|
|
|
|
:: (Foldable t, MouseClickable clickable us)
|
2017-04-17 10:40:17 +00:00
|
|
|
=> SDL.EventPayload -- ^ Piped event in
|
|
|
|
-> t clickable -- ^ 'MouseClickable' elemt to be checked
|
2017-11-27 22:30:11 +00:00
|
|
|
-> Affection us msg SDL.EventPayload -- ^ Unaltered event
|
2017-03-06 16:48:12 +00:00
|
|
|
handleMouseClicks e clickables =
|
2017-04-17 10:40:17 +00:00
|
|
|
case e of
|
2017-03-06 16:48:12 +00:00
|
|
|
SDL.MouseButtonEvent dat -> do
|
|
|
|
mapM_ (\clickable -> do
|
|
|
|
let SDL.P (SDL.V2 x y) = SDL.mouseButtonEventPos dat
|
|
|
|
onClick
|
|
|
|
clickable
|
|
|
|
(SDL.mouseButtonEventButton dat)
|
|
|
|
(fromIntegral x, fromIntegral y)
|
|
|
|
(SDL.mouseButtonEventMotion dat)
|
|
|
|
(fromIntegral $ SDL.mouseButtonEventClicks dat)
|
|
|
|
) clickables
|
|
|
|
return e
|
|
|
|
_ -> return e
|