Created
August 5, 2014 14:38
-
-
Save uehaj/c1f0b3f6e97baeea04ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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