{-|
Module      : Monomer.Event.Util
Copyright   : (c) 2018 Francisco Vallarino
License     : BSD-3-Clause (see the LICENSE file)
Maintainer  : fjvallarino@gmail.com
Stability   : experimental
Portability : non-portable

Utility functions for event handling.
-}
module Monomer.Event.Util (
  isShiftPressed,
  isCtrlPressed,
  isAltPressed,
  isGUIPressed,
  isOnClick,
  isOnButtonAction,
  isOnWheelScroll,
  isOnKeyAction,
  isOnTextInput,
  isOnClipboard,
  isOnFocus,
  isOnBlur,
  isOnEnter,
  isOnMove,
  isOnLeave,
  isOnDrag,
  isOnDrop,
  checkKeyboard
) where

import Data.Maybe (fromMaybe)

import qualified Data.Map as M

import Monomer.Event.Core
import Monomer.Event.Keyboard
import Monomer.Event.Types

-- | Checks if Winddows/Cmd key is pressed.
isGUIPressed :: KeyMod -> Bool
isGUIPressed :: KeyMod -> Bool
isGUIPressed KeyMod
mod = KeyMod -> Bool
_kmLeftGUI KeyMod
mod Bool -> Bool -> Bool
|| KeyMod -> Bool
_kmRightGUI KeyMod
mod

-- | Checks if Ctrl key is pressed.
isCtrlPressed :: KeyMod -> Bool
isCtrlPressed :: KeyMod -> Bool
isCtrlPressed KeyMod
keyMod = KeyMod -> Bool
_kmLeftCtrl KeyMod
keyMod Bool -> Bool -> Bool
|| KeyMod -> Bool
_kmRightCtrl KeyMod
keyMod

-- | Checks if Shift key is pressed.
isShiftPressed :: KeyMod -> Bool
isShiftPressed :: KeyMod -> Bool
isShiftPressed KeyMod
keyMod = KeyMod -> Bool
_kmLeftShift KeyMod
keyMod Bool -> Bool -> Bool
|| KeyMod -> Bool
_kmRightShift KeyMod
keyMod

-- | Checks if Alt key is pressed.
isAltPressed :: KeyMod -> Bool
isAltPressed :: KeyMod -> Bool
isAltPressed KeyMod
keyMod = KeyMod -> Bool
_kmLeftAlt KeyMod
keyMod Bool -> Bool -> Bool
|| KeyMod -> Bool
_kmRightAlt KeyMod
keyMod

-- | Checks if it's a Click event.
isOnClick :: SystemEvent -> Bool
isOnClick :: SystemEvent -> Bool
isOnClick Click{} = Bool
True
isOnClick SystemEvent
_ = Bool
False

-- | Checks if it's a ButtonAction event.
isOnButtonAction :: SystemEvent -> Bool
isOnButtonAction :: SystemEvent -> Bool
isOnButtonAction ButtonAction{} = Bool
True
isOnButtonAction SystemEvent
_ = Bool
False

-- | Checks if it's a WheelScroll event.
isOnWheelScroll :: SystemEvent -> Bool
isOnWheelScroll :: SystemEvent -> Bool
isOnWheelScroll WheelScroll{} = Bool
True
isOnWheelScroll SystemEvent
_ = Bool
False

-- | Checks if it's a KeyAction event.
isOnKeyAction :: SystemEvent -> Bool
isOnKeyAction :: SystemEvent -> Bool
isOnKeyAction KeyAction{} = Bool
True
isOnKeyAction SystemEvent
_ = Bool
False

-- | Checks if it's a TextInput event.
isOnTextInput :: SystemEvent -> Bool
isOnTextInput :: SystemEvent -> Bool
isOnTextInput TextInput{} = Bool
True
isOnTextInput SystemEvent
_ = Bool
False

-- | Checks if it's a Clipboard event.
isOnClipboard :: SystemEvent -> Bool
isOnClipboard :: SystemEvent -> Bool
isOnClipboard Clipboard{} = Bool
True
isOnClipboard SystemEvent
_ = Bool
False

-- | Checks if it's a Focus event.
isOnFocus :: SystemEvent -> Bool
isOnFocus :: SystemEvent -> Bool
isOnFocus Focus{} = Bool
True
isOnFocus SystemEvent
_ = Bool
False

-- | Checks if it's a Blur event.
isOnBlur :: SystemEvent -> Bool
isOnBlur :: SystemEvent -> Bool
isOnBlur Blur{} = Bool
True
isOnBlur SystemEvent
_ = Bool
False

-- | Checks if it's an Enter event.
isOnEnter :: SystemEvent -> Bool
isOnEnter :: SystemEvent -> Bool
isOnEnter Enter{} = Bool
True
isOnEnter SystemEvent
_ = Bool
False

-- | Checks if it's a Move event.
isOnMove :: SystemEvent -> Bool
isOnMove :: SystemEvent -> Bool
isOnMove Move{} = Bool
True
isOnMove SystemEvent
_ = Bool
False

-- | Checks if it's a Leave event.
isOnLeave :: SystemEvent -> Bool
isOnLeave :: SystemEvent -> Bool
isOnLeave Leave{} = Bool
True
isOnLeave SystemEvent
_ = Bool
False

-- | Checks if it's a Drag event.
isOnDrag :: SystemEvent -> Bool
isOnDrag :: SystemEvent -> Bool
isOnDrag Drag{} = Bool
True
isOnDrag SystemEvent
_ = Bool
False

-- | Checks if it's a Drop event.
isOnDrop :: SystemEvent -> Bool
isOnDrop :: SystemEvent -> Bool
isOnDrop Drop{} = Bool
True
isOnDrop SystemEvent
_ = Bool
False

-- | Appplies a provided function to test a KeyAction event
checkKeyboard :: SystemEvent -> (KeyMod -> KeyCode -> KeyStatus -> Bool) -> Bool
checkKeyboard :: SystemEvent -> (KeyMod -> KeyCode -> KeyStatus -> Bool) -> Bool
checkKeyboard (KeyAction KeyMod
mod KeyCode
code KeyStatus
motion) KeyMod -> KeyCode -> KeyStatus -> Bool
testFn = KeyMod -> KeyCode -> KeyStatus -> Bool
testFn KeyMod
mod KeyCode
code KeyStatus
motion
checkKeyboard SystemEvent
_ KeyMod -> KeyCode -> KeyStatus -> Bool
_ = Bool
False