|
1 | 1 | // upload to ram via espruino web ide while in development. |
2 | | - |
| 2 | +// TODO: |
| 3 | +// - [ ] make the ui agnostic to screen size |
3 | 4 | { |
4 | | - /* |
5 | | -Bangle.on('drag', function(event) { ... }); |
6 | | -Parameters |
7 | | -
|
8 | | -event - Object of form {x,y,dx,dy,b} containing touch coordinates, difference in touch coordinates, and an integer b containing number of touch points (currently 1 or 0) |
9 | 5 |
|
| 6 | + let level = 0; |
10 | 7 |
|
11 | | - */ |
| 8 | + let callback = (step) => { |
| 9 | + level += step; |
| 10 | + { //development |
| 11 | + print(step, level); |
| 12 | + g.clear().setFont("Vector:40").setFontAlign(0, 0).drawString(level, 85, 85); |
| 13 | + } |
| 14 | + } |
12 | 15 |
|
13 | | -let cumulativeDxPlusDy = 0; |
14 | | -let level = 0; |
15 | | -let dragHandler = function(e) { "ram" |
16 | | - // Use cross product to decide if dialing down/left/screwing out or up/right/screwing in? |
17 | | -/* |
18 | | - // Quadrants: (1 || 2 || 3 || 4) |
19 | | - if ((e.dx>0&&e.dy>0) || (e.dx>0&&e.dy>0) || (e.dx>0&&e.dy>0) || (e.dx>0&&e.dy>0)) { //screwing into the screen - increase |
| 16 | + let dial = function (cb, options) { |
| 17 | + "ram" |
| 18 | + if (!options) { options = {}; } |
| 19 | + let cumulativeDxPlusDy = 0; |
20 | 20 |
|
21 | | - } |
| 21 | + let dialRect = options.dialRect || { |
| 22 | + x: 0, y: 0, x2: g.getWidth(), y2: g.getHeight(), |
| 23 | + w: g.getWidth() / 2, h: g.getHeight() / 2 |
| 24 | + }; |
22 | 25 |
|
23 | | - if ((e.dx>0&&e.dy>0) || (e.dx>0&&e.dy>0) || (e.dx>0&&e.dy>0) || (e.dx>0&&e.dy>0)) { //screwing out of the screen - decrease |
| 26 | + let triggerDistance = 45; // TODO: triggerDistance -> 45 * g.getWidth() / 176 . To remap to screens of different resolutions. |
24 | 27 |
|
25 | | - } |
| 28 | + let origo = { x: dialRect.x + dialRect.w / 2, y: dialRect.y + dialRect.h / 2 }; |
| 29 | + let dragHandler = function (e) { |
| 30 | + "ram" |
26 | 31 |
|
27 | | -*/ |
| 32 | + if (!(e.y >= dialRect.y && e.y < dialRect.y2 && |
| 33 | + e.x >= dialRect.x && e.x < dialRect.x2)) {return;} |
28 | 34 |
|
29 | | - const PREV_DX_PLUS_DY = cumulativeDxPlusDy; |
30 | | - if (e.y<g.getHeight()/2 ) { |
31 | | - cumulativeDxPlusDy += e.dx; |
32 | | - } else { |
33 | | - cumulativeDxPlusDy -= e.dx; |
34 | | - } |
35 | | - if (e.x<g.getWidth()/2 ) { |
36 | | - cumulativeDxPlusDy -= e.dy; |
37 | | - } else { |
38 | | - cumulativeDxPlusDy += e.dy; |
39 | | - } |
| 35 | + if (e.y < origo.y) { cumulativeDxPlusDy += e.dx; } else { cumulativeDxPlusDy -= e.dx; } |
| 36 | + if (e.x < origo.x) { cumulativeDxPlusDy -= e.dy; } else { cumulativeDxPlusDy += e.dy; } |
40 | 37 |
|
41 | | - if (Math.abs(cumulativeDxPlusDy)<Math.abs(PREV_DX_PLUS_DY)) { |
42 | | - cumulativeDxPlusDy = 0; |
43 | | - } |
| 38 | + let onStep = (step) => { |
| 39 | + Bangle.buzz(20, 0.2) |
| 40 | + cumulativeDxPlusDy -= triggerDistance * step; |
| 41 | + cb(step); |
| 42 | + } |
44 | 43 |
|
45 | | - let onStep = (step)=>{ |
46 | | - if (step>0) { |
47 | | - level ++; |
| 44 | + if (cumulativeDxPlusDy > triggerDistance) { |
| 45 | + onStep(1); |
48 | 46 | } |
49 | | - if (step<0) { |
50 | | - level --; |
| 47 | + if (cumulativeDxPlusDy < -triggerDistance) { |
| 48 | + onStep(-1); |
51 | 49 | } |
52 | | - print(step, level); |
53 | | - g.clear().setFont("Vector:40").setFontAlign(0,0).drawString(level, 85, 85); |
54 | | - Bangle.buzz(20, 0.2) |
55 | | - cumulativeDxPlusDy = 0; |
56 | | - } |
57 | 50 |
|
58 | | - if (cumulativeDxPlusDy > 50) { |
59 | | - onStep(1); |
60 | | - } |
61 | | - if (cumulativeDxPlusDy < -50) { |
62 | | - onStep(-1); |
| 51 | + E.stopEventPropagation(); |
63 | 52 | } |
64 | | - |
65 | 53 |
|
| 54 | + Bangle.prependListener("drag", dragHandler); |
66 | 55 | } |
67 | 56 |
|
68 | | -Bangle.on("drag", dragHandler); |
| 57 | + dial(callback); |
69 | 58 | } |
70 | 59 |
|
71 | 60 |
|
0 commit comments