Skip to content

Commit 87b5c8e

Browse files
XR input sources interact with Elements (playcanvas#1988)
* XR input sources interact with Elements * update input source ray * add new input source property - which element it hovering Co-authored-by: Will Eastcott <will@playcanvas.com>
1 parent 0a73500 commit 87b5c8e

File tree

6 files changed

+380
-51
lines changed

6 files changed

+380
-51
lines changed

src/framework/application.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ Object.assign(pc, function () {
566566
this.vr = null;
567567
this.xr = new pc.XrManager(this);
568568

569+
if (this.elementInput)
570+
this.elementInput.attachSelectEvents();
571+
569572
this._inTools = false;
570573

571574
this._skyboxLast = 0;

src/framework/components/button/component.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Object.assign(pc, function () {
5353

5454
this._visualState = VisualState.DEFAULT;
5555
this._isHovering = false;
56+
this._hoveringCounter = 0;
5657
this._isPressed = false;
5758

5859
this._defaultTint = new pc.Color(1, 1, 1, 1);
@@ -150,6 +151,10 @@ Object.assign(pc, function () {
150151
this.entity.element[onOrOff]('touchend', this._onTouchEnd, this);
151152
this.entity.element[onOrOff]('touchleave', this._onTouchLeave, this);
152153
this.entity.element[onOrOff]('touchcancel', this._onTouchCancel, this);
154+
this.entity.element[onOrOff]('selectstart', this._onSelectStart, this);
155+
this.entity.element[onOrOff]('selectend', this._onSelectEnd, this);
156+
this.entity.element[onOrOff]('selectenter', this._onSelectEnter, this);
157+
this.entity.element[onOrOff]('selectleave', this._onSelectLeave, this);
153158
this.entity.element[onOrOff]('click', this._onClick, this);
154159

155160
this._hasHitElementListeners = isAdding;
@@ -278,6 +283,41 @@ Object.assign(pc, function () {
278283
this._fireIfActive('touchcancel', event);
279284
},
280285

286+
_onSelectStart: function (event) {
287+
this._isPressed = true;
288+
this._updateVisualState();
289+
this._fireIfActive('selectstart', event);
290+
},
291+
292+
_onSelectEnd: function (event) {
293+
this._isPressed = false;
294+
this._updateVisualState();
295+
this._fireIfActive('selectend', event);
296+
},
297+
298+
_onSelectEnter: function (event) {
299+
this._hoveringCounter++;
300+
301+
if (this._hoveringCounter === 1) {
302+
this._isHovering = true;
303+
this._updateVisualState();
304+
}
305+
306+
this._fireIfActive('selectenter', event);
307+
},
308+
309+
_onSelectLeave: function (event) {
310+
this._hoveringCounter--;
311+
312+
if (this._hoveringCounter === 0) {
313+
this._isHovering = false;
314+
this._isPressed = false;
315+
this._updateVisualState();
316+
}
317+
318+
this._fireIfActive('selectleave', event);
319+
},
320+
281321
_onClick: function (event) {
282322
this._fireIfActive('click', event);
283323
},
@@ -524,6 +564,34 @@ Object.assign(pc, function () {
524564
* @param {pc.ElementTouchEvent} event - The event.
525565
*/
526566

567+
/**
568+
* @event
569+
* @name pc.ButtonComponent#selectstart
570+
* @description Fired when a xr select starts on the component.
571+
* @param {pc.ElementSelectEvent} event - The event.
572+
*/
573+
574+
/**
575+
* @event
576+
* @name pc.ButtonComponent#selectend
577+
* @description Fired when a xr select ends on the component.
578+
* @param {pc.ElementSelectEvent} event - The event.
579+
*/
580+
581+
/**
582+
* @event
583+
* @name pc.ButtonComponent#selectenter
584+
* @description Fired when a xr select now hovering over the component.
585+
* @param {pc.ElementSelectEvent} event - The event.
586+
*/
587+
588+
/**
589+
* @event
590+
* @name pc.ButtonComponent#selectleave
591+
* @description Fired when a xr select not hovering over the component.
592+
* @param {pc.ElementSelectEvent} event - The event.
593+
*/
594+
527595
/**
528596
* @event
529597
* @name pc.ButtonComponent#hoverstart

0 commit comments

Comments
 (0)