Skip to content

Commit

Permalink
bitECS: Take ownership when rotatin or scaling object
Browse files Browse the repository at this point in the history
**Problem**

With new loader enabled, objects are not rotated or scaled
even if you attempt to rotate or scale objects that you don't
have ownership. You first need to click objects to take
ownership and then you can rotate or scale.

#6199

But it is not a expected limitation. You should be able to
rotate or scale objects without taking ownership with
other operations beforehand.

**Root issue**

Rotate and Scale with Object menu don't take ownership.

**Solution**

Take ownership when starting rotate or scale.
  • Loading branch information
takahirox committed Aug 17, 2023
1 parent 170cf09 commit 101f967
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/bit-systems/object-menu.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { defineQuery, enterQuery, entityExists, exitQuery, hasComponent } from "bitecs";
import { addComponent, defineQuery, enterQuery, entityExists, exitQuery, hasComponent } from "bitecs";
import { Matrix4, Quaternion, Vector3 } from "three";
import type { HubsWorld } from "../app";
import {
EntityStateDirty,
HeldRemoteRight,
HoveredRemoteRight,
Interacted,
Networked,
ObjectMenu,
ObjectMenuTarget,
RemoteRight,
Expand All @@ -14,6 +16,7 @@ import { anyEntityWith, findAncestorWithComponent } from "../utils/bit-utils";
import { createNetworkedEntity } from "../utils/create-networked-entity";
import HubChannel from "../utils/hub-channel";
import type { EntityID } from "../utils/networking-types";
import { takeOwnership } from "../utils/take-ownership";
import { setMatrixWorld } from "../utils/three-utils";
import { deleteTheDeletableAncestor } from "./delete-entity-system";
import { createMessageDatas, isPinned } from "./networking";
Expand Down Expand Up @@ -81,6 +84,9 @@ function moveToTarget(world: HubsWorld, menu: EntityID) {
// temporary implementation that rely on the old systems.
// They should be rewritten more elegantly with bitecs.
function startRotation(world: HubsWorld, targetEid: EntityID) {
if (hasComponent(world, Networked, targetEid)) {
takeOwnership(world, targetEid);
}
const transformSystem = APP.scene!.systems["transform-selected-object"];
const physicsSystem = AFRAME.scenes[0].systems["hubs-systems"].physicsSystem;
physicsSystem.updateRigidBodyOptions(Rigidbody.bodyId[targetEid], { type: "kinematic" });
Expand All @@ -90,12 +96,18 @@ function startRotation(world: HubsWorld, targetEid: EntityID) {
});
}

function stopRotation() {
function stopRotation(world: HubsWorld, targetEid: EntityID) {
if (hasComponent(world, Networked, targetEid)) {
addComponent(world, EntityStateDirty, targetEid);
}
const transformSystem = APP.scene!.systems["transform-selected-object"];
transformSystem.stopTransform();
}

function startScaling(world: HubsWorld, targetEid: EntityID) {
if (hasComponent(world, Networked, targetEid)) {
takeOwnership(world, targetEid);
}
// TODO: Don't use any
// TODO: Remove the dependency with AFRAME
const transformSystem = (AFRAME as any).scenes[0].systems["transform-selected-object"];
Expand All @@ -107,7 +119,10 @@ function startScaling(world: HubsWorld, targetEid: EntityID) {
scalingHandler!.startScaling(world.eid2obj.get(rightCursorEid));
}

function stopScaling(world: HubsWorld) {
function stopScaling(world: HubsWorld, targetEid: EntityID) {
if (hasComponent(world, Networked, targetEid)) {
addComponent(world, EntityStateDirty, targetEid);
}
const rightCursorEid = anyEntityWith(world, RemoteRight)!;
scalingHandler!.endScaling(world.eid2obj.get(rightCursorEid));
scalingHandler = null;
Expand Down Expand Up @@ -188,10 +203,10 @@ function handleHeldEnter(world: HubsWorld, eid: EntityID, menuEid: EntityID) {
function handleHeldExit(world: HubsWorld, eid: EntityID, menuEid: EntityID) {
switch (eid) {
case ObjectMenu.rotateButtonRef[menuEid]:
stopRotation();
stopRotation(world, ObjectMenu.targetRef[menuEid]);
break;
case ObjectMenu.scaleButtonRef[menuEid]:
stopScaling(world);
stopScaling(world, ObjectMenu.targetRef[menuEid]);
break;
}
}
Expand Down

0 comments on commit 101f967

Please sign in to comment.