module Vis.Interface ( display
, animate
, animateIO
, simulate
, simulateIO
, play
, playIO
) where
import Data.Maybe ( fromMaybe )
import Graphics.UI.GLUT ( Key, KeyState, Position, Modifiers, Cursor(..) )
import Vis.Vis ( Options(..), vis )
import Vis.Camera ( Camera, Camera0(..), makeCamera, setCamera, cameraMotion, cameraKeyboardMouse )
import Vis.VisObject ( VisObject(..) )
display :: Real b =>
Options
-> VisObject b
-> IO ()
display opts visobjects = animate opts (\_ -> visobjects)
animate :: Real b =>
Options
-> (Float -> VisObject b)
-> IO ()
animate opts userDrawFun = animateIO opts (return . userDrawFun)
animateIO :: Real b =>
Options
-> (Float -> IO (VisObject b))
-> IO ()
animateIO opts userDrawFun =
vis opts ts (userState0, cameraState0) simFun drawFun setCameraFun (Just kmCallback) (Just motionCallback) Nothing
where
ts = 0.01
userState0 = ()
cameraState0 = makeCamera $ fromMaybe defaultCamera (optInitialCamera opts)
drawFun (_,time) = do
obs <- userDrawFun time
return (obs, Nothing)
simFun (state,_) = return state
kmCallback (state, camState) k0 k1 _ _ = (state, cameraKeyboardMouse camState k0 k1)
motionCallback (state, cameraState) pos = (state, cameraMotion cameraState pos)
setCameraFun :: ((), Camera) -> IO ()
setCameraFun (_,cameraState) = setCamera cameraState
simulate :: Real b =>
Options
-> Double
-> world
-> (world -> VisObject b)
-> (Float -> world -> world)
-> IO ()
simulate opts ts state0 userDrawFun userSimFun =
simulateIO opts ts state0 (return . userDrawFun) (\t -> return . (userSimFun t))
simulateIO :: Real b =>
Options
-> Double
-> world
-> (world -> IO (VisObject b))
-> (Float -> world -> IO world)
-> IO ()
simulateIO opts ts userState0 userDrawFun userSimFun =
vis opts ts (userState0, cameraState0) simFun drawFun setCameraFun (Just kmCallback) (Just motionCallback) Nothing
where
drawFun ((userState, _),_) = do
obs <- userDrawFun userState
return (obs, Nothing)
simFun ((userState,cameraState),time) = do
nextUserState <- userSimFun time userState
return (nextUserState, cameraState)
cameraState0 = makeCamera $ fromMaybe defaultCamera (optInitialCamera opts)
kmCallback (state, camState) k0 k1 _ _ = (state, cameraKeyboardMouse camState k0 k1)
motionCallback (state, cameraState) pos = (state, cameraMotion cameraState pos)
setCameraFun (_,cameraState) = setCamera cameraState
play :: Real b =>
Options
-> Double
-> world
-> (world -> (VisObject b, Maybe Cursor))
-> (Float -> world -> world)
-> (world -> IO ())
-> Maybe (world -> Key -> KeyState -> Modifiers -> Position -> world)
-> Maybe (world -> Position -> world)
-> Maybe (world -> Position -> world)
-> IO ()
play opts ts userState0 userDrawFun userSimFun =
vis opts ts userState0 simFun drawFun
where
drawFun (userState, _) = return $ userDrawFun userState
simFun (userState,time) = return $ userSimFun time userState
playIO :: Real b =>
Options
-> Double
-> world
-> (world -> IO (VisObject b, Maybe Cursor))
-> (Float -> world -> IO world)
-> (world -> IO ())
-> Maybe (world -> Key -> KeyState -> Modifiers -> Position -> world)
-> Maybe (world -> Position -> world)
-> Maybe (world -> Position -> world)
-> IO ()
playIO opts ts userState0 userDrawFun userSimFun =
vis opts ts userState0 simFun drawFun
where
drawFun (userState, _) = userDrawFun userState
simFun (userState,time) = userSimFun time userState
defaultCamera :: Camera0
defaultCamera =
Camera0
{ phi0 = 60
, theta0 = 20
, rho0 = 7}