Wrap program in IIFE so variables don't leak

master
stweedo 2023-06-16 09:24:11 -05:00
parent 0a4e01fd2c
commit c734ef8347
1 changed files with 213 additions and 211 deletions

View File

@ -1,19 +1,20 @@
let w = g.getWidth(); (function() {
let h = g.getHeight(); let w = g.getWidth();
let totalWidth, totalHeight; let h = g.getHeight();
let touchHandler; let totalWidth, totalHeight;
let dragHandler; let touchHandler;
let drawTimeout; let dragHandler;
let enableSuffix = true; let drawTimeout;
let storage = require("Storage"); let enableSuffix = true;
let locale = require("locale"); let storage = require("Storage");
let date = new Date(); let locale = require("locale");
let date = new Date();
let bgImage; let bgImage;
let boxesConfig = storage.readJSON('boxclk.json', 1) || {}; let boxesConfig = storage.readJSON('boxclk.json', 1) || {};
let boxes = {}; let boxes = {};
function loadCustomFont() { function loadCustomFont() {
Graphics.prototype.setFontBrunoAce = function() { Graphics.prototype.setFontBrunoAce = function() {
// Actual height 23 (24 - 2) // Actual height 23 (24 - 2)
return this.setFontCustom( return this.setFontCustom(
@ -23,21 +24,21 @@ function loadCustomFont() {
32|65536 32|65536
); );
}; };
} }
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 { } else {
boxes[key] = Object.assign({}, boxesConfig[key]); boxes[key] = Object.assign({}, boxesConfig[key]);
} }
} }
let boxPos = {}; let boxPos = {};
let isDragging = {}; let isDragging = {};
let wasDragging = {}; let wasDragging = {};
Object.keys(boxes).forEach((boxKey) => { Object.keys(boxes).forEach((boxKey) => {
let boxConfig = boxes[boxKey]; let boxConfig = boxes[boxKey];
boxPos[boxKey] = { boxPos[boxKey] = {
x: w * boxConfig.boxPos.x, x: w * boxConfig.boxPos.x,
@ -45,16 +46,16 @@ Object.keys(boxes).forEach((boxKey) => {
}; };
isDragging[boxKey] = false; isDragging[boxKey] = false;
wasDragging[boxKey] = false; wasDragging[boxKey] = false;
}); });
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);
}; };
function outlineText(box, str, x, y) { function outlineText(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];
@ -62,9 +63,9 @@ function outlineText(box, str, x, y) {
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]);
} }
} }
function calcBoxSize(boxItem) { function calcBoxSize(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);
@ -72,18 +73,18 @@ function calcBoxSize(boxItem) {
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;
} }
function calcBoxPos(boxKey) { function calcBoxPos(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
}; };
} }
function getDate() { function getDate() {
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);
@ -100,13 +101,13 @@ function getDate() {
} }
let dayOfMonthStr = enableSuffix ? dayOfMonth + suffix : dayOfMonth; let dayOfMonthStr = enableSuffix ? dayOfMonth + suffix : dayOfMonth;
return month + " " + dayOfMonthStr + ", " + year; return month + " " + dayOfMonthStr + ", " + year;
} }
function getDayOfWeek(date) { function getDayOfWeek(date) {
return locale.dow(date, 0); return locale.dow(date, 0);
} }
function draw(boxes) { function draw(boxes) {
date = new Date(); date = new Date();
g.clear(); g.clear();
if (bgImage) { if (bgImage) {
@ -143,9 +144,9 @@ function draw(boxes) {
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(() => draw(boxes), 60000 - (Date.now() % 60000)); drawTimeout = setTimeout(() => draw(boxes), 60000 - (Date.now() % 60000));
} }
} }
function setup() { function setup() {
// Define the touchHandler function // Define the touchHandler function
touchHandler = function(zone, e) { touchHandler = function(zone, e) {
wasDragging = Object.assign({}, isDragging); wasDragging = Object.assign({}, isDragging);
@ -207,17 +208,18 @@ function setup() {
}); });
loadCustomFont(); loadCustomFont();
draw(boxes); draw(boxes);
} }
function touchInText(e, boxItem, boxKey) { function touchInText(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;
} }
Bangle.loadWidgets(); Bangle.loadWidgets();
require("widget_utils").swipeOn(); require("widget_utils").swipeOn();
setup(); setup();
})();