blockbench/js/modes.js

211 lines
5.8 KiB
JavaScript
Raw Permalink Normal View History

2020-03-10 22:19:17 +01:00
class Mode extends KeybindItem {
2020-12-22 13:32:49 +01:00
constructor(id, data) {
if (typeof id == 'object') {
data = id;
id = data.id;
}
super(id, data)
2020-12-22 13:32:49 +01:00
this.id = id;
2020-03-10 22:19:17 +01:00
this.name = data.name || tl('mode.'+this.id);
this.selected = false
2020-12-22 13:32:49 +01:00
2020-03-10 22:19:17 +01:00
this.default_tool = data.default_tool;
2021-07-28 17:49:09 +02:00
this.selectElements = data.selectElements !== false
this.hidden_node_types = data.hidden_node_types instanceof Array ? data.hidden_node_types.slice() : [];
2020-12-22 13:32:49 +01:00
2020-03-10 22:19:17 +01:00
this.hide_toolbars = data.hide_toolbars
2020-12-22 13:32:49 +01:00
this.hide_sidebars = data.hide_sidebars
2021-01-10 21:05:30 +01:00
this.hide_status_bar = data.hide_status_bar
2020-12-22 13:32:49 +01:00
2020-03-10 22:19:17 +01:00
this.condition = data.condition;
this.onSelect = data.onSelect;
this.onUnselect = data.onUnselect;
2020-12-22 13:32:49 +01:00
2020-03-10 22:19:17 +01:00
Modes.options[this.id] = this;
2020-12-22 13:32:49 +01:00
if (data.component) {
let node = document.createElement('div');
let mount = document.createElement('div');
node.id = 'mode_screen_' + this.id;
node.appendChild(mount);
document.getElementById('center').appendChild(node);
this.vue = new Vue(data.component)
this.vue.$mount(mount);
}
2020-03-10 22:19:17 +01:00
}
select() {
if (Modes.selected) {
Modes.selected.unselect();
2020-03-10 22:19:17 +01:00
}
this.selected = true;
Mode.selected = this;
2020-03-10 22:19:17 +01:00
Modes.selected = this;
Modes[Modes.selected.id] = true;
2021-07-10 22:22:02 +02:00
if (Project) Project.mode = this.id;
2020-03-10 22:19:17 +01:00
document.body.setAttribute('mode', this.id);
2021-01-10 21:05:30 +01:00
$('#main_toolbar .toolbar_wrapper').css('visibility', this.hide_toolbars ? 'hidden' : 'visible');
$('#status_bar').css('display', this.hide_status_bar ? 'none' : 'flex');
2020-03-10 22:19:17 +01:00
Outliner.vue.options.hidden_types.replace(this.hidden_node_types);
2020-03-10 22:19:17 +01:00
if (typeof this.onSelect === 'function') {
this.onSelect()
}
updatePanelSelector();
2020-03-10 22:19:17 +01:00
if (Interface.Panels[Prop.active_panel] && !Condition(Interface.Panels[Prop.active_panel].condition)) {
Prop.active_panel = 'preview';
}
2020-03-10 22:19:17 +01:00
Canvas.updateRenderSides()
if (this.tool && BarItems[this.tool] && Condition(BarItems[this.tool])) {
BarItems[this.tool].select();
} else if (BarItems[this.default_tool]) {
if (!BarItems[this.default_tool].selected) BarItems[this.default_tool].select();
2020-03-10 22:19:17 +01:00
} else {
if (!BarItems.move_tool.selected) BarItems.move_tool.select();
2020-03-10 22:19:17 +01:00
}
TickUpdates.interface = true;
2020-03-10 22:19:17 +01:00
TickUpdates.selection = true;
Blockbench.dispatchEvent('select_mode', {mode: this})
2020-03-10 22:19:17 +01:00
}
unselect() {
delete Modes[this.id];
Modes.previous_id = this.id;
if (typeof this.onUnselect === 'function') {
Blockbench.dispatchEvent('unselect_mode', {mode: this})
this.onUnselect()
}
this.selected = false;
Mode.selected = Modes.selected = 0;
}
2020-03-10 22:19:17 +01:00
trigger() {
if (Condition(this.condition)) {
this.select()
}
}
2020-11-27 23:23:16 +01:00
delete() {
if (Mode.selected == this) {
2021-07-07 13:08:56 +02:00
Modes.options.edit.select();
2020-11-27 23:23:16 +01:00
}
delete Modes.options[this.id];
}
2020-03-10 22:19:17 +01:00
}
const Modes = {
2020-11-27 23:23:16 +01:00
get id() {
return Mode.selected ? Mode.selected.id : ''
2020-11-27 23:23:16 +01:00
},
2020-03-10 22:19:17 +01:00
selected: false,
options: {},
};
onVueSetup(function() {
Modes.vue = new Vue({
el: '#mode_selector',
data: {
options: Modes.options
},
methods: {
2022-08-06 13:37:20 +02:00
showModes() {
let count = 0;
for (let key in this.options) {
if (Condition(this.options[key].condition)) count++;
}
return count > 1;
},
Condition
2020-03-10 22:19:17 +01:00
}
})
2021-07-07 13:08:56 +02:00
});
BARS.defineActions(function() {
2020-12-22 13:32:49 +01:00
new Mode('edit', {
2020-03-10 22:19:17 +01:00
default_tool: 'move_tool',
category: 'navigate',
2022-08-06 13:37:20 +02:00
condition: () => Format && Format.edit_mode,
2021-12-07 18:31:16 +01:00
onUnselect: () => {
if (Undo) Undo.closeAmendEditMenu();
}
2020-03-10 22:19:17 +01:00
})
2020-12-22 13:32:49 +01:00
new Mode('paint', {
2020-03-10 22:19:17 +01:00
default_tool: 'brush_tool',
category: 'navigate',
2022-08-06 13:37:20 +02:00
condition: () => Format && Format.paint_mode,
2020-03-10 22:19:17 +01:00
onSelect: () => {
if (Modes.previous_id == 'animate') {
Animator.preview();
}
Outliner.elements.forEach(cube => {
if (cube.preview_controller.updatePaintingGrid) cube.preview_controller.updatePaintingGrid(cube);
2020-03-10 22:19:17 +01:00
})
$('#main_colorpicker').spectrum('set', ColorPanel.vue._data.main_color);
if (StateMemory.color_picker_rgb) {
BarItems.slider_color_red.update();
BarItems.slider_color_green.update();
BarItems.slider_color_blue.update();
} else {
BarItems.slider_color_h.update();
BarItems.slider_color_s.update();
BarItems.slider_color_v.update();
}
2020-03-10 22:19:17 +01:00
Panels.uv.handle.firstChild.textContent = tl('mode.paint');
2022-08-06 13:37:20 +02:00
if (Format.image_editor) {
2022-08-06 13:37:20 +02:00
let old_color_slot = Panels.color.slot;
Panels.color.position_data = Interface.data.panels.color_2d;
if (Panels.color.slot !== old_color_slot) Panels.color.moveTo(Panels.color.slot);
}
Panels.uv.position_data = Interface.data.panels.paint;
if (Panels.uv.slot !== Interface.data.panels.uv.slot) Panels.uv.moveTo(Panels.uv.slot);
2021-08-17 22:02:23 +02:00
UVEditor.vue.setMode('paint');
2020-03-10 22:19:17 +01:00
three_grid.visible = false;
},
onUnselect: () => {
Canvas.updateAllBones()
Outliner.elements.forEach(cube => {
if (cube.preview_controller.updatePaintingGrid) cube.preview_controller.updatePaintingGrid(cube);
2020-03-10 22:19:17 +01:00
})
Panels.uv.handle.firstChild.textContent = tl('panel.uv');
Panels.uv.position_data = Interface.data.panels.uv;
if (Panels.uv.slot !== Interface.data.panels.paint.slot) Panels.uv.moveTo(Panels.uv.slot);
2022-08-06 13:37:20 +02:00
if (Panels.color.position_data == Interface.data.panels.color_2d) {
let old_color_slot = Panels.color.slot;
Panels.color.position_data = Interface.data.panels.color;
if (Panels.color.slot !== old_color_slot) Panels.color.moveTo(Panels.color.slot);
}
2021-08-17 22:02:23 +02:00
UVEditor.vue.setMode('uv');
2020-03-10 22:19:17 +01:00
three_grid.visible = true;
},
})
2021-11-23 12:51:09 +01:00
new Mode('pose', {
default_tool: 'rotate_tool',
category: 'navigate',
condition: () => Format && Format.pose_mode,
})
2020-12-22 13:32:49 +01:00
new Mode('display', {
2021-07-28 17:49:09 +02:00
selectElements: false,
2020-03-10 22:19:17 +01:00
default_tool: 'move_tool',
category: 'navigate',
condition: () => Format.display_mode,
onSelect: () => {
enterDisplaySettings()
},
onUnselect: () => {
exitDisplaySettings()
},
})
2020-12-22 13:32:49 +01:00
new Mode('animate', {
2020-03-10 22:19:17 +01:00
default_tool: 'move_tool',
category: 'navigate',
hidden_node_types: ['cube', 'mesh', 'texture_mesh'],
2020-03-10 22:19:17 +01:00
condition: () => Format.animation_mode,
onSelect: () => {
Animator.join()
},
onUnselect: () => {
Animator.leave()
}
})
})