Skip to content

Commit 69dbac6

Browse files
committed
Add require("clock_info").addInteractive to allow info displays to be added to the screen easily (ref #2226)
1 parent 1b1f1d7 commit 69dbac6

File tree

1 file changed

+61
-2
lines changed

1 file changed

+61
-2
lines changed

modules/clock_info.js

+61-2
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,71 @@ exports.load = function() {
111111
return menu;
112112
};
113113

114+
/** Adds an interactive menu that could be used on a clock face by swiping.
115+
Simply supply the menu data (from .load) and a function to draw the clock info.
114116
115-
// Code for testing
117+
For example:
118+
119+
var clockInfoMenu = require("clock_info").addInteractive(require("clock_info").load(), (itm, info) => {
120+
var y = 0;
121+
g.reset().setFont("6x8:2").setFontAlign(-1,0);
122+
g.clearRect(0,y,g.getWidth(),y+23);
123+
g.drawImage(info.img, 0,y);
124+
g.drawString(info.text, 48,y+12);
125+
});
126+
127+
Then if you need to unload the clock info so it no longer
128+
uses memory or responds to swipes, you can call clockInfoMenu.remove()
129+
and delete clockInfoMenu
130+
*/
131+
exports.addInteractive = function(menu, drawFn) {
132+
if (!menu.length || !menu[0].items.length) return; // no info
133+
var menuA = 0, menuB = 0;
134+
function menuShowItem(itm) {
135+
itm.on('redraw', ()=>drawFn(itm, itm.get()));
136+
itm.show();
137+
itm.emit("redraw");
138+
}
139+
// handling for swipe between menu items
140+
function swipeHandler(lr,ud){
141+
var oldMenuItem;
142+
if (ud) {
143+
if (menu[menuA].items.length==1) return; // 1 item - can't move
144+
oldMenuItem = menu[menuA].items[menuB];
145+
menuB += ud;
146+
if (menuB<0) menuB = menu[menuA].items.length-1;
147+
if (menuB>=menu[menuA].items.length) menuB = 0;
148+
} else if (lr) {
149+
if (menu.length==1) return; // 1 item - can't move
150+
oldMenuItem = menu[menuA].items[menuB];
151+
menuA += ud;
152+
if (menuA<0) menuA = menu.length-1;
153+
if (menuA>=menu.length) menuA = 0;
154+
menuB = 0;
155+
}
156+
if (oldMenuItem) {
157+
oldMenuItem.hide();
158+
oldMenuItem.removeAllListeners("draw");
159+
menuShowItem(menu[menuA].items[menuB]);
160+
}
161+
}
162+
Bangle.on("swipe",swipeHandler);
163+
// draw the first item
164+
menuShowItem(menu[menuA].items[menuB]);
165+
// return an object with info that can be used to remove the info
166+
return {
167+
remove : function() {
168+
Bangle.removeListener("swipe",swipeHandler);
169+
menu[menuA].items[menuB].hide();
170+
}
171+
};
172+
};
173+
174+
// Code for testing (plots all elements from first list)
116175
/*
117176
g.clear();
118177
var menu = exports.load(); // or require("clock_info").load()
119-
var itemsFirstMenu = menu[0].items;
178+
var items = menu[0].items;
120179
items.forEach((itm,i) => {
121180
var y = i*24;
122181
console.log("Starting", itm.name);

0 commit comments

Comments
 (0)