Skip to content

Commit 17d8287

Browse files
author
thyttan
committed
dial: wip changes
1 parent c3b5500 commit 17d8287

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

modules/dial.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// upload to ram via espruino web ide while in development.
22
// TODO:
3-
// - [ ] make the ui agnostic to screen size
43
{
54

65
let level = 0;
@@ -18,48 +17,51 @@
1817
if (!options) { options = {}; }
1918
let cumulativeDxPlusDy = 0;
2019

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,
2322
w: g.getWidth(), h: g.getHeight()
2423
};
24+
const ORIGO = { x: DIAL_RECT.x + DIAL_RECT.w / 2, y: DIAL_RECT.y + DIAL_RECT.h / 2 };
2525

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

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) {
3131
"ram"
3232

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

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

3939
let onStep = (step) => {
4040
Bangle.buzz(20, 0.2)
41-
cumulativeDxPlusDy -= triggerDistance * step;
41+
cumulativeDxPlusDy -= THRESHOLD * step;
4242
cb(step);
4343
}
4444

45-
if (cumulativeDxPlusDy > triggerDistance) {
45+
if (cumulativeDxPlusDy > THRESHOLD) {
4646
onStep(1);
4747
}
48-
if (cumulativeDxPlusDy < -triggerDistance) {
48+
if (cumulativeDxPlusDy < -THRESHOLD) {
4949
onStep(-1);
5050
}
5151

5252
E.stopEventPropagation();
5353
}
5454

55-
Bangle.prependListener("drag", dragHandler);
55+
Bangle.prependListener("drag", DRAG_HANDLER);
5656
}
5757

5858
// 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+
});
6365
}
6466

6567

0 commit comments

Comments
 (0)