Skip to content

Commit db3d2f3

Browse files
author
thyttan
committed
dial: wip changes
1 parent fb8dfaf commit db3d2f3

File tree

1 file changed

+38
-49
lines changed

1 file changed

+38
-49
lines changed

modules/dial.js

Lines changed: 38 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,60 @@
11
// upload to ram via espruino web ide while in development.
2-
2+
// TODO:
3+
// - [ ] make the ui agnostic to screen size
34
{
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)
95

6+
let level = 0;
107

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+
}
1215

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;
2020

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+
};
2225

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.
2427

25-
}
28+
let origo = { x: dialRect.x + dialRect.w / 2, y: dialRect.y + dialRect.h / 2 };
29+
let dragHandler = function (e) {
30+
"ram"
2631

27-
*/
32+
if (!(e.y >= dialRect.y && e.y < dialRect.y2 &&
33+
e.x >= dialRect.x && e.x < dialRect.x2)) {return;}
2834

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; }
4037

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+
}
4443

45-
let onStep = (step)=>{
46-
if (step>0) {
47-
level ++;
44+
if (cumulativeDxPlusDy > triggerDistance) {
45+
onStep(1);
4846
}
49-
if (step<0) {
50-
level --;
47+
if (cumulativeDxPlusDy < -triggerDistance) {
48+
onStep(-1);
5149
}
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-
}
5750

58-
if (cumulativeDxPlusDy > 50) {
59-
onStep(1);
60-
}
61-
if (cumulativeDxPlusDy < -50) {
62-
onStep(-1);
51+
E.stopEventPropagation();
6352
}
64-
6553

54+
Bangle.prependListener("drag", dragHandler);
6655
}
6756

68-
Bangle.on("drag", dragHandler);
57+
dial(callback);
6958
}
7059

7160

0 commit comments

Comments
 (0)