| 1 |
-------------------------------------------------------------------------------- |
| 2 |
-- | |
| 3 |
-- Module : Graphics.UI.GLUT.Callbacks.Global |
| 4 |
-- Copyright : (c) Sven Panne 2002-2018 |
| 5 |
-- License : BSD3 |
| 6 |
-- |
| 7 |
-- Maintainer : Sven Panne <[email protected]> |
| 8 |
-- Stability : stable |
| 9 |
-- Portability : portable |
| 10 |
-- |
| 11 |
-------------------------------------------------------------------------------- |
| 12 |
|
| 13 |
module Graphics.UI.GLUT.Callbacks.Global ( |
| 14 |
-- * Menu status callback |
| 15 |
MenuUsage(..), MenuStatusCallback, menuStatusCallback, |
| 16 |
|
| 17 |
-- * Idle callback |
| 18 |
IdleCallback, idleCallback, |
| 19 |
|
| 20 |
-- * Timer callbacks |
| 21 |
Timeout, TimerCallback, addTimerCallback |
| 22 |
) where |
| 23 |
|
| 24 |
import Control.Monad.Fix ( mfix ) |
| 25 |
import Data.StateVar ( SettableStateVar, makeSettableStateVar ) |
| 26 |
import Foreign.C.Types ( CInt ) |
| 27 |
import Graphics.Rendering.OpenGL ( Position(..) ) |
| 28 |
|
| 29 |
import Graphics.UI.GLUT.Callbacks.Registration |
| 30 |
import Graphics.UI.GLUT.Raw |
| 31 |
|
| 32 |
-------------------------------------------------------------------------------- |
| 33 |
|
| 34 |
data MenuUsage |
| 35 |
= NotInUse |
| 36 |
| InUse |
| 37 |
deriving ( Eq, Ord, Show ) |
| 38 |
|
| 39 |
unmarshalMenuUsage :: CInt -> MenuUsage |
| 40 |
unmarshalMenuUsage x |
| 41 |
| x == glut_MENU_NOT_IN_USE = NotInUse |
| 42 |
| x == glut_MENU_IN_USE = InUse |
| 43 |
| otherwise = error ("unmarshalMenuUsage: illegal value " ++ show x) |
| 44 |
|
| 45 |
type MenuStatusCallback = MenuUsage -> Position -> IO () |
| 46 |
|
| 47 |
-- | Controls the global menu status callback so a GLUT program can determine |
| 48 |
-- when a menu is in use or not. When a menu status callback is registered, it |
| 49 |
-- will be called with the value 'InUse' when pop-up menus are in use by the |
| 50 |
-- user; and the callback will be called with the value 'NotInUse' when pop-up |
| 51 |
-- menus are no longer in use. Additionally, the location in window coordinates |
| 52 |
-- of the button press that caused the menu to go into use, or the location where |
| 53 |
-- the menu was released (maybe outside the window). Other callbacks continue to |
| 54 |
-- operate (except mouse motion callbacks) when pop-up menus are in use so the |
| 55 |
-- menu status callback allows a program to suspend animation or other tasks |
| 56 |
-- when menus are in use. The cascading and unmapping of sub-menus from an |
| 57 |
-- initial pop-up menu does not generate menu status callbacks. There is a |
| 58 |
-- single menu status callback for GLUT. |
| 59 |
-- |
| 60 |
-- When the menu status callback is called, the /current menu/ will be set to |
| 61 |
-- the initial pop-up menu in both the 'InUse' and 'NotInUse' cases. The |
| 62 |
-- /current window/ will be set to the window from which the initial menu was |
| 63 |
-- popped up from, also in both cases. |
| 64 |
|
| 65 |
menuStatusCallback :: SettableStateVar (Maybe MenuStatusCallback) |
| 66 |
menuStatusCallback = |
| 67 |
makeSettableStateVar $ |
| 68 |
setCallback MenuStatusCB glutMenuStatusFunc |
| 69 |
(makeMenuStatusFunc . unmarshal) |
| 70 |
where unmarshal cb s x y = |
| 71 |
cb (unmarshalMenuUsage s) |
| 72 |
(Position (fromIntegral x) (fromIntegral y)) |
| 73 |
|
| 74 |
-------------------------------------------------------------------------------- |
| 75 |
|
| 76 |
type IdleCallback = IO () |
| 77 |
|
| 78 |
-- | Controls the global idle callback so a GLUT program can perform background |
| 79 |
-- processing tasks or continuous animation when window system events are not |
| 80 |
-- being received. If enabled, the idle callback is continuously called when |
| 81 |
-- events are not being received. The /current window/ and /current menu/ will |
| 82 |
-- not be changed before the idle callback. Programs with multiple windows |
| 83 |
-- and\/or menus should explicitly set the /current window/ and\/or /current |
| 84 |
-- menu/ and not rely on its current setting. |
| 85 |
-- |
| 86 |
-- The amount of computation and rendering done in an idle callback should be |
| 87 |
-- minimized to avoid affecting the program\'s interactive response. In general, |
| 88 |
-- not more than a single frame of rendering should be done in an idle callback. |
| 89 |
|
| 90 |
idleCallback :: SettableStateVar (Maybe IdleCallback) |
| 91 |
idleCallback = |
| 92 |
makeSettableStateVar $ setCallback IdleCB glutIdleFunc makeIdleFunc |
| 93 |
|
| 94 |
-------------------------------------------------------------------------------- |
| 95 |
|
| 96 |
-- | Timeout for the timer callback in milliseconds |
| 97 |
type Timeout = Int |
| 98 |
|
| 99 |
type TimerCallback = IO () |
| 100 |
|
| 101 |
-- | Register a one-shot timer callback to be triggered after at least the given |
| 102 |
-- amount of time. Multiple timer callbacks at same or differing times may be |
| 103 |
-- registered simultaneously. There is no support for canceling a registered |
| 104 |
-- callback. |
| 105 |
-- |
| 106 |
-- The number of milliseconds is a lower bound on the time before the callback |
| 107 |
-- is generated. GLUT attempts to deliver the timer callback as soon as possible |
| 108 |
-- after the expiration of the callback\'s time interval. |
| 109 |
|
| 110 |
addTimerCallback :: Timeout -> TimerCallback -> IO () |
| 111 |
addTimerCallback msecs timerCallback = do |
| 112 |
funPtr <- mfix (\self -> makeTimerFunc (\_ -> do registerForCleanup self |
| 113 |
timerCallback)) |
| 114 |
glutTimerFunc (fromIntegral msecs) funPtr 0 |