fix WASD on 2D canvas

(on 3D still partially broken)
This commit is contained in:
HF 2024-01-21 13:47:28 +01:00
parent c6b22cf415
commit e0904f6ebe
2 changed files with 16 additions and 53 deletions

View File

@ -11,10 +11,6 @@ import {
unsetHover, unsetHover,
setScale, setScale,
selectColor, selectColor,
moveNorth,
moveWest,
moveSouth,
moveEast,
} from '../store/actions'; } from '../store/actions';
import pixelTransferController from '../ui/PixelTransferController'; import pixelTransferController from '../ui/PixelTransferController';
import { import {
@ -363,17 +359,18 @@ class PixelPainterControls {
} }
} }
zoomIn(origin) { zoom(direction, origin) {
const [x, y, scale] = this.renderer.view; const [x, y, scale] = this.renderer.view;
const deltaScale = scale >= 1.0 ? 1.1 : 1.04; const deltaScale = scale >= 1.0 ? 1.1 : 1.04;
this.renderer.updateView([x, y, scale * deltaScale], origin); const newScale = (direction > 0) ? scale * deltaScale : scale / deltaScale;
this.renderer.updateView([x, y, newScale], origin);
this.renderer.storeViewInState(); this.renderer.storeViewInState();
} }
zoomOut(origin) { move(direction) {
const [x, y, scale] = this.renderer.view; const [x, y, scale] = this.renderer.view;
const deltaScale = scale >= 1.0 ? 1.1 : 1.04; const [dx, dy] = direction.map((z) => z * 100.0 / scale);
this.renderer.updateView([x, y, scale / deltaScale], origin); this.renderer.updateView([x + dx, y + dy]);
this.renderer.storeViewInState(); this.renderer.storeViewInState();
} }
@ -386,10 +383,10 @@ class PixelPainterControls {
const { hover } = store.getState().canvas; const { hover } = store.getState().canvas;
const origin = hover || null; const origin = hover || null;
if (deltaY < 0) { if (deltaY < 0) {
this.zoomIn(origin); this.zoom(1, origin);
} }
if (deltaY > 0) { if (deltaY > 0) {
this.zoomOut(origin); this.zoom(-1, origin);
} }
} }
@ -538,25 +535,25 @@ class PixelPainterControls {
switch (event.code) { switch (event.code) {
case 'ArrowUp': case 'ArrowUp':
case 'KeyW': case 'KeyW':
store.dispatch(moveNorth()); this.move([0, -1]);
return; return;
case 'ArrowLeft': case 'ArrowLeft':
case 'KeyA': case 'KeyA':
store.dispatch(moveWest()); this.move([-1, 0]);
return; return;
case 'ArrowDown': case 'ArrowDown':
case 'KeyS': case 'KeyS':
store.dispatch(moveSouth()); this.move([0, 1]);
return; return;
case 'ArrowRight': case 'ArrowRight':
case 'KeyD': case 'KeyD':
store.dispatch(moveEast()); this.move([1, 0]);
return; return;
case 'KeyE': case 'KeyE':
this.zoomIn(); this.zoom(1);
return; return;
case 'KeyQ': case 'KeyQ':
this.zoomOut(); this.zoom(-1);
return; return;
default: default:
} }
@ -566,10 +563,10 @@ class PixelPainterControls {
*/ */
switch (event.key) { switch (event.key) {
case '+': case '+':
this.zoomIn(); this.zoom(1);
return; return;
case '-': case '-':
this.zoomOut(); this.zoom(-1);
return; return;
case 'Control': case 'Control':
case 'Shift': { case 'Shift': {

View File

@ -160,40 +160,6 @@ export function setViewCoordinates(view) {
}; };
} }
export function move([dx, dy]) {
return (dispatch, getState) => {
const { view } = getState().canvas;
const [x, y] = view;
dispatch(setViewCoordinates([x + dx, y + dy]));
};
}
export function moveDirection([vx, vy]) {
return (dispatch, getState) => {
const [,, scale] = getState().canvas.view;
const speed = 100.0 / scale;
dispatch(move([speed * vx, speed * vy]));
};
}
export function moveNorth() {
return moveDirection([0, -1]);
}
export function moveWest() {
return moveDirection([-1, 0]);
}
export function moveSouth() {
return moveDirection([0, 1]);
}
export function moveEast() {
return moveDirection([1, 0]);
}
export function setScale(scale, zoompoint) { export function setScale(scale, zoompoint) {
return { return {
type: 'SET_SCALE', type: 'SET_SCALE',