import { Mesh, Vector3 } from "@babylonjs/core"; import { Button, Rectangle, StackPanel } from "@babylonjs/gui"; import { SceneManager } from "../controller/scene-manager"; import { UIManager } from "../controller/ui-manager"; import { BabylonUIStyleTool } from "./babylon-ui-style-tool"; import { GizmoTool } from "./gizmo-tool"; export class PosPointTool { static readonly c_key = "PosPoint"; static instance: PosPointTool; mesh: Mesh; pos: Vector3; uiRoot: Rectangle; //#region UI initUI() { let instance = this; this.uiRoot = new Rectangle("PosPointTooUI"); UIManager.Instance.uiRoot.addControl(this.uiRoot); BabylonUIStyleTool.setStyle_size(this.uiRoot, "40px", "20px"); let stack = new StackPanel("stack"); this.uiRoot.addControl(stack); stack.isVertical = false; let btn_add = Button.CreateSimpleButton("add", "+"); stack.addControl(btn_add); btn_add.background = BabylonUIStyleTool.c_color_blue; btn_add.color = "white"; btn_add.thickness = 0; btn_add.onPointerClickObservable.add(() => { instance.btn_add(); }); BabylonUIStyleTool.setStyle_size(btn_add, "20px", "20px"); let btn_reduce = Button.CreateSimpleButton("reduce", "-"); stack.addControl(btn_reduce); btn_reduce.background = BabylonUIStyleTool.c_color_red; btn_reduce.color = "white"; btn_reduce.thickness = 0; btn_reduce.onPointerClickObservable.add(() => { instance.btn_reduce(); }); BabylonUIStyleTool.setStyle_size(btn_reduce, "20px", "20px"); } showUI(show: boolean) { this.uiRoot.isVisible = show; this.uiRoot.linkWithMesh(this.mesh); this.uiRoot.linkOffsetY = "-50px"; } //#endregion static get Instance() { if (PosPointTool.instance == null) { PosPointTool.instance = new PosPointTool(); PosPointTool.instance.initUI(); SceneManager.Instance.scene.onBeforeRenderObservable.add(PosPointTool.onUpdate); GizmoTool.onGizmoAimMeshObservable.add((mesh) => { if (mesh == null || !mesh.name.match(PosPointTool.c_key)) { PosPointTool.attachMesh(null); } }); } return PosPointTool.instance; } onAdd: () => void; onReduce: () => void; static attachMesh(mesh: Mesh, pos?: Vector3, onAdd?: () => void, onReduce?: () => void) { PosPointTool.Instance.mesh = mesh; PosPointTool.Instance.pos = pos; PosPointTool.Instance.showUI(mesh != null); PosPointTool.Instance.onAdd = onAdd; PosPointTool.Instance.onReduce = onReduce; } static onUpdate() { if (PosPointTool.Instance.mesh != null) { } } //添加 btn_add() { if (this.onAdd) { this.onAdd(); } } //减少 btn_reduce() { if (this.onReduce) { this.onReduce(); } } }