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

View file

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

View file

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