Last active
August 29, 2015 14:07
-
-
Save gmp26/6c105ad18fef37cb516f to your computer and use it in GitHub Desktop.
Drag2Rects.elm
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 Graphics.Input as Input | |
import Mouse | |
clicked = Input.input 0 | |
rectangle : Color -> Element | |
rectangle c = [rect 50 40 |> filled c] |> collage 50 50 | |
rect1 = Input.hoverable clicked.handle (\b -> if b then 1 else 0) (rectangle red) | |
rect2 = Input.hoverable clicked.handle (\b -> if b then 2 else 0) (rectangle green) | |
initialState = ((-50, 0), (50, 0)) | |
tfPair : (Int,Int) -> (Float, Float) | |
tfPair (m,n) = (toFloat <| m - 200, toFloat <| 200 - n) | |
step (down, start, pos, rect) state = | |
if down | |
then if | rect == 2 -> (fst state, tfPair pos) | |
| rect == 1 -> (tfPair pos, snd state) | |
| otherwise -> state | |
else state | |
inputs = (,,,) <~ Mouse.isDown ~ (sampleOn Mouse.isDown Mouse.position) ~ Mouse.position ~ (sampleOn Mouse.isDown clicked.signal) | |
state = foldp step initialState inputs | |
display offsets x = collage 400 400 [toForm rect1 |> move (fst offsets), toForm rect2 |> move (snd offsets)] `above` asText x | |
main = display <~ state ~ clicked.signal | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Drag 2 rectangles. I'm sure this can be simplified further