{-# OPTIONS_GHC -fno-cse #-}
module Graphics.UI.GLUT.Menu (
Menu(..), MenuItem(..), MenuCallback, attachMenu,
numMenuItems
) where
import Control.Monad.IO.Class ( MonadIO(..) )
import Control.Monad ( when, unless, zipWithM )
import Data.Array ( listArray, (!) )
import Data.IORef ( IORef, newIORef, readIORef, modifyIORef )
import qualified Data.Map as M
import Data.StateVar ( get, ($=), GettableStateVar, makeGettableStateVar
, StateVar, makeStateVar )
import Foreign.C.String ( withCString )
import Foreign.C.Types ( CInt )
import Foreign.Ptr ( freeHaskellFunPtr )
import System.IO.Unsafe ( unsafePerformIO )
import Graphics.UI.GLUT.Callbacks.Registration
import Graphics.UI.GLUT.QueryUtils
import Graphics.UI.GLUT.Raw
import Graphics.UI.GLUT.Types
data
= [MenuItem]
| BitmapFont [MenuItem]
menuFont :: Menu -> Maybe BitmapFont
(Menu [MenuItem]
_) = Maybe BitmapFont
forall a. Maybe a
Nothing
menuFont (MenuWithFont BitmapFont
font [MenuItem]
_) = BitmapFont -> Maybe BitmapFont
forall a. a -> Maybe a
Just BitmapFont
font
menuItems :: Menu -> [MenuItem]
(Menu [MenuItem]
items) = [MenuItem]
items
menuItems (MenuWithFont BitmapFont
_ [MenuItem]
items) = [MenuItem]
items
data
= String MenuCallback
| String Menu
type = IO ()
attachMenu :: MonadIO m => MouseButton -> Menu -> m ()
MouseButton
mouseButton Menu
menu = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Window
win <- String -> IO Window
getCurrentWindow String
"attachMenu"
let hook :: MenuHook
hook = Window -> MouseButton -> MenuHook
MenuHook Window
win MouseButton
mouseButton
MenuHook -> IO ()
detachMenu MenuHook
hook
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([MenuItem] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Menu -> [MenuItem]
menuItems Menu
menu)) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
(MenuID
_, IO ()
destructor) <- Menu -> IO (MenuID, IO ())
traverseMenu Menu
menu
MenuHook -> IO () -> IO ()
addToMenuTable MenuHook
hook IO ()
destructor
MouseButton -> IO ()
attachMenu_ MouseButton
mouseButton
detachMenu :: MenuHook -> IO ()
hook :: MenuHook
hook@(MenuHook Window
_ MouseButton
mouseButton) = do
Maybe (IO ())
maybeDestructor <- MenuHook -> IO (Maybe (IO ()))
lookupInMenuTable MenuHook
hook
case Maybe (IO ())
maybeDestructor of
Maybe (IO ())
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just IO ()
destructor -> do MouseButton -> IO ()
detachMenu_ MouseButton
mouseButton
IO ()
destructor
MenuHook -> IO ()
deleteFromMenuTable MenuHook
hook
traverseMenu :: Menu -> IO (MenuID, Destructor)
Menu
menu = do
let items :: [MenuItem]
items = Menu -> [MenuItem]
menuItems Menu
menu
callbackArray :: Array Int (IO ())
callbackArray = (Int, Int) -> [IO ()] -> Array Int (IO ())
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
1, [MenuItem] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [MenuItem]
items) ((MenuItem -> IO ()) -> [MenuItem] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map MenuItem -> IO ()
makeCallback [MenuItem]
items)
FunPtr MenuFunc
cb <- MenuFunc -> IO (FunPtr MenuFunc)
makeMenuFunc (\MenuID
i -> Array Int (IO ())
callbackArray Array Int (IO ()) -> Int -> IO ()
forall i e. Ix i => Array i e -> i -> e
! (MenuID -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral MenuID
i))
MenuID
menuID <- FunPtr MenuFunc -> IO MenuID
forall (m :: * -> *). MonadIO m => FunPtr MenuFunc -> m MenuID
glutCreateMenu FunPtr MenuFunc
cb
IO () -> (BitmapFont -> IO ()) -> Maybe BitmapFont -> IO ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (MenuID -> BitmapFont -> IO ()
setMenuFont MenuID
menuID) (Menu -> Maybe BitmapFont
menuFont Menu
menu)
[IO ()]
destructors <- (MenuItem -> MenuID -> IO (IO ()))
-> [MenuItem] -> [MenuID] -> IO [IO ()]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM MenuItem -> MenuID -> IO (IO ())
addMenuItem [MenuItem]
items [MenuID
1..]
let destructor :: IO ()
destructor = do [IO ()] -> IO ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [IO ()]
destructors
MenuFunc
forall (m :: * -> *). MonadIO m => MenuID -> m ()
glutDestroyMenu MenuID
menuID
FunPtr MenuFunc -> IO ()
forall a. FunPtr a -> IO ()
freeHaskellFunPtr FunPtr MenuFunc
cb
(MenuID, IO ()) -> IO (MenuID, IO ())
forall (m :: * -> *) a. Monad m => a -> m a
return (MenuID
menuID, IO ()
destructor)
makeCallback :: MenuItem -> MenuCallback
makeCallback :: MenuItem -> IO ()
makeCallback (MenuEntry String
_ IO ()
cb) = IO ()
cb
makeCallback MenuItem
_ = String -> IO ()
forall a. HasCallStack => String -> a
error String
"shouldn't receive a callback for submenus"
addMenuItem :: MenuItem -> Value -> IO Destructor
(MenuEntry String
s IO ()
_) MenuID
v = do
String -> MenuFunc
addMenuEntry String
s MenuID
v
IO () -> IO (IO ())
forall (m :: * -> *) a. Monad m => a -> m a
return (IO () -> IO (IO ())) -> IO () -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ MenuFunc
forall (m :: * -> *). MonadIO m => MenuID -> m ()
glutRemoveMenuItem MenuID
1
addMenuItem (SubMenu String
s Menu
m) MenuID
_ = do
(MenuID
menuID, IO ()
destructor) <- IO (MenuID, IO ()) -> IO (MenuID, IO ())
forall a. IO a -> IO a
saveExcursion (Menu -> IO (MenuID, IO ())
traverseMenu Menu
m)
String -> MenuFunc
addSubMenu String
s MenuID
menuID
IO () -> IO (IO ())
forall (m :: * -> *) a. Monad m => a -> m a
return (IO () -> IO (IO ())) -> IO () -> IO (IO ())
forall a b. (a -> b) -> a -> b
$ do MenuFunc
forall (m :: * -> *). MonadIO m => MenuID -> m ()
glutRemoveMenuItem MenuID
1
IO ()
destructor
saveExcursion :: IO a -> IO a
saveExcursion :: IO a -> IO a
saveExcursion IO a
act = do
MenuID
menuID <- StateVar MenuID -> IO MenuID
forall t a (m :: * -> *). (HasGetter t a, MonadIO m) => t -> m a
get StateVar MenuID
currentMenu
a
returnValue <- IO a
act
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (MenuID -> Bool
isRealMenu MenuID
menuID) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
StateVar MenuID
currentMenu StateVar MenuID -> MenuFunc
forall t a (m :: * -> *).
(HasSetter t a, MonadIO m) =>
t -> a -> m ()
$= MenuID
menuID
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
returnValue
{-# NOINLINE theMenuTable #-}
theMenuTable :: IORef MenuTable
= IO (IORef MenuTable) -> IORef MenuTable
forall a. IO a -> a
unsafePerformIO (MenuTable -> IO (IORef MenuTable)
forall a. a -> IO (IORef a)
newIORef MenuTable
emptyMenuTable)
getMenuTable :: IO MenuTable
= IORef MenuTable -> IO MenuTable
forall a. IORef a -> IO a
readIORef IORef MenuTable
theMenuTable
modifyMenuTable :: (MenuTable -> MenuTable) -> IO ()
= IORef MenuTable -> (MenuTable -> MenuTable) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef IORef MenuTable
theMenuTable
data = Window MouseButton
deriving ( MenuHook -> MenuHook -> Bool
(MenuHook -> MenuHook -> Bool)
-> (MenuHook -> MenuHook -> Bool) -> Eq MenuHook
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MenuHook -> MenuHook -> Bool
$c/= :: MenuHook -> MenuHook -> Bool
== :: MenuHook -> MenuHook -> Bool
$c== :: MenuHook -> MenuHook -> Bool
Eq, Eq MenuHook
Eq MenuHook
-> (MenuHook -> MenuHook -> Ordering)
-> (MenuHook -> MenuHook -> Bool)
-> (MenuHook -> MenuHook -> Bool)
-> (MenuHook -> MenuHook -> Bool)
-> (MenuHook -> MenuHook -> Bool)
-> (MenuHook -> MenuHook -> MenuHook)
-> (MenuHook -> MenuHook -> MenuHook)
-> Ord MenuHook
MenuHook -> MenuHook -> Bool
MenuHook -> MenuHook -> Ordering
MenuHook -> MenuHook -> MenuHook
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: MenuHook -> MenuHook -> MenuHook
$cmin :: MenuHook -> MenuHook -> MenuHook
max :: MenuHook -> MenuHook -> MenuHook
$cmax :: MenuHook -> MenuHook -> MenuHook
>= :: MenuHook -> MenuHook -> Bool
$c>= :: MenuHook -> MenuHook -> Bool
> :: MenuHook -> MenuHook -> Bool
$c> :: MenuHook -> MenuHook -> Bool
<= :: MenuHook -> MenuHook -> Bool
$c<= :: MenuHook -> MenuHook -> Bool
< :: MenuHook -> MenuHook -> Bool
$c< :: MenuHook -> MenuHook -> Bool
compare :: MenuHook -> MenuHook -> Ordering
$ccompare :: MenuHook -> MenuHook -> Ordering
$cp1Ord :: Eq MenuHook
Ord )
type Destructor = IO ()
type = M.Map MenuHook Destructor
emptyMenuTable :: MenuTable
= MenuTable
forall k a. Map k a
M.empty
lookupInMenuTable :: MenuHook -> IO (Maybe Destructor)
MenuHook
callbackID =
(MenuTable -> Maybe (IO ())) -> IO MenuTable -> IO (Maybe (IO ()))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (MenuHook -> MenuTable -> Maybe (IO ())
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup MenuHook
callbackID) IO MenuTable
getMenuTable
deleteFromMenuTable :: MenuHook -> IO ()
MenuHook
callbackID =
(MenuTable -> MenuTable) -> IO ()
modifyMenuTable (MenuHook -> MenuTable -> MenuTable
forall k a. Ord k => k -> Map k a -> Map k a
M.delete MenuHook
callbackID)
addToMenuTable :: MenuHook -> Destructor -> IO ()
MenuHook
callbackID IO ()
funPtr =
(MenuTable -> MenuTable) -> IO ()
modifyMenuTable (MenuHook -> IO () -> MenuTable -> MenuTable
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert MenuHook
callbackID IO ()
funPtr)
type = CInt
type Value = CInt
currentMenu :: StateVar MenuID
= IO MenuID -> MenuFunc -> StateVar MenuID
forall a. IO a -> (a -> IO ()) -> StateVar a
makeStateVar IO MenuID
forall (m :: * -> *). MonadIO m => m MenuID
glutGetMenu MenuFunc
forall (m :: * -> *). MonadIO m => MenuID -> m ()
glutSetMenu
isRealMenu :: MenuID -> Bool
= (MenuID -> MenuID -> Bool
forall a. Eq a => a -> a -> Bool
/= MenuID
0)
addMenuEntry :: String -> Value -> IO ()
String
name MenuID
value = String -> (CString -> IO ()) -> IO ()
forall a. String -> (CString -> IO a) -> IO a
withCString String
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
n -> CString -> MenuFunc
forall (m :: * -> *). MonadIO m => CString -> MenuID -> m ()
glutAddMenuEntry CString
n MenuID
value
addSubMenu :: String -> MenuID -> IO ()
String
name MenuID
menuID = String -> (CString -> IO ()) -> IO ()
forall a. String -> (CString -> IO a) -> IO a
withCString String
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
n -> CString -> MenuFunc
forall (m :: * -> *). MonadIO m => CString -> MenuID -> m ()
glutAddSubMenu CString
n MenuID
menuID
attachMenu_ :: MouseButton -> IO ()
= MenuFunc
forall (m :: * -> *). MonadIO m => MenuID -> m ()
glutAttachMenu MenuFunc -> (MouseButton -> MenuID) -> MouseButton -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MouseButton -> MenuID
marshalMouseButton
detachMenu_ :: MouseButton -> IO ()
= MenuFunc
forall (m :: * -> *). MonadIO m => MenuID -> m ()
glutDetachMenu MenuFunc -> (MouseButton -> MenuID) -> MouseButton -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MouseButton -> MenuID
marshalMouseButton
numMenuItems :: GettableStateVar Int
= GettableStateVar Int -> GettableStateVar Int
forall a. IO a -> IO a
makeGettableStateVar (GettableStateVar Int -> GettableStateVar Int)
-> GettableStateVar Int -> GettableStateVar Int
forall a b. (a -> b) -> a -> b
$ Getter Int
forall a. Getter a
simpleGet MenuID -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral GLenum
glut_MENU_NUM_ITEMS
setMenuFont :: MenuID -> BitmapFont -> IO ()
MenuID
menuID BitmapFont
font = MenuID -> Ptr () -> IO ()
forall (m :: * -> *) a. MonadIO m => MenuID -> Ptr a -> m ()
glutSetMenuFont MenuID
menuID (Ptr () -> IO ()) -> IO (Ptr ()) -> IO ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< BitmapFont -> IO (Ptr ())
forall (m :: * -> *). MonadIO m => BitmapFont -> m (Ptr ())
marshalBitmapFont BitmapFont
font