|
6 | 6 |
|
7 | 7 | (def service-id "00ff")
|
8 | 8 | (def characteristic-id "ff01")
|
9 |
| -(def joystick-img (js/require "./images/joystick.png")) |
10 | 9 |
|
11 | 10 | (defn ble-send [{:keys [lf lb rf rb]}]
|
12 | 11 | (let [current-device (subscribe [:get-current-device])
|
|
20 | 19 | rate-y (/ (- p-y view-y harf-h) harf-h)]
|
21 | 20 | {:x rate-x :y rate-y}))
|
22 | 21 |
|
23 |
| -(defn joystick [id on-move on-release] |
| 22 | +(defn joystick [id on-move on-release & [{:keys [width height] :or {width 300 height 300}}]] |
24 | 23 | (let [view-x (r/atom nil)
|
25 | 24 | 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) |
28 | 33 | ref (str "joystick-" id)
|
29 | 34 | ref-obj (r/atom nil)
|
30 | 35 | update-view-x-y #(.measure @ref-obj (fn [fx fy w h px py]
|
31 | 36 | (reset! view-x px)
|
32 | 37 | (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)) |
33 | 46 | action (fn [evt]
|
34 | 47 | (-> (rate-x-y (.-pageX (.-nativeEvent evt))
|
35 | 48 | (.-pageY (.-nativeEvent evt))
|
|
38 | 51 | (r/create-class
|
39 | 52 | {:reagent-render
|
40 | 53 | (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)}}]]) |
56 | 80 |
|
57 | 81 | :component-did-mount
|
58 | 82 | #(reset! ref-obj (-> (.-refs %)
|
|
0 commit comments