Formatting

master
stweedo 2023-06-16 23:34:50 -05:00
parent bc7451cbea
commit e3cab1d4e6
1 changed files with 239 additions and 240 deletions

View File

@ -1,242 +1,241 @@
{ {
// 1. Module dependencies and initial configurations // 1. Module dependencies and initial configurations
let storage = require("Storage"); let storage = require("Storage");
let locale = require("locale"); let locale = require("locale");
let date = new Date(); let date = new Date();
let bgImage; let bgImage;
let configNumber = (storage.readJSON("boxclk.json", 1) || {}).selectedConfig || 0; let configNumber = (storage.readJSON("boxclk.json", 1) || {}).selectedConfig || 0;
let fileName = 'boxclk' + (configNumber > 0 ? `-${configNumber}` : '') + '.json'; let fileName = 'boxclk' + (configNumber > 0 ? `-${configNumber}` : '') + '.json';
let boxesConfig = storage.readJSON(fileName, 1) || {}; let boxesConfig = storage.readJSON(fileName, 1) || {};
let boxes = {}; let boxes = {};
let boxPos = {}; let boxPos = {};
let isDragging = {}; let isDragging = {};
let wasDragging = {}; let wasDragging = {};
// 2. Graphical and visual configurations // 2. Graphical and visual configurations
let w = g.getWidth(); let w = g.getWidth();
let h = g.getHeight(); let h = g.getHeight();
let totalWidth, totalHeight; let totalWidth, totalHeight;
let enableSuffix = true; let enableSuffix = true;
let drawTimeout; let drawTimeout;
// 3. Handlers // 3. Handlers
let touchHandler; let touchHandler;
let dragHandler; let dragHandler;
// 4. Font loading function // 4. Font loading function
let loadCustomFont = function() { let loadCustomFont = function() {
Graphics.prototype.setFontBrunoAce = function() { Graphics.prototype.setFontBrunoAce = function() {
// Actual height 23 (24 - 2) // Actual height 23 (24 - 2)
return this.setFontCustom( return this.setFontCustom(
E.toString(require('heatshrink').decompress(atob('ABMHwADBh4DKg4bKgIPDAYUfAYV/AYX/AQMD/gmC+ADBn/AByE/GIU8AYUwLxcfAYX/8AnB//4JIP/FgMP4F+CQQBBjwJBFYRbBAd43DHoJpBh/g/xPEK4ZfDgEEORKDDAY8////wADLfZrTCgITBnhEBAYJMBAYMPw4DCM4QDjhwDCjwDBn0+AYMf/gDBh/4AYMH+ADB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CBEdChgYGhgaGBsaCQ=="), atob("CBEdChgYGhgaGBsaCQ=="),
32|65536 32|65536
); );
}; };
}; };
// 5. Initial settings of boxes and their positions // 5. Initial settings of boxes and their positions
for (let key in boxesConfig) { for (let key in boxesConfig) {
if (key === 'bg' && boxesConfig[key].img) { if (key === 'bg' && boxesConfig[key].img) {
bgImage = storage.read(boxesConfig[key].img); bgImage = storage.read(boxesConfig[key].img);
} else if (key !== 'selectedConfig') { } else if (key !== 'selectedConfig') {
boxes[key] = Object.assign({}, boxesConfig[key]); boxes[key] = Object.assign({}, boxesConfig[key]);
} }
} }
Object.keys(boxes).forEach((key) => { Object.keys(boxes).forEach((key) => {
let boxConfig = boxes[key]; let boxConfig = boxes[key];
boxPos[key] = { boxPos[key] = {
x: w * boxConfig.boxPos.x, x: w * boxConfig.boxPos.x,
y: h * boxConfig.boxPos.y y: h * boxConfig.boxPos.y
}; };
isDragging[key] = false; isDragging[key] = false;
wasDragging[key] = false; wasDragging[key] = false;
}); });
// 6. Text and drawing functions // 6. Text and drawing functions
let g_drawString = g.drawString; let g_drawString = g.drawString;
g.drawString = function(box, str, x, y) { g.drawString = function(box, str, x, y) {
outlineText(box, str, x, y); outlineText(box, str, x, y);
g.setColor(box.color); g.setColor(box.color);
g_drawString.call(g, str, x, y); g_drawString.call(g, str, x, y);
}; };
let outlineText = function(box, str, x, y) { let outlineText = function(box, str, x, y) {
let px = box.outline; let px = box.outline;
let dx = [-px, 0, px, -px, px, -px, 0, px]; let dx = [-px, 0, px, -px, px, -px, 0, px];
let dy = [-px, -px, -px, 0, 0, px, px, px]; let dy = [-px, -px, -px, 0, 0, px, px, px];
g.setColor(box.outlineColor); g.setColor(box.outlineColor);
for (let i = 0; i < dx.length; i++) { for (let i = 0; i < dx.length; i++) {
g_drawString.call(g, str, x + dx[i], y + dy[i]); g_drawString.call(g, str, x + dx[i], y + dy[i]);
} }
}; };
let calcBoxSize = function(boxItem) { let calcBoxSize = function(boxItem) {
g.reset(); g.reset();
g.setFontAlign(0,0); g.setFontAlign(0,0);
g.setFont(boxItem.font, boxItem.fontSize); g.setFont(boxItem.font, boxItem.fontSize);
let strWidth = g.stringWidth(boxItem.string) + 2 * boxItem.outline; let strWidth = g.stringWidth(boxItem.string) + 2 * boxItem.outline;
let fontHeight = g.getFontHeight() + 2 * boxItem.outline; let fontHeight = g.getFontHeight() + 2 * boxItem.outline;
totalWidth = strWidth + 2 * boxItem.xPadding; totalWidth = strWidth + 2 * boxItem.xPadding;
totalHeight = fontHeight + 2 * boxItem.yPadding; totalHeight = fontHeight + 2 * boxItem.yPadding;
}; };
let calcBoxPos = function(boxKey) { let calcBoxPos = function(boxKey) {
return { return {
x1: boxPos[boxKey].x - totalWidth / 2, x1: boxPos[boxKey].x - totalWidth / 2,
y1: boxPos[boxKey].y - totalHeight / 2, y1: boxPos[boxKey].y - totalHeight / 2,
x2: boxPos[boxKey].x + totalWidth / 2, x2: boxPos[boxKey].x + totalWidth / 2,
y2: boxPos[boxKey].y + totalHeight / 2 y2: boxPos[boxKey].y + totalHeight / 2
}; };
}; };
// 7. Date and time related functions // 7. Date and time related functions
let getDate = function() { let getDate = function() {
const date = new Date(); const date = new Date();
const dayOfMonth = date.getDate(); const dayOfMonth = date.getDate();
const month = locale.month(date, 1); const month = locale.month(date, 1);
const year = date.getFullYear(); const year = date.getFullYear();
let suffix; let suffix;
if ([1, 21, 31].includes(dayOfMonth)) { if ([1, 21, 31].includes(dayOfMonth)) {
suffix = "st"; suffix = "st";
} else if ([2, 22].includes(dayOfMonth)) { } else if ([2, 22].includes(dayOfMonth)) {
suffix = "nd"; suffix = "nd";
} else if ([3, 23].includes(dayOfMonth)) { } else if ([3, 23].includes(dayOfMonth)) {
suffix = "rd"; suffix = "rd";
} else { } else {
suffix = "th"; suffix = "th";
} }
let dayOfMonthStr = enableSuffix ? dayOfMonth + suffix : dayOfMonth; let dayOfMonthStr = enableSuffix ? dayOfMonth + suffix : dayOfMonth;
return month + " " + dayOfMonthStr + ", " + year; return month + " " + dayOfMonthStr + ", " + year;
}; };
let getDayOfWeek = function(date) { let getDayOfWeek = function(date) {
return locale.dow(date, 0); return locale.dow(date, 0);
}; };
// 8. Main draw function // 8. Main draw function
let draw = function(boxes) { let draw = function(boxes) {
date = new Date(); date = new Date();
g.clear(); g.clear();
if (bgImage) { if (bgImage) {
g.drawImage(bgImage, 0, 0); g.drawImage(bgImage, 0, 0);
} }
if (boxes.time) { if (boxes.time) {
boxes.time.string = locale.time(date, 1); boxes.time.string = locale.time(date, 1);
} }
if (boxes.date) { if (boxes.date) {
boxes.date.string = getDate(); boxes.date.string = getDate();
} }
if (boxes.dow) { if (boxes.dow) {
boxes.dow.string = getDayOfWeek(date); boxes.dow.string = getDayOfWeek(date);
} }
if (boxes.batt) { if (boxes.batt) {
boxes.batt.string = E.getBattery() + "%"; boxes.batt.string = E.getBattery() + "%";
} }
Object.keys(boxes).forEach((boxKey) => { Object.keys(boxes).forEach((boxKey) => {
let boxItem = boxes[boxKey]; let boxItem = boxes[boxKey];
calcBoxSize(boxItem); calcBoxSize(boxItem);
const pos = calcBoxPos(boxKey); const pos = calcBoxPos(boxKey);
if (isDragging[boxKey]) { if (isDragging[boxKey]) {
g.setColor(boxItem.border); g.setColor(boxItem.border);
g.drawRect(pos.x1, pos.y1, pos.x2, pos.y2); g.drawRect(pos.x1, pos.y1, pos.x2, pos.y2);
} }
g.drawString( g.drawString(
boxItem, boxItem,
boxItem.string, boxItem.string,
boxPos[boxKey].x + boxItem.xOffset, boxPos[boxKey].x + boxItem.xOffset,
boxPos[boxKey].y + boxItem.yOffset boxPos[boxKey].y + boxItem.yOffset
); );
}); });
if (!Object.values(isDragging).some(Boolean)) { if (!Object.values(isDragging).some(Boolean)) {
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(() => draw(boxes), 60000 - (Date.now() % 60000)); drawTimeout = setTimeout(() => draw(boxes), 60000 - (Date.now() % 60000));
} }
}; };
// 9. Helper function for touch event // 9. Helper function for touch event
let touchInText = function(e, boxItem, boxKey) { let touchInText = function(e, boxItem, boxKey) {
calcBoxSize(boxItem); calcBoxSize(boxItem);
const pos = calcBoxPos(boxKey); const pos = calcBoxPos(boxKey);
return e.x >= pos.x1 && return e.x >= pos.x1 &&
e.x <= pos.x2 && e.x <= pos.x2 &&
e.y >= pos.y1 && e.y >= pos.y1 &&
e.y <= pos.y2; e.y <= pos.y2;
}; };
// 10. Setup function to configure event handlers // 10. Setup function to configure event handlers
let setup = function() { let setup = function() {
// Define the touchHandler function // Define the touchHandler function
touchHandler = function(zone, e) { touchHandler = function(zone, e) {
wasDragging = Object.assign({}, isDragging); wasDragging = Object.assign({}, isDragging);
let boxTouched = false; let boxTouched = false;
Object.keys(boxes).forEach((boxKey) => { Object.keys(boxes).forEach((boxKey) => {
if (touchInText(e, boxes[boxKey], boxKey)) { if (touchInText(e, boxes[boxKey], boxKey)) {
isDragging[boxKey] = true; isDragging[boxKey] = true;
wasDragging[boxKey] = true; wasDragging[boxKey] = true;
boxTouched = true; boxTouched = true;
} }
}); });
if (!boxTouched) { if (!boxTouched) {
Object.keys(isDragging).forEach((boxKey) => { Object.keys(isDragging).forEach((boxKey) => {
isDragging[boxKey] = false; isDragging[boxKey] = false;
}); });
require("widget_utils").show(); require("widget_utils").show();
require("widget_utils").swipeOn(); require("widget_utils").swipeOn();
} }
if (Object.values(wasDragging).some(Boolean) || !boxTouched) { if (Object.values(wasDragging).some(Boolean) || !boxTouched) {
draw(boxes); draw(boxes);
} }
}; };
// Define the dragHandler function // Define the dragHandler function
dragHandler = function(e) { dragHandler = function(e) {
Object.keys(boxes).forEach((boxKey) => { Object.keys(boxes).forEach((boxKey) => {
if (isDragging[boxKey]) { if (isDragging[boxKey]) {
require("widget_utils").hide(); require("widget_utils").hide();
let boxItem = boxes[boxKey]; let boxItem = boxes[boxKey];
calcBoxSize(boxItem); calcBoxSize(boxItem);
let newX = boxPos[boxKey].x + e.dx; let newX = boxPos[boxKey].x + e.dx;
let newY = boxPos[boxKey].y + e.dy; let newY = boxPos[boxKey].y + e.dy;
if (newX - totalWidth / 2 >= 0 && if (newX - totalWidth / 2 >= 0 &&
newX + totalWidth / 2 <= w && newX + totalWidth / 2 <= w &&
newY - totalHeight / 2 >= 0 && newY - totalHeight / 2 >= 0 &&
newY + totalHeight / 2 <= h ) { newY + totalHeight / 2 <= h ) {
boxPos[boxKey].x = newX; boxPos[boxKey].x = newX;
boxPos[boxKey].y = newY; boxPos[boxKey].y = newY;
} }
const pos = calcBoxPos(boxKey); const pos = calcBoxPos(boxKey);
g.clearRect(pos.x1, pos.y1, pos.x2, pos.y2); g.clearRect(pos.x1, pos.y1, pos.x2, pos.y2);
} }
}); });
draw(boxes); draw(boxes);
}; };
Bangle.on('touch', touchHandler); Bangle.on('touch', touchHandler);
Bangle.on('drag', dragHandler); Bangle.on('drag', dragHandler);
Bangle.setUI({ Bangle.setUI({
mode : "clock", mode : "clock",
remove : function() { remove : function() {
// Remove event handlers, stop draw timer, remove custom font if used // Remove event handlers, stop draw timer, remove custom font if used
Bangle.removeListener('touch', touchHandler); Bangle.removeListener('touch', touchHandler);
Bangle.removeListener('drag', dragHandler); Bangle.removeListener('drag', dragHandler);
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = undefined; drawTimeout = undefined;
delete Graphics.prototype.setFontBrunoAce; delete Graphics.prototype.setFontBrunoAce;
g.drawString = g_drawString; // Return to original without outlines g.drawString = g_drawString; // Return to original without outlines
require("widget_utils").show(); require("widget_utils").show();
} }
}); });
loadCustomFont(); loadCustomFont();
draw(boxes); draw(boxes);
}; };
// 11. Main execution part
Bangle.loadWidgets();
require("widget_utils").swipeOn();
setup();
}
// 11. Main execution part
Bangle.loadWidgets();
require("widget_utils").swipeOn();
setup();
}