Skip to content

Commit

Permalink
some progress on spritePath
Browse files Browse the repository at this point in the history
  • Loading branch information
Nik Nyby committed Apr 27, 2010
1 parent fa60f7d commit d5872fa
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
71 changes: 44 additions & 27 deletions Bindings.hs
Expand Up @@ -12,14 +12,17 @@ import State
import Sprite import Sprite


keyboardMouse :: Window -> IORef Env -> Key -> KeyState -> Modifiers -> Position keyboardMouse :: Window -> IORef Env -> Key -> KeyState -> Modifiers -> Position
-> IO () -> IO ()
keyboardMouse _ env key state _ _ = do keyboardMouse _ env key state _ _ = do
e <- get env e <- get env


-- TODO: put these modes in different files, maybe
let let
dispatchAction = case (mode $ vars $ e) of dispatchAction = case (mode$vars$e) of
Intro -> introAction
Animator -> animatorAction Animator -> animatorAction
Intro -> introAction
Play -> playAction
Record -> recordAction


env $= dispatchAction e key state env $= dispatchAction e key state


Expand All @@ -28,9 +31,11 @@ motion env pos = do
e <- get env e <- get env


let let
dispatchMotion = case (mode $ vars $ e) of dispatchMotion = case (mode$vars$e) of
Intro -> introMotion
Animator -> animatorMotion Animator -> animatorMotion
Intro -> introMotion
Play -> playMotion
Record -> recordMotion


env $= dispatchMotion e pos env $= dispatchMotion e pos


Expand All @@ -39,19 +44,22 @@ motion env pos = do
- keyboard/mouse buttons - keyboard/mouse buttons
-} -}


introAction :: Env -> Key -> KeyState -> Env animatorAction, introAction, playAction, recordAction :: Env -> Key -> KeyState -> Env
introAction (Env v s) (MouseButton _) Down =
Env (v {mode = Animator}) s
introAction e _ _ = e

animatorAction :: Env -> Key -> KeyState -> Env
-- place a sprite -- place a sprite
animatorAction e (MouseButton RightButton) Down = animatorAction e (MouseButton RightButton) Down =
e { sprites = (makeSprite (mousePos $ vars $ e)) : sprites e } e { sprites = ( (makeSprite (mousePos$vars$e)) { spritePath = mkPath } )
: sprites e }
where
-- Make the initial animation path for this sprite, using (1000,1000) as
-- a dummy value for "not visible"
mkPath :: [Position]
mkPath = replicate (animClock$vars$e) (Position 1000 1000) ++ [(mousePos$vars$e)]


animatorAction e (MouseButton LeftButton) Down = animatorAction e (MouseButton LeftButton) Down =
--
-- TODO: just look at this mess!
--
e { sprites = (updateSelected . updateDragged) (sprites e), e { sprites = (updateSelected . updateDragged) (sprites e),

-- TODO: map over MmaMenu? -- TODO: map over MmaMenu?
vars = (vars e) { menu = MmaMenu { vars = (vars e) { menu = MmaMenu {
playButton = updateButton $ (playButton$menu$vars$e), playButton = updateButton $ (playButton$menu$vars$e),
Expand All @@ -72,11 +80,7 @@ animatorAction e (MouseButton LeftButton) Down =
} }
} }
} }

where where
--
-- TODO: just look at this mess!
--
updateSelected :: [Sprite] -> [Sprite] updateSelected :: [Sprite] -> [Sprite]
updateSelected ss = map (\s -> s {selected=True}) (spriteUnder ss) ++ updateSelected ss = map (\s -> s {selected=True}) (spriteUnder ss) ++
map (\s -> s {selected=False}) (ss \\ (spriteUnder ss)) map (\s -> s {selected=False}) (ss \\ (spriteUnder ss))
Expand All @@ -88,10 +92,6 @@ animatorAction e (MouseButton LeftButton) Down =
spriteUnder :: [Sprite] -> [Sprite] spriteUnder :: [Sprite] -> [Sprite]
spriteUnder ss = oneOrNone $ filter ((within mp) . rectangle) ss spriteUnder ss = oneOrNone $ filter ((within mp) . rectangle) ss


-- stupid... Maybe I should learn how to use Maybe?
oneOrNone :: [a] -> [a]
oneOrNone x = if length x >= 1 then [head x] else []

updateButton :: MmaButton -> MmaButton updateButton :: MmaButton -> MmaButton
updateButton b = b { buttonState = if within mp (buttonRect b) updateButton b = b { buttonState = if within mp (buttonRect b)
then not $ buttonState b then not $ buttonState b
Expand All @@ -100,6 +100,10 @@ animatorAction e (MouseButton LeftButton) Down =
updateWindow :: MmaWindow -> MmaWindow updateWindow :: MmaWindow -> MmaWindow
updateWindow w = w updateWindow w = w


-- stupid... Maybe I should learn how to use Maybe?
oneOrNone :: [a] -> [a]
oneOrNone x = if length x >= 1 then [head x] else []

mp :: Position mp :: Position
mp = mousePos $ vars $ e mp = mousePos $ vars $ e


Expand All @@ -111,16 +115,29 @@ animatorAction e (MouseButton LeftButton) Up =


animatorAction e _ _ = e animatorAction e _ _ = e


introAction (Env v s) (MouseButton _) Down =
Env (v {mode = Animator}) s
introAction e _ _ = e

playAction e _ _ = e

recordAction e _ _ = e


{- {-
- mouse motion - mouse motion (mouse button held down)
-} -}


introMotion :: Env -> Position -> Env
introMotion e _ = e

-- drag a sprite -- drag a sprite
animatorMotion :: Env -> Position -> Env animatorMotion, introMotion, playMotion, recordMotion :: Env -> Position -> Env
animatorMotion (Env v s) p = animatorMotion (Env v s) p =
-- update mouse position and any sticky sprites -- update mouse position and any sticky sprites
Env v{mousePos = p} $ map (dragSprite p) s Env v{mousePos = p} $ map (\x -> if sticky x then dragSprite p x else x) s

introMotion e _ = e

playMotion e _ = e

recordMotion (Env v s) p =
-- update mouse position and any sticky sprites
Env v{mousePos = p}
$ map (\x -> if sticky x then dragSpriteUpdatingPath p x else x) s
9 changes: 4 additions & 5 deletions Render.hs
Expand Up @@ -31,16 +31,15 @@ drawIntro e t = do


drawSprite :: Sprite -> IO () drawSprite :: Sprite -> IO ()
drawSprite s = do drawSprite s = do
currentColor $= Color4 0.2 0 0.3 0
renderPrimitive Quads $ mapM_ vertex $ spritePoints s

-- TODO: why isn't this transparent?
if selected s if selected s
then do then do
currentColor $= Color4 0.85 0 0.4 0.8 currentColor $= Color4 0.85 0 0.4 0
renderPrimitive Quads $ mapM_ vertex $ selectPoints s renderPrimitive Quads $ mapM_ vertex $ selectPoints s
else return () else return ()


currentColor $= Color4 0.2 0 0.3 0
renderPrimitive Quads $ mapM_ vertex $ spritePoints s

-- TODO: is there a better way???? -- TODO: is there a better way????
drawMenu :: MmaMenu -> MmaTextures -> IO () drawMenu :: MmaMenu -> MmaTextures -> IO ()
drawMenu m t = do drawMenu m t = do
Expand Down
25 changes: 15 additions & 10 deletions Sprite.hs
Expand Up @@ -5,6 +5,7 @@ module Sprite (
selectPoints, selectPoints,
initDragSprite, initDragSprite,
dragSprite, dragSprite,
dragSpriteUpdatingPath,
) where ) where
import Graphics.UI.GLUT import Graphics.UI.GLUT


Expand All @@ -14,12 +15,16 @@ import Util
data Sprite = data Sprite =
Sprite { Sprite {
rectangle :: Rectangle, rectangle :: Rectangle,

-- recorded path -- recorded path
spritePath :: [Position], spritePath :: [Position],

-- is it being dragged? -- is it being dragged?
sticky :: Bool, sticky :: Bool,

-- is it selected? -- is it selected?
selected :: Bool, selected :: Bool,

-- mouse offset for dragging -- mouse offset for dragging
offset :: Pos offset :: Pos
} deriving (Show, Eq) } deriving (Show, Eq)
Expand All @@ -33,7 +38,7 @@ spritePoints s = vertexRect $ rectangle s


-- calculate a rectangle around the sprite -- calculate a rectangle around the sprite
selectPoints :: Sprite -> [Vertex2 GLdouble] selectPoints :: Sprite -> [Vertex2 GLdouble]
selectPoints s = vertexRect $ boxAroundRect (rectangle s) 8.0 selectPoints s = vertexRect $ boxAroundRect (rectangle s) 4.0


-- start dragging a sprite -- start dragging a sprite
initDragSprite :: Position -> Sprite -> Sprite initDragSprite :: Position -> Sprite -> Sprite
Expand All @@ -43,12 +48,12 @@ initDragSprite offs s =
myPos = ( (rectX$rectangle$s),(rectY$rectangle$s) ) myPos = ( (rectX$rectangle$s),(rectY$rectangle$s) )


dragSprite :: Position -> Sprite -> Sprite dragSprite :: Position -> Sprite -> Sprite
dragSprite p s = dragSprite p s = s{ rectangle = newRect }
if sticky s where
then s{ rectangle = newRect } newRect :: Rectangle
else s newRect = (rectangle s){rectX = newX, rectY = newY}
where
newRect :: Rectangle (newX,newY) = posOp (+) (posConv p) (offset s)
newRect = (rectangle s){rectX = newX, rectY = newY}

dragSpriteUpdatingPath :: Position -> Sprite -> Sprite
(newX,newY) = posOp (+) (posConv p) (offset s) dragSpriteUpdatingPath p s = dragSprite p s { spritePath = spritePath s ++ [p] }
12 changes: 10 additions & 2 deletions State.hs
Expand Up @@ -19,6 +19,7 @@ initialEnvironment :: Env
initialEnvironment = initialEnvironment =
Env ( Vars { Env ( Vars {
clock = 0, clock = 0,
animClock = 0,
mousePos = Position 0 0, mousePos = Position 0 0,
menu = initialMenu, menu = initialMenu,
mode = Intro } mode = Intro }
Expand All @@ -27,11 +28,18 @@ initialEnvironment =


data Vars = Vars data Vars = Vars
{ {
-- the uptime
clock :: Int, clock :: Int,

-- current position of the Animator's clock
animClock :: Int,

mousePos :: Position, mousePos :: Position,

menu :: MmaMenu, menu :: MmaMenu,

mode :: Mode mode :: Mode
} deriving Show } deriving Show


data Mode = Animator | Intro data Mode = Animator | Intro | Play | Record
deriving (Show, Eq) deriving (Show, Eq)

0 comments on commit d5872fa

Please sign in to comment.