2025-11-21 02:36:50 +01:00
|
|
|
import { BaseTool } from './base-tool';
|
|
|
|
|
import type { PointerState } from '@/types';
|
|
|
|
|
import { useLayerStore } from '@/store/layer-store';
|
2025-11-21 09:19:09 +01:00
|
|
|
import { useHistoryStore } from '@/store/history-store';
|
|
|
|
|
import { MoveCommand } from '@/core/commands';
|
2025-11-21 02:36:50 +01:00
|
|
|
|
|
|
|
|
export class MoveTool extends BaseTool {
|
|
|
|
|
private startX = 0;
|
|
|
|
|
private startY = 0;
|
|
|
|
|
private layerStartX = 0;
|
|
|
|
|
private layerStartY = 0;
|
2025-11-21 09:19:09 +01:00
|
|
|
private moveCommand: MoveCommand | null = null;
|
2025-11-21 02:36:50 +01:00
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super('Move');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onPointerDown(pointer: PointerState): void {
|
|
|
|
|
this.isActive = true;
|
|
|
|
|
this.isDrawing = true;
|
|
|
|
|
|
|
|
|
|
const layer = this.getActiveLayer();
|
|
|
|
|
if (!layer) return;
|
|
|
|
|
|
|
|
|
|
this.startX = pointer.x;
|
|
|
|
|
this.startY = pointer.y;
|
|
|
|
|
this.layerStartX = layer.x;
|
|
|
|
|
this.layerStartY = layer.y;
|
2025-11-21 09:19:09 +01:00
|
|
|
|
|
|
|
|
// Create move command with initial position
|
|
|
|
|
this.moveCommand = new MoveCommand(layer.id, { x: layer.x, y: layer.y });
|
2025-11-21 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onPointerMove(pointer: PointerState): void {
|
|
|
|
|
if (!this.isDrawing) return;
|
|
|
|
|
|
|
|
|
|
const layer = this.getActiveLayer();
|
|
|
|
|
if (!layer) return;
|
|
|
|
|
|
|
|
|
|
const dx = pointer.x - this.startX;
|
|
|
|
|
const dy = pointer.y - this.startY;
|
|
|
|
|
|
2025-11-21 09:19:09 +01:00
|
|
|
// Update position in real-time (for visual feedback)
|
2025-11-21 02:36:50 +01:00
|
|
|
const { updateLayer } = useLayerStore.getState();
|
|
|
|
|
updateLayer(layer.id, {
|
|
|
|
|
x: this.layerStartX + dx,
|
|
|
|
|
y: this.layerStartY + dy,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onPointerUp(): void {
|
2025-11-21 09:19:09 +01:00
|
|
|
if (this.moveCommand) {
|
|
|
|
|
const layer = this.getActiveLayer();
|
|
|
|
|
if (layer) {
|
|
|
|
|
// Capture final position
|
|
|
|
|
this.moveCommand.captureAfterPosition(layer.x, layer.y);
|
|
|
|
|
|
|
|
|
|
// Only add to history if position actually changed
|
|
|
|
|
if (this.moveCommand.hasChanged()) {
|
|
|
|
|
const { executeCommand } = useHistoryStore.getState();
|
|
|
|
|
executeCommand(this.moveCommand);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.moveCommand = null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-21 02:36:50 +01:00
|
|
|
this.isDrawing = false;
|
|
|
|
|
this.isActive = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCursor(): string {
|
|
|
|
|
return 'move';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getActiveLayer() {
|
|
|
|
|
const { activeLayerId, layers } = useLayerStore.getState();
|
|
|
|
|
return layers.find((l) => l.id === activeLayerId);
|
|
|
|
|
}
|
|
|
|
|
}
|