Last active
December 10, 2015 14:44
-
-
Save chrisschreiner/666707181f4eb394384b to your computer and use it in GitHub Desktop.
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
module Main (..) where | |
import Maybe exposing (Maybe, withDefault) | |
import List exposing (head, length) | |
import Graphics.Collage as GC | |
import Graphics.Element as GE | |
import Graphics.Element | |
import Graphics.Collage exposing (rotate) | |
import Signal exposing (..) | |
import Mouse | |
import Color exposing (red, blue, green) | |
import Text | |
import Window | |
import Keyboard | |
import Char | |
import Time exposing (fps) | |
import String | |
{- | |
an update function that take some params and a model, and returns a new updated model | |
-} | |
formatFloatWithSeparator : String -> Int -> Float -> String | |
formatFloatWithSeparator separator digits number = | |
let | |
strNumber = toString number | |
i = String.indexes separator strNumber | |
n = (head i ?? (String.length strNumber)) + 1 | |
f = String.slice 0 n strNumber | |
p = String.padRight digits '0' (String.slice n (n + digits ) strNumber) | |
in | |
f ++ p | |
{- | |
main : GE.Element | |
main = | |
let | |
n = 123.321 | |
f = (\x -> Text.fromString ((toString x) ++ ": " ++ (formatFloat x n) ++ " ")) | |
in | |
GE.flow | |
GE.down | |
[ GE.leftAligned | |
<| Text.concat <| List.map f [0..5] | |
] | |
-} | |
formatFloat : Int -> Float -> String | |
formatFloat digits number = | |
formatFloatWithSeparator "." digits number | |
main : Signal GE.Element | |
main = | |
Signal.map5 scene pos pos2 Window.dimensions aIsPressed (Signal.foldp update model input) | |
-- Takes a rotation factor and a model, and returns an updated model (?) | |
update : Bool -> Model -> Model | |
update isPressed m = | |
{ m | |
| rotation = | |
m.rotation | |
+ (if isPressed then | |
1.0e-2 | |
else | |
0 | |
) | |
} | |
input : Signal Bool | |
input = | |
let | |
delta = Signal.map (\t -> t / 5) (fps 40) | |
in | |
Signal.sampleOn delta (Keyboard.isDown <| Char.toCode 'R') | |
type alias Model = | |
{ rotation : Float } | |
model : Model | |
model = | |
{ rotation = 0 } | |
type alias ThreeBools = | |
( Bool, Bool, Bool ) | |
aIsPressed : Signal ThreeBools | |
aIsPressed = | |
Signal.map3 | |
(,,) | |
(Keyboard.isDown <| Char.toCode 'A') | |
(Keyboard.isDown <| Char.toCode 'B') | |
(Keyboard.isDown <| Char.toCode 'C') | |
scene : List ( Int, Int ) -> ( Int, Int ) -> ( Int, Int ) -> ThreeBools -> Model -> GE.Element | |
scene a mousePosition ( w, h ) pressedAKey model = | |
let | |
( x, y ) = head a ?? ( 440, 440 ) | |
( x', y' ) = mousePosition | |
--red square | |
( dx, dy ) = ( toFloat x' - toFloat w / 2, toFloat h / 2 - toFloat y' ) | |
--blue textbox | |
( dx', dy' ) = ( toFloat x - toFloat w / 2, toFloat h / 2 - toFloat y ) | |
dxy' = ( toFloat x - toFloat w / 2, toFloat h / 2 - toFloat y ) | |
rotationFactor = atan2 dy dx | |
aKeyPressed = | |
case pressedAKey of | |
( True, _, _ ) -> | |
True | |
_ -> | |
False | |
bKeyPressed = | |
case pressedAKey of | |
( _, True, _ ) -> | |
True | |
_ -> | |
False | |
in | |
Graphics.Element.layers | |
[ GE.leftAligned | |
<| Text.concat | |
[ Text.fromString "type " | |
, Text.bold (Text.fromString (toString a)) | |
, Text.fromString " = Just a | Nothing" | |
] | |
, (GC.collage | |
w | |
h | |
[ GC.move | |
dxy' | |
(GC.group | |
[ filledPolygon | |
, rotate rotationFactor | |
<| headOfCoordList a | |
] | |
) | |
] | |
) | |
, GC.collage | |
w | |
h | |
[ rotate model.rotation | |
<| (GC.filled | |
(red) | |
(GC.rect | |
(if aKeyPressed then | |
100 | |
else | |
50 | |
) | |
(if bKeyPressed then | |
200 | |
else | |
50 | |
) | |
) | |
|> GC.move ( dx, dy ) | |
) | |
] | |
, GC.collage | |
w | |
h | |
[ GC.toForm | |
<| GE.centered | |
<| Text.style { myStyle | height = Just 50, color = Color.black } | |
<| Text.fromString (toString <| formatFloat 3 model.rotation) | |
] | |
] | |
type alias Coords = | |
( Int, Int ) | |
headOfCoordList : List Coords -> GC.Form | |
headOfCoordList a = | |
GC.toForm | |
<| GE.centered | |
<| Text.style myStyle | |
<| Text.fromString | |
<| toString | |
<| head a | |
?? ( 0, 0 ) | |
filledPolygon : GC.Form | |
filledPolygon = | |
GC.filled (Color.blue) (GC.ngon 5 35) | |
myStyle : Text.Style | |
myStyle = | |
{ typeface = [ "Helvetica Neue" ] | |
, height = Just 32 | |
, color = Color.yellow | |
, bold = True | |
, italic = False | |
, line = Just Text.Under | |
} | |
(??) : Maybe a -> a -> a | |
(??) a b = | |
withDefault b a | |
infixl 2 ?? | |
listOfStrings : List String | |
listOfStrings = | |
[ "Peter" ] | |
pos2 : Signal ( Int, Int ) | |
pos2 = | |
Mouse.position | |
pos : Signal (List ( Int, Int )) | |
pos = | |
Signal.foldp | |
(::) | |
[ ( 12, 12 ) ] | |
(sampleOn Mouse.clicks Mouse.position) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment