Skip to content

Commit 91a6b9d

Browse files
committed
Make DialDisplay a class
1 parent e372807 commit 91a6b9d

File tree

3 files changed

+68
-49
lines changed

3 files changed

+68
-49
lines changed

apps/spotrem/app.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ let dragHandler = function(e) {
129129

130130
if (volumeChangedThisGoAround && Math.abs(dx)>32) {
131131
// setup volume knob here.
132-
let cbVisual = (step, value, reinit)=>{cb(step); volumeKnobVisual(step, value, reinit)};
133-
cbVisual(Math.sign(dx)*Math.sign(g.getHeight()/2-e.y), 0, true)
132+
let cbVisual = (step)=>{
133+
cb(step);
134+
volumeKnobVisual.step(step);
135+
};
136+
cbVisual(Math.sign(dx)*Math.sign(g.getHeight()/2-e.y));
134137
resetOuterScopeVariables();
135138
let volumeKnob = require("Dial")(cbVisual);
136139
let timingOutVolumeKnob = (e)=>{

modules/Dial_Display.js

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
let dialDisplayGenerator = function(options) {
2-
"ram";
1+
function DialDisplay(options) {
32
const SCREEN_W = g.getWidth();
43
const SCREEN_H = g.getHeight();
54

6-
options = Object.assign(
7-
{ stepsPerWholeTurn : 7, // 7 chosen as it felt the best in use.
5+
this.options = Object.assign(
6+
{
7+
stepsPerWholeTurn : 7, // 7 chosen as it felt the best in use.
88
dialRect : {
99
x: 0,
1010
y: 0,
@@ -13,47 +13,56 @@ let dialDisplayGenerator = function(options) {
1313
},
1414
}, options);
1515

16-
const DIAL_RECT = options.dialRect;
16+
this.value = 0;
17+
this.reset();
18+
}
19+
20+
DialDisplay.prototype.reset = function() {
21+
this.isFirstDraw = true;
22+
};
23+
24+
DialDisplay.prototype.set = function(value) {
25+
this.prevValue = this.value;
26+
this.value = value;
27+
};
28+
29+
DialDisplay.prototype.step = function(step) {
30+
"ram";
31+
let prevValue = this.prevValue != null ? this.prevValue : this.value;
32+
this.value += step;
33+
//g.setFont("Vector:30");
34+
//g.drawString(this.value);
35+
36+
const DIAL_RECT = this.options.dialRect;
1737

1838
const CENTER = {
1939
x: DIAL_RECT.x + DIAL_RECT.w / 2,
2040
y: DIAL_RECT.y + DIAL_RECT.h / 2,
2141
};
2242

23-
let dialDisplay = function(step, value, isReinit) {
24-
let prevValue = this.value;
25-
if (value!==undefined) this.value = value;
26-
if (!this.value) this.value = 0;
27-
if (this.isFirstDraw===undefined || isReinit) this.isFirstDraw = true;
28-
this.value += step;
29-
//g.setFont("Vector:30");
30-
//g.drawString(this.value);
31-
32-
let drawCircle = (value, R, G, B, rad, isFill)=>{
33-
let x = CENTER.x+27*Math.sin(value*(2*Math.PI/options.stepsPerWholeTurn));
34-
let y = CENTER.y-27*Math.cos(value*(2*Math.PI/options.stepsPerWholeTurn));
35-
g.setColor(R,G,B)
36-
if (!isFill) g.drawCircle(x, y, rad);
37-
if (isFill) g.fillCircle(x, y, rad);
38-
}
39-
if (this.isFirstDraw) {
40-
g.setColor(0,0,0).fillCircle(CENTER.x, CENTER.y, 25);
41-
g.setColor(1,1,1).drawCircle(CENTER.x, CENTER.y, 25);
42-
for (let i=0; i<options.stepsPerWholeTurn; i++) {
43-
drawCircle(i, 1, 1, 1, 1, true);
44-
}
45-
this.isFirstDraw = false;
43+
let drawCircle = (value, R, G, B, rad, isFill)=>{
44+
let x = CENTER.x+27*Math.sin(value*(2*Math.PI/this.options.stepsPerWholeTurn));
45+
let y = CENTER.y-27*Math.cos(value*(2*Math.PI/this.options.stepsPerWholeTurn));
46+
g.setColor(R,G,B)
47+
if (!isFill) g.drawCircle(x, y, rad);
48+
if (isFill) g.fillCircle(x, y, rad);
49+
}
50+
if (this.isFirstDraw) {
51+
g.setColor(0,0,0).fillCircle(CENTER.x, CENTER.y, 25);
52+
g.setColor(1,1,1).drawCircle(CENTER.x, CENTER.y, 25);
53+
for (let i=0; i<this.options.stepsPerWholeTurn; i++) {
54+
drawCircle(i, 1, 1, 1, 1, true);
4655
}
56+
this.isFirstDraw = false;
57+
}
4758

48-
//drawCircle(this.value, 1, 1, 1, 2, false);
49-
//drawCircle(prevValue, 0, 0, 0, 2, false);
50-
g.setColor(0,0,0).drawLine(CENTER.x, CENTER.y, CENTER.x+23*Math.sin(prevValue*(2*Math.PI/options.stepsPerWholeTurn)), CENTER.y-23*Math.cos(prevValue*(2*Math.PI/options.stepsPerWholeTurn)));
51-
g.setColor(1,1,1).drawLine(CENTER.x, CENTER.y, CENTER.x+23*Math.sin(this.value*(2*Math.PI/options.stepsPerWholeTurn)), CENTER.y-23*Math.cos(this.value*(2*Math.PI/options.stepsPerWholeTurn)));
52-
g.setColor(0,0,0).fillCircle(CENTER.x, CENTER.y, 9);
59+
//drawCircle(this.value, 1, 1, 1, 2, false);
60+
//drawCircle(prevValue, 0, 0, 0, 2, false);
61+
g.setColor(0,0,0).drawLine(CENTER.x, CENTER.y, CENTER.x+23*Math.sin(prevValue*(2*Math.PI/this.options.stepsPerWholeTurn)), CENTER.y-23*Math.cos(prevValue*(2*Math.PI/this.options.stepsPerWholeTurn)));
62+
g.setColor(1,1,1).drawLine(CENTER.x, CENTER.y, CENTER.x+23*Math.sin(this.value*(2*Math.PI/this.options.stepsPerWholeTurn)), CENTER.y-23*Math.cos(this.value*(2*Math.PI/this.options.stepsPerWholeTurn)));
63+
g.setColor(0,0,0).fillCircle(CENTER.x, CENTER.y, 9);
5364

54-
return this.value;
55-
}
56-
return dialDisplay;
57-
}
65+
delete this.prevValue;
66+
};
5867

59-
exports = dialDisplayGenerator;
68+
exports = DialDisplay;

modules/Dial_Display.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ Usage
1010
```JS
1111
var DialDisplay = require("Dial_Display");
1212
var dialDisplay = new DialDisplay(options);
13-
var value = dialDisplay(-1, 0, true)
13+
14+
dialDisplay.step(-1);
15+
16+
var value = dialDisplay.value;
17+
18+
// ... after some time:
19+
dialDisplay.reset();
20+
dialDisplay.set(0);
1421
```
1522

1623
For example in use with the Dial module:
@@ -29,12 +36,12 @@ var options = {
2936
var DialDisplay = require("Dial_Display");
3037
var dialDisplay = new DialDisplay(options);
3138

32-
var cb = (step)=>{
33-
var value = dialDisplay(step, undefined, true);
39+
var cb = (step) => {
40+
dialDisplay.reset();
41+
dialDisplay.step(step);
3442
};
3543

36-
var Dial = require("Dial");
37-
var dial = new Dial(cb, options)
44+
var dial = require("Dial")(cb, options)
3845
Bangle.on("drag", dial);
3946
```
4047

@@ -55,10 +62,10 @@ Defaults:
5562
}
5663
```
5764

58-
The generated function takes three arguments:
59-
`step` - +1 or -1
60-
`value` - the previous value the step acts on.
61-
`isReinit` - true/false, whether to draw all of the dial or just the indicator.
65+
The Dial Display has three functions:
66+
`step(amount)` - +1 or -1 to step the dial.
67+
`set(value)` - set the value for the next `step()` to act on.
68+
`reset()` - draw all of the dial (instead of just the indicator) on the next `step()`.
6269

6370
Notes:
6471
======

0 commit comments

Comments
 (0)