blockbench/js/modes.js

204 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-03-11 05:19:17 +08:00
class Mode extends KeybindItem {
2020-12-22 20:32:49 +08:00
constructor(id, data) {
if (typeof id == 'object') {
data = id;
id = data.id;
}
super(id, data)
2020-12-22 20:32:49 +08:00
this.id = id;
2020-03-11 05:19:17 +08:00
this.name = data.name || tl('mode.'+this.id);
this.selected = false
2020-12-22 20:32:49 +08:00
2020-03-11 05:19:17 +08:00
this.default_tool = data.default_tool;
2021-07-28 23:49:09 +08:00
this.selectElements = data.selectElements !== false
this.hidden_node_types = data.hidden_node_types instanceof Array ? data.hidden_node_types.slice() : [];
2020-12-22 20:32:49 +08:00
2020-03-11 05:19:17 +08:00
this.hide_toolbars = data.hide_toolbars
2020-12-22 20:32:49 +08:00
this.hide_sidebars = data.hide_sidebars
2021-01-11 04:05:30 +08:00
this.hide_status_bar = data.hide_status_bar
2020-12-22 20:32:49 +08:00
2020-03-11 05:19:17 +08:00
this.condition = data.condition;
this.onSelect = data.onSelect;
this.onUnselect = data.onUnselect;
2020-12-22 20:32:49 +08:00
2020-03-11 05:19:17 +08:00
Modes.options[this.id] = this;
2020-12-22 20:32:49 +08: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-11 05:19:17 +08:00
}
select() {
if (Modes.selected) {
Modes.selected.unselect();
2020-03-11 05:19:17 +08:00
}
this.selected = true;
Mode.selected = this;
2020-03-11 05:19:17 +08:00
Modes.selected = this;
Modes[Modes.selected.id] = true;
2021-07-11 04:22:02 +08:00
if (Project) Project.mode = this.id;
2020-03-11 05:19:17 +08:00
document.body.setAttribute('mode', this.id);
2021-01-11 04:05:30 +08:00
$('#main_toolbar .toolbar_wrapper').css('visibility', this.hide_toolbars ? 'hidden' : 'visible');
$('#status_bar').css('display', this.hide_status_bar ? 'none' : 'flex');
2020-03-11 05:19:17 +08:00
Outliner.vue.options.hidden_types.replace(this.hidden_node_types);
2020-03-11 05:19:17 +08:00
if (typeof this.onSelect === 'function') {
this.onSelect()
}
updatePanelSelector();
ReferenceImage.updateAll();
2020-03-11 05:19:17 +08:00
if (Interface.Panels[Prop.active_panel] && !Condition(Interface.Panels[Prop.active_panel].condition)) {
Prop.active_panel = 'preview';
}
for (let id in Panels) {
Panels[id].position_data = Interface.getModeData().panels[id];
Panels[id].updateSlot();
}
updateSidebarOrder();
2020-03-11 05:19:17 +08: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-11 05:19:17 +08:00
} else {
if (!BarItems.move_tool.selected) BarItems.move_tool.select();
2020-03-11 05:19:17 +08:00
}
TickUpdates.interface = true;
2020-03-11 05:19:17 +08:00
TickUpdates.selection = true;
Blockbench.dispatchEvent('select_mode', {mode: this})
2020-03-11 05:19:17 +08: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-11 05:19:17 +08:00
trigger() {
if (Condition(this.condition)) {
this.select()
}
}
2020-11-28 06:23:16 +08:00
delete() {
if (Mode.selected == this) {
2021-07-07 19:08:56 +08:00
Modes.options.edit.select();
2020-11-28 06:23:16 +08:00
}
delete Modes.options[this.id];
}
2020-03-11 05:19:17 +08:00
}
const Modes = {
2020-11-28 06:23:16 +08:00
get id() {
return Mode.selected ? Mode.selected.id : ''
2020-11-28 06:23:16 +08:00
},
2020-03-11 05:19:17 +08:00
selected: false,
options: {},
};
onVueSetup(function() {
Modes.vue = new Vue({
el: '#mode_selector',
data: {
options: Modes.options
},
methods: {
2022-08-06 19:37:20 +08:00
showModes() {
let count = 0;
for (let key in this.options) {
if (Condition(this.options[key].condition)) count++;
}
return count > 1;
},
Condition
2020-03-11 05:19:17 +08:00
}
})
2021-07-07 19:08:56 +08:00
});
BARS.defineActions(function() {
2020-12-22 20:32:49 +08:00
new Mode('edit', {
2020-03-11 05:19:17 +08:00
default_tool: 'move_tool',
category: 'navigate',
2022-08-06 19:37:20 +08:00
condition: () => Format && Format.edit_mode,
2021-12-08 01:31:16 +08:00
onUnselect: () => {
if (Undo) Undo.closeAmendEditMenu();
}
2020-03-11 05:19:17 +08:00
})
2020-12-22 20:32:49 +08:00
new Mode('paint', {
2020-03-11 05:19:17 +08:00
default_tool: 'brush_tool',
category: 'navigate',
2022-08-06 19:37:20 +08:00
condition: () => Format && Format.paint_mode,
2020-03-11 05:19:17 +08: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-11 05:19:17 +08: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-11 05:19:17 +08:00
Panels.uv.handle.firstChild.textContent = tl('mode.paint');
2022-08-06 19:37:20 +08:00
2021-08-18 04:02:23 +08:00
UVEditor.vue.setMode('paint');
2020-03-11 05:19:17 +08:00
three_grid.visible = false;
},
onUnselect: () => {
Canvas.updateAllBones()
Outliner.elements.forEach(cube => {
if (cube.preview_controller.updatePaintingGrid) cube.preview_controller.updatePaintingGrid(cube);
2020-03-11 05:19:17 +08:00
})
Panels.uv.handle.firstChild.textContent = tl('panel.uv');
2021-08-18 04:02:23 +08:00
UVEditor.vue.setMode('uv');
2020-03-11 05:19:17 +08:00
three_grid.visible = true;
},
})
2021-11-23 19:51:09 +08:00
new Mode('pose', {
default_tool: 'rotate_tool',
category: 'navigate',
condition: () => Format && Format.pose_mode,
})
2020-12-22 20:32:49 +08:00
new Mode('display', {
2021-07-28 23:49:09 +08:00
selectElements: false,
2020-03-11 05:19:17 +08:00
default_tool: 'move_tool',
category: 'navigate',
condition: () => Format.display_mode,
onSelect: () => {
enterDisplaySettings()
},
onUnselect: () => {
exitDisplaySettings()
},
})
2020-12-22 20:32:49 +08:00
new Mode('animate', {
2020-03-11 05:19:17 +08:00
default_tool: 'move_tool',
category: 'navigate',
hidden_node_types: ['cube', 'mesh', 'texture_mesh'],
2020-03-11 05:19:17 +08:00
condition: () => Format.animation_mode,
onSelect: () => {
Animator.join()
},
onUnselect: () => {
Animator.leave()
}
})
})