Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea99ce4

Browse files
committed
Finish radar UI
1 parent 04dcd69 commit ea99ce4

File tree

3 files changed

+92
-14
lines changed

3 files changed

+92
-14
lines changed

radar/radar-ui/elm-package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"dependencies": {
1111
"elm-lang/core": "5.1.1 <= v < 6.0.0",
1212
"elm-lang/html": "2.0.0 <= v < 3.0.0",
13-
"elm-lang/svg": "2.0.0 <= v < 3.0.0"
13+
"elm-lang/svg": "2.0.0 <= v < 3.0.0",
14+
"elm-lang/websocket": "1.0.2 <= v < 2.0.0"
1415
},
1516
"elm-version": "0.18.0 <= v < 0.19.0"
1617
}

radar/radar-ui/src/Main.elm

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import Html exposing (Html, div, program)
44
import Html.Attributes exposing (style)
55
import Types exposing (..)
66
import Dict exposing (Dict)
7+
import List
78
import Svg exposing (svg, circle)
89
import Svg.Attributes exposing (cx, cy, r, width, height, fill, viewBox)
10+
import WebSocket exposing (listen)
11+
import Json.Decode as Decode exposing (Decoder, field, float, decodeString)
912

1013

1114
init =
@@ -16,33 +19,98 @@ update : Msg -> Model -> ( Model, Cmd Msg )
1619
update msg model =
1720
case msg of
1821
Noop ->
19-
init
22+
model ! []
23+
24+
Frame msg ->
25+
let
26+
trigoPoint =
27+
msg
28+
|> decodeString trigoPointDecoder
29+
|> Result.withDefault { angle = 0, distance = 0 }
30+
31+
angle =
32+
trigoPoint.angle
33+
34+
point =
35+
trigoToCartesianPoint trigoPoint
36+
37+
_ =
38+
Debug.log "trigoPoint" point
39+
in
40+
{ model | points = Dict.insert angle point model.points } ! []
41+
42+
43+
trigoPointDecoder : Decoder TrigoPoint
44+
trigoPointDecoder =
45+
Decode.map2 TrigoPoint
46+
(field "angle" float)
47+
(field "distance" float)
48+
49+
50+
trigoToCartesianPoint : TrigoPoint -> Point
51+
trigoToCartesianPoint { distance, angle } =
52+
let
53+
zoom =
54+
(*) 5
55+
56+
x =
57+
distance
58+
|> (*) (cos (degrees angle))
59+
|> floor
60+
|> zoom
61+
62+
y =
63+
distance
64+
|> (*) (sin (degrees (angle + 180)))
65+
|> floor
66+
|> zoom
67+
in
68+
( x, y )
2069

2170

2271
subscriptions : Model -> Sub Msg
2372
subscriptions model =
24-
Sub.none
73+
listen "ws://localhost:9999" Frame
74+
75+
76+
viewPoint : Point -> Html Msg
77+
viewPoint ( x, y ) =
78+
circle
79+
[ cx (toString (250 + x))
80+
, cy (toString (250 + y))
81+
, r "5"
82+
, fill "#aacc66"
83+
]
84+
[]
2585

2686

2787
view : Model -> Html Msg
2888
view model =
29-
div
30-
[ style
31-
[ ( "width", "500px" )
32-
, ( "display", "block" )
33-
, ( "margin", "50px auto" )
34-
]
35-
]
36-
[ svg [ width "500", height "250", viewBox "0 0 500 250" ]
37-
[ circle
89+
let
90+
layout =
91+
circle
3892
[ cx "250"
3993
, cy "250"
4094
, r "250"
4195
, fill "#efefef"
4296
]
4397
[]
98+
99+
circles =
100+
model.points
101+
|> Dict.values
102+
|> List.map viewPoint
103+
in
104+
div
105+
[ style
106+
[ ( "width", "500px" )
107+
, ( "display", "block" )
108+
, ( "margin", "50px auto" )
109+
]
110+
]
111+
[ svg [ width "500", height "250", viewBox "0 0 500 250" ]
112+
(layout :: circles)
44113
]
45-
]
46114

47115

48116
main =

radar/radar-ui/src/Types.elm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@ import Dict exposing (Dict)
44

55

66
type alias Angle =
7-
Int
7+
Float
8+
9+
10+
type alias Distance =
11+
Float
812

913

1014
type alias Point =
1115
( Int, Int )
1216

1317

18+
type alias TrigoPoint =
19+
{ angle : Angle, distance : Float }
20+
21+
1422
type alias Model =
1523
{ points : Dict Angle Point
1624
}
1725

1826

1927
type Msg
2028
= Noop
29+
| Frame String

0 commit comments

Comments
 (0)