|
1 | 1 | // upload to ram via espruino web ide while in development. |
2 | 2 | // TODO: |
3 | | -// - [ ] make the ui agnostic to screen size |
4 | 3 | { |
5 | 4 |
|
6 | 5 | let level = 0; |
|
18 | 17 | if (!options) { options = {}; } |
19 | 18 | let cumulativeDxPlusDy = 0; |
20 | 19 |
|
21 | | - let dialRect = options.dialRect || { |
22 | | - x: 0, y: 0, x2: g.getWidth(), y2: g.getHeight(), |
| 20 | + const DIAL_RECT = options.dialRect || { |
| 21 | + x: 0, y: 0, x2: g.getWidth()-1, y2: g.getHeight()-1, |
23 | 22 | w: g.getWidth(), h: g.getHeight() |
24 | 23 | }; |
| 24 | + const ORIGO = { x: DIAL_RECT.x + DIAL_RECT.w / 2, y: DIAL_RECT.y + DIAL_RECT.h / 2 }; |
25 | 25 |
|
26 | | - let stepsPerWholeTurn = options.stepsPerWholeTurn || 10; |
27 | | - let triggerDistance = 50 * (10 / stepsPerWholeTurn) * dialRect.w / 176; // baseDistance * stepsPerWholeTurnScaling * rectangeScaling. |
| 26 | + const BASE_RECT_W = 176; // Bangle.js 2 screen pixel width. |
| 27 | + const STEPS_PER_WHOLE_TURN = options.stepsPerWholeTurn || 10; |
| 28 | + const THRESHOLD = 50 * (10 / STEPS_PER_WHOLE_TURN) * (DIAL_RECT.w / BASE_RECT_W); // baseThreshold * stepsPerWholeTurnScaling * rectangeScaling. |
28 | 29 |
|
29 | | - let origo = { x: dialRect.x + dialRect.w / 2, y: dialRect.y + dialRect.h / 2 }; |
30 | | - let dragHandler = function (e) { |
| 30 | + const DRAG_HANDLER = function (e) { |
31 | 31 | "ram" |
32 | 32 |
|
33 | | - if (!(e.y >= dialRect.y && e.y < dialRect.y2 && |
34 | | - e.x >= dialRect.x && e.x < dialRect.x2)) { return; } |
| 33 | + if (!(e.y >= DIAL_RECT.y && e.y <= DIAL_RECT.y2 && |
| 34 | + e.x >= DIAL_RECT.x && e.x <= DIAL_RECT.x2)) { return; } |
35 | 35 |
|
36 | | - if (e.y < origo.y) { cumulativeDxPlusDy += e.dx; } else { cumulativeDxPlusDy -= e.dx; } |
37 | | - if (e.x < origo.x) { cumulativeDxPlusDy -= e.dy; } else { cumulativeDxPlusDy += e.dy; } |
| 36 | + if (e.y < ORIGO.y) { cumulativeDxPlusDy += e.dx; } else { cumulativeDxPlusDy -= e.dx; } |
| 37 | + if (e.x < ORIGO.x) { cumulativeDxPlusDy -= e.dy; } else { cumulativeDxPlusDy += e.dy; } |
38 | 38 |
|
39 | 39 | let onStep = (step) => { |
40 | 40 | Bangle.buzz(20, 0.2) |
41 | | - cumulativeDxPlusDy -= triggerDistance * step; |
| 41 | + cumulativeDxPlusDy -= THRESHOLD * step; |
42 | 42 | cb(step); |
43 | 43 | } |
44 | 44 |
|
45 | | - if (cumulativeDxPlusDy > triggerDistance) { |
| 45 | + if (cumulativeDxPlusDy > THRESHOLD) { |
46 | 46 | onStep(1); |
47 | 47 | } |
48 | | - if (cumulativeDxPlusDy < -triggerDistance) { |
| 48 | + if (cumulativeDxPlusDy < -THRESHOLD) { |
49 | 49 | onStep(-1); |
50 | 50 | } |
51 | 51 |
|
52 | 52 | E.stopEventPropagation(); |
53 | 53 | } |
54 | 54 |
|
55 | | - Bangle.prependListener("drag", dragHandler); |
| 55 | + Bangle.prependListener("drag", DRAG_HANDLER); |
56 | 56 | } |
57 | 57 |
|
58 | 58 | // Trying it out: |
59 | | - dial(callback, { stepsPerWholeTurn: 15, dialRect: { |
60 | | - x: 0, y: 0, x2: g.getWidth()/2, y2: g.getHeight()/2, |
61 | | - w: g.getWidth()/2, h: g.getHeight()/2 |
62 | | - }}); |
| 59 | + dial(callback, { |
| 60 | + stepsPerWholeTurn: 15/*, dialRect: { |
| 61 | + x: 0, y: 0, x2: g.getWidth() / 2, y2: g.getHeight() / 2, |
| 62 | + w: g.getWidth() / 2, h: g.getHeight() / 2 |
| 63 | + }*/ |
| 64 | + }); |
63 | 65 | } |
64 | 66 |
|
65 | 67 |
|
0 commit comments