Skip to content

Instantly share code, notes, and snippets.

@gmp26
Last active August 29, 2015 14:07
Show Gist options
  • Save gmp26/6c105ad18fef37cb516f to your computer and use it in GitHub Desktop.
Save gmp26/6c105ad18fef37cb516f to your computer and use it in GitHub Desktop.
Drag2Rects.elm
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
@gmp26
Copy link
Author

gmp26 commented Oct 12, 2014

Drag 2 rectangles. I'm sure this can be simplified further

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment