From af271f85b2178ab7d90d409a7967bd8479af77dd Mon Sep 17 00:00:00 2001 From: nek0 Date: Mon, 6 Mar 2017 17:48:12 +0100 Subject: [PATCH] adding handler function --- src/Affection/MouseInteractable.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Affection/MouseInteractable.hs b/src/Affection/MouseInteractable.hs index 0a3454f..9af9a64 100644 --- a/src/Affection/MouseInteractable.hs +++ b/src/Affection/MouseInteractable.hs @@ -18,3 +18,25 @@ class MouseClickable a us where -> SDL.InputMotion -- The 'SDL.InputMotion' of the click -> Int -- The number of clicks -> Affection us () + +-- | 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) + => SDL.Event -- ^ Piped event in + -> t clickable -- ^ 'MouseClickable' elemt to be checked + -> Affection us SDL.Event -- ^ Unaltered event +handleMouseClicks e clickables = + case SDL.eventPayload e of + 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