Skip to content

Commit

Permalink
Dragging, Clicking, Hovered, Focused only getter (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
agargaro authored Sep 23, 2023
1 parent 4456da5 commit 87270f1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/events/DragAndDropManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class DragAndDropManager {

public startDragging(event: PointerEvent, camera: Camera): void {
const currentTarget = this._targetInstanced ?? this._target;
this._target.dragging = true;
this._target.__dragging = true;
this.isDragging = true;
this._startPosition.copy(currentTarget.position);
this.trigger("dragstart", event, this._target, false, undefined, undefined, this._startIntersection);
Expand Down Expand Up @@ -137,7 +137,7 @@ export class DragAndDropManager {

private clear(): void {
this.isDragging = false;
this._target.dragging = false;
this._target.__dragging = false;
this._target = undefined;
this._targetInstanced = undefined;
this._dataTransfer = undefined;
Expand Down
10 changes: 5 additions & 5 deletions src/events/InteractionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class InteractionManager {
this._pointerDownTarget[event.pointerId] = target;

if (this.isMainClick(event)) {
target.clicking = true;
target.__clicking = true;
}

if (!pointerDownEvent?._defaultPrevented && event.isPrimary) {
Expand Down Expand Up @@ -221,9 +221,9 @@ export class InteractionManager {
if (target !== lastHoveredTarget) {
if (event.isPrimary) {
if (lastHoveredTarget) {
lastHoveredTarget.hovered = false;
lastHoveredTarget.__hovered = false;
}
target.hovered = true;
target.__hovered = true;
}
this._lastHoveredTarget[event.pointerId] = target;
this.triggerAncestorPointer("pointerout", event, lastHoveredTarget, target);
Expand All @@ -238,7 +238,7 @@ export class InteractionManager {
const startTarget = this._pointerDownTarget[event.pointerId];

if (event.pointerType !== "mouse") {
target.hovered = false;
target.__hovered = false;
this.triggerAncestorPointer("pointerout", event, target);
this.triggerPointer("pointerleave", event, target);
}
Expand All @@ -257,7 +257,7 @@ export class InteractionManager {
}

if (startTarget && this.isMainClick(event)) {
startTarget.clicking = false;
startTarget.__clicking = false;
}

if (event.pointerId !== this._primaryIdentifier) {
Expand Down
2 changes: 1 addition & 1 deletion src/instancedMesh/InstancedMesh2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class InstancedMesh2 extends InstancedMesh {
* @param target Optional. The instance to focus on. If not provided, the focus is cleared.
*/
public focus(target?: InstancedMeshEntity): void {
if (!this.focused) return;
if (!this.__focused) return;

const focusableObj = target?.focusable ? target : undefined;
if ((!target || focusableObj?.enabled) && this._focusedInstance !== focusableObj) {
Expand Down
44 changes: 36 additions & 8 deletions src/patch/Object3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface Object3DExtPrototype {
/** @internal */ __smartRenderingPatched: boolean;
/** @internal */ __enabled: boolean;
/** @internal */ __visible: boolean;
/** @internal */ __hovered: boolean;
/** @internal */ __focused: boolean;
/** @internal */ __clicking: boolean;
/** @internal */ __dragging: boolean;
/** @internal */ __isDropTarget: boolean;
/** @internal */ __baseVisibleDescriptor: PropertyDescriptor;
/** @internal */ __onChangeBaseEuler: () => void;
Expand Down Expand Up @@ -54,13 +58,13 @@ export interface Object3DExtPrototype {
/** Indicates whether the scene needs rendering. */
needsRender: boolean;
/** Indicates if the primary pointer is over this object. */
hovered: boolean;
get hovered(): boolean;
/** Indicates if the object is currently focused. */
focused: boolean;
get focused(): boolean;
/** Indicates if the object is currently being clicked. */
clicking: boolean;
get clicking(): boolean;
/** Indicates if the object is currently being dragged. */
dragging: boolean;
get dragging(): boolean;
/** Retrieves the combined enabled state considering parent objects. */
get enabledState(): boolean;
/** Retrieves the combined visibility state considering parent objects. */
Expand Down Expand Up @@ -143,14 +147,14 @@ export interface Object3DExtPrototype {
}

Object3D.prototype.focusable = true;
Object3D.prototype.focused = false;
Object3D.prototype.clicking = false;
Object3D.prototype.dragging = false;
Object3D.prototype.draggable = false;
Object3D.prototype.hovered = false;
Object3D.prototype.interceptByRaycaster = true;
Object3D.prototype.findDropTarget = false;
Object3D.prototype.__manualDetection = false;
Object3D.prototype.__focused = false;
Object3D.prototype.__clicking = false;
Object3D.prototype.__dragging = false;
Object3D.prototype.__hovered = false;

Object3D.prototype.__visible = true;
Object.defineProperty(Object3D.prototype, "visible", {
Expand Down Expand Up @@ -219,6 +223,30 @@ Object.defineProperty(Object3D.prototype, "needsRender", {
}
});

Object.defineProperty(Object3D.prototype, "hovered", {
get: function (this: Object3D) {
return this.__hovered;
}
});

Object.defineProperty(Object3D.prototype, "focused", {
get: function (this: Object3D) {
return this.__focused;
}
});

Object.defineProperty(Object3D.prototype, "clicking", {
get: function (this: Object3D) {
return this.__clicking;
}
});

Object.defineProperty(Object3D.prototype, "dragging", {
get: function (this: Object3D) {
return this.__dragging;
}
});

Object3D.prototype.on = function <K extends keyof Events>(this: Object3D, types: K | K[], listener: (event: Events[K]) => void): (event: Events[K]) => void {
if (typeof types === "string") {
return this.__eventsDispatcher.add(types, listener);
Expand Down
4 changes: 2 additions & 2 deletions src/patch/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ Scene.prototype.focus = function (target?: Object3D): void {
this.focusedObject = focusableObj;

if (oldFocusedObj?.enabledState) {
oldFocusedObj.focused = false;
oldFocusedObj.__focused = false;
oldFocusedObj.__eventsDispatcher.dispatchDOMAncestor("blur", new FocusEventExt(focusableObj));
oldFocusedObj.__eventsDispatcher.dispatchDOM("focusout", new FocusEventExt(focusableObj));
}

if (focusableObj) {
focusableObj.focused = true
focusableObj.__focused = true
focusableObj.__eventsDispatcher.dispatchDOMAncestor("focus", new FocusEventExt(oldFocusedObj));
focusableObj.__eventsDispatcher.dispatchDOM("focusin", new FocusEventExt(oldFocusedObj));
}
Expand Down

0 comments on commit 87270f1

Please sign in to comment.