Skip to content

Commit d6e8d4f

Browse files
author
thyttan
committed
Dial: new module providing a circular dial
Only the drag gesture functionality provided. I intend to add a companion module providing some relevant graphics, sharing the same options object.
1 parent fd5235d commit d6e8d4f

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

apps/modules/Dial.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
let Dial = function(cb, options) {
2+
"ram";
3+
const SCREEN_W = g.getWidth();
4+
const SCREEN_H = g.getHeight();
5+
6+
options = Object.assign(
7+
{ stepsPerWholeTurn : 8,
8+
dialRect : {
9+
x: 0,
10+
y: 0,
11+
x2: SCREEN_W - 1,
12+
y2: SCREEN_H - 1,
13+
w: SCREEN_W,
14+
h: SCREEN_H,
15+
},
16+
}, options);
17+
18+
const DIAL_RECT = options.dialRect;
19+
20+
const CENTER = {
21+
x: DIAL_RECT.x + DIAL_RECT.w / 2,
22+
y: DIAL_RECT.y + DIAL_RECT.h / 2,
23+
};
24+
25+
const BASE_SCREEN_W = 176;
26+
const STEPS_PER_TURN = options.stepsPerWholeTurn;
27+
const BASE_THRESHOLD = 50;
28+
const THRESHOLD =
29+
BASE_THRESHOLD *
30+
(10 / STEPS_PER_TURN) *
31+
(DIAL_RECT.w / BASE_SCREEN_W);
32+
33+
let cumulative = 0;
34+
35+
function onDrag(e) {
36+
"ram";
37+
38+
if (
39+
e.x < DIAL_RECT.x ||
40+
e.x > DIAL_RECT.x2 ||
41+
e.y < DIAL_RECT.y ||
42+
e.y > DIAL_RECT.y2
43+
) {
44+
return;
45+
}
46+
47+
if (e.y < CENTER.y) {
48+
cumulative += e.dx;
49+
} else {
50+
cumulative -= e.dx;
51+
}
52+
53+
if (e.x < CENTER.x) {
54+
cumulative -= e.dy;
55+
} else {
56+
cumulative += e.dy;
57+
}
58+
59+
function stepHandler(step) {
60+
cumulative -= THRESHOLD * step;
61+
cb(step);
62+
}
63+
64+
while (cumulative > THRESHOLD) {
65+
stepHandler(1);
66+
}
67+
while (cumulative < -THRESHOLD) {
68+
stepHandler(-1);
69+
}
70+
71+
E.stopEventPropagation();
72+
}
73+
74+
return onDrag;
75+
/*
76+
if (exports.dial._handler) {
77+
Bangle.removeListener("drag", dial._handler);
78+
}
79+
80+
exports.dial._handler = onDrag;
81+
Bangle.prependListener("drag", onDrag);
82+
83+
Bangle.prependListener("swipe", ()=>{
84+
E.stopEventPropagation();
85+
}
86+
);*/
87+
};
88+
89+
exports = Dial;

0 commit comments

Comments
 (0)