-
Notifications
You must be signed in to change notification settings - Fork 12
Add xr-active-switch.ts #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NSTCG
wants to merge
1
commit into
WonderlandEngine:main
Choose a base branch
from
NSTCG:add-xr-mode-switch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import {Component, Object3D} from '@wonderlandengine/api'; | ||
| import {property} from '@wonderlandengine/api/decorators.js'; | ||
|
|
||
| /** | ||
| * Scopes for selecting which components to affect. | ||
| */ | ||
| export enum Scope { | ||
| ThisObject, | ||
| Children, | ||
| ThisObjectAndChildren, | ||
| } | ||
|
|
||
| // Mutable array of names for the enum UI | ||
| export const ScopeNames: string[] = [ | ||
| 'this object', | ||
| 'children only', | ||
| 'this object & children', | ||
| ]; | ||
|
|
||
| /** | ||
| * Actions to perform when entering/exiting XR. | ||
| */ | ||
| export enum Action { | ||
| None, | ||
| Activate, | ||
| Deactivate, | ||
| Toggle, | ||
| } | ||
|
|
||
| // Mutable array of names for the enum UI | ||
| export const ActionNames: string[] = ['none', 'activate', 'deactivate', 'toggle']; | ||
|
|
||
| /** | ||
| * XR Active Switch Component for controlling component activation states | ||
| * based on XR session status. | ||
| * | ||
| * This component allows toggling, activating, deactivating, or keeping the | ||
| * current state of components when entering or exiting XR sessions. | ||
| * | ||
| * The scope of affected components can be limited to the current object, | ||
| * its children, or both. | ||
| * | ||
| * Use this component to manage visibility, interactivity, or other properties | ||
| * that depend on XR session states. | ||
| * | ||
| */ | ||
| export class XrActiveSwitch extends Component { | ||
| static TypeName = 'xr-active-switch'; | ||
|
|
||
| /** Action to perform when XR session starts */ | ||
| @property.enum(ActionNames) | ||
| ifInXR!: Action; | ||
|
|
||
| /** Action to perform when XR session ends */ | ||
| @property.enum(ActionNames) | ||
| ifNotInXR!: Action; | ||
|
|
||
| /** Scope of elements to affect */ | ||
| @property.enum(ScopeNames) | ||
| scope!: Scope; | ||
|
|
||
| private components: Component[] = []; | ||
|
|
||
| start() { | ||
| // Initial state setup | ||
| this.applyAction(this.engine.xr?.session ? this.ifInXR : this.ifNotInXR); | ||
|
|
||
| // Bind event handlers | ||
| this.engine.onXRSessionStart.add(this.onSessionStart); | ||
| this.engine.onXRSessionEnd.add(this.onSessionEnd); | ||
| } | ||
|
|
||
| onActivate() { | ||
| this.engine.onXRSessionStart.add(this.onSessionStart); | ||
| this.engine.onXRSessionEnd.add(this.onSessionEnd); | ||
| } | ||
|
|
||
| onDeactivate() { | ||
| this.engine.onXRSessionStart.remove(this.onSessionStart); | ||
| this.engine.onXRSessionEnd.remove(this.onSessionEnd); | ||
| } | ||
|
|
||
| private collectComponents() { | ||
| this.components = []; | ||
| const mode = this.scope; | ||
|
|
||
| if (mode === Scope.ThisObject || mode === Scope.ThisObjectAndChildren) { | ||
| this.object | ||
| .getComponents() | ||
| .filter((c) => c.type !== XrActiveSwitch.TypeName) | ||
| .forEach((c) => this.components.push(c)); | ||
| } | ||
|
|
||
| if (mode === Scope.Children || mode === Scope.ThisObjectAndChildren) { | ||
| this.processChildren(this.object); | ||
| } | ||
| } | ||
|
|
||
| private processChildren(obj: Object3D) { | ||
| for (const child of obj.children) { | ||
| child | ||
| .getComponents() | ||
| .filter((c) => c.type !== XrActiveSwitch.TypeName) | ||
| .forEach((c) => this.components.push(c)); | ||
| this.processChildren(child); // Recurse through all descendants | ||
| } | ||
| } | ||
|
|
||
| private applyAction(action: Action) { | ||
| this.collectComponents(); | ||
| for (const comp of this.components) { | ||
| switch (action) { | ||
| case Action.Activate: | ||
| comp.active = true; | ||
| break; | ||
| case Action.Deactivate: | ||
| comp.active = false; | ||
| break; | ||
| case Action.Toggle: | ||
| comp.active = !comp.active; | ||
| break; | ||
| case Action.None: | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private onSessionStart = () => this.applyAction(this.ifInXR); | ||
| private onSessionEnd = () => this.applyAction(this.ifNotInXR); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.