aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/EditorGuizmo.cpp131
-rw-r--r--source/EditorGuizmo.hpp12
2 files changed, 66 insertions, 77 deletions
diff --git a/source/EditorGuizmo.cpp b/source/EditorGuizmo.cpp
index 66ef6a3..a0ee212 100644
--- a/source/EditorGuizmo.cpp
+++ b/source/EditorGuizmo.cpp
@@ -743,7 +743,6 @@ struct Context {
vec_t mBoundsLocalPivot;
int mBoundsBestAxis;
int mBoundsAxis[2];
- float mBoundsOriginalBounds[6];
bool mbUsingBounds;
matrix_t mBoundsMatrix;
@@ -1560,7 +1559,7 @@ static bool CanActivate() {
return false;
}
-static void HandleAndDrawLocalBounds(float* bounds, matrix_t* matrix, const float* snapValues, OPERATION operation) {
+static void HandleAndDrawLocalBounds(const float* bounds, matrix_t* matrix, const float* snapValues, OPERATION operation) {
ImGuiIO& io = ImGui::GetIO();
ImDrawList* drawList = gContext.mDrawList;
@@ -1711,7 +1710,6 @@ static void HandleAndDrawLocalBounds(float* bounds, matrix_t* matrix, const floa
gContext.mBoundsPivot.TransformPoint(aabb[(i + 2) % 4], gContext.mModelSource);
gContext.mBoundsAnchor.TransformPoint(aabb[i], gContext.mModelSource);
gContext.mBoundsPlan = BuildPlan(gContext.mBoundsAnchor, bestAxisWorldDirection);
- memcpy(gContext.mBoundsOriginalBounds, bounds, sizeof(gContext.mBoundsOriginalBounds));
gContext.mBoundsBestAxis = bestAxis;
gContext.mBoundsAxis[0] = secondAxis;
gContext.mBoundsAxis[1] = thirdAxis;
@@ -1731,7 +1729,6 @@ static void HandleAndDrawLocalBounds(float* bounds, matrix_t* matrix, const floa
gContext.mBoundsPivot.TransformPoint(midPointOpposite, gContext.mModelSource);
gContext.mBoundsAnchor.TransformPoint(midPoint, gContext.mModelSource);
gContext.mBoundsPlan = BuildPlan(gContext.mBoundsAnchor, bestAxisWorldDirection);
- memcpy(gContext.mBoundsOriginalBounds, bounds, sizeof(gContext.mBoundsOriginalBounds));
gContext.mBoundsBestAxis = bestAxis;
int indices[] = { secondAxis, thirdAxis };
gContext.mBoundsAxis[0] = indices[i % 2];
@@ -1748,6 +1745,9 @@ static void HandleAndDrawLocalBounds(float* bounds, matrix_t* matrix, const floa
if (gContext.mbUsingBounds && (gContext.mActualID == -1 || gContext.mActualID == gContext.mEditingID))
{
+ matrix_t scale;
+ scale.SetToIdentity();
+
// compute projected mouse position on plan
const float len = IntersectRayPlane(gContext.mRayOrigin, gContext.mRayVector, gContext.mBoundsPlan);
vec_t newPos = gContext.mRayOrigin + gContext.mRayVector * len;
@@ -1755,53 +1755,49 @@ static void HandleAndDrawLocalBounds(float* bounds, matrix_t* matrix, const floa
// compute a reference and delta vectors base on mouse move
vec_t deltaVector = (newPos - gContext.mBoundsPivot).Abs();
vec_t referenceVector = (gContext.mBoundsAnchor - gContext.mBoundsPivot).Abs();
- ImGui::Text("Delta: %.2f, %.2f, %.2f", deltaVector.x, deltaVector.y, deltaVector.z);
- ImGui::Text("Ref: %.2f, %.2f, %.2f", referenceVector.x, referenceVector.y, referenceVector.z);
// for 1 or 2 axes, compute a ratio that's used for scale and snap it based on resulting length
- for (int axisIndex1 : gContext.mBoundsAxis) {
- if (axisIndex1 == -1) {
+ for (int i = 0; i < 2; i++)
+ {
+ int axisIndex1 = gContext.mBoundsAxis[i];
+ if (axisIndex1 == -1)
+ {
continue;
}
float ratioAxis = 1.f;
vec_t axisDir = gContext.mBoundsMatrix.component[axisIndex1].Abs();
- ImGui::Text("Axisdir: %.2f, %.2f, %.2f", axisDir.x, axisDir.y, axisDir.z);
float dtAxis = axisDir.Dot(referenceVector);
- ImGui::Text("dtAxis: %.2f", dtAxis);
- if (dtAxis > FLT_EPSILON) {
+ float boundSize = bounds[axisIndex1 + 3] - bounds[axisIndex1];
+ if (dtAxis > FLT_EPSILON)
+ {
ratioAxis = axisDir.Dot(deltaVector) / dtAxis;
- ImGui::Text("axis %d, %f", axisIndex1, ratioAxis);
}
- float originalBoundSize = gContext.mBoundsOriginalBounds[axisIndex1 + 3] - gContext.mBoundsOriginalBounds[axisIndex1];
- float length = originalBoundSize * ratioAxis;
- if (snapValues) {
+ if (snapValues)
+ {
+ float length = boundSize * ratioAxis;
ComputeSnap(&length, snapValues[axisIndex1]);
- if (originalBoundSize > FLT_EPSILON) {
- ratioAxis = length / originalBoundSize;
+ if (boundSize > FLT_EPSILON)
+ {
+ ratioAxis = length / boundSize;
}
}
-
- length = originalBoundSize * ratioAxis;
- bounds[axisIndex1] = length;
+ scale.component[axisIndex1] *= ratioAxis;
}
- // Translation (object center) fixup
- matrix_t preScale, postScale;
- preScale.Translation(-gContext.mBoundsLocalPivot);
- postScale.Translation(gContext.mBoundsLocalPivot);
+ // transform matrix
+ matrix_t preScale, postScale;
+ preScale.Translation(-gContext.mBoundsLocalPivot);
+ postScale.Translation(gContext.mBoundsLocalPivot);
+ matrix_t res = preScale * scale * postScale * gContext.mBoundsMatrix;
+ *matrix = res;
// info text
char tmps[512];
ImVec2 destinationPosOnScreen = worldToPos(gContext.mModel.v.position, gContext.mViewProjection);
- ImFormatString(tmps, sizeof(tmps),
- // Size of the bounds in each axis direction
- "X: %.2f Y: %.2f Z:%.2f",
- (bounds[3] - bounds[0]) * gContext.mBoundsMatrix.component[0].Length(),
- (bounds[4] - bounds[1]) * gContext.mBoundsMatrix.component[1].Length(),
- (bounds[5] - bounds[2]) * gContext.mBoundsMatrix.component[2].Length());
+ ImFormatString(tmps, sizeof(tmps), "X: %.2f Y: %.2f Z:%.2f", (bounds[3] - bounds[0]) * gContext.mBoundsMatrix.component[0].Length() * scale.component[0].Length(), (bounds[4] - bounds[1]) * gContext.mBoundsMatrix.component[1].Length() * scale.component[1].Length(), (bounds[5] - bounds[2]) * gContext.mBoundsMatrix.component[2].Length() * scale.component[2].Length());
drawList->AddText(ImVec2(destinationPosOnScreen.x + 15, destinationPosOnScreen.y + 15), IM_COL32_BLACK, tmps);
drawList->AddText(ImVec2(destinationPosOnScreen.x + 14, destinationPosOnScreen.y + 14), IM_COL32_WHITE, tmps);
}
@@ -2389,7 +2385,7 @@ void AllowAxisFlip(bool value) {
gContext.mAllowAxisFlip = value;
}
-bool Manipulate(const float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float* deltaMatrix, const float* snap, float* localBounds, const float* boundsSnap) {
+bool Manipulate(const float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float* deltaMatrix, const float* snap, const float* localBounds, const float* boundsSnap) {
// Scale is always local or matrix will be skewed when applying world scale or oriented matrix
ComputeContext(view, projection, matrix, (operation & SCALE) ? LOCAL : mode);
@@ -2419,6 +2415,7 @@ bool Manipulate(const float* view, const float* projection, OPERATION operation,
HandleRotation(matrix, deltaMatrix, operation, type, snap);
}
}
+
if (localBounds && !gContext.mbUsing)
{
HandleAndDrawLocalBounds(localBounds, (matrix_t*)matrix, boundsSnap, operation);
@@ -2689,6 +2686,7 @@ void ViewManipulate(float* view, float length, ImVec2 position, ImVec2 size, ImU
// tag faces
bool boxes[27]{};
+ static int overBox = -1;
for (int iPass = 0; iPass < 2; iPass++)
{
for (int iFace = 0; iFace < 6; iFace++)
@@ -2758,38 +2756,10 @@ void ViewManipulate(float* view, float length, ImVec2 position, ImVec2 size, ImU
{
gContext.mDrawList->AddConvexPolyFilled(faceCoordsScreen, 4, IM_COL32(0xF0, 0xA0, 0x60, 0x80));
- if (!io.MouseDown[0] && !isDraging && isClicking)
- {
- // apply new view direction
- int cx = boxCoordInt / 9;
- int cy = (boxCoordInt - cx * 9) / 3;
- int cz = boxCoordInt % 3;
- interpolationDir = makeVect(1.f - (float)cx, 1.f - (float)cy, 1.f - (float)cz);
- interpolationDir.Normalize();
-
- if (fabsf(Dot(interpolationDir, referenceUp)) > 1.0f - 0.01f)
- {
- vec_t right = viewInverse.v.right;
- if (fabsf(right.x) > fabsf(right.z))
- {
- right.z = 0.f;
- } else
- {
- right.x = 0.f;
- }
- right.Normalize();
- interpolationUp = Cross(interpolationDir, right);
- interpolationUp.Normalize();
- } else
- {
- interpolationUp = referenceUp;
- }
- interpolationFrames = 40;
- isClicking = false;
- }
- if (io.MouseClicked[0] && !isDraging)
- {
+ if (io.MouseDown[0] && !isClicking && !isDraging) {
+ overBox = boxCoordInt;
isClicking = true;
+ isDraging = true;
}
}
}
@@ -2812,13 +2782,42 @@ void ViewManipulate(float* view, float length, ImVec2 position, ImVec2 size, ImU
}
isInside = gContext.mbMouseOver && ImRect(position, position + size).Contains(io.MousePos);
- // drag view
- if (!isDraging && io.MouseClicked[0] && isInside)
+ if (io.MouseDown[0] && (fabsf(io.MouseDelta[0]) || fabsf(io.MouseDelta[1])) && isClicking)
{
- isDraging = true;
isClicking = false;
- } else if (isDraging && !io.MouseDown[0])
+ }
+
+ if (!io.MouseDown[0])
{
+ if (isClicking)
+ {
+ // apply new view direction
+ int cx = overBox / 9;
+ int cy = (overBox - cx * 9) / 3;
+ int cz = overBox % 3;
+ interpolationDir = makeVect(1.f - (float)cx, 1.f - (float)cy, 1.f - (float)cz);
+ interpolationDir.Normalize();
+
+ if (fabsf(Dot(interpolationDir, referenceUp)) > 1.0f - 0.01f)
+ {
+ vec_t right = viewInverse.v.right;
+ if (fabsf(right.x) > fabsf(right.z))
+ {
+ right.z = 0.f;
+ } else
+ {
+ right.x = 0.f;
+ }
+ right.Normalize();
+ interpolationUp = Cross(interpolationDir, right);
+ interpolationUp.Normalize();
+ } else
+ {
+ interpolationUp = referenceUp;
+ }
+ interpolationFrames = 40;
+ }
+ isClicking = false;
isDraging = false;
}
diff --git a/source/EditorGuizmo.hpp b/source/EditorGuizmo.hpp
index 0560050..b11759c 100644
--- a/source/EditorGuizmo.hpp
+++ b/source/EditorGuizmo.hpp
@@ -201,17 +201,7 @@ enum MODE {
WORLD
};
-IMGUI_API bool Manipulate(
- const float* view,
- const float* projection,
- OPERATION operation,
- MODE mode,
- float* matrix,
- float* deltaMatrix = NULL,
- const float* snap = NULL,
- float* localBounds = NULL,
- const float* boundsSnap = NULL);
-
+IMGUI_API bool Manipulate(const float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float* deltaMatrix = NULL, const float* snap = NULL, const float* localBounds = NULL, const float* boundsSnap = NULL);
//
// Please note that this cubeview is patented by Autodesk : https://patents.google.com/patent/US7782319B2/en
// It seems to be a defensive patent in the US. I don't think it will bring troubles using it as