Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export * from './vrm.js';
export * from './wasd-controls.js';
export * from './input-profile.js';
export * from './orbital-camera.js';
export * from './xr-active-switch.js';

export * from '@wonderlandengine/spatial-audio';
106 changes: 106 additions & 0 deletions xr-active-switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {Component, Object3D} from '@wonderlandengine/api';
import {property} from '@wonderlandengine/api/decorators.js';

const SCOPE_OPTIONS = ['this object', 'children only', 'this object & children'];
const ACTION_OPTIONS = ['activate', 'deactivate', 'toggle', 'keep'];

/**
* 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.
*
*/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous newline here

export class XrActiveSwitch extends Component {
static TypeName = 'xr-active-switch';

/** Action to perform when XR session starts */
@property.enum(ACTION_OPTIONS)
ifInXR: number = 0;

/** Action to perform when XR session ends */
@property.enum(ACTION_OPTIONS)
ifNotInXR: number = 1;

/** Scope of elements to affect */
@property.enum(SCOPE_OPTIONS)
scope: number = 2;

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() {
const mode = this.scope;

// Handle current object
if (mode === 0 || mode === 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe either add comments like 0 /* this object */, or an enum, such that this reads mode == Scope.ThisObject

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That enum can also be exported and this.scope can be typed to it (I think), such that people can easily add them at runtime and just this.object.addComponent('xr-active-switch', {scope: Scope.ThisObject, ifInXR: Action.Activate});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, there it may make sense to make the defaults Action.Keep, since the most sensible default is that leaving out an explicit configuration just does nothing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe we call it Action.None? That's a bit clearer than Keep, also for the enum value 'none'

this.object
.getComponents()
.filter((c) => c.type !== XrActiveSwitch.TypeName)
.forEach((c) => this.components.push(c));
}

// Handle children
if (mode === 1 || mode === 2) {
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: number) {
this.components = [];
this.collectComponents();
for (const comp of this.components) {
if (action === 0) {
comp.active = true;
} else if (action === 1) {
comp.active = false;
} else if (action === 2) {
comp.active = !comp.active;
}
}
}

private onSessionStart = () => {
this.applyAction(this.ifInXR);
};

private onSessionEnd = () => {
this.applyAction(this.ifNotInXR);
};
}