Skip to content

Commit

Permalink
autoConnect remembers last setting and clean code (#431)
Browse files Browse the repository at this point in the history
* IncMeshEditorOne: clean up unused setters

* Make autoConnect disabled color consistent with "Mirror Vertical"

* Use static autoConnect variable to retain state and reduce repeated clicks within a single Inochi2D Creator instance.

* MeshAddAction: Tracking Vertices axis information
  • Loading branch information
r888800009 authored Nov 29, 2024
1 parent b848654 commit 457c5ab
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 21 deletions.
7 changes: 6 additions & 1 deletion source/creator/actions/mesh.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ abstract class MeshAction : LazyBoundAction {

class MeshAddAction : MeshAction {
MeshVertex*[] vertices;
MeshVertex*[][4] axisVertices;

this(string name, IncMeshEditorOne editor, IncMesh mesh, void delegate() update = null) {
super(name, editor, mesh, update);
}

void addVertex(MeshVertex* vertex) {
void addVertex(MeshVertex* vertex, int axis = 0) {
vertices ~= vertex;
mesh.vertices ~= vertex;
axisVertices[axis] ~= vertex;
dirty = true;
}

Expand All @@ -96,6 +98,9 @@ class MeshAddAction : MeshAction {
override
void clear() {
vertices.length = 0;
foreach (v; axisVertices) {
v.length = 0;
}
super.clear();
}

Expand Down
13 changes: 0 additions & 13 deletions source/creator/viewport/common/mesheditor/operations/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,14 @@ public:
bool mutateSelection = false;
bool invertSelection = false;
ulong maybeSelectOne;

// you should call updateVtxAtMouse() for updating vtxAtMouse
// because it also updates prevVtxAtMouse
// Note: prevVtxAtMouse for Undo may not safe to use currently
ulong vtxAtMouse;
ulong prevVtxAtMouse;

vec2 selectOrigin;
IncMesh previewMesh;

bool deforming = false;
float meshEditAOE = 4;

void updateVtxAtMouse(ulong vtxIndex) {
// we hope prevVtxAtMouse tracks the previous != -1 vtxAtMouse
if (vtxAtMouse != -1)
prevVtxAtMouse = vtxAtMouse;
vtxAtMouse = vtxIndex;
}

bool isSelected(ulong vertIndex) {
import std.algorithm.searching : canFind;
return selected.canFind(vertIndex);
Expand Down Expand Up @@ -257,7 +245,6 @@ public:

this(bool deformOnly) {
this.deformOnly = deformOnly;
prevVtxAtMouse = ulong(-1);
}

VertexToolMode getToolMode() {
Expand Down
2 changes: 1 addition & 1 deletion source/creator/viewport/common/mesheditor/tools/grid.d
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class GridTool : NodeSelect {
meshData.regenerateGrid();
mesh.copyFromMeshData(meshData);
impl.refreshMesh();
impl.updateVtxAtMouse(ulong(-1));
impl.vtxAtMouse = ulong(-1);
}

} else {
Expand Down
12 changes: 7 additions & 5 deletions source/creator/viewport/common/mesheditor/tools/point.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import std.stdio;

class PointTool : NodeSelect {
Action action;
bool autoConnect = true;

// using static for remembering the last setting
static bool autoConnect = true;

override bool onDragStart(vec2 mousePos, IncMeshEditorOne impl) {
if (!impl.deformOnly) {
Expand Down Expand Up @@ -130,7 +132,7 @@ class PointTool : NodeSelect {
impl.selected.length = 0;
impl.updateMirrorSelected();
impl.maybeSelectOne = ulong(-1);
impl.updateVtxAtMouse(ulong(-1));
impl.vtxAtMouse = ulong(-1);
changed = true;
}

Expand All @@ -146,7 +148,7 @@ class PointTool : NodeSelect {
} else {
impl.foreachMirror((uint axis) {
vertex = new MeshVertex(impl.mirror(axis, impl.mousePos));
action.addVertex(vertex);
action.addVertex(vertex, axis);
});
}
impl.refreshMesh();
Expand All @@ -167,7 +169,7 @@ class PointTool : NodeSelect {
auto lastAddAction = incActionFindLast!MeshAddAction(3);
if (lastAddAction is null || lastAddAction.vertices.length == 0) return;

auto prevVertexIdx = impl.getVertexFromPoint(lastAddAction.vertices[$ - 1].position);
auto prevVertexIdx = impl.getVertexFromPoint(lastAddAction.axisVertices[0][$ - 1].position);
auto prevVertex = impl.getVerticesByIndex([prevVertexIdx])[0];
if (prevVertex == null) return;
auto action = new MeshConnectAction(impl.getTarget().name, impl, mesh);
Expand Down Expand Up @@ -483,7 +485,7 @@ class PointToolInfo : ToolInfoBase!PointTool {
} else {
auto pointTool = cast(PointTool)(editors.length == 0 ? null: editors.values()[0].getTool());
igBeginGroup();
if (incButtonColored("", ImVec2(0, 0), (pointTool !is null && !pointTool.isAutoConnect())? ImVec4.init : ImVec4(0.6, 0.6, 0.6, 1))) {
if (incButtonColored("", ImVec2(0, 0), (pointTool !is null && !pointTool.isAutoConnect())? ImVec4(0.6, 0.6, 0.6, 1) : ImVec4.init)) {
foreach (e; editors) {
auto pt = cast(PointTool)(e.getTool());
if (pt !is null)
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
Expand Up @@ -114,7 +114,7 @@ class NodeSelect : Tool, Draggable {
impl.mousePos = -impl.mousePos;
}

impl.updateVtxAtMouse(impl.getVertexFromPoint(impl.mousePos));
impl.vtxAtMouse = impl.getVertexFromPoint(impl.mousePos);

return 0;
}
Expand Down

0 comments on commit 457c5ab

Please sign in to comment.