Skip to content

Commit

Permalink
Merge pull request #1730 from xeokit/feature/custom-movement-cursors
Browse files Browse the repository at this point in the history
Implement custom cursors for camera movement
  • Loading branch information
xeolabs authored Nov 11, 2024
2 parents d9541dc + 9933a51 commit a6b3721
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
38 changes: 38 additions & 0 deletions src/viewer/scene/CameraControl/CameraControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,13 @@ class CameraControl extends Component {
new KeyboardPanRotateDollyHandler(this.scene, this._controllers, this._configs, this._states, this._updates)
];

this._cursors = {
dollyForward: "zoom-in",
dollyBackward: "zoom-out",
rotate: 'grabbing',
pan: 'move',
}

// Applies scheduled updates to the Camera on each Scene "tick" event

this._cameraUpdater = new CameraUpdater(this.scene, this._controllers, this._configs, this._states, this._updates);
Expand Down Expand Up @@ -1181,6 +1188,37 @@ class CameraControl extends Component {
this._configs.pointerEnabled = !!value;
}

/**
* Sets the cursor to be used when a particular action is being performed.
*
* Accepted actions are:
*
* * "dollyForward" - when the camera is dollying in the forward direction
* * "dollyBackward" - when the camera is dollying in the backward direction
* * "pan" - when the camera is being panned
* * "rotate" - when the camera is being rotated
*
* @param {String} action
* @param {String} style
*/
setCursorStyle(action, style) {
if (Object.prototype.hasOwnProperty.call(this._cursors, action)) {
this._cursors = { ...this._cursors, [action]: style };
}
else
console.warn(`Action '${action}' is not valid for cursor styles.`);
}

/**
* Gets the current style for a particular action.
*
* @param {String} action To get the style for
* @returns {String} style set on the cursor for action
*/
getCursorStyle(action) {
return this._cursors[action] || null;
}

_reset() {
for (let i = 0, len = this._handlers.length; i < len; i++) {
const handler = this._handlers[i];
Expand Down
9 changes: 5 additions & 4 deletions src/viewer/scene/CameraControl/lib/CameraUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CameraUpdater {
const pickController = controllers.pickController;
const pivotController = controllers.pivotController;
const panController = controllers.panController;
const cameraControl = controllers.cameraControl;

let countDown = SCALE_DOLLY_EACH_FRAME; // Decrements on each tick
let dollyDistFactor = 1.0; // Calculated when countDown is zero
Expand Down Expand Up @@ -142,7 +143,7 @@ class CameraUpdater {
updates.rotateDeltaX *= configs.rotationInertia;
updates.rotateDeltaY *= configs.rotationInertia;

cursorType = "grabbing";
cursorType = cameraControl._cursors.rotate;
}

//----------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -208,7 +209,7 @@ class CameraUpdater {
camera.pan(vec);
}

cursorType = "grabbing";
cursorType = cameraControl._cursors.pan;
}

updates.panDeltaX *= configs.panInertia;
Expand All @@ -222,9 +223,9 @@ class CameraUpdater {
if (dollyDeltaForDist !== 0) {

if (dollyDeltaForDist < 0) {
cursorType = "zoom-in";
cursorType = cameraControl._cursors.dollyForward;
} else {
cursorType = "zoom-out";
cursorType = cameraControl._cursors.dollyBackward;
}

if (configs.firstPerson) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ class KeyboardPanRotateDollyHandler {
return;
}
keyDownMap[keyCode] = true;

if (keyCode === input.KEY_SHIFT) {
canvas.style.cursor = "move";
}
});

this._onSceneKeyUp = input.on("keyup", (keyCode) => {
Expand All @@ -38,10 +34,6 @@ class KeyboardPanRotateDollyHandler {
}
keyDownMap[keyCode] = false;

if (keyCode === input.KEY_SHIFT) {
canvas.style.cursor = null;
}

if (controllers.pivotController.getPivoting()) {
controllers.pivotController.endPivot()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class MousePanRotateDollyHandler {
});

function setMousedownState(pick = true) {
canvas.style.cursor = "move";
setMousedownPositions();
if (pick) {
setMousedownPick();
Expand Down
23 changes: 23 additions & 0 deletions types/viewer/scene/CameraControl/CameraControl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,29 @@ export declare class CameraControl extends Component {
*/
get pointerEnabled(): boolean;

/**
* Sets the cursor to be used when a particular action is being performed.
*
* Accepted actions are:
*
* * "dollyForward" - when the camera is dollying in the forward direction
* * "dollyBackward" - when the camera is dollying in the backward direction
* * "pan" - when the camera is being panned
* * "rotate" - when the camera is being rotated
*
* @param {String} action
* @param {String} style
*/
setCursorStyle(action: string, style: string): void;

/**
* Gets the current style for a particular action.
*
* @param {String} action To get the style for
* @returns {String} style set on the cursor for action
*/
getCursorStyle(action: string): string | null;

/**
* Sets how much the {@link Camera} dollys each second with keyboard input.
*
Expand Down

0 comments on commit a6b3721

Please sign in to comment.