blockbench/js/modes.js

180 lines
4.5 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) {
delete Modes[Modes.selected.id];
Modes.previous_id = Modes.selected.id;
}
if (typeof Modes.selected.onUnselect === 'function') {
Blockbench.dispatchEvent('unselect_mode', {mode: Modes.selected})
2020-03-11 05:19:17 +08:00
Modes.selected.onUnselect()
}
if (Modes.selected.selected) {
Modes.selected.selected = false
}
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()
}
if (Blockbench.isMobile) {
Interface.PanelSelectorVue.$forceUpdate();
Interface.PanelSelectorVue.select(null);
}
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';
}
2020-03-11 05:19:17 +08:00
updateInterface()
Canvas.updateRenderSides()
if (BarItems[this.default_tool]) {
BarItems[this.default_tool].select()
} else {
BarItems.move_tool.select()
}
TickUpdates.selection = true;
Blockbench.dispatchEvent('select_mode', {mode: this})
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 : 'edit'
},
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: {
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',
2021-11-23 19:51:09 +08:00
condition: () => Format && !Format.pose_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',
condition: () => Format,
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);
BarItems.slider_color_h.update();
BarItems.slider_color_s.update();
BarItems.slider_color_v.update();
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
})
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()
}
})
})