ctrlpad: update js

pull/3383/head
Rob Pilling 2024-04-25 12:56:36 +01:00
parent d1642565cd
commit a9140ff158
1 changed files with 100 additions and 76 deletions

View File

@ -31,7 +31,9 @@
Overlay.prototype.renderG2 = function () { Overlay.prototype.renderG2 = function () {
this.g2 this.g2
.reset() .reset()
.clearRect(0, 0, this.width, this.height) .setColor(g.theme.bg)
.fillRect(0, 0, this.width, this.height)
.setColor(colour.on.bg)
.drawRect(0, 0, this.width - 1, this.height - 1) .drawRect(0, 0, this.width - 1, this.height - 1)
.drawRect(1, 1, this.width - 2, this.height - 2); .drawRect(1, 1, this.width - 2, this.height - 2);
}; };
@ -48,18 +50,25 @@
}, },
}; };
var Controls = (function () { var Controls = (function () {
function Controls(g) { function Controls(g, controls) {
var height = g.getHeight(); var height = g.getHeight();
var centreY = height / 2; var centreY = height / 2;
var circleGapY = 30; var circleGapY = 30;
var width = g.getWidth(); var width = g.getWidth();
this.controls = [ this.controls = [
{ x: width / 4 - 10, y: centreY - circleGapY, text: "BLE", fg: colour.on.fg, bg: colour.on.bg }, { x: width / 4 - 10, y: centreY - circleGapY },
{ x: width / 2, y: centreY - circleGapY, text: "DnD", fg: colour.off.fg, bg: colour.off.bg }, { x: width / 2, y: centreY - circleGapY },
{ x: width * 3 / 4 + 10, y: centreY - circleGapY, text: "HRM", fg: colour.off.fg, bg: colour.off.bg }, { x: width * 3 / 4 + 10, y: centreY - circleGapY },
{ x: width / 3, y: centreY + circleGapY, text: "B-", fg: colour.on.fg, bg: colour.on.bg }, { x: width / 3, y: centreY + circleGapY },
{ x: width * 2 / 3, y: centreY + circleGapY, text: "B+", fg: colour.on.fg, bg: colour.on.bg }, { x: width * 2 / 3, y: centreY + circleGapY },
]; ].map(function (xy, i) {
var ctrl = xy;
var from = controls[i];
ctrl.text = from.text;
ctrl.cb = from.cb;
Object.assign(ctrl, from.cb(false) ? colour.on : colour.off);
return ctrl;
});
} }
Controls.prototype.draw = function (g, single) { Controls.prototype.draw = function (g, single) {
g g
@ -97,6 +106,78 @@
var upDragAnim; var upDragAnim;
var ui; var ui;
var touchDown = false; var touchDown = false;
var initUI = function () {
if (ui)
return;
function noop(tap) {
return (this.bg === colour.on.bg) !== tap;
}
var controls = [
{
text: "BLE",
cb: function (tap) {
var on = NRF.getSecurityStatus().advertising;
if (tap) {
if (on)
NRF.sleep();
else
NRF.wake();
}
return on !== tap;
}
},
{
text: "DnD",
cb: function (tap) {
var on;
if (on = !!origBuzz) {
if (tap) {
Bangle.buzz = origBuzz;
origBuzz = undefined;
}
}
else {
if (tap) {
origBuzz = Bangle.buzz;
Bangle.buzz = function () { return Promise.resolve(); };
setTimeout(function () {
if (!origBuzz)
return;
Bangle.buzz = origBuzz;
origBuzz = undefined;
}, 1000 * 60 * 10);
}
}
return on !== tap;
}
},
{
text: "HRM",
cb: function (tap) {
var _a;
var id = "widhid";
var hrm = (_a = Bangle._PWR) === null || _a === void 0 ? void 0 : _a.HRM;
var off = !hrm || hrm.indexOf(id) === -1;
if (off) {
if (tap)
Bangle.setHRMPower(1, id);
}
else if (tap) {
Bangle.setHRMPower(0, id);
}
return !off !== tap;
}
},
{ text: "B-", cb: noop },
{ text: "B+", cb: noop },
];
var overlay = new Overlay();
ui = {
overlay: overlay,
ctrls: new Controls(overlay.g2, controls),
};
ui.ctrls.draw(ui.overlay.g2);
};
var onDrag = (function (e) { var onDrag = (function (e) {
var _a, _b; var _a, _b;
var dragDistance = 30; var dragDistance = 30;
@ -104,10 +185,8 @@
touchDown = startedUpDrag = false; touchDown = startedUpDrag = false;
switch (state) { switch (state) {
case 2: case 2:
if (e.b === 0) { if (e.b === 0)
state = 0; state = 0;
ui = undefined;
}
break; break;
case 0: case 0:
if (e.b && !touchDown) { if (e.b && !touchDown) {
@ -117,34 +196,29 @@
} }
else { else {
state = 2; state = 2;
ui = undefined;
} }
} }
break; break;
case 1: case 1:
if (e.b === 0) { if (e.b === 0) {
if (e.y > startY + dragDistance) { if (e.y > startY + dragDistance) {
initUI();
state = 3; state = 3;
startY = 0; startY = 0;
Bangle.prependListener("touch", onTouch); Bangle.prependListener("touch", onTouch);
Bangle.buzz(20); Bangle.buzz(20);
ui.overlay.setBottom(g.getHeight()); ui.overlay.setBottom(g.getHeight());
break;
} }
else {
state = 0; state = 0;
ui === null || ui === void 0 ? void 0 : ui.overlay.hide(); ui === null || ui === void 0 ? void 0 : ui.overlay.hide();
ui = undefined; ui = undefined;
break;
}
} }
else { else {
var dragOffset = 32; var dragOffset = 32;
if (!ui) { initUI();
var overlay = new Overlay();
ui = {
overlay: overlay,
ctrls: new Controls(overlay.g2),
};
ui.ctrls.draw(ui.overlay.g2);
}
ui.overlay.setBottom(e.y - dragOffset); ui.overlay.setBottom(e.y - dragOffset);
} }
(_a = E.stopEventPropagation) === null || _a === void 0 ? void 0 : _a.call(E); (_a = E.stopEventPropagation) === null || _a === void 0 ? void 0 : _a.call(E);
@ -203,66 +277,16 @@
}); });
var origBuzz; var origBuzz;
var onCtrlTap = function (ctrl, ui) { var onCtrlTap = function (ctrl, ui) {
var _a; Bangle.buzz(20);
Bangle.buzz(80); var col = ctrl.cb(true) ? colour.on : colour.off;
var on = true;
switch (ctrl.text) {
case "BLE":
if (NRF.getSecurityStatus().advertising) {
NRF.sleep();
on = false;
}
else {
NRF.wake();
}
break;
case "DnD":
if (origBuzz) {
Bangle.buzz = origBuzz;
origBuzz = undefined;
on = false;
}
else {
origBuzz = Bangle.buzz;
Bangle.buzz = function () { return Promise.resolve(); };
setTimeout(function () {
if (!origBuzz)
return;
Bangle.buzz = origBuzz;
origBuzz = undefined;
}, 1000 * 60 * 10);
}
break;
case "HRM": {
var id = "widhid";
var hrm = (_a = Bangle._PWR) === null || _a === void 0 ? void 0 : _a.HRM;
if (!hrm || hrm.indexOf(id) === -1) {
Bangle.setHRMPower(1, id);
}
else {
Bangle.setHRMPower(0, id);
on = false;
}
break;
}
default:
console.log("widhid: couldn't handle \"".concat(ctrl.text, "\" tap"));
on = ctrl.fg !== colour.on.fg;
}
var col = on ? colour.on : colour.off;
ctrl.fg = col.fg; ctrl.fg = col.fg;
ctrl.bg = col.bg; ctrl.bg = col.bg;
ui.ctrls.draw(ui.overlay.g2, ctrl); ui.ctrls.draw(ui.overlay.g2, ctrl);
}; };
Bangle.prependListener("drag", onDrag); Bangle.prependListener("drag", onDrag);
Bangle.on("lock", function () { Bangle.on("lock", function () {
state = 0;
ui === null || ui === void 0 ? void 0 : ui.overlay.hide(); ui === null || ui === void 0 ? void 0 : ui.overlay.hide();
ui = undefined; ui = undefined;
}); });
WIDGETS["hid"] = {
area: "tr",
sortorder: -20,
draw: function () { },
width: 0,
};
})(); })();