Skip to content

Commit 4b728dc

Browse files
committed
Show circle to joystick
1 parent 838e51e commit 4b728dc

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

images/joystick.png

-2.51 KB
Binary file not shown.

src/re_natal_esp32control_app/views/ble_control/common.cljs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
(def service-id "00ff")
88
(def characteristic-id "ff01")
9-
(def joystick-img (js/require "./images/joystick.png"))
109

1110
(defn ble-send [{:keys [lf lb rf rb]}]
1211
(let [current-device (subscribe [:get-current-device])
@@ -20,16 +19,30 @@
2019
rate-y (/ (- p-y view-y harf-h) harf-h)]
2120
{:x rate-x :y rate-y}))
2221

23-
(defn joystick [id on-move on-release]
22+
(defn joystick [id on-move on-release & [{:keys [width height] :or {width 300 height 300}}]]
2423
(let [view-x (r/atom nil)
2524
view-y (r/atom nil)
26-
view-w 300
27-
view-h 300
25+
view-w width
26+
view-h height
27+
center-r 75
28+
target-r 50
29+
posi-x-base (/ view-w 2)
30+
posi-y-base (/ view-h 2)
31+
value-x (r/atom 0)
32+
value-y (r/atom 0)
2833
ref (str "joystick-" id)
2934
ref-obj (r/atom nil)
3035
update-view-x-y #(.measure @ref-obj (fn [fx fy w h px py]
3136
(reset! view-x px)
3237
(reset! view-y py)))
38+
on-move (fn [{:keys [x y] :as xy}]
39+
(reset! value-x x)
40+
(reset! value-y y)
41+
(on-move xy))
42+
on-release (fn []
43+
(reset! value-x 0)
44+
(reset! value-y 0)
45+
(on-release))
3346
action (fn [evt]
3447
(-> (rate-x-y (.-pageX (.-nativeEvent evt))
3548
(.-pageY (.-nativeEvent evt))
@@ -38,21 +51,32 @@
3851
(r/create-class
3952
{:reagent-render
4053
(fn []
41-
[v.common/image
42-
{:source joystick-img
43-
:style {:resize-mode "cover"
44-
:background-color "#beb"
45-
:align-self "center"}}
46-
[v.common/view
47-
{:ref ref
48-
:style {:width view-w
49-
:height view-h}
50-
:on-layout #(update-view-x-y)
51-
:on-start-should-set-responder (fn [] true)
52-
:on-move-should-set-responder (fn [] true)
53-
:on-responder-grant #(action %)
54-
:on-responder-move #(action %)
55-
:on-responder-release #(on-release)}]])
54+
[v.common/view
55+
{:ref ref
56+
:style {:background-color "#beb"
57+
:align-self "center"
58+
:width view-w
59+
:height view-h}
60+
:on-layout #(update-view-x-y)
61+
:on-start-should-set-responder (fn [] true)
62+
:on-move-should-set-responder (fn [] true)
63+
:on-responder-grant #(action %)
64+
:on-responder-move #(action %)
65+
:on-responder-release #(on-release)}
66+
[v.common/view {:style {:background-color "#fff"
67+
:width (* center-r 2)
68+
:height (* center-r 2)
69+
:border-radius center-r
70+
:position "absolute"
71+
:top (- (/ view-h 2) center-r)
72+
:left (- (/ view-w 2) center-r)}}]
73+
[v.common/view {:style {:background-color "#777"
74+
:width (* target-r 2)
75+
:height (* target-r 2)
76+
:border-radius target-r
77+
:position "absolute"
78+
:top (- (+ posi-y-base (* posi-y-base @value-y)) target-r)
79+
:left (- (+ posi-x-base (* posi-x-base @value-x)) target-r)}}]])
5680

5781
:component-did-mount
5882
#(reset! ref-obj (-> (.-refs %)

0 commit comments

Comments
 (0)