Skip to content

Instantly share code, notes, and snippets.

@uehaj
Created August 5, 2014 14:38
Show Gist options
  • Save uehaj/c1f0b3f6e97baeea04ca to your computer and use it in GitHub Desktop.
Save uehaj/c1f0b3f6e97baeea04ca to your computer and use it in GitHub Desktop.
import Mouse
import Window
data Event = MouseUp | MouseDown | MouseMove Int Int
type Status = { mouseDown:Bool, points:[(Int, Int)] }
initialState : Status
initialState = Status False []
nextState : Event -> Status -> Status
nextState event state = case event of
MouseUp -> { state | mouseDown <- False }
MouseDown -> { state | mouseDown <- True }
MouseMove x y -> if state.mouseDown
then { state | points <- (x,y)::state.points }
else state
inputSignal : Signal Event
inputSignal = merges [ MouseMove <~ Mouse.x ~ Mouse.y
, (\b -> if b then MouseDown else MouseUp) <~ Mouse.isDown ]
currentState : Signal Status
currentState = foldp nextState initialState inputSignal
mousePosition : Int -> Int -> Int -> Int -> (Float, Float)
mousePosition x y w h = (toFloat x-(toFloat w/2), -(toFloat y)+(toFloat h/2))
main : Signal Element
main = let disp state w h =
collage w h (map (\(x,y) -> move (mousePosition x y w h) <| filled red (circle 4)) state.points )
in disp <~ currentState ~ Window.width ~ Window.height
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment