Created
October 23, 2014 17:10
-
-
Save vagmi/5965859811a22d15b1c1 to your computer and use it in GitHub Desktop.
First Elm experiment
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 | |
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