Skip to content

Instantly share code, notes, and snippets.

@vagmi
Created October 23, 2014 17:10
Show Gist options
  • Save vagmi/5965859811a22d15b1c1 to your computer and use it in GitHub Desktop.
Save vagmi/5965859811a22d15b1c1 to your computer and use it in GitHub Desktop.
First Elm experiment
import Mouse
import Window
relativeMouse : (Int, Int) -> (Int, Int) -> (Int, Int)
relativeMouse (dimx, dimy) (x, y) = let (cx, cy) = center (dimx, dimy)
in (x - cx, cy - y)
center : (Int, Int) -> (Int, Int)
center (x, y) = ( x//2, y//2)
drawCirc : Pill -> Form
drawCirc {size, pos, col} = circle size |> filled col
|> move (toFloat <| fst pos, toFloat <| snd pos)
type Pill = {pos: (Int,Int),col:Color,size: Float}
defaultPill = {pos= (0,0), col=red, size=15}
type Game = {pills: [Pill], points: [(Float, Float)]}
game = {pills = [], points = []}
stepGame : Event -> Game -> Game
stepGame event game =
case event of
Click () ((w, h) as dim) mp -> let cpos = relativeMouse dim mp
newPills = {defaultPill | pos<-cpos} :: game.pills
in {game | pills <- newPills}
Draw ((w, h) as dim) mp isDown -> let cpos = relativeMouse dim mp
newPoints = (toFloat <| fst cpos,toFloat <| snd cpos) :: game.points
in (if isDown then {game | points<-newPoints} else game)
scene : (Int, Int) -> (Int, Int) -> Game -> Element
scene ((w, h) as dim) mp g = let cpos = relativeMouse dim mp
pointer = {defaultPill | pos<-cpos, col<-green }
pills = map drawCirc <| pointer :: g.pills
forms = traced (solid purple) (path g.points) :: pills
in color lightGray <| container w h middle
<| color white
<| collage 400 400 forms
data Event = Draw (Int, Int) (Int, Int) Bool | Click () (Int, Int) (Int, Int)
event = merges [lift3 Draw Window.dimensions Mouse.position Mouse.isDown
, lift3 Click Mouse.clicks Window.dimensions Mouse.position]
main = lift3 scene Window.dimensions Mouse.position <| foldp stepGame game event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment