UNPKG

@vrspace/babylonjs

Version:

vrspace.org babylonjs client

154 lines (142 loc) 5.93 kB
import { VRSPACEUI } from '../vrspace-ui.js'; import { VRSpaceAPI } from '../../client/rest-api.js'; import { World } from '../../world/world.js'; import { WorldEditor } from './world-editor.js'; import { TerrainEditor } from './terrain-editor.js'; import { SkyboxSelector } from './skybox-selector.js'; import { GroundGrid } from './ground-grid.js' import { WorldPersistence } from './world-persistence.js'; import { PromptUI } from '../widget/prompt-ui.js'; export class WorldEditorUI { /** @type {GroundGrid} */ groundGrid = null; constructor(scene) { this.scene = scene; this.hud = VRSPACEUI.hud; this.contentBase = VRSPACEUI.contentBase; /** @type {VRSpaceAPI} */ this.api = VRSpaceAPI.getInstance(); /** TODO appropriate API calls * @type {GroupsApi} */ this.groupApi = this.api.endpoint.groups; this.terrainEdit = null; this.skyboxEdit = null; this.gridButton = null; /** @type {WorldEditor} */ this.worldEditor = null; /** @type {TerrainEditor} */ this.terrainEditor = null; /** @type {SkyboxSelector} */ this.skyboxSelector = null; this.editing = false; } async show(button) { VRSPACEUI.hud.showButtons(false, button); VRSPACEUI.hud.newRow(); if (await PromptUI.agentsEnabled()) { this.promptButton = VRSPACEUI.hud.addButton("Prompt", this.contentBase + "/content/icons/prompt.png", null, false); } this.worldEdit = VRSPACEUI.hud.addButton("World", this.contentBase + "/content/icons/world.png", (b, i) => this.editWorld(b, i), false); this.terrainEdit = VRSPACEUI.hud.addButton("Terrain", this.contentBase + "/content/icons/terrain.png", (b, i) => this.editTerrain(b, i), false); this.skyboxEdit = VRSPACEUI.hud.addButton("Skybox", this.contentBase + "/content/icons/sky.png", (b, i) => this.editSkybox(b, i), false); this.gridButton = VRSPACEUI.hud.addButton("Grid", this.contentBase + "/content/icons/grid.png", (b, i) => this.showGrid(b, i), false); this.saveButton = VRSPACEUI.hud.addButton("Save", this.contentBase + "/content/icons/save.png", (b, i) => new WorldPersistence(World.lastInstance).save(), false); this.loadButton = VRSPACEUI.hud.addButton("Load", this.contentBase + "/content/icons/open.png", (b, i) => new WorldPersistence(World.lastInstance).load(), false); if (!World.lastInstance.terrain || World.lastInstance.inAR) { this.hud.markDisabled(this.terrainEdit); } if (!World.lastInstance.skyBox || World.lastInstance.inAR) { this.hud.markDisabled(this.skyboxEdit); } else { this.skyboxSelector = new SkyboxSelector(World.lastInstance); } if (WorldEditorUI.groundGrid) { this.hud.markActive(this.gridButton); } PromptUI.getInstance(World.lastInstance, 'sceneAgent', response=>this.agentResponse(response), this.promptButton); VRSPACEUI.hud.enableSpeech(true); } hide() { VRSPACEUI.hud.clearRow(); VRSPACEUI.hud.showButtons(true); this.dispose(); } dispose() { this.worldEdit.dispose(); this.terrainEdit.dispose(); this.skyboxEdit.dispose(); if (this.worldEditor) { this.worldEditor.dispose(); } if (this.terrainEditor) { this.terrainEditor.dispose(); } if (this.skyboxSelector) { this.skyboxSelector.dispose(); } } agentResponse(response) { PromptUI.getInstance().log('Scene Agent', response); } editWorld(button, vector3WithInfo) { this.editing = !this.editing; console.log("World editor active:" + this.editing); if (this.editing) { VRSPACEUI.hud.showButtons(!this.editing, button); VRSPACEUI.hud.newRow(); this.worldEditor = new WorldEditor(World.lastInstance, this.fileInputElement); } else { //while ( VRSPACEUI.hud.rows.length > 1 ) { VRSPACEUI.hud.clearRow(); //} this.worldEditor.dispose(); VRSPACEUI.hud.showButtons(!this.editing, button); // and pop agent response handler PromptUI.getInstance(World.lastInstance, 'sceneAgent', response=>this.agentResponse(response), this.promptButton); } } editTerrain(button, vector3WithInfo) { this.editing = !this.editing; console.log("Terrain editor active:" + this.editing); if (this.editing) { this.terrainEditor = new TerrainEditor(World.lastInstance); World.lastInstance.terrain.mesh().setEnabled(true); VRSPACEUI.hud.showButtons(!this.editing, button); VRSPACEUI.hud.newRow(); this.terrainEditor.edit(); // ground is not selectable while editing terrain, but otherwise must be to allow teleportation World.lastInstance.enableFloorSelection(false); } else { VRSPACEUI.hud.clearRow(); this.terrainEditor.dispose(); VRSPACEUI.hud.showButtons(!this.editing, button); // ground is not selectable while editing terrain, but otherwise must be to allow teleportation World.lastInstance.enableFloorSelection(true); } } editSkybox(button, vector3WithInfo) { this.editing = !this.editing; console.log("Skybox editor active:" + this.editing); if (this.editing) { VRSPACEUI.hud.showButtons(!this.editing, button); VRSPACEUI.hud.newRow(); this.skyboxSelector.show(); VRSPACEUI.hud.enableSpeech(true); } else { VRSPACEUI.hud.clearRow(); this.skyboxSelector.hide(); VRSPACEUI.hud.showButtons(!this.editing, button); } } showGrid(button) { if (WorldEditorUI.groundGrid) { WorldEditorUI.groundGrid.hide(); WorldEditorUI.groundGrid = null; this.hud.markEnabled(this.gridButton); } else { WorldEditorUI.groundGrid = new GroundGrid(World.lastInstance); WorldEditorUI.groundGrid.show(); this.hud.markActive(this.gridButton); } } }