diff --git a/js/interface/actions.js b/js/interface/actions.js index ac3a703f..e458781b 100644 --- a/js/interface/actions.js +++ b/js/interface/actions.js @@ -1,7 +1,8 @@ var Toolbars, BarItems, Toolbox; //Bars -class BarItem { +class BarItem extends EventSystem { constructor(id, data) { + super(); this.id = id; if (!data.private) { if (this.id && !BarItems[this.id]) { @@ -189,6 +190,7 @@ class BarItem { this.sub_keybinds[id].keybind.setAction(this.id, id); } delete() { + this.dispatchEvent('delete'); var scope = this; this.toolbars.forEach(bar => { bar.remove(scope); @@ -256,7 +258,14 @@ class Action extends BarItem { this.searchable = data.searchable; //Node - if (!this.click) this.click = data.click + if (!this.click && data.click) { + this.onClick = data.click; + this.click = (...args) => { + this.dispatchEvent('use'); + this.onClick(...args); + this.dispatchEvent('used'); + }; + } this.icon_node = Blockbench.getIconNode(this.icon, this.color) this.icon_states = data.icon_states; this.node = document.createElement('div'); @@ -308,7 +317,9 @@ class Action extends BarItem { } trigger(event) { var scope = this; - if (BARS.condition(scope.condition, scope)) { + let condition_met = BARS.condition(scope.condition, this); + this.dispatchEvent('trigger', {condition_met}); + if (condition_met) { if (event && event.type === 'click' && event.altKey && scope.keybind) { var record = function() { document.removeEventListener('keyup', record) @@ -358,6 +369,7 @@ class Action extends BarItem { } } } + this.dispatchEvent('get_node', {node: clone}); return clone; } setIcon(icon) { @@ -429,6 +441,7 @@ class Tool extends Action { } select() { if (this === Toolbox.selected) return; + let previous_tool = Toolbox.selected; if (Toolbox.selected) { Toolbox.selected.nodes.forEach(node => { node.classList.remove('enabled') @@ -467,6 +480,7 @@ class Tool extends Action { if (typeof this.onSelect == 'function') { this.onSelect() } + this.dispatchEvent('select', {previous_tool}); Interface.preview.style.cursor = this.cursor ? this.cursor : 'default'; this.nodes.forEach(node => { node.classList.add('enabled') @@ -516,6 +530,7 @@ class Toggle extends Action { Settings.saveLocalStorages(); } if (this.onChange) this.onChange(this.value); + this.dispatchEvent('change', {state: this.value}); this.updateEnabledState(); } @@ -572,7 +587,12 @@ class NumSlider extends Widget { this.onBefore = data.onBefore; this.onChange = data.onChange; this.onAfter = data.onAfter; - if (typeof data.change === 'function') this.change = data.change; + if (typeof data.change === 'function') { + this.change = (modify, ...args) => { + data.change(modify, ...args) + this.dispatchEvent('changed', {number: num, modify}); + }; + } if (data.settings) { this.settings = data.settings; if (this.settings.default) { @@ -977,6 +997,7 @@ class NumSlider extends Widget { if (typeof this.onChange === 'function') { this.onChange(num); } + this.dispatchEvent('change', {number: num}); } get() { //Solo Sliders only @@ -995,6 +1016,7 @@ class NumSlider extends Widget { if (isNaN(number) && !this.jq_inner.hasClass('editing') && this.jq_inner[0].textContent) { this.jq_inner.text('') } + this.dispatchEvent('update'); } } NumSlider.MolangParser = new Molang() @@ -1049,6 +1071,7 @@ class BarSlider extends Widget { if (this.onChange) { this.onChange(this, event) } + this.dispatchEvent('change', {value: this.value}); } set(value) { this.value = value @@ -1197,13 +1220,16 @@ class BarSelect extends Widget { } } let menu = new Menu(this.id, items); + this.dispatchEvent('open', {menu, items}); menu.node.style['min-width'] = this.node.clientWidth+'px'; menu.open(event.target, this); } trigger(event) { if (!event) event = 0; var scope = this; - if (BARS.condition(scope.condition, scope)) { + let condition_met = BARS.condition(this.condition, this); + this.dispatchEvent('trigger', {condition_met}); + if (condition_met) { if (event && event.type === 'click' && event.altKey && scope.keybind) { var record = function() { document.removeEventListener('keyup', record) @@ -1243,6 +1269,7 @@ class BarSelect extends Widget { if (this.onChange) { this.onChange(this, event); } + this.dispatchEvent('change', {value, event}); return this; } getNameFor(key) { @@ -1309,10 +1336,12 @@ class BarText extends Widget { if (typeof this.onUpdate === 'function') { this.onUpdate() } + this.dispatchEvent('update'); return this; } trigger(event) { if (!Condition(this.condition)) return false; + this.dispatchEvent('trigger'); Blockbench.showQuickMessage(this.text) return true; } @@ -1362,6 +1391,7 @@ class ColorPicker extends Widget { if (this.onChange) { this.onChange() } + this.dispatchEvent('change', {color}); } hide() { this.jq.spectrum('cancel');