BangleApps/apps/counter2/app.js

96 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-03-22 05:24:39 +00:00
Bangle.loadWidgets();
var s = Object.assign({
counter0:10,
counter1:20,
max0:15,
max1:25,
buzz: true,
colortext: true,
}, require('Storage').readJSON("counter2.json", true) || {});
2024-04-04 20:43:11 +00:00
const f1 = (s.colortext) ? "#f00" : "#fff";
const f2 = (s.colortext) ? "#00f" : "#fff";
const b1 = (s.colortext) ? g.theme.bg : "#f00";
const b2 = (s.colortext) ? g.theme.bg : "#00f";
2024-03-22 05:24:39 +00:00
var drag;
2024-04-04 20:43:11 +00:00
const screenwidth = g.getWidth();
const screenheight = g.getHeight();
const halfwidth = screenwidth / 2;
const halfheight = screenheight / 2;
2024-03-22 05:24:39 +00:00
2024-04-08 11:50:18 +00:00
const counter = [];
2024-03-22 05:24:39 +00:00
counter[0] = s.counter0;
counter[1] = s.counter1;
2024-04-04 20:43:11 +00:00
const defaults = [];
2024-03-22 05:24:39 +00:00
defaults[0] = s.max0;
defaults[1] = s.max1;
function saveSettings() {
s.counter0 = counter[0];
s.counter1 = counter[1];
s.max0 = defaults[0];
s.max1 = defaults[1];
require('Storage').writeJSON("counter2.json", s);
}
2024-04-04 20:43:11 +00:00
let ignoreonce = false;
2024-03-22 05:24:39 +00:00
var dragtimeout;
function updateScreen() {
g.setBgColor(b1);
g.clearRect(0, 0, halfwidth, screenheight);
g.setBgColor(b2);
g.clearRect(halfwidth, 0, screenwidth, screenheight);
g.setFont("Vector", 60).setFontAlign(0, 0);
g.setColor(f1);
g.drawString(Math.floor(counter[0]), halfwidth * 0.5, halfheight);
g.setColor(f2);
g.drawString(Math.floor(counter[1]), halfwidth * 1.5, halfheight);
saveSettings();
if (s.buzz) Bangle.buzz(50,.5);
Bangle.drawWidgets();
}
Bangle.on("drag", e => {
2024-04-04 20:43:11 +00:00
const c = (e.x < halfwidth) ? 0 : 1;
2024-03-22 05:24:39 +00:00
if (!drag) {
if (ignoreonce) {
ignoreonce = false;
return;
}
drag = { x: e.x, y: e.y };
dragtimeout = setTimeout(function () { resetcounter(c); }, 600); //if dragging for 500ms, reset counter
}
else if (drag && !e.b) { // released
2024-04-04 20:43:11 +00:00
let adjust = 0;
2024-03-22 05:24:39 +00:00
const dx = e.x - drag.x, dy = e.y - drag.y;
if (Math.abs(dy) > Math.abs(dx) + 30) {
adjust = (dy > 0) ? -1 : 1;
} else {
adjust = (e.y > halfwidth) ? -1 : 1;
}
counter[c] += adjust;
updateScreen();
drag = undefined;
clearTimeout(dragtimeout);
}
});
function resetcounter(which) {
counter[which] = defaults[which];
console.log("resetting counter ", which);
updateScreen();
drag = undefined;
ignoreonce = true;
}
updateScreen();
setWatch(function() {
load();
2024-04-04 20:43:11 +00:00
}, BTN1, {repeat:true, edge:"falling"});