Skip to content

Commit

Permalink
Implemented undo/redo for mesh generation / edit mode. (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
seagetch authored Feb 11, 2023
1 parent 2ae9b87 commit 9e28c3c
Show file tree
Hide file tree
Showing 13 changed files with 652 additions and 30 deletions.
500 changes: 500 additions & 0 deletions source/creator/actions/mesh.d

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions source/creator/actions/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public import creator.actions.parameter;
public import creator.actions.binding;
public import creator.actions.mesheditor;
public import creator.actions.drawable;
public import creator.actions.mesh;

/**
An undo/redo-able action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- Luna Nielsen
- Asahi Lina
*/
module creator.viewport.common.mesheditor.base;
module creator.viewport.common.mesheditor.operations.base;

import creator.viewport.common.mesheditor.tools.base;
import i18n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import i18n;
import creator.viewport;
import creator.viewport.common;
import creator.viewport.common.mesh;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.impl;
import creator.viewport.common.mesheditor.operations;
import creator.viewport.common.spline;
import creator.core.input;
import creator.core.actionstack;
Expand Down Expand Up @@ -170,7 +169,7 @@ public:
mesh = previewMesh;
previewMesh = null;
previewTriangulate = false;
}
}

override
void pushDeformAction() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module creator.viewport.common.mesheditor.impl;
module creator.viewport.common.mesheditor.operations.impl;

import creator.viewport.common.mesheditor.tools;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.operations.base;
import i18n;
import creator.viewport;
import creator.viewport.common;
Expand Down
3 changes: 1 addition & 2 deletions source/creator/viewport/common/mesheditor/operations/node.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import i18n;
import creator.viewport;
import creator.viewport.common;
import creator.viewport.common.mesh;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.impl;
import creator.viewport.common.mesheditor.operations;
import creator.viewport.common.spline;
import creator.core.input;
import creator.core.actionstack;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module creator.viewport.common.mesheditor.operations;

public import creator.viewport.common.mesheditor.operations.base;
public import creator.viewport.common.mesheditor.operations.impl;
public import creator.viewport.common.mesheditor.operations.drawable;
public import creator.viewport.common.mesheditor.operations.node;
4 changes: 1 addition & 3 deletions source/creator/viewport/common/mesheditor/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ module creator.viewport.common.mesheditor;
*/
import i18n;
import creator.viewport;
public import creator.viewport.common.mesheditor.base;
public import creator.viewport.common.mesheditor.operations.drawable;
public import creator.viewport.common.mesheditor.operations.node;
public import creator.viewport.common.mesheditor.operations;
import creator.viewport.common;
import creator.viewport.common.mesh;
import creator.viewport.common.spline;
Expand Down
2 changes: 1 addition & 1 deletion source/creator/viewport/common/mesheditor/tools/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import i18n;
import creator.viewport;
import creator.viewport.common;
import creator.viewport.common.mesh;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.operations;
import creator.viewport.common.spline;
import creator.core.input;
import creator.core.actionstack;
Expand Down
23 changes: 17 additions & 6 deletions source/creator/viewport/common/mesheditor/tools/connect.d
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module creator.viewport.common.mesheditor.tools.connect;

import creator.viewport.common.mesheditor.tools.select;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.operations;
import i18n;
import creator.viewport;
import creator.viewport.common;
import creator.viewport.common.mesh;
import creator.viewport.common.spline;
import creator.core.input;
import creator.core.actionstack;
import creator.actions;
Expand All @@ -17,8 +16,6 @@ import inochi2d;
import inochi2d.core.dbg;
import bindbc.opengl;
import bindbc.imgui;
import std.algorithm.mutation;
import std.algorithm.searching;
import std.stdio;

class ConnectTool : NodeSelect {
Expand All @@ -36,21 +33,35 @@ class ConnectTool : NodeSelect {
auto prev = impl.selectOne(impl.vtxAtMouse);
if (prev !is null) {
if (prev != impl.selected[$-1]) {
auto implDrawable = cast(IncMeshEditorOneDrawable)(impl);
auto mesh = implDrawable.getMesh();

// Connect or disconnect between previous and this node
if (!prev.isConnectedTo(impl.selected[$-1])) {
auto action = new MeshConnectAction(impl.getTarget().name, impl, mesh);
impl.foreachMirror((uint axis) {
MeshVertex *mPrev = impl.mirrorVertex(axis, prev);
MeshVertex *mSel = impl.mirrorVertex(axis, impl.selected[$-1]);
if (mPrev !is null && mSel !is null) mPrev.connect(mSel);
if (mPrev !is null && mSel !is null) {
action.connect(mPrev, mSel);
// mPrev.connect(mSel);
}
});
action.updateNewState();
incActionPush(action);
changed = true;
} else {
auto action = new MeshDisconnectAction(impl.getTarget().name, impl, mesh);
impl.foreachMirror((uint axis) {
MeshVertex *mPrev = impl.mirrorVertex(axis, prev);
MeshVertex *mSel = impl.mirrorVertex(axis, impl.selected[$-1]);
if (mPrev !is null && mSel !is null) mPrev.disconnect(mSel);
if (mPrev !is null && mSel !is null) {
action.disconnect(mPrev, mSel);
// mPrev.disconnect(mSel);
}
});
action.updateNewState();
incActionPush(action);
changed = true;
}
if (!io.KeyShift) impl.deselectAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module creator.viewport.common.mesheditor.tools.pathdeform;
import creator.viewport.common.mesheditor.tools.select;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.operations;
import i18n;
import creator.viewport;
import creator.viewport.common;
Expand Down
128 changes: 118 additions & 10 deletions source/creator/viewport/common/mesheditor/tools/point.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module creator.viewport.common.mesheditor.tools.point;

import creator.viewport.common.mesheditor.tools.select;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.operations;
import i18n;
import creator.viewport;
import creator.viewport.common;
Expand All @@ -22,35 +22,138 @@ import std.algorithm.searching;
import std.stdio;

class PointTool : NodeSelect {
Action action;
void resetAction() {
}
void pushAction() {
}

override bool onDragStart(vec2 mousePos, IncMeshEditorOne impl) {
if (!impl.deformOnly && !impl.isSelecting && !isDragging) {
auto implDrawable = cast(IncMeshEditorOneDrawable)impl;
auto mesh = implDrawable.getMesh();

isDragging = true;
action = new MeshMoveAction(impl.getTarget().name, impl, mesh);
return true;
}
return super.onDragStart(mousePos, impl);
}

override bool onDragEnd(vec2 mousePos, IncMeshEditorOne impl) {
if (action !is null) {
if (auto meshAction = cast(MeshAction)(action)) {
if (meshAction.dirty) {
meshAction.updateNewState();
incActionPush(action);
}
}
action = null;
}
return super.onDragEnd(mousePos, impl);
}

override bool onDragUpdate(vec2 mousePos, IncMeshEditorOne impl) {
if (!impl.deformOnly) {
if (isDragging) {
if (auto meshAction = cast(MeshMoveAction)action) {
foreach(select; impl.selected) {
impl.foreachMirror((uint axis) {
MeshVertex *v = impl.mirrorVertex(axis, select);
if (v is null) return;
meshAction.moveVertex(v, v.position + impl.mirror(axis, mousePos - impl.lastMousePos));
});
}
}

if (impl.selected.length > 0)
impl.maybeSelectOne = null;
impl.refreshMesh();
return true;
}
return false;
} else
return super.onDragUpdate(mousePos, impl);
}

bool updateMeshEdit(ImGuiIO* io, IncMeshEditorOne impl, out bool changed) {
incStatusTooltip(_("Select"), _("Left Mouse"));
incStatusTooltip(_("Create"), _("Ctrl+Left Mouse"));

auto implDrawable = cast(IncMeshEditorOneDrawable)impl;
auto mesh = implDrawable.getMesh();

assert(implDrawable !is null);

void addOrRemoveVertex(bool selectedOnly) {
// Check if mouse is over a vertex
if (impl.vtxAtMouse !is null) {
changed = impl.removeVertex(io, selectedOnly);
auto action = new MeshRemoveAction(impl.getTarget().name, impl, mesh);

if (!selectedOnly || impl.isSelected(impl.vtxAtMouse)) {
impl.foreachMirror((uint axis) {
MeshVertex* vertex = mesh.getVertexFromPoint(impl.mirror(axis, impl.mousePos));
action.removeVertex(vertex);
});
impl.refreshMesh();
impl.vertexMapDirty = true;
impl.selected.length = 0;
impl.updateMirrorSelected();
impl.maybeSelectOne = null;
impl.vtxAtMouse = null;
changed = true;
}

action.updateNewState();
incActionPush(action);
} else {
changed = impl.addVertex(io);
auto action = new MeshAddAction(impl.getTarget().name, impl, mesh);

ulong off = mesh.vertices.length;
if (impl.isOnMirror(impl.mousePos, impl.meshEditAOE)) {
impl.placeOnMirror(impl.mousePos, impl.meshEditAOE);
} else {
impl.foreachMirror((uint axis) {
MeshVertex* vertex = new MeshVertex(impl.mirror(axis, impl.mousePos));
action.addVertex(vertex);
});
}
impl.refreshMesh();
impl.vertexMapDirty = true;
if (io.KeyCtrl) impl.selectOne(mesh.vertices[$-1]);
else impl.selectOne(mesh.vertices[off]);
changed = true;

action.updateNewState();
incActionPush(action);
}
}

//FROM:-------------should be updateDeformEdit --------------------
// Key actions
if (incInputIsKeyPressed(ImGuiKey.Delete)) {
auto action = new MeshRemoveAction(impl.getTarget().name, impl, mesh);

impl.foreachMirror((uint axis) {
foreach(v; impl.selected) {
MeshVertex *v2 = impl.mirrorVertex(axis, v);
if (v2 !is null) impl.removeMeshVertex(v2);
if (v2 !is null) {
action.removeVertex(v2);
}
}
});

action.updateNewState();
incActionPush(action);

impl.selected = [];
impl.updateMirrorSelected();
impl.refreshMesh();
impl.vertexMapDirty = true;
changed = true;
}

MeshMoveAction moveAction = null;
void shiftSelection(vec2 delta) {
float magnitude = 10.0;
if (io.KeyAlt) magnitude = 1.0;
Expand All @@ -61,7 +164,12 @@ class PointTool : NodeSelect {
vec2 mDelta = impl.mirrorDelta(axis, delta);
foreach(v; impl.selected) {
MeshVertex *v2 = impl.mirrorVertex(axis, v);
if (v2 !is null) v2.position += mDelta;
if (v2 !is null) {
if (moveAction is null) {
moveAction = new MeshMoveAction(implDrawable.getTarget().name, impl, mesh);
moveAction.moveVertex(v2, v2.position + mDelta);
}
}
}
});
impl.refreshMesh();
Expand All @@ -77,6 +185,11 @@ class PointTool : NodeSelect {
} else if (incInputIsKeyPressed(ImGuiKey.UpArrow)) {
shiftSelection(vec2(0, -1));
}

if (moveAction !is null) {
moveAction.updateNewState();
incActionPush(moveAction);
}
//TO:-------------should be updateDeformEdit --------------------

// Left click selection
Expand All @@ -86,7 +199,6 @@ class PointTool : NodeSelect {
addOrRemoveVertex(false);
} else {
//FROM:-------------should be updateDeformEdit --------------------
Action action;
// Select / drag start
// action = getCleanDeformAction();

Expand Down Expand Up @@ -153,10 +265,6 @@ class PointTool : NodeSelect {

// Left click selection
if (igIsMouseClicked(ImGuiMouseButton.Left)) {
Action action;
// Select / drag start
action = impl.getCleanDeformAction();

if (impl.isPointOver(impl.mousePos)) {
if (io.KeyShift) impl.toggleSelect(impl.vtxAtMouse);
else if (!impl.isSelected(impl.vtxAtMouse)) impl.selectOne(impl.vtxAtMouse);
Expand Down
2 changes: 1 addition & 1 deletion source/creator/viewport/common/mesheditor/tools/select.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module creator.viewport.common.mesheditor.tools.select;

import creator.viewport.common.mesheditor.tools.base;
import creator.viewport.common.mesheditor.base;
import creator.viewport.common.mesheditor.operations;
import i18n;
import creator.viewport;
import creator.viewport.common;
Expand Down

0 comments on commit 9e28c3c

Please sign in to comment.