From 29692a03c33a361f04cefe0fe49ee23acecf853f Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Mon, 8 Jul 2024 11:46:39 -0400 Subject: [PATCH 1/8] improved component cleanup. --- package.json | 2 +- src/component.tsx | 29 +++++++++++++++---- src/container.tsx | 16 +++++++++- src/helpers/deferredPromise.ts | 29 +++++++++++++++++++ src/main.tsx | 53 ++++------------------------------ src/vimComponentRef.ts | 2 ++ 6 files changed, 76 insertions(+), 55 deletions(-) create mode 100644 src/helpers/deferredPromise.ts diff --git a/package.json b/package.json index 1feb86ff..226931e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vim-webgl-component", - "version": "0.3.9", + "version": "0.3.10-dev.1", "description": "A demonstration app built on top of the vim-webgl-viewer", "files": [ "dist" diff --git a/src/component.tsx b/src/component.tsx index 92cbfa9c..58548149 100644 --- a/src/component.tsx +++ b/src/component.tsx @@ -38,6 +38,7 @@ import { LogoMemo } from './panels/logo' import { VimComponentRef } from './vimComponentRef' import { createBimInfoState } from './bim/bimInfoData' import { whenTrue } from './helpers/utils' +import { DeferredPromise } from './helpers/deferredPromise' export * as VIM from 'vim-webgl-viewer/' export const THREE = VIM.THREE @@ -56,24 +57,39 @@ export * from './container' * @returns An object containing the resulting container, reactRoot, and viewer. */ export function createVimComponent ( - onMount: (component: VimComponentRef) => void, container?: VimComponentContainer, componentSettings: PartialComponentSettings = {}, viewerSettings: VIM.PartialViewerSettings = {} -) { +) : Promise { + const promise = new DeferredPromise() + + // Create the viewer and container const viewer = new VIM.Viewer(viewerSettings) container = container ?? createContainer() viewer.viewport.reparent(container.gfx) + + // Create the React root const reactRoot = createRoot(container.ui) + + // Patch the component to clean up after itself + const patchRef = (cmp : VimComponentRef) => { + cmp.dispose = () => { + viewer.dispose() + container.dispose() + reactRoot.unmount() + } + return cmp + } + reactRoot.render( promise.resolve(patchRef(cmp))} settings={componentSettings} /> ) - return { container, reactRoot, viewer } + return promise } /** @@ -148,7 +164,8 @@ export function VimComponent (props: { show: (message: string, info: string) => setMsg({ message, info }), hide: () => setMsg(undefined) }, - bimInfo: bimInfoRef + bimInfo: bimInfoRef, + dispose: () => {} }) // Clean up diff --git a/src/container.tsx b/src/container.tsx index 586e7a06..2d230ae7 100644 --- a/src/container.tsx +++ b/src/container.tsx @@ -19,6 +19,8 @@ export type VimComponentContainer = { * Div to hold viewer canvases and ui */ gfx: HTMLDivElement + + dispose: () => void } /** @@ -49,5 +51,17 @@ export function createContainer (element?: HTMLElement): VimComponentContainer { root.append(gfx) root.append(ui) - return { root, ui, gfx } + const dispose = () => { + if (element === undefined) { + // We own the element, so we remove it + root.remove() + } else { + root.classList.remove('vim-component') + // We don't own the element, so we just remove our children + gfx.remove() + ui.remove() + } + } + + return { root, ui, gfx, dispose } } diff --git a/src/helpers/deferredPromise.ts b/src/helpers/deferredPromise.ts new file mode 100644 index 00000000..9b33eed6 --- /dev/null +++ b/src/helpers/deferredPromise.ts @@ -0,0 +1,29 @@ +export class DeferredPromise extends Promise { + resolve: (value: T | PromiseLike) => void + reject: (reason: T | Error) => void + + initialCallStack: Error['stack'] + + constructor (executor: ConstructorParameters>[0] = () => {}) { + let resolver: (value: T | PromiseLike) => void + let rejector: (reason: T | Error) => void + + super((resolve, reject) => { + resolver = resolve + rejector = reject + return executor(resolve, reject) // Promise magic: this line is unexplicably essential + }) + + this.resolve = resolver! + this.reject = rejector! + + // store call stack for location where instance is created + this.initialCallStack = Error().stack?.split('\n').slice(2).join('\n') + } + + /** @throws error with amended call stack */ + rejectWithError (error: Error) { + error.stack = [error.stack?.split('\n')[0], this.initialCallStack].join('\n') + this.reject(error) + } +} diff --git a/src/main.tsx b/src/main.tsx index 4a849cc3..49e70846 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -4,8 +4,7 @@ import { VimComponentRef, getLocalSettings, THREE, - VIM, - createContainer + VIM } from './component' // Parse URL @@ -15,64 +14,24 @@ const url = params.has('vim') ? params.get('vim') : null -const parent = document.createElement('div') -document.body.appendChild(parent) - -parent.style.position = 'absolute' -parent.style.top = '0%' -parent.style.right = '0%' -parent.style.left = '0%' -parent.style.bottom = '0%' - -const container = createContainer(parent) -// const container = createContainer() -createVimComponent(loadVim, container, getLocalSettings(), VIM.getViewerSettingsFromUrl(window.location.search)) +for(let i =0; i < 10; i++){ + createVimComponent(undefined, getLocalSettings(), VIM.getViewerSettingsFromUrl(window.location.search)) + .then(cmp => loadVim(cmp)) +} async function loadVim (cmp: VimComponentRef) { const time = Date.now() const vim = await cmp.loader.open( url ?? 'https://vim02.azureedge.net/samples/residence.v1.2.75.vim', + // url ?? 'https://vim02.azureedge.net/samples/skanska.vim', // url ?? 'https://vim02.azureedge.net/samples/residence.v1.2.75.vimx', { progressive: true, rotation: new THREE.Vector3(270, 0, 0) } ) - //cmp.message.show('Loading completed') - /* - const gen = run() - setInterval(() => { - gen.next() - cmp.viewer.viewport.ResizeToParent() - }, 50) - const inc = 0.25 - function * run () { - while (true) { - for (let i = 0; i < 40; i += 0.25) { - parent.style.right = i + '%' - parent.style.left = i + '%' - yield 0 - } - for (let i = 0; i < 40; i += 0.25) { - parent.style.top = i + '%' - parent.style.bottom = i + '%' - yield 0 - } - for (let i = 40; i > 0; i -= 0.25) { - parent.style.top = i + '%' - parent.style.bottom = i + '%' - yield 0 - } - for (let i = 40; i > 0; i -= 0.25) { - parent.style.right = i + '%' - parent.style.left = i + '%' - yield 0 - } - } - } -*/ console.log(`Loading completed in ${((Date.now() - time) / 1000).toFixed(2)} seconds`) globalThis.THREE = THREE globalThis.component = cmp diff --git a/src/vimComponentRef.ts b/src/vimComponentRef.ts index 497e0539..a69f9cb1 100644 --- a/src/vimComponentRef.ts +++ b/src/vimComponentRef.ts @@ -105,4 +105,6 @@ export type VimComponentRef = { * API To interact with the BIM info panel. */ bimInfo: BimInfoPanelRef + + dispose: () => void } From 6b2014b463131bf83c2a8196d2aa0cc2c4dffdd0 Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Fri, 12 Jul 2024 15:05:32 -0400 Subject: [PATCH 2/8] changed usage of create component --- src/main.tsx | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/main.tsx b/src/main.tsx index 49e70846..1e6da7cb 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,10 +1,8 @@ import { createVimComponent, - VimComponentRef, getLocalSettings, - THREE, - VIM + THREE } from './component' // Parse URL @@ -14,12 +12,9 @@ const url = params.has('vim') ? params.get('vim') : null -for(let i =0; i < 10; i++){ - createVimComponent(undefined, getLocalSettings(), VIM.getViewerSettingsFromUrl(window.location.search)) - .then(cmp => loadVim(cmp)) -} - -async function loadVim (cmp: VimComponentRef) { +demo() +async function demo () { + const cmp = await createVimComponent(undefined, getLocalSettings()) const time = Date.now() const vim = await cmp.loader.open( @@ -35,4 +30,5 @@ async function loadVim (cmp: VimComponentRef) { console.log(`Loading completed in ${((Date.now() - time) / 1000).toFixed(2)} seconds`) globalThis.THREE = THREE globalThis.component = cmp + globalThis.vim = vim } From 2538add06fc3ac83cf5d53f00feef829264a1935 Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Fri, 12 Jul 2024 15:15:32 -0400 Subject: [PATCH 3/8] version 0.3.10 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 226931e1..9dde4c4f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vim-webgl-component", - "version": "0.3.10-dev.1", + "version": "0.3.10", "description": "A demonstration app built on top of the vim-webgl-viewer", "files": [ "dist" From fa1759310a5b91c22e38ff1f6eab1b54118c368f Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Thu, 1 Aug 2024 15:51:56 -0400 Subject: [PATCH 4/8] customizable control bar --- src/controlbar/controlBarButton.tsx | 48 ++++----- src/controlbar/controlBarCamera.tsx | 102 +++++++++--------- src/controlbar/controlBarSettings.tsx | 84 ++++++++------- src/controlbar/controlBarTools.tsx | 143 ++++++++++++-------------- 4 files changed, 182 insertions(+), 195 deletions(-) diff --git a/src/controlbar/controlBarButton.tsx b/src/controlbar/controlBarButton.tsx index 591b8e5c..f5ced38e 100644 --- a/src/controlbar/controlBarButton.tsx +++ b/src/controlbar/controlBarButton.tsx @@ -1,42 +1,32 @@ import React from 'react' const btnStyle = 'vim-control-bar-button vc-rounded-full vc-items-center vc-justify-center vc-flex vc-transition-all hover:vc-scale-110' +export function stdStyle (on: boolean) { + return on + ? btnStyle + ' vc-text-primary' + : btnStyle + ' vc-text-gray-medium' +} -export function createButton ( - enabled: () => boolean, - tip: string, - action: () => void, - icon: ({ height, width, fill, className }) => JSX.Element, - isOn?: () => boolean -) { - return createAnyButton(enabled, tip, action, icon, isOn) +export function blueStyle (on: boolean) { + return btnStyle + ' vc-text-white' } -export function createBlueButton ( - enabled: () => boolean, - tip: string, - action: () => void, - icon: ({ height, width, fill, className }) => JSX.Element, + +export interface IControlBarButtonItem { + enabled?: (() => boolean) | undefined + tip: string + action: () => void + icon: ({ height, width, fill, className }) => JSX.Element isOn?: () => boolean -) { - return createAnyButton(enabled, tip, action, icon, isOn, { on: 'vc-text-white', off: 'vc-text-white' }) + style: (on: boolean) => string } -function createAnyButton ( - enabled: () => boolean, - tip: string, - action: () => void, - icon: ({ height, width, fill, className }) => JSX.Element, - isOn: () => boolean, - colors: { on: string, off: string } = { on: 'vc-text-primary', off: 'vc-text-gray-medium' } -) { - if (!enabled()) return null - const style = isOn?.() - ? btnStyle + ' ' + colors.on - : btnStyle + ' ' + colors.off +export function createButton (button: IControlBarButtonItem) { + if (button.enabled !== undefined && !button.enabled()) return null + const style = button.style(button.isOn?.()) return ( - ) } diff --git a/src/controlbar/controlBarCamera.tsx b/src/controlbar/controlBarCamera.tsx index 99531b61..25e9bf3f 100644 --- a/src/controlbar/controlBarCamera.tsx +++ b/src/controlbar/controlBarCamera.tsx @@ -10,7 +10,7 @@ import { ComponentSettings, isTrue } from '../settings/settings' -import { createButton } from './controlBarButton' +import { createButton, IControlBarButtonItem, stdStyle } from './controlBarButton' import { createSection } from './controlBarSection' export function TabCamera (props: {viewer: VIM.Viewer, camera: ComponentCamera; settings: ComponentSettings }) { @@ -34,52 +34,60 @@ export function TabCamera (props: {viewer: VIM.Viewer, camera: ComponentCamera; setMode(next) } - const btnOrbit = createButton( - () => isTrue(props.settings.ui.orbit), - 'Orbit', - () => onModeBtn('orbit'), - Icons.orbit, - () => mode === 'orbit' - ) - const btnLook = createButton( - () => isTrue(props.settings.ui.lookAround), - 'Look Around', - () => onModeBtn('look'), - Icons.look, - () => mode === 'look' - ) - const btnPan = createButton( - () => isTrue(props.settings.ui.pan), - 'Pan', - () => onModeBtn('pan'), - Icons.pan, - () => mode === 'pan' - ) - const btnZoom = createButton( - () => isTrue(props.settings.ui.zoom), - 'Zoom', - () => onModeBtn('zoom'), - Icons.zoom, - () => mode === 'zoom' - ) - const btnFrameRect = createButton( - () => isTrue(props.settings.ui.zoomWindow), - 'Zoom Window', - () => { - onModeBtn('rect') - viewer.gizmos.section.visible = false - viewer.gizmos.section.interactive = false + const buttons : IControlBarButtonItem[] = [ + { + enabled: () => isTrue(props.settings.ui.orbit), + tip: 'Orbit', + action: () => onModeBtn('orbit'), + icon: Icons.orbit, + isOn: () => mode === 'orbit', + style: stdStyle }, - Icons.frameRect, - () => mode === 'rect' - ) - const btnFrame = createButton( - () => isTrue(props.settings.ui.zoomToFit), - 'Zoom to Fit', - () => props.camera.frameContext(), - Icons.frameSelection, - () => false - ) + { + enabled: () => isTrue(props.settings.ui.lookAround), + tip: 'Look Around', + action: () => onModeBtn('look'), + icon: Icons.look, + isOn: () => mode === 'look', + style: stdStyle + }, + { + enabled: () => isTrue(props.settings.ui.pan), + tip: 'Pan', + action: () => onModeBtn('pan'), + icon: Icons.pan, + isOn: () => mode === 'pan', + style: stdStyle + }, + { + enabled: () => isTrue(props.settings.ui.zoom), + tip: 'Zoom', + action: () => onModeBtn('zoom'), + icon: Icons.zoom, + isOn: () => mode === 'zoom', + style: stdStyle + }, + { + enabled: () => isTrue(props.settings.ui.zoomWindow), + tip: 'Zoom Window', + action: () => { + onModeBtn('rect') + viewer.gizmos.section.visible = false + viewer.gizmos.section.interactive = false + }, + icon: Icons.frameRect, + isOn: () => mode === 'rect', + style: stdStyle + }, + { + enabled: () => isTrue(props.settings.ui.zoomToFit), + tip: 'Zoom to Fit', + action: () => props.camera.frameContext(), + icon: Icons.frameSelection, + isOn: () => false, + style: stdStyle + } + ] - return createSection('white', [btnOrbit, btnLook, btnPan, btnZoom, btnFrameRect, btnFrame]) + return createSection('white', buttons.map(b => createButton(b))) } diff --git a/src/controlbar/controlBarSettings.tsx b/src/controlbar/controlBarSettings.tsx index 9ee81fa8..bf207da2 100644 --- a/src/controlbar/controlBarSettings.tsx +++ b/src/controlbar/controlBarSettings.tsx @@ -9,7 +9,7 @@ import { HelpState } from '../panels/help' import { ComponentSettings, isTrue } from '../settings/settings' -import { createButton } from './controlBarButton' +import { createButton, IControlBarButtonItem, stdStyle } from './controlBarButton' import { createSection } from './controlBarSection' import { FullScreenObserver } from '../helpers/fullScreenObserver' @@ -40,48 +40,46 @@ export function TabSettings (props: { props.side.toggleContent('settings') } - const btnTreeView = createButton( - () => isTrue(props.settings.ui.projectInspector), - 'Project Inspector', - onTreeViewBtn, - Icons.treeView, - () => props.side.getContent() === 'bim' - ) - const btnSettings = createButton( - () => isTrue(props.settings.ui.settings), - 'Settings', - onSettingsBtn, - Icons.settings, - () => props.side.getContent() === 'settings' - ) - - const btnHelp = createButton( - () => isTrue(props.settings.ui.help), - 'Help', - onHelpBtn, - Icons.help, - () => props.help.visible - ) - - const btnFullScreen = createButton( - () => - isTrue(props.settings.ui.maximise) && - props.settings.capacity.canGoFullScreen, - fullScreen ? 'Fullscreen' : 'Minimize', - () => { - if (fullScreen) { - document.exitFullscreen() - } else { - document.body.requestFullscreen() - } + const buttons : IControlBarButtonItem[] = [ + { + enabled: () => isTrue(props.settings.ui.projectInspector) && ( + isTrue(props.settings.ui.bimTreePanel) || + isTrue(props.settings.ui.bimInfoPanel)), + tip: 'Project Inspector', + action: onTreeViewBtn, + icon: Icons.treeView, + style: stdStyle }, - fullScreen ? Icons.minimize : Icons.fullsScreen - ) - - const tree = isTrue(props.settings.ui.bimTreePanel) || - isTrue(props.settings.ui.bimInfoPanel) - ? btnTreeView - : null + { + enabled: () => isTrue(props.settings.ui.settings), + tip: 'Settings', + action: onSettingsBtn, + icon: Icons.settings, + style: stdStyle + }, + { + enabled: () => isTrue(props.settings.ui.help), + tip: 'Help', + action: onHelpBtn, + icon: Icons.help, + style: stdStyle + }, + { + enabled: () => + isTrue(props.settings.ui.maximise) && + props.settings.capacity.canGoFullScreen, + tip: fullScreen ? 'Minimize' : 'Fullscreen', + action: () => { + if (fullScreen) { + document.exitFullscreen() + } else { + document.body.requestFullscreen() + } + }, + icon: fullScreen ? Icons.minimize : Icons.fullsScreen, + style: stdStyle + } + ] - return createSection('white', [tree, btnSettings, btnHelp, btnFullScreen]) + return createSection('white', buttons.map(b => createButton(b))) } diff --git a/src/controlbar/controlBarTools.tsx b/src/controlbar/controlBarTools.tsx index 45ec5227..fee3fe58 100644 --- a/src/controlbar/controlBarTools.tsx +++ b/src/controlbar/controlBarTools.tsx @@ -12,7 +12,7 @@ import { ComponentSettings, isTrue } from '../settings/settings' import { loopMeasure } from '../helpers/measureLoop' -import { createBlueButton, createButton } from './controlBarButton' +import { createButton, stdStyle, blueStyle, IControlBarButtonItem } from './controlBarButton' import { createSection } from './controlBarSection' /* TAB TOOLS */ @@ -91,83 +91,74 @@ export function TabTools (props: { onMeasureBtn() } - const btnSection = createButton( - () => isTrue(props.settings.ui.sectioningMode), - 'Sectioning Mode', - onSectionBtn, - Icons.sectionBox - ) - - const btnMeasure = createButton( - () => isTrue(props.settings.ui.measuringMode), - 'Measuring Mode', - onMeasureBtn, - Icons.measure - ) - - const btnIsolation = createButton( - () => isTrue(props.settings.ui.toggleIsolation), - 'Toggle Isolation', - () => { - props.isolation.toggleIsolation('controlBar') + const toolButtons : IControlBarButtonItem[] = [ + { + enabled: () => isTrue(props.settings.ui.sectioningMode), + tip: 'Sectioning Mode', + action: onSectionBtn, + icon: Icons.sectionBox, + style: stdStyle }, - Icons.toggleIsolation - ) - - const toolsTab = createSection('white', [btnSection, btnMeasure, btnIsolation]) - - const btnMeasureDelete = createBlueButton( - () => true, - 'Delete', - onMeasureDeleteBtn, - Icons.trash - ) - const btnMeasureConfirm = createBlueButton( - () => true, - 'Done', - onMeasureBtn, - Icons.checkmark - ) - const measureTab = createSection('blue', [btnMeasureDelete, btnMeasureConfirm]) - - const btnSectionReset = createBlueButton( - () => true, - 'Reset Section Box', - onResetSectionBtn, - Icons.sectionBoxReset - ) - const btnSectionShrink = createBlueButton( - () => true, - 'Shrink to Selection', - () => viewer.gizmos.section.fitBox(viewer.selection.getBoundingBox()), - Icons.sectionBoxShrink - ) - - const btnSectionClip = createBlueButton( - () => true, - 'Apply Section Box', - onSectionClip, - Icons.sectionBoxNoClip - ) - const btnSectionNoClip = createBlueButton( - () => true, - 'Ignore Section Box', - onSectionNoClip, - Icons.sectionBoxClip - ) - const btnSectionConfirm = createBlueButton( - () => true, - 'Done', - onSectionBtn, - Icons.checkmark - ) + { + enabled: () => isTrue(props.settings.ui.measuringMode), + tip: 'Measuring Mode', + action: onMeasureBtn, + icon: Icons.measure, + style: stdStyle + }, + { + enabled: () => isTrue(props.settings.ui.toggleIsolation), + tip: 'Toggle Isolation', + action: () => props.isolation.toggleIsolation('controlBar'), + icon: Icons.toggleIsolation, + style: stdStyle + } + ] + + const toolsTab = createSection('white', toolButtons.map(b => createButton(b))) + const buttonsMeasure : IControlBarButtonItem[] = [ + { + tip: 'Delete', + action: onMeasureDeleteBtn, + icon: Icons.trash, + style: blueStyle + }, + { + tip: 'Done', + action: onMeasureBtn, + icon: Icons.checkmark, + style: blueStyle + } + ] + const measureTab = createSection('blue', buttonsMeasure.map(b => createButton(b))) + const sectionButtons : IControlBarButtonItem[] = [ + { + tip: 'Reset Section Box', + action: onResetSectionBtn, + icon: Icons.sectionBoxReset, + style: blueStyle + }, + { + tip: 'Shrink to Selection', + action: () => viewer.gizmos.section.fitBox(viewer.selection.getBoundingBox()), + icon: Icons.sectionBoxShrink, + style: blueStyle + }, + { + tip: section.clip ? 'Clip Section Box' : 'Ignore Section Box', + action: section.clip ? onSectionClip : onSectionNoClip, + icon: section.clip ? Icons.sectionBoxClip : Icons.sectionBoxNoClip, + style: blueStyle + }, + { + tip: 'Done', + action: onSectionBtn, + icon: Icons.checkmark, + style: blueStyle + } + ] - const sectionTab = createSection('blue', [ - btnSectionReset, - btnSectionShrink, - section.clip ? btnSectionNoClip : btnSectionClip, - btnSectionConfirm - ]) + const sectionTab = createSection('blue', sectionButtons.map(b => createButton(b))) // There is a weird bug with tooltips not working properly // if measureTab or sectionTab do not have the same number of buttons as toolstab From 3f06dfe7e06ffe69a3a507e4e79fcd2fe2cf67c2 Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Mon, 5 Aug 2024 14:30:19 -0400 Subject: [PATCH 5/8] Refactored control bar so it can be customized --- src/component.tsx | 14 +- src/controlbar/controlBar.tsx | 327 +++++++++++++++++++++----- src/controlbar/controlBarButton.tsx | 7 +- src/controlbar/controlBarCamera.tsx | 93 -------- src/controlbar/controlBarCommands.ts | 23 ++ src/controlbar/controlBarSection.tsx | 20 +- src/controlbar/controlBarSettings.tsx | 85 ------- src/controlbar/controlBarTools.tsx | 190 --------------- src/controlbar/fullScreenState.ts | 25 ++ src/controlbar/measureState.tsx | 65 +++++ src/controlbar/pointerState.ts | 25 ++ src/controlbar/restOfScreen.tsx | 36 +++ src/controlbar/sectionBoxState.ts | 60 +++++ src/helpers/measureLoop.ts | 36 --- src/panels/contextMenu.tsx | 21 +- src/settings/settings.ts | 27 +-- src/vimComponentRef.ts | 18 +- 17 files changed, 576 insertions(+), 496 deletions(-) delete mode 100644 src/controlbar/controlBarCamera.tsx create mode 100644 src/controlbar/controlBarCommands.ts delete mode 100644 src/controlbar/controlBarSettings.tsx delete mode 100644 src/controlbar/controlBarTools.tsx create mode 100644 src/controlbar/fullScreenState.ts create mode 100644 src/controlbar/measureState.tsx create mode 100644 src/controlbar/pointerState.ts create mode 100644 src/controlbar/restOfScreen.tsx create mode 100644 src/controlbar/sectionBoxState.ts delete mode 100644 src/helpers/measureLoop.ts diff --git a/src/component.tsx b/src/component.tsx index 58548149..3f982b3d 100644 --- a/src/component.tsx +++ b/src/component.tsx @@ -10,11 +10,12 @@ import 'vim-webgl-viewer/dist/style.css' import * as VIM from 'vim-webgl-viewer/' import { AxesPanelMemo } from './panels/axesPanel' -import { ControlBar, RestOfScreen } from './controlbar/controlBar' +import { ControlBar, ControlBarCustomization } from './controlbar/controlBar' +import { RestOfScreen } from './controlbar/restOfScreen' import { LoadingBoxMemo, MsgInfo, ComponentLoader } from './panels/loading' import { OptionalBimPanel } from './bim/bimPanel' import { - contextMenuCustomization, + ContextMenuCustomization, showContextMenu, VimContextMenuMemo } from './panels/contextMenu' @@ -44,6 +45,8 @@ export * as VIM from 'vim-webgl-viewer/' export const THREE = VIM.THREE export * as ContextMenu from './panels/contextMenu' export * as BimInfo from './bim/bimInfoData' +export * as ControlBar from './controlbar/controlBar' +export * as Icons from './panels/icons' export * from './vimComponentRef' export { getLocalComponentSettings as getLocalSettings } from './settings/settingsStorage' export { type ComponentSettings as Settings, type PartialComponentSettings as PartialSettings, defaultSettings } from './settings/settings' @@ -118,7 +121,8 @@ export function VimComponent (props: { isTrue(settings.value.ui.bimInfoPanel), Math.min(props.container.root.clientWidth * 0.25, 340) ) - const [contextMenu, setcontextMenu] = useState() + const [contextMenu, setcontextMenu] = useState() + const [controlBar, setControlBar] = useState() const bimInfoRef = createBimInfoState() const help = useHelp() @@ -160,6 +164,9 @@ export function VimComponent (props: { contextMenu: { customize: (v) => setcontextMenu(() => v) }, + controlBar: { + customize: (v) => setControlBar(() => v) + }, message: { show: (message: string, info: string) => setMsg({ message, info }), hide: () => setMsg(undefined) @@ -217,6 +224,7 @@ export function VimComponent (props: { isolation={isolation} cursor={cursor} settings={settings.value} + customization={controlBar} /> IControlBarSection[] + +export const elementIds = { + // Sections + sectionCamera: 'controlBar.sectionCamera', + sectionTools: 'controlBar.sectionTools', + sectionSettings: 'controlBar.sectionSettings', + sectionMeasure: 'controlBar.sectionMeasure', + sectionSectionBox: 'controlBar.sectionSectionBox', + + // Camera buttons + buttonCameraOrbit: 'controlBar.camera.orbit', + buttonCameraLook: 'controlBarcamera.look', + buttonCameraPan: 'controlBar.camera.pan', + buttonCameraZoom: 'controlBar.camera.zoom', + buttonCameraZoomWindow: 'controlBar.camera.zoomWindow', + buttonCameraZoomToFit: 'controlBar.camera.zoomToFit', + + // Settings buttons + buttonProjectInspector: 'controlBar.projectInspector', + buttonSettings: 'controlBar.settings', + buttonHelp: 'controlBar.help', + buttonMaximize: 'controlBar.maximize', + + // Tools buttons + buttonSectionBox: 'controlBar.sectionBox', + buttonMeasure: 'controlBar.measure', + buttonToggleIsolation: 'controlBar.toggleIsolation', + + // Measure buttons + buttonMeasureDelete: 'controlBar.measure.delete', + buttonMeasureDone: 'controlBar.measure.done', + + // Section box buttons + buttonSectionBoxReset: 'controlBar.sectionBox.reset', + buttonSectionBoxShrinkToSelection: 'controlBar.sectionBox.shrinkToSelection', + buttonSectionBoxClip: 'controlBar.sectionBox.clip', + buttonSectionBoxIgnore: 'controlBar.sectionBox.ignore', + buttonSectionBoxDone: 'controlBar.sectionBox.done' +} /** * JSX Component for the control bar. @@ -32,66 +86,229 @@ export function ControlBar (props: { isolation: Isolation cursor: CursorManager settings: ComponentSettings + customization: ControlBarCustomization }) { - // eslint-disable-next-line no-unused-vars - const [show, setShow] = useState(true) - const cameraObserver = useRef() + const pointer = getPointerState(props.viewer) + const fullScreen = getFullScreenState() + const section = getSectionBoxState(props.viewer) + const measure = getMeasureState(props.viewer, props.cursor) // On Each Render useEffect(() => { ReactTooltip.rebuild() }) - useEffect(() => { - cameraObserver.current = new CameraObserver(props.viewer, 400) - cameraObserver.current.onChange = (moving) => setShow(!moving) + const cameraSection : IControlBarSection = { + id: elementIds.sectionCamera, + enable: () => anyUiCursorButton(props.settings), + style: sectionDefaultStyle, + buttons: [ + { + id: elementIds.buttonCameraOrbit, + enabled: () => isTrue(props.settings.ui.orbit), + tip: 'Orbit', + action: () => pointer.onButton('orbit'), + icon: Icons.orbit, + isOn: () => pointer.mode === 'orbit', + style: buttonDefaultStyle + }, + { + id: elementIds.buttonCameraLook, + enabled: () => isTrue(props.settings.ui.lookAround), + tip: 'Look Around', + action: () => pointer.onButton('look'), + icon: Icons.look, + isOn: () => pointer.mode === 'look', + style: buttonDefaultStyle + }, + { + id: elementIds.buttonCameraPan, + enabled: () => isTrue(props.settings.ui.pan), + tip: 'Pan', + action: () => pointer.onButton('pan'), + icon: Icons.pan, + isOn: () => pointer.mode === 'pan', + style: buttonDefaultStyle + }, + { + id: elementIds.buttonCameraZoom, + enabled: () => isTrue(props.settings.ui.zoom), + tip: 'Zoom', + action: () => pointer.onButton('zoom'), + icon: Icons.zoom, + isOn: () => pointer.mode === 'zoom', + style: buttonDefaultStyle + }, + { + id: elementIds.buttonCameraZoomWindow, + enabled: () => isTrue(props.settings.ui.zoomWindow), + tip: 'Zoom Window', + action: () => { + pointer.onButton('rect') + section.hide() + }, + icon: Icons.frameRect, + isOn: () => pointer.mode === 'rect', + style: buttonDefaultStyle + }, + { + id: elementIds.buttonCameraZoomToFit, + enabled: () => isTrue(props.settings.ui.zoomToFit), + tip: 'Zoom to Fit', + action: () => props.camera.frameContext(), + icon: Icons.frameSelection, + isOn: () => false, + style: buttonDefaultStyle + } + ] + } + + const settingsSection : IControlBarSection = { + id: elementIds.sectionSettings, + enable: () => anyUiSettingButton(props.settings), + style: sectionDefaultStyle, + buttons: [ + { + id: elementIds.buttonProjectInspector, + enabled: () => isTrue(props.settings.ui.projectInspector) && ( + isTrue(props.settings.ui.bimTreePanel) || + isTrue(props.settings.ui.bimInfoPanel)), + tip: 'Project Inspector', + action: () => props.side.toggleContent('bim'), + icon: Icons.treeView, + style: buttonDefaultStyle + }, + { + id: elementIds.buttonSettings, + enabled: () => isTrue(props.settings.ui.settings), + tip: 'Settings', + action: () => props.side.toggleContent('settings'), + icon: Icons.settings, + style: buttonDefaultStyle + }, + { + id: elementIds.buttonHelp, + enabled: () => isTrue(props.settings.ui.help), + tip: 'Help', + action: () => props.help.setVisible(!props.help.visible), + icon: Icons.help, + style: buttonDefaultStyle + }, + { + id: elementIds.buttonMaximize, + enabled: () => + isTrue(props.settings.ui.maximise) && + props.settings.capacity.canGoFullScreen, + tip: fullScreen.get() ? 'Minimize' : 'Fullscreen', + action: () => fullScreen.toggle(), + icon: fullScreen.get() ? Icons.minimize : Icons.fullsScreen, + style: buttonDefaultStyle + } + ] + } + + const sectionBoxSection : IControlBarSection = { + id: elementIds.sectionSectionBox, + enable: () => !measure.active && section.active, + style: sectionBlueStyle, + buttons: [ + { + id: elementIds.buttonSectionBoxReset, + tip: 'Reset Section Box', + action: () => section.reset(), + icon: Icons.sectionBoxReset, + style: buttonBlueStyle + }, + { + id: elementIds.buttonSectionBoxShrinkToSelection, + tip: 'Shrink to Selection', + action: () => section.shrinkToSelection(), + icon: Icons.sectionBoxShrink, + style: buttonBlueStyle + }, + { + id: elementIds.buttonSectionBoxClip, + tip: section.clip ? 'Clip Section Box' : 'Ignore Section Box', + action: () => section.toggleClip(), + icon: section.clip ? Icons.sectionBoxClip : Icons.sectionBoxNoClip, + style: buttonBlueStyle + }, + { + id: elementIds.buttonSectionBoxDone, + tip: 'Done', + action: () => section.toggle(), + icon: Icons.checkmark, + style: buttonBlueStyle + } + ] + } - return () => { - cameraObserver.current?.dispose() - } - }, []) + const measureSection : IControlBarSection = { + id: elementIds.sectionMeasure, + enable: () => measure.active && !section.active, + style: sectionBlueStyle, + buttons: [ + { + id: elementIds.buttonMeasureDelete, + tip: 'Delete', + action: () => measure.clear(), + icon: Icons.trash, + style: buttonBlueStyle + }, + { + id: elementIds.buttonMeasureDone, + tip: 'Done', + action: () => measure.toggle(), + icon: Icons.checkmark, + style: buttonBlueStyle + } + ] + } + const toolSections: IControlBarSection = { + id: elementIds.sectionTools, + enable: () => anyUiToolButton(props.settings) && !measure.active && !section.active, + style: measure.active || section.active ? sectionBlueStyle : sectionDefaultStyle, + buttons: [ + { + id: elementIds.buttonSectionBox, + enabled: () => isTrue(props.settings.ui.sectioningMode), + tip: 'Sectioning Mode', + action: () => section.toggle(), + icon: Icons.sectionBox, + style: buttonDefaultStyle + }, + { + id: elementIds.buttonMeasure, + enabled: () => isTrue(props.settings.ui.measuringMode), + tip: 'Measuring Mode', + action: () => measure.toggle(), + icon: Icons.measure, + style: buttonDefaultStyle + }, + { + id: elementIds.buttonToggleIsolation, + enabled: () => isTrue(props.settings.ui.toggleIsolation), + tip: 'Toggle Isolation', + action: () => props.isolation.toggleIsolation('controlBar'), + icon: Icons.toggleIsolation, + style: buttonDefaultStyle + } + ] + } + + // Apply user customization + let controlBar = [cameraSection, toolSections, measureSection, sectionBoxSection, settingsSection] + controlBar = props.customization?.(controlBar) ?? controlBar + + return createBar(controlBar) +} + +function createBar (sections: IControlBarSection[]) { return
- {anyUiCursorButton(props.settings) ? : null} - {anyUiToolButton(props.settings) ? : null} - {anyUiSettingButton(props.settings) ? : null} + }} className='vim-control-bar vc-pointer-events-auto vc-flex-wrap vc-mx-2 vc-min-w-0 vc-absolute vc-left-0 vc-right-0 vc-z-20 vc-flex vc-items-center vc-justify-center transition-all'> + {sections.map(createSection)}
} - -export function RestOfScreen (props:{ - side: SideState, - content: () => JSX.Element -}) { - const [, setVersion] = useState(0) - const resizeObserver = useRef() - - // On Each Render - useEffect(() => { - ReactTooltip.rebuild() - }) - - useEffect(() => { - resizeObserver.current = new ResizeObserver(() => { - setVersion((prev) => prev ^ 1) - }) - resizeObserver.current.observe(document.body) - - return () => { - resizeObserver.current?.disconnect() - } - }, []) - - return ( -
- {props.content()} -
) -} diff --git a/src/controlbar/controlBarButton.tsx b/src/controlbar/controlBarButton.tsx index f5ced38e..06039d73 100644 --- a/src/controlbar/controlBarButton.tsx +++ b/src/controlbar/controlBarButton.tsx @@ -1,17 +1,18 @@ import React from 'react' const btnStyle = 'vim-control-bar-button vc-rounded-full vc-items-center vc-justify-center vc-flex vc-transition-all hover:vc-scale-110' -export function stdStyle (on: boolean) { +export function buttonDefaultStyle (on: boolean) { return on ? btnStyle + ' vc-text-primary' : btnStyle + ' vc-text-gray-medium' } -export function blueStyle (on: boolean) { +export function buttonBlueStyle (on: boolean) { return btnStyle + ' vc-text-white' } export interface IControlBarButtonItem { + id: string, enabled?: (() => boolean) | undefined tip: string action: () => void @@ -25,7 +26,7 @@ export function createButton (button: IControlBarButtonItem) { const style = button.style(button.isOn?.()) return ( - ) diff --git a/src/controlbar/controlBarCamera.tsx b/src/controlbar/controlBarCamera.tsx deleted file mode 100644 index 25e9bf3f..00000000 --- a/src/controlbar/controlBarCamera.tsx +++ /dev/null @@ -1,93 +0,0 @@ -/** - * @module viw-webgl-component - */ - -import { useEffect, useState } from 'react' -import * as VIM from 'vim-webgl-viewer/' -import { ComponentCamera } from '../helpers/camera' -import * as Icons from '../panels/icons' -import { - ComponentSettings, - isTrue -} from '../settings/settings' -import { createButton, IControlBarButtonItem, stdStyle } from './controlBarButton' -import { createSection } from './controlBarSection' - -export function TabCamera (props: {viewer: VIM.Viewer, camera: ComponentCamera; settings: ComponentSettings }) { - const viewer = props.viewer - const [mode, setMode] = useState(viewer.inputs.pointerActive) - - useEffect(() => { - const subPointer = viewer.inputs.onPointerModeChanged.subscribe(() => { - setMode(viewer.inputs.pointerActive) - }) - - // Clean up - return () => { - subPointer() - } - }, []) - - const onModeBtn = (target: VIM.PointerMode) => { - const next = mode === target ? viewer.inputs.pointerFallback : target - viewer.inputs.pointerActive = next - setMode(next) - } - - const buttons : IControlBarButtonItem[] = [ - { - enabled: () => isTrue(props.settings.ui.orbit), - tip: 'Orbit', - action: () => onModeBtn('orbit'), - icon: Icons.orbit, - isOn: () => mode === 'orbit', - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.lookAround), - tip: 'Look Around', - action: () => onModeBtn('look'), - icon: Icons.look, - isOn: () => mode === 'look', - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.pan), - tip: 'Pan', - action: () => onModeBtn('pan'), - icon: Icons.pan, - isOn: () => mode === 'pan', - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.zoom), - tip: 'Zoom', - action: () => onModeBtn('zoom'), - icon: Icons.zoom, - isOn: () => mode === 'zoom', - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.zoomWindow), - tip: 'Zoom Window', - action: () => { - onModeBtn('rect') - viewer.gizmos.section.visible = false - viewer.gizmos.section.interactive = false - }, - icon: Icons.frameRect, - isOn: () => mode === 'rect', - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.zoomToFit), - tip: 'Zoom to Fit', - action: () => props.camera.frameContext(), - icon: Icons.frameSelection, - isOn: () => false, - style: stdStyle - } - ] - - return createSection('white', buttons.map(b => createButton(b))) -} diff --git a/src/controlbar/controlBarCommands.ts b/src/controlbar/controlBarCommands.ts new file mode 100644 index 00000000..0a0cd357 --- /dev/null +++ b/src/controlbar/controlBarCommands.ts @@ -0,0 +1,23 @@ +import ReactTooltip from 'react-tooltip' +import * as VIM from 'vim-webgl-viewer' + +export function getControlBarCommands (viewer: VIM.Viewer) { + const section = viewer.gizmos.section + + return { + toggleSectionBox: (viewer: VIM.Viewer) => { + ReactTooltip.hide() + + if (viewer.inputs.pointerActive === 'rect') { + viewer.inputs.pointerActive = viewer.inputs.pointerFallback + } + + const next = !( + section.visible && section.interactive + ) + + section.interactive = next + section.visible = next + } + } +} diff --git a/src/controlbar/controlBarSection.tsx b/src/controlbar/controlBarSection.tsx index f4ea955b..06544138 100644 --- a/src/controlbar/controlBarSection.tsx +++ b/src/controlbar/controlBarSection.tsx @@ -1,10 +1,20 @@ import React from 'react' +import { createButton, IControlBarButtonItem } from './controlBarButton' -export function createSection (theme: 'white' | 'blue', elements : (JSX.Element | null)[]) { - const bg = theme === 'white' ? 'vc-bg-white' : 'vc-bg-primary' - const style = 'vc-flex vc-items-center vc-rounded-full vc-mb-2 vc-px-2 vc-shadow-md' +const sectionStyle = 'vc-flex vc-items-center vc-rounded-full vc-mb-2 vc-px-2 vc-shadow-md' +export const sectionDefaultStyle = sectionStyle + ' vc-bg-white' +export const sectionBlueStyle = sectionStyle + ' vc-bg-primary' - return
- {...elements} +export interface IControlBarSection { + id: string, + enable? : (() => boolean) | undefined + buttons: IControlBarButtonItem[] + style: string +} + +export function createSection (section: IControlBarSection) { + if (section.enable !== undefined && !section.enable()) return null + return
+ {section.buttons.map(b => createButton(b))}
} diff --git a/src/controlbar/controlBarSettings.tsx b/src/controlbar/controlBarSettings.tsx deleted file mode 100644 index bf207da2..00000000 --- a/src/controlbar/controlBarSettings.tsx +++ /dev/null @@ -1,85 +0,0 @@ -/** - * @module viw-webgl-component - */ - -import { useEffect, useRef, useState } from 'react' -import { SideState } from '../sidePanel/sideState' -import * as Icons from '../panels/icons' -import { HelpState } from '../panels/help' -import { - ComponentSettings, isTrue -} from '../settings/settings' -import { createButton, IControlBarButtonItem, stdStyle } from './controlBarButton' -import { createSection } from './controlBarSection' -import { FullScreenObserver } from '../helpers/fullScreenObserver' - -export function TabSettings (props: { - help: HelpState - side: SideState - settings: ComponentSettings -}) { - const fullScreenRef = useRef(new FullScreenObserver()) - const [fullScreen, setFullScreen] = useState( - fullScreenRef.current.isFullScreen() - ) - - useEffect(() => { - fullScreenRef.current.onFullScreenChange = (value) => setFullScreen(value) - return () => fullScreenRef.current.dispose() - }, []) - - const onHelpBtn = () => { - props.help.setVisible(!props.help.visible) - } - - const onTreeViewBtn = () => { - props.side.toggleContent('bim') - } - - const onSettingsBtn = () => { - props.side.toggleContent('settings') - } - - const buttons : IControlBarButtonItem[] = [ - { - enabled: () => isTrue(props.settings.ui.projectInspector) && ( - isTrue(props.settings.ui.bimTreePanel) || - isTrue(props.settings.ui.bimInfoPanel)), - tip: 'Project Inspector', - action: onTreeViewBtn, - icon: Icons.treeView, - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.settings), - tip: 'Settings', - action: onSettingsBtn, - icon: Icons.settings, - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.help), - tip: 'Help', - action: onHelpBtn, - icon: Icons.help, - style: stdStyle - }, - { - enabled: () => - isTrue(props.settings.ui.maximise) && - props.settings.capacity.canGoFullScreen, - tip: fullScreen ? 'Minimize' : 'Fullscreen', - action: () => { - if (fullScreen) { - document.exitFullscreen() - } else { - document.body.requestFullscreen() - } - }, - icon: fullScreen ? Icons.minimize : Icons.fullsScreen, - style: stdStyle - } - ] - - return createSection('white', buttons.map(b => createButton(b))) -} diff --git a/src/controlbar/controlBarTools.tsx b/src/controlbar/controlBarTools.tsx deleted file mode 100644 index fee3fe58..00000000 --- a/src/controlbar/controlBarTools.tsx +++ /dev/null @@ -1,190 +0,0 @@ -/** - * @module viw-webgl-component - */ - -import { useEffect, useRef, useState } from 'react' -import ReactTooltip from 'react-tooltip' -import * as VIM from 'vim-webgl-viewer/' -import { Isolation } from '../helpers/isolation' -import { CursorManager } from '../helpers/cursor' -import * as Icons from '../panels/icons' -import { - ComponentSettings, isTrue -} from '../settings/settings' -import { loopMeasure } from '../helpers/measureLoop' -import { createButton, stdStyle, blueStyle, IControlBarButtonItem } from './controlBarButton' -import { createSection } from './controlBarSection' - -/* TAB TOOLS */ -export function TabTools (props: { - viewer: VIM.Viewer - cursor: CursorManager - isolation: Isolation - settings: ComponentSettings -}) { - const viewer = props.viewer - // Need a ref to get the up to date value in callback. - const [measuring, setMeasuring] = useState(false) - const firstSection = useRef(true) - // eslint-disable-next-line no-unused-vars - const [measurement, setMeasurement] = useState() - const [section, setSection] = useState<{ clip: boolean; active: boolean }>({ - clip: viewer.gizmos.section.clip, - active: viewer.gizmos.section.visible && viewer.gizmos.section.interactive - }) - - const measuringRef = useRef() - measuringRef.current = measuring - - useEffect(() => { - const subSection = viewer.gizmos.section.onStateChanged.subscribe(() => - setSection({ - clip: viewer.gizmos.section.clip, - active: - viewer.gizmos.section.visible && viewer.gizmos.section.interactive - }) - ) - - // Clean up - return () => { - subSection() - } - }, []) - - const onSectionBtn = () => { - ReactTooltip.hide() - toggleSectionBox(viewer, firstSection.current) - firstSection.current = false - } - - const onMeasureBtn = () => { - ReactTooltip.hide() - - if (measuring) { - viewer.gizmos.measure.abort() - setMeasuring(false) - } else { - setMeasuring(true) - loopMeasure( - viewer, - () => measuringRef.current, - (m) => setMeasurement(m), - props.cursor.setCursor - ) - } - } - - const onResetSectionBtn = () => { - viewer.gizmos.section.fitBox(viewer.renderer.getBoundingBox()) - } - - const onSectionClip = () => { - viewer.gizmos.section.clip = true - } - const onSectionNoClip = () => { - viewer.gizmos.section.clip = false - } - - const onMeasureDeleteBtn = () => { - ReactTooltip.hide() - viewer.gizmos.measure.abort() - onMeasureBtn() - } - - const toolButtons : IControlBarButtonItem[] = [ - { - enabled: () => isTrue(props.settings.ui.sectioningMode), - tip: 'Sectioning Mode', - action: onSectionBtn, - icon: Icons.sectionBox, - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.measuringMode), - tip: 'Measuring Mode', - action: onMeasureBtn, - icon: Icons.measure, - style: stdStyle - }, - { - enabled: () => isTrue(props.settings.ui.toggleIsolation), - tip: 'Toggle Isolation', - action: () => props.isolation.toggleIsolation('controlBar'), - icon: Icons.toggleIsolation, - style: stdStyle - } - ] - - const toolsTab = createSection('white', toolButtons.map(b => createButton(b))) - const buttonsMeasure : IControlBarButtonItem[] = [ - { - tip: 'Delete', - action: onMeasureDeleteBtn, - icon: Icons.trash, - style: blueStyle - }, - { - tip: 'Done', - action: onMeasureBtn, - icon: Icons.checkmark, - style: blueStyle - } - ] - const measureTab = createSection('blue', buttonsMeasure.map(b => createButton(b))) - const sectionButtons : IControlBarButtonItem[] = [ - { - tip: 'Reset Section Box', - action: onResetSectionBtn, - icon: Icons.sectionBoxReset, - style: blueStyle - }, - { - tip: 'Shrink to Selection', - action: () => viewer.gizmos.section.fitBox(viewer.selection.getBoundingBox()), - icon: Icons.sectionBoxShrink, - style: blueStyle - }, - { - tip: section.clip ? 'Clip Section Box' : 'Ignore Section Box', - action: section.clip ? onSectionClip : onSectionNoClip, - icon: section.clip ? Icons.sectionBoxClip : Icons.sectionBoxNoClip, - style: blueStyle - }, - { - tip: 'Done', - action: onSectionBtn, - icon: Icons.checkmark, - style: blueStyle - } - ] - - const sectionTab = createSection('blue', sectionButtons.map(b => createButton(b))) - - // There is a weird bug with tooltips not working properly - // if measureTab or sectionTab do not have the same number of buttons as toolstab - - return measuring ? measureTab : section.active ? sectionTab : toolsTab -} - -function toggleSectionBox (viewer: VIM.Viewer, isFirst: boolean) { - if (viewer.inputs.pointerActive === 'rect') { - viewer.inputs.pointerActive = viewer.inputs.pointerFallback - } - - const next = !( - viewer.gizmos.section.visible && viewer.gizmos.section.interactive - ) - - if (next) { - if (isFirst) { - viewer.gizmos.section.clip = true - viewer.gizmos.section.fitBox(viewer.renderer.getBoundingBox().expandByScalar(1)) - } - if (viewer.gizmos.section.box.containsPoint(viewer.camera.position)) { - viewer.camera.lerp(1).frame(viewer.renderer.section.box) - } - } - - viewer.gizmos.section.interactive = next - viewer.gizmos.section.visible = next -} diff --git a/src/controlbar/fullScreenState.ts b/src/controlbar/fullScreenState.ts new file mode 100644 index 00000000..37338204 --- /dev/null +++ b/src/controlbar/fullScreenState.ts @@ -0,0 +1,25 @@ +import { useEffect, useState, useRef } from 'react' +import { FullScreenObserver } from '../helpers/fullScreenObserver' + +export function getFullScreenState () { + const fullScreenRef = useRef(new FullScreenObserver()) + const isFullScren = () => fullScreenRef.current.isFullScreen() + const [, setFullScreen] = useState(isFullScren()) + useEffect(() => { + fullScreenRef.current.onFullScreenChange = (value) => setFullScreen(value) + + // Clean up + return () => fullScreenRef.current.dispose() + }, []) + + return { + get: () => isFullScren(), + toggle: () => { + if (isFullScren()) { + document.exitFullscreen() + } else { + document.body.requestFullscreen() + } + } + } +} diff --git a/src/controlbar/measureState.tsx b/src/controlbar/measureState.tsx new file mode 100644 index 00000000..b12b195b --- /dev/null +++ b/src/controlbar/measureState.tsx @@ -0,0 +1,65 @@ +import ReactTooltip from 'react-tooltip' +import { useRef, useState } from 'react' +import * as VIM from 'vim-webgl-viewer/' +import { CursorManager, pointerToCursor } from '../helpers/cursor' + +export function getMeasureState (viewer: VIM.Viewer, cursor: CursorManager) { + const measuringRef = useRef(false) + const activeRef = useRef(false) + const [active, setActive] = useState(measuringRef.current) + const [, setMeasurement] = useState() + + const toggle = () => { + ReactTooltip.hide() + + if (activeRef.current) { + viewer.gizmos.measure.abort() + activeRef.current = false + setActive(false) + } else { + activeRef.current = true + setActive(true) + loop() + } + } + + const clear = () => { + ReactTooltip.hide() + viewer.gizmos.measure.abort() + toggle() + } + + /** + * Behaviour to have measure gizmo loop over and over. + */ + const loop = () => { + const onMouseMove = () => { + setMeasurement(viewer.gizmos.measure.measurement) + } + cursor.setCursor('cursor-measure') + viewer.viewport.canvas.addEventListener('mousemove', onMouseMove) + viewer.gizmos.measure + .start() + .then(() => { + setMeasurement(viewer.gizmos.measure.measurement) + }) + .catch(() => { + setMeasurement(undefined) + }) + .finally(() => { + cursor.setCursor(pointerToCursor(viewer.inputs.pointerActive)) + viewer.viewport.canvas.removeEventListener('mousemove', onMouseMove) + if (activeRef.current) { + loop() + } else { + viewer.gizmos.measure.clear() + } + }) + } + + return { + active, + toggle, + clear + } +} diff --git a/src/controlbar/pointerState.ts b/src/controlbar/pointerState.ts new file mode 100644 index 00000000..f266b833 --- /dev/null +++ b/src/controlbar/pointerState.ts @@ -0,0 +1,25 @@ +import { useEffect, useState } from 'react' +import * as VIM from 'vim-webgl-viewer/' + +export function getPointerState (viewer: VIM.Viewer) { + const [mode, setMode] = useState(viewer.inputs.pointerActive) + + useEffect(() => { + const sub = viewer.inputs.onPointerModeChanged.subscribe(() => { + setMode(viewer.inputs.pointerActive) + }) + return () => sub() + }, []) + + const onModeBtn = (target: VIM.PointerMode) => { + const next = mode === target ? viewer.inputs.pointerFallback : target + viewer.inputs.pointerActive = next + setMode(next) + } + + return { + mode, + setMode, + onButton: onModeBtn + } +} diff --git a/src/controlbar/restOfScreen.tsx b/src/controlbar/restOfScreen.tsx new file mode 100644 index 00000000..14a861da --- /dev/null +++ b/src/controlbar/restOfScreen.tsx @@ -0,0 +1,36 @@ + +import React, { useEffect, useRef, useState } from 'react' +import ReactTooltip from 'react-tooltip' +import { SideState } from '../sidePanel/sideState' + +export function RestOfScreen (props:{ + side: SideState, + content: () => JSX.Element +}) { + const [, setVersion] = useState(0) + const resizeObserver = useRef() + + // On Each Render + useEffect(() => { + ReactTooltip.rebuild() + }) + + useEffect(() => { + resizeObserver.current = new ResizeObserver(() => { + setVersion((prev) => prev ^ 1) + }) + resizeObserver.current.observe(document.body) + + return () => { + resizeObserver.current?.disconnect() + } + }, []) + + return ( +
+ {props.content()} +
) +} diff --git a/src/controlbar/sectionBoxState.ts b/src/controlbar/sectionBoxState.ts new file mode 100644 index 00000000..806f4071 --- /dev/null +++ b/src/controlbar/sectionBoxState.ts @@ -0,0 +1,60 @@ +import { useEffect, useState } from 'react' +import ReactTooltip from 'react-tooltip' +import * as VIM from 'vim-webgl-viewer/' + +export type SectionState = { + clip: boolean + active: boolean +} + +export function getSectionBoxState (viewer: VIM.Viewer) { + const sectionGizmo = viewer.gizmos.section + + const [section, setSection] = useState({ + clip: sectionGizmo.clip, + active: sectionGizmo.visible && sectionGizmo.interactive + }) + + // On First Render + useEffect(() => { + sectionGizmo.clip = true + const subSection = sectionGizmo.onStateChanged.subscribe(() => + setSection({ + clip: sectionGizmo.clip, + active: sectionGizmo.visible && sectionGizmo.interactive + }) + ) + + // Clean up + return () => subSection() + }, []) + + const toggle = () => { + ReactTooltip.hide() + + if (viewer.inputs.pointerActive === 'rect') { + viewer.inputs.pointerActive = viewer.inputs.pointerFallback + } + + const next = !( + sectionGizmo.visible && sectionGizmo.interactive + ) + + sectionGizmo.interactive = next + sectionGizmo.visible = next + } + + return { + clip: section.clip, + active: section.active, + set: setSection, + toggle, + hide: () => { + sectionGizmo.visible = false + sectionGizmo.interactive = false + }, + reset: () => sectionGizmo.fitBox(viewer.renderer.getBoundingBox()), + shrinkToSelection: () => sectionGizmo.fitBox(viewer.selection.getBoundingBox()), + toggleClip: () => { sectionGizmo.clip = (!section.clip) } + } +} diff --git a/src/helpers/measureLoop.ts b/src/helpers/measureLoop.ts deleted file mode 100644 index 25ade37a..00000000 --- a/src/helpers/measureLoop.ts +++ /dev/null @@ -1,36 +0,0 @@ - -import * as VIM from 'vim-webgl-viewer/' -import { Cursor, pointerToCursor } from './cursor' - -/** - * Behaviour to have measure gizmo loop over and over. - */ -export function loopMeasure ( - viewer: VIM.Viewer, - getMeasuring: () => boolean, - setMeasure: (value: VIM.THREE.Vector3) => void, - setCursor: (cursor: Cursor) => void -) { - const onMouseMove = () => { - setMeasure(viewer.gizmos.measure.measurement) - } - setCursor('cursor-measure') - viewer.viewport.canvas.addEventListener('mousemove', onMouseMove) - viewer.gizmos.measure - .start() - .then(() => { - setMeasure(viewer.gizmos.measure.measurement) - }) - .catch(() => { - setMeasure(undefined) - }) - .finally(() => { - setCursor(pointerToCursor(viewer.inputs.pointerActive)) - viewer.viewport.canvas.removeEventListener('mousemove', onMouseMove) - if (getMeasuring()) { - loopMeasure(viewer, getMeasuring, setMeasure, setCursor) - } else { - viewer.gizmos.measure.clear() - } - }) -} diff --git a/src/panels/contextMenu.tsx b/src/panels/contextMenu.tsx index 443acefc..3caec030 100644 --- a/src/panels/contextMenu.tsx +++ b/src/panels/contextMenu.tsx @@ -61,7 +61,7 @@ export const contextMenuElementIds = { /** * Represents a button in the context menu. It can't be clicked triggering given action. */ -export type contextMenuButton = { +export interface IContextMenuButton { id: string label: string keyboard: string @@ -72,18 +72,19 @@ export type contextMenuButton = { /** * Represents a divider in the context menu. It can't be clicked. */ -export type contextMenuDivider = { +export interface IContextMenuDivider { id: string enabled: boolean } -export type contextMenuElement = contextMenuButton | contextMenuDivider + +export type ContextMenuElement = IContextMenuButton | IContextMenuDivider /** * A map function that changes the context menu. */ -export type contextMenuCustomization = ( - e: contextMenuElement[] -) => contextMenuElement[] +export type ContextMenuCustomization = ( + e: ContextMenuElement[] +) => ContextMenuElement[] /** * Memoized version of VimContextMenu. @@ -99,7 +100,7 @@ export function VimContextMenu (props: { help: HelpState isolation: Isolation selection: VIM.IObject[] - customization?: (e: contextMenuElement[]) => contextMenuElement[] + customization?: (e: ContextMenuElement[]) => ContextMenuElement[] treeRef: React.MutableRefObject }) { const viewer = props.viewer @@ -207,7 +208,7 @@ export function VimContextMenu (props: { viewer.gizmos.section.fitBox(viewer.selection.getBoundingBox()) } - const createButton = (button: contextMenuButton) => { + const createButton = (button: IContextMenuButton) => { if (!button.enabled) return null return ( ) } - const createDivider = (divider: contextMenuDivider) => { + const createDivider = (divider: IContextMenuDivider) => { return divider.enabled ? ( void + customize : (customization: ContextMenuCustomization) => void + } + +export type ControlBarRef = { + /** + * Defines a callback function to dynamically customize the control bar. + * @param customization The configuration object specifying the customization options for the control bar. + */ + customize : (customization: ControlBarCustomization) => void } /** @@ -86,6 +95,11 @@ export type VimComponentRef = { */ contextMenu : ContextMenuRef + /** + * Context menu API managing the content and behavior of the context menu. + */ + controlBar : ControlBarRef + /** * Settings API managing settings applied to the component. */ From 383d1f306e912f62db933299038aafee93b133f9 Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Mon, 5 Aug 2024 16:06:59 -0400 Subject: [PATCH 6/8] "vim-webgl-viewer": "2.0.5-dev.3" --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9dde4c4f..51e6e372 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "react-tooltip": "^4.2.21", "stats-js": "^1.0.1", "tailwindcss-scoped-preflight": "^3.2.8", - "vim-webgl-viewer": "2.0.4-dev.0" + "vim-webgl-viewer": "2.0.5-dev.3" }, "peerDependencies": { "react": "^18.2.0", From 51f82343a73e0b40ba5e0fcc40d62be47551f659 Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Mon, 5 Aug 2024 16:08:43 -0400 Subject: [PATCH 7/8] removed unused import --- src/vimComponentRef.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vimComponentRef.ts b/src/vimComponentRef.ts index 63aa9d3f..5f348a53 100644 --- a/src/vimComponentRef.ts +++ b/src/vimComponentRef.ts @@ -10,7 +10,7 @@ import { Isolation } from './helpers/isolation' import { ComponentCamera } from './helpers/camera' import { VimComponentContainer } from './container' import { BimInfoPanelRef } from './bim/bimInfoData' -import { ControlBarCustomization, elementIds } from './controlbar/controlBar' +import { ControlBarCustomization } from './controlbar/controlBar' /** * Settings API managing settings applied to the component. From 1acd1fc9fec39783c6ced2f6a51e518635173cfb Mon Sep 17 00:00:00 2001 From: vim-sroberge Date: Mon, 5 Aug 2024 16:10:19 -0400 Subject: [PATCH 8/8] "version": "0.3.11-dev.0", --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 51e6e372..cdd9900a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vim-webgl-component", - "version": "0.3.10", + "version": "0.3.11-dev.0", "description": "A demonstration app built on top of the vim-webgl-viewer", "files": [ "dist"