handle sketch and sketch item move undo/redo

This commit is contained in:
Stewart Allen 2025-06-15 18:49:45 -04:00
commit 4d8622eed6
4 changed files with 62 additions and 33 deletions

View file

@ -475,7 +475,6 @@ function space_init(data) {
space.mouse.onDrag((delta, offset, up = false) => { space.mouse.onDrag((delta, offset, up = false) => {
let { mode, modes } = api; let { mode, modes } = api;
if (delta) { if (delta) {
// if (delta.event?.shiftKey && selection.count()) {
if (selection.count()) { if (selection.count()) {
selection.drag({ delta, offset }); selection.drag({ delta, offset });
} }

View file

@ -217,6 +217,8 @@ const selection = {
broker.publish('drag_start', selected); broker.publish('drag_start', selected);
} else if (opt.end) { } else if (opt.end) {
broker.publish('drag_end', selected); broker.publish('drag_end', selected);
} else {
broker.publish('drag', opt);
} }
selected.forEach(s => s.drag(opt)); selected.forEach(s => s.drag(opt));
return selection; return selection;

View file

@ -1,10 +1,13 @@
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */ /** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
import { broker } from "../moto/broker.js"; import { broker } from "../moto/broker.js";
import { target } from "./sketch.js";
class History { class History {
#pos = -1; #pos = -1;
#stack = []; #stack = [];
#offset; // store final drag move
#target; // store sketch drag target
#tracking = false; #tracking = false;
constructor() { constructor() {
@ -14,26 +17,35 @@ class History {
this.#tracking = true this.#tracking = true
}, },
drag_start(selected) { drag_start(selected) {},
this.push('position', selected.map(model => ({
model, drag(opt) {
pos: model.position().toArray() this.#target = target();
}))); this.#offset = opt.offset;
}, },
drag_end(selected) { drag_end(selected) {
this.update(selected.map(model => ({ // console.log({
model, // drag_end: selected,
pos: model.position().toArray() // offset: this.#offset,
}))); // target: this.#target
// });
const { x, y, z } = this.#offset;
broker.publish('move', {
target: this.#target,
set: selected,
dx: x,
dy: y,
dz: z
});
}, },
move(data) { move(data) {
const { set, dx, dy, dz } = data; const { target, set, dx, dy, dz } = data;
this.push('move', { this.push('move', {
set, dx: -dx, dy: -dy, dz: -dz set, dx: -dx, dy: -dy, dz: -dz, target
}, { }, {
set, dx, dy, dz set, dx, dy, dz, target
}); });
}, },
@ -67,32 +79,32 @@ class History {
}); });
} }
update(redo, undo) { // update(redo, undo) {
let peek = this.#stack.peek(); // let peek = this.#stack.peek();
if (peek) { // if (peek) {
Object.assign(peek, { // Object.assign(peek, {
redo: redo || peek.redo, // redo: redo || peek.redo,
undo: undo || peek.undo // undo: undo || peek.undo
}); // });
} // }
} // }
do(type, data) { do(type, data) {
console.log({ type, data }); // console.log({ type, data });
switch (type) { switch (type) {
case 'move': { case 'move': {
let { set, dx, dy, dz } = data; let { set, dx, dy, dz, target } = data;
for (let grp of set) { for (let grp of set) {
grp.move(dx, dy, dz); grp.move(dx, dy, dz, target);
}
break;
}
case 'position': {
for (let { model, pos } of data) {
model.position(...pos);
} }
break; break;
} }
// case 'position': {
// for (let { model, pos } of data) {
// model.position(...pos);
// }
// break;
// }
case 'rotate': { case 'rotate': {
let { set, dx, dy, dz } = data; let { set, dx, dy, dz } = data;
for (let grp of set) { for (let grp of set) {

View file

@ -395,8 +395,20 @@ class MeshSketch extends meshObject {
return bool; return bool;
} }
move(x, y, z = 0) { position() {
const { target } = drag; const { target } = drag;
console.log({ sk_pos: [ ...arguments ], target });
const [ nx, ny, nz ] = [ ...arguments ];
if (nx || ny || nz) {
this.center = { x: nx, y: ny, z: nz };
this.render();
} else {
const { x, y, z } = this.center;
return new Vector3(x, y, z);
}
}
move(x, y, z = 0, target = drag.target) {
const { center, scale, plane, handles } = this; const { center, scale, plane, handles } = this;
const handle = handles.indexOf(target); const handle = handles.indexOf(target);
if (target === plane) { if (target === plane) {
@ -636,4 +648,8 @@ class SketchItem {
} }
} }
export { MeshSketch as sketch, SketchItem }; function target() {
return drag.target;
}
export { MeshSketch as sketch, SketchItem, target };