Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added autoConnect Feature #401

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions source/creator/viewport/common/mesheditor/operations/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,25 @@ public:
bool mutateSelection = false;
bool invertSelection = false;
ulong maybeSelectOne;

// you should call updateVtxAtMouse() for updating vtxAtMouse
// because it also updates prevVtxAtMouse
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 @@ -150,6 +162,14 @@ public:
return vInd;
}

MeshVertex* mirrorVertex(uint axis, MeshVertex* vtx) {
if (axis == 0) return vtx;
ulong vInd = getVertexFromPoint(mirror(axis, vtx.position));
MeshVertex* v = getVerticesByIndex([vInd])[0];
if (v is null || v == vtx) return null;
return getVerticesByIndex([vInd])[0];
}

bool isOnMirror(vec2 pos, float aoe) {
return
(mirrorVert && pos.y > -aoe && pos.y < aoe) ||
Expand Down Expand Up @@ -211,6 +231,7 @@ 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.vtxAtMouse = ulong(-1);
impl.updateVtxAtMouse(ulong(-1));
}

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

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

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

action.updateNewState();
incActionPush(action);
} else {
auto action = new MeshAddAction(impl.getTarget().name, impl, mesh);
void addVertex(ref MeshVertex* vertex) {
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 {
ulong off = mesh.vertices.length;
if (impl.isOnMirror(impl.mousePos, impl.meshEditAOE)) {
impl.placeOnMirror(impl.mousePos, impl.meshEditAOE);
} else {
impl.foreachMirror((uint axis) {
vertex = new MeshVertex(impl.mirror(axis, impl.mousePos));
action.addVertex(vertex);
});
}
impl.refreshMesh();
impl.vertexMapDirty = true;
if (io.KeyCtrl) impl.selectOne(mesh.vertices.length - 1);
else impl.selectOne(off);
changed = true;

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

// connect if there is a selected vertex
void connectVertex(ref MeshVertex* vertex) {
if (vertex is null) return;

auto prevMouse = impl.getVerticesByIndex([impl.prevVtxAtMouse])[0];
auto action = new MeshConnectAction(impl.getTarget().name, impl, mesh);
impl.foreachMirror((uint axis) {
MeshVertex* vertex = new MeshVertex(impl.mirror(axis, impl.mousePos));
action.addVertex(vertex);
MeshVertex* mPrev = impl.mirrorVertex(axis, prevMouse);
MeshVertex* mSel = impl.mirrorVertex(axis, vertex);

if (mPrev !is null && mSel !is null) {
action.connect(mPrev, mSel);
}
});
impl.refreshMesh();
action.updateNewState();
incActionPush(action);

changed = true;
}
impl.refreshMesh();
impl.vertexMapDirty = true;
if (io.KeyCtrl) impl.selectOne(mesh.vertices.length - 1);
else impl.selectOne(off);
changed = true;

action.updateNewState();
incActionPush(action);
MeshVertex* vertex;
addVertex(vertex);
if (autoConnect)
connectVertex(vertex);
}
}

Expand Down Expand Up @@ -424,6 +453,13 @@ class PointTool : NodeSelect {
return changed;
}

bool isAutoConnect() {
return autoConnect;
}

void setAutoConnect(bool autoConnect) {
this.autoConnect = autoConnect;
}
}

class ToolInfoImpl(T: PointTool) : ToolInfoBase!(T) {
Expand All @@ -433,4 +469,25 @@ class ToolInfoImpl(T: PointTool) : ToolInfoBase!(T) {
string icon() { return ""; }
override
string description() { return _("Vertex Tool"); }

override
bool displayToolOptions(bool deformOnly, VertexToolMode toolMode, IncMeshEditorOne[Node] editors) {
if (deformOnly) {

} 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))) {
foreach (e; editors) {
auto pt = cast(PointTool)(e.getTool());
if (pt !is null)
pt.setAutoConnect(!pt.isAutoConnect());
}
}
incTooltip(_("Auto connect vertices"));
igEndGroup();
}

return false;
}
}
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 @@ -58,7 +58,7 @@ class NodeSelect : Tool, Draggable {
impl.mousePos = -impl.mousePos;
}

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

return 0;
}
Expand Down
3 changes: 3 additions & 0 deletions source/creator/viewport/vertex/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ void incViewportVertexOptions() {
incTooltip(_("Auto Meshing Options"));
igEndGroup();

igSameLine(0, 4);

editor.displayToolOptions();
igPopStyleVar(2);
}

Expand Down
Loading