Simplified code by removing boxKeys array

master
stweedo 2024-09-06 08:21:00 -05:00 committed by GitHub
parent 48e02ba13f
commit 5f6fd9247d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 42 deletions

View File

@ -28,19 +28,20 @@
let boxTouched = false; let boxTouched = false;
let touchedBox = null; let touchedBox = null;
boxKeys.forEach((boxKey) => { for (let boxKey in boxes) {
if (touchInText(e, boxes[boxKey], boxKey)) { if (touchInText(e, boxes[boxKey])) {
touchedBox = boxKey; touchedBox = boxKey;
boxTouched = true; boxTouched = true;
break;
} }
}); }
if (boxTouched) { if (boxTouched) {
// Toggle the selected state of the touched box // Toggle the selected state of the touched box
boxes[touchedBox].selected = !boxes[touchedBox].selected; boxes[touchedBox].selected = !boxes[touchedBox].selected;
// Update isDragging based on whether any box is selected // Update isDragging based on whether any box is selected
isDragging = boxKeys.some(key => boxes[key].selected); isDragging = Object.values(boxes).some(box => box.selected);
if (isDragging) { if (isDragging) {
widgets.hide(); widgets.hide();
@ -60,10 +61,10 @@
if (doubleTapTimer) { if (doubleTapTimer) {
clearTimeout(doubleTapTimer); clearTimeout(doubleTapTimer);
doubleTapTimer = null; doubleTapTimer = null;
Object.keys(boxes).forEach((boxKey) => { for (let boxKey in boxes) {
boxesConfig[boxKey].boxPos.x = (boxes[boxKey].pos.x / w).toFixed(3); boxesConfig[boxKey].boxPos.x = (boxes[boxKey].pos.x / w).toFixed(3);
boxesConfig[boxKey].boxPos.y = (boxes[boxKey].pos.y / h).toFixed(3); boxesConfig[boxKey].boxPos.y = (boxes[boxKey].pos.y / h).toFixed(3);
}); }
storage.write(fileName, JSON.stringify(boxesConfig)); storage.write(fileName, JSON.stringify(boxesConfig));
displaySaveIcon(); displaySaveIcon();
return; return;
@ -76,30 +77,30 @@
}; };
let dragHandler = function(e) { let dragHandler = function(e) {
// Check if any box is being dragged // Check if any box is being dragged
if (!isDragging) return; if (!isDragging) return;
// Stop propagation of the drag event to prevent other handlers
E.stopEventPropagation();
boxKeys.forEach(key => { // Stop propagation of the drag event to prevent other handlers
if (boxes[key].selected) { E.stopEventPropagation();
let boxItem = boxes[key];
calcBoxSize(boxItem);
let newX = boxItem.pos.x + e.dx;
let newY = boxItem.pos.y + e.dy;
if (newX - boxItem.cachedSize.width / 2 >= 0 && for (let key in boxes) {
newX + boxItem.cachedSize.width / 2 <= w && if (boxes[key].selected) {
newY - boxItem.cachedSize.height / 2 >= 0 && let boxItem = boxes[key];
newY + boxItem.cachedSize.height / 2 <= h) { calcBoxSize(boxItem);
boxItem.pos.x = newX; let newX = boxItem.pos.x + e.dx;
boxItem.pos.y = newY; let newY = boxItem.pos.y + e.dy;
}
}
});
draw(); if (newX - boxItem.cachedSize.width / 2 >= 0 &&
newX + boxItem.cachedSize.width / 2 <= w &&
newY - boxItem.cachedSize.height / 2 >= 0 &&
newY + boxItem.cachedSize.height / 2 <= h) {
boxItem.pos.x = newX;
boxItem.pos.y = newY;
}
}
}
draw();
}; };
// 4. Font loading function // 4. Font loading function
@ -299,42 +300,42 @@
let draw = function() { let draw = function() {
g.clear(); g.clear();
// Always draw backgrounds full screen // Always draw backgrounds full screen
if (bgImage) { // Check for bg in boxclk config if (bgImage) { // Check for bg in boxclk config
g.drawImage(bgImage, 0, 0); g.drawImage(bgImage, 0, 0);
} else { // Otherwise use clockbg module } else { // Otherwise use clockbg module
background.fillRect(0, 0, g.getWidth(), g.getHeight()); background.fillRect(0, 0, g.getWidth(), g.getHeight());
} }
if (!isDragging) { if (!isDragging) {
updateBoxData(); updateBoxData();
} }
boxKeys.forEach((boxKey) => { for (let boxKey in boxes) {
let boxItem = boxes[boxKey]; let boxItem = boxes[boxKey];
// Set font and alignment for each box individually // Set font and alignment for each box individually
g.setFont(boxItem.font, boxItem.fontSize); g.setFont(boxItem.font, boxItem.fontSize);
g.setFontAlign(0, 0); g.setFontAlign(0, 0);
calcBoxSize(boxItem); calcBoxSize(boxItem);
const pos = calcBoxPos(boxItem); const pos = calcBoxPos(boxItem);
if (boxItem.selected) { if (boxItem.selected) {
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,
boxItem.pos.x + boxItem.xOffset, boxItem.pos.x + boxItem.xOffset,
boxItem.pos.y + boxItem.yOffset boxItem.pos.y + boxItem.yOffset
); );
}); }
if (!isDragging) { if (!isDragging) {
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
let updateInterval = boxes.time && !isBool(boxes.time.short, true) ? 1000 : 60000 - (Date.now() % 60000); let updateInterval = boxes.time && !isBool(boxes.time.short, true) ? 1000 : 60000 - (Date.now() % 60000);
@ -375,7 +376,7 @@
return boxItem.cachedSize; return boxItem.cachedSize;
}; };
let touchInText = function(e, boxItem, boxKey) { let touchInText = function(e, boxItem) {
calcBoxSize(boxItem); calcBoxSize(boxItem);
const pos = calcBoxPos(boxItem); const pos = calcBoxPos(boxItem);
return e.x >= pos.x1 && return e.x >= pos.x1 &&
@ -386,9 +387,9 @@
let deselectAllBoxes = function() { let deselectAllBoxes = function() {
isDragging = false; isDragging = false;
boxKeys.forEach((boxKey) => { for (let boxKey in boxes) {
boxes[boxKey].selected = false; boxes[boxKey].selected = false;
}); }
restoreSetColor(); restoreSetColor();
widgets.show(); widgets.show();
widgets.swipeOn(); widgets.swipeOn();
@ -453,4 +454,4 @@
draw(); draw();
} }
}); });
} }