test events for button / ui binding

This commit is contained in:
Stewart Allen 2021-12-09 23:12:35 -05:00
commit a1539d9ae7
3 changed files with 115 additions and 123 deletions

View file

@ -449,7 +449,7 @@
showDevices(); showDevices();
break; break;
case cca('w'): // scale case cca('w'): // scale
UI.tool.next(); API.event.emit("tool.next");
break; break;
case cca('o'): // tools case cca('o'): // tools
showTools(); showTools();
@ -1429,10 +1429,12 @@
// MAIN INITIALIZATION FUNCTION // MAIN INITIALIZATION FUNCTION
function init_one() { function init_one() {
API.event.emit('init.one'); let { event, conf, view, show } = API;
event.emit('init.one');
// ensure we have settings from last session // ensure we have settings from last session
API.conf.restore(); conf.restore();
let container = $('container'), let container = $('container'),
welcome = $('welcome'), welcome = $('welcome'),
@ -1443,43 +1445,44 @@
UC.setHoverPop(false); UC.setHoverPop(false);
WIN.addEventListener("resize", () => { WIN.addEventListener("resize", () => {
API.event.emit('resize'); event.emit('resize');
}); });
API.event.on('resize', () => { event.on('resize', () => {
if (WIN.innerHeight < 800) { if (WIN.innerHeight < 800) {
UI.modalBox.classList.add('mh85'); UI.modalBox.classList.add('mh85');
} else { } else {
UI.modalBox.classList.remove('mh85'); UI.modalBox.classList.remove('mh85');
} }
API.view.update_slider(); view.update_slider();
}); });
SPACE.showSkyGrid(false); SPACE.showSkyGrid(false);
SPACE.setSkyColor(controller.dark ? 0 : 0xffffff); SPACE.setSkyColor(controller.dark ? 0 : 0xffffff);
SPACE.init(container, function (delta) { SPACE.init(container, function (delta) {
if (API.var.layer_max === 0 || !delta) return; let vars = API.var;
if (vars.layer_max === 0 || !delta) return;
if (controller.reverseZoom) delta = -delta; if (controller.reverseZoom) delta = -delta;
let same = API.var.layer_hi === API.var.layer_lo; let same = vars.layer_hi === vars.layer_lo;
let track = API.var.layer_lo > 0; let track = vars.layer_lo > 0;
if (delta > 0) { if (delta > 0) {
API.var.layer_hi = Math.max(same ? 0 : API.var.layer_lo, API.var.layer_hi - 1); vars.layer_hi = Math.max(same ? 0 : vars.layer_lo, vars.layer_hi - 1);
if (track) { if (track) {
API.var.layer_lo = Math.max(0, API.var.layer_lo - 1); vars.layer_lo = Math.max(0, vars.layer_lo - 1);
} }
} else if (delta < 0) { } else if (delta < 0) {
API.var.layer_hi = Math.min(API.var.layer_max, API.var.layer_hi + 1); vars.layer_hi = Math.min(vars.layer_max, vars.layer_hi + 1);
if (track) { if (track) {
API.var.layer_lo = Math.min(API.var.layer_hi, API.var.layer_lo + 1); vars.layer_lo = Math.min(vars.layer_hi, vars.layer_lo + 1);
} }
} }
if (same) { if (same) {
API.var.layer_lo = API.var.layer_hi; vars.layer_lo = vars.layer_hi;
} }
API.view.update_slider(); view.update_slider();
API.show.slices(); show.slices();
}, controller.ortho); }, controller.ortho);
SPACE.platform.onMove(API.conf.save); SPACE.platform.onMove(conf.save);
SPACE.platform.setRound(true); SPACE.platform.setRound(true);
SPACE.useDefaultKeys(API.feature.on_key === undefined || API.feature.on_key_defaults); SPACE.useDefaultKeys(API.feature.on_key === undefined || API.feature.on_key_defaults);
@ -1511,12 +1514,6 @@
search: $('dev-search'), search: $('dev-search'),
filter: $('dev-filter') filter: $('dev-filter')
}, },
tool: {
rotate: () => { UI.tool.show('ft-rotate') },
scale: () => { UI.tool.show('ft-scale') },
mesh: () => { UI.tool.show('ft-mesh') },
select: () => { UI.tool.show('ft-select') },
},
mesh: { mesh: {
name: $('mesh-name'), name: $('mesh-name'),
points: $('mesh-points'), points: $('mesh-points'),
@ -2740,25 +2737,29 @@
}; };
}; };
}); });
// bind tool show buttons
UI.tool.show = function(tictac) { // bind tool buttons
if (typeof(tictac) === 'string') { {
nextool = listool.indexOf(tictac)+1; let { event } = API;
tictac = $(tictac); let next = 0;
} let list = ['rotate','scale','mesh','select'];
let mover = parentWithClass(tictac.control, 'movable'); event.on("tool.next", () => {
mover.style.display = 'flex'; event.emit("tool.show", list[next++ % 4]);
tictac.control.onclick(); });
event.on("tool.show", tictac => {
if (typeof(tictac) === 'string') {
next = list.indexOf(tictac)+1;
tictac = $(`ft-${tictac}`);
}
let mover = parentWithClass(tictac.control, 'movable');
mover.style.display = 'flex';
tictac.control.onclick();
});
$('tool-rotate').onclick = event.bind("tool.show", "rotate");
$('tool-scale').onclick = event.bind("tool.show", "scale");
$('tool-mesh').onclick = event.bind("tool.show", "mesh");
$('tool-selector').onclick = event.bind("tool.show", "select");
} }
let nextool = 0;
let listool = ['ft-rotate','ft-scale','ft-mesh','ft-select'];
UI.tool.next = function() {
UI.tool.show(listool[nextool++ % 4]);
};
$('tool-rotate').onclick = () => { UI.tool.show('ft-rotate') };
$('tool-scale').onclick = () => { UI.tool.show('ft-scale') };
$('tool-mesh').onclick = () => { UI.tool.show('ft-mesh') };
$('tool-selector').onclick = () => { UI.tool.show('ft-select') };
// warn users they are running a beta release // warn users they are running a beta release
if (KIRI.beta && KIRI.beta > 0 && SDB.kiri_beta != KIRI.beta) { if (KIRI.beta && KIRI.beta > 0 && SDB.kiri_beta != KIRI.beta) {

View file

@ -307,8 +307,9 @@
file: showHelpFile file: showHelpFile
}, },
event: { event: {
on: addOnEvent, on: (t,l) => { EVENT.subscribe(t,l) },
emit: sendOnEvent, emit: (t,m,o) => { EVENT.publish(t,m,o) },
bind: (t,m,o) => { return EVENT.bind(t,m,o) },
import: loadFile, import: loadFile,
alerts: updateAlerts, alerts: updateAlerts,
settings: triggerSettingsEvent settings: triggerSettingsEvent
@ -584,88 +585,78 @@
* Utility Functions * Utility Functions
******************************************************************* */ ******************************************************************* */
function unitScale() { function unitScale() {
return MODE === MODES.CAM && return MODE === MODES.CAM && settings.controller.units === 'in' ? 25.4 : 1;
settings.controller.units === 'in' ? 25.4 : 1; }
}
function alert2(message, time) { function alert2(message, time) {
if (message === undefined || message === null) { if (message === undefined || message === null) {
return updateAlerts(true); return updateAlerts(true);
} }
let rec = [message, Date.now(), time, true]; let rec = [message, Date.now(), time, true];
if (feature.alert_event) { if (feature.alert_event) {
API.event.emit('alert', rec); API.event.emit('alert', rec);
} else { } else {
alerts.push(rec); alerts.push(rec);
updateAlerts(); updateAlerts();
} }
return rec; return rec;
} }
function alert2cancel(rec,recs) { function alert2cancel(rec,recs) {
if (Array.isArray(recs)) { if (Array.isArray(recs)) {
for (let r of recs) { for (let r of recs) {
alert2cancel(r); alert2cancel(r);
} }
return; return;
} }
if (feature.alert_event) { if (feature.alert_event) {
API.event.emit('alert.cancel', rec); API.event.emit('alert.cancel', rec);
return; return;
} }
if (Array.isArray(rec)) { if (Array.isArray(rec)) {
rec[3] = false; rec[3] = false;
updateAlerts(); updateAlerts();
} }
} }
function updateAlerts(clear) { function updateAlerts(clear) {
if (clear) { if (clear) {
alerts = []; alerts = [];
} }
let now = Date.now(); let now = Date.now();
// filter out by age and active flag // filter out by age and active flag
alerts = alerts.filter(alert => { alerts = alerts.filter(alert => {
return alert[3] && (now - alert[1]) < ((alert[2] || 5) * 1000); return alert[3] && (now - alert[1]) < ((alert[2] || 5) * 1000);
}); });
// limit to 5 showing // limit to 5 showing
while (alerts.length > 5) { while (alerts.length > 5) {
alerts.shift(); alerts.shift();
} }
// return if called before UI configured // return if called before UI configured
if (!UI.alert) { if (!UI.alert) {
return; return;
} }
if (alerts.length > 0) { if (alerts.length > 0) {
UI.alert.text.innerHTML = alerts.map(v => ['<p>',v[0],'</p>'].join('')).join(''); UI.alert.text.innerHTML = alerts.map(v => ['<p>',v[0],'</p>'].join('')).join('');
UI.alert.dialog.style.display = 'flex'; UI.alert.dialog.style.display = 'flex';
} else { } else {
UI.alert.dialog.style.display = 'none'; UI.alert.dialog.style.display = 'none';
} }
} }
function getShowFavorites(bool) { function getShowFavorites(bool) {
if (bool !== undefined) { if (bool !== undefined) {
SDB.setItem('dev-favorites', bool); SDB.setItem('dev-favorites', bool);
showFavorites = bool; showFavorites = bool;
return bool; return bool;
} }
return showFavorites; return showFavorites;
} }
function sendOnEvent(name, data, options) { function triggerSettingsEvent() {
EVENT.publish(name, data, options); API.event.emit('settings', settings);
} }
function addOnEvent(name, handler) {
EVENT.subscribe(name, handler);
return API.event;
}
function triggerSettingsEvent() {
API.event.emit('settings', settings);
}
function isSecure(proto) { function isSecure(proto) {
return proto.toLowerCase().indexOf("https") === 0; return proto.toLowerCase().indexOf("https") === 0;

View file

@ -50,8 +50,8 @@ class Broker {
// return function bound to a topic // return function bound to a topic
bind(topic, message, options) { bind(topic, message, options) {
let broker = this; let broker = this;
return function(msg = message, opt = options) { return function(msg, opt) {
broker.publish(topic, msg, opt); broker.publish(topic, message || msg, options || opt);
} }
} }
} }