Last active
March 16, 2016 17:39
-
-
Save lovasoa/15d65b3c09dcef93ffdb to your computer and use it in GitHub Desktop.
Start of work of a gui for drawing graphs in elm
This file contains hidden or 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
module Counter where | |
import Array | |
import Graphics.Element exposing (..) | |
import Graphics.Collage exposing (..) | |
import Color exposing (..) | |
import Mouse | |
-- MODEL | |
type alias Point = (Float,Float) | |
type alias Points = Array.Array Point | |
type alias Arrow = (Point,Point) | |
type alias Model = { | |
points: Points, | |
arrows: List (Int,Int), | |
curLineStart: Maybe Point, | |
mouse: Point | |
} | |
startModel = { | |
points=Array.fromList [(0,0), (10,-200)], | |
arrows=[(0,1)], | |
curLineStart = Nothing, | |
mouse = (0,0) | |
} | |
-- UPDATE | |
type Action = Pressed Bool | Move Point | |
update : Action -> Model -> Model | |
update action model = | |
case action of | |
Pressed isdown -> {model | | |
points = Array.push model.mouse model.points} | |
Move p -> {model | mouse = p} | |
-- VIEW | |
w = 500 | |
h = 500 | |
view : Model -> Element | |
view model = | |
collage w h | |
((List.map showPoint <| Array.toList model.points) ++ | |
(List.map (showArrow model.points) model.arrows)) | |
showPoint: (Float, Float) -> Form | |
showPoint point = circle 10 | |
|> filled red | |
|> move point | |
showArrow : Points -> (Int,Int) -> Form | |
showArrow points (a,b) = | |
let getPoint n = Maybe.withDefault (0,0) (Array.get n points) | |
(p,q) = (getPoint a, getPoint b) in | |
segment p q | |
|> traced defaultLine | |
convertCoords : (Int, Int) -> (Float,Float) | |
convertCoords (x,y) = (-w/2 + toFloat x, h/2 - toFloat y) | |
mousepos = Signal.map convertCoords Mouse.position | |
main = | |
Signal.foldp update startModel | |
(Signal.mergeMany [Signal.map Move mousepos, Signal.map Pressed Mouse.isDown]) | |
|> Signal.map view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment