-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathimoguizmo.hpp
More file actions
422 lines (373 loc) · 19.8 KB
/
Copy pathimoguizmo.hpp
File metadata and controls
422 lines (373 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
MIT License
Copyright(c) 2026 Lukas Lipp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this softwareand associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright noticeand this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <imgui.h>
#include <cmath>
#include <cstring>
#include <array>
#include <algorithm>
#include <utility>
namespace ImOGuizmo {
enum class CoordinateSystem {
XYZ, YZX, ZXY, // RHS
XZY, YXZ, ZYX // LHS
};
namespace internal {
static struct Config {
float mX = 0.f;
float mY = 0.f;
float mSize = 100.f;
ImDrawList* mDrawList = nullptr;
} config;
static struct Interaction {
bool dragging = false;
bool dragged = false;
ImVec2 clickMousePos = {};
} interaction;
struct ImVec3
{
ImVec3(const float x, const float y, const float z) : mData{ x, y, z } {}
explicit ImVec3(const float* const data) : mData{ data[0], data[1], data[2] } {}
float operator[](const int idx) const { return mData[idx]; }
ImVec3 operator+(const ImVec3& other) const { return { mData[0] + other[0], mData[1] + other[1], mData[2] + other[2] }; }
ImVec3 operator-(const ImVec3& other) const { return { mData[0] - other[0], mData[1] - other[1], mData[2] - other[2] }; }
ImVec3 operator*(const float scalar) const { return { mData[0] * scalar, mData[1] * scalar, mData[2] * scalar }; }
ImVec3 operator*(const ImVec3& other) const { return { mData[0] * other[0], mData[1] * other[1], mData[2] * other[2] }; }
float mData[3];
};
inline ImVec4 multiply(const float* const m, const ImVec4& v)
{
const float x = m[0] * v.x + m[4] * v.y + m[8] * v.z + m[12] * v.w;
const float y = m[1] * v.x + m[5] * v.y + m[9] * v.z + m[13] * v.w;
const float z = m[2] * v.x + m[6] * v.y + m[10] * v.z + m[14] * v.w;
const float w = m[3] * v.x + m[7] * v.y + m[11] * v.z + m[15] * v.w;
return { x, y, z, w };
}
inline void multiply(const float* const l, const float* const r, float* out)
{
out[0] = l[0] * r[0] + l[1] * r[4] + l[2] * r[8] + l[3] * r[12];
out[1] = l[0] * r[1] + l[1] * r[5] + l[2] * r[9] + l[3] * r[13];
out[2] = l[0] * r[2] + l[1] * r[6] + l[2] * r[10] + l[3] * r[14];
out[3] = l[0] * r[3] + l[1] * r[7] + l[2] * r[11] + l[3] * r[15];
out[4] = l[4] * r[0] + l[5] * r[4] + l[6] * r[8] + l[7] * r[12];
out[5] = l[4] * r[1] + l[5] * r[5] + l[6] * r[9] + l[7] * r[13];
out[6] = l[4] * r[2] + l[5] * r[6] + l[6] * r[10] + l[7] * r[14];
out[7] = l[4] * r[3] + l[5] * r[7] + l[6] * r[11] + l[7] * r[15];
out[8] = l[8] * r[0] + l[9] * r[4] + l[10] * r[8] + l[11] * r[12];
out[9] = l[8] * r[1] + l[9] * r[5] + l[10] * r[9] + l[11] * r[13];
out[10] = l[8] * r[2] + l[9] * r[6] + l[10] * r[10] + l[11] * r[14];
out[11] = l[8] * r[3] + l[9] * r[7] + l[10] * r[11] + l[11] * r[15];
out[12] = l[12] * r[0] + l[13] * r[4] + l[14] * r[8] + l[15] * r[12];
out[13] = l[12] * r[1] + l[13] * r[5] + l[14] * r[9] + l[15] * r[13];
out[14] = l[12] * r[2] + l[13] * r[6] + l[14] * r[10] + l[15] * r[14];
out[15] = l[12] * r[3] + l[13] * r[7] + l[14] * r[11] + l[15] * r[15];
}
inline float dot(const ImVec3& a, const ImVec3& b)
{
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
inline ImVec3 cross(const ImVec3& a, const ImVec3& b)
{
return {
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
};
}
inline ImVec3 normalize(const ImVec3& a)
{
float il = 1.f / (sqrtf(dot(a, a)) + FLT_EPSILON);
return a * il;
}
inline bool isRightHanded(const CoordinateSystem coordinateSystem)
{
switch (coordinateSystem) {
case CoordinateSystem::XYZ:
case CoordinateSystem::YZX:
case CoordinateSystem::ZXY:
return true;
default:
return false;
}
}
inline std::array<int, 3> axes(const CoordinateSystem coordinateSystem)
{
switch (coordinateSystem) {
case CoordinateSystem::YZX: return { 1, 2, 0 };
case CoordinateSystem::ZXY: return { 2, 0, 1 };
case CoordinateSystem::XZY: return { 0, 2, 1 };
case CoordinateSystem::YXZ: return { 1, 0, 2 };
case CoordinateSystem::ZYX: return { 2, 1, 0 };
default: return { 0, 1, 2 }; // CoordinateSystem::XYZ
}
}
inline ImVec3 axisVector(const int axis, const float sign = 1.0f)
{
if (axis == 0) return { sign, 0.0f, 0.0f };
if (axis == 1) return { 0.0f, sign, 0.0f };
return { 0.0f, 0.0f, sign };
}
inline ImVec3 rotateAroundAxis(const ImVec3& v, const ImVec3& axis, const float angle)
{
const ImVec3 n = normalize(axis);
const float c = cosf(angle);
const float s = sinf(angle);
return v * c + cross(n, v) * s + n * dot(n, v) * (1.0f - c);
}
inline bool checkInsideCircle(const ImVec2 center, const float radius, const ImVec2 point)
{
return (point.x - center.x) * (point.x - center.x) + (point.y - center.y) * (point.y - center.y) <= radius * radius;
}
inline void drawPositiveLine(const ImVec2 center, const ImVec2 axis, const ImU32 color, const float radius, const float thickness, const char* text, const bool selected) {
const auto lineEndPositive = ImVec2{ center.x + axis.x, center.y + axis.y };
internal::config.mDrawList->AddLine(center, lineEndPositive, color, thickness);
internal::config.mDrawList->AddCircleFilled(lineEndPositive, radius, color);
const auto labelSize = ImGui::CalcTextSize(text);
const auto textPos = ImVec2(floor(lineEndPositive.x - 0.5f * labelSize.x), floor(lineEndPositive.y - 0.5f * labelSize.y));
if (selected) {
internal::config.mDrawList->AddCircle(lineEndPositive, radius, IM_COL32_WHITE, 0, 1.1f);
internal::config.mDrawList->AddText(textPos, IM_COL32_WHITE, text);
}
else internal::config.mDrawList->AddText(textPos, IM_COL32_BLACK, text);
}
inline void drawNegativeLine(const ImVec2 center, const ImVec2 axis, const ImU32 color, const float radius, const bool selected) {
const auto lineEndNegative = ImVec2{ center.x - axis.x, center.y - axis.y };
internal::config.mDrawList->AddCircleFilled(lineEndNegative, radius, color);
if (selected) {
internal::config.mDrawList->AddCircle(lineEndNegative, radius, IM_COL32_WHITE, 0, 1.1f);
}
}
inline void lookAt(ImVec3 const& eye, ImVec3 const& at, ImVec3 const& up, float* viewMatrix, const CoordinateSystem coordinateSystem)
{
const bool rightHanded = isRightHanded(coordinateSystem);
const auto f = normalize(at - eye);
const auto r = normalize(rightHanded ? cross(f, up) : cross(up, f));
const auto u = rightHanded ? cross(r, f) : cross(f, r);
const float zSign = rightHanded ? -1.0f : 1.0f;
viewMatrix[0] = r[0]; viewMatrix[1] = u[0]; viewMatrix[2] = zSign * f[0]; viewMatrix[3] = 0.0f;
viewMatrix[4] = r[1]; viewMatrix[5] = u[1]; viewMatrix[6] = zSign * f[1]; viewMatrix[7] = 0.0f;
viewMatrix[8] = r[2]; viewMatrix[9] = u[2]; viewMatrix[10] = zSign * f[2]; viewMatrix[11] = 0.0f;
viewMatrix[12] = -dot(r, eye); viewMatrix[13] = -dot(u, eye); viewMatrix[14] = -zSign * dot(f, eye); viewMatrix[15] = 1.0f;
}
inline void invert4x4(const float* m, float* out)
{
out[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] - m[9] * m[6] * m[15] + m[9] * m[7] * m[14] + m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
out[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] + m[8] * m[6] * m[15] - m[8] * m[7] * m[14] - m[12] * m[6] * m[11] + m[12] * m[7] * m[10];
out[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] - m[8] * m[5] * m[15] + m[8] * m[7] * m[13] + m[12] * m[5] * m[11] - m[12] * m[7] * m[9];
out[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] + m[8] * m[5] * m[14] - m[8] * m[6] * m[13] - m[12] * m[5] * m[10] + m[12] * m[6] * m[9];
out[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] + m[9] * m[2] * m[15] - m[9] * m[3] * m[14] - m[13] * m[2] * m[11] + m[13] * m[3] * m[10];
out[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] - m[8] * m[2] * m[15] + m[8] * m[3] * m[14] + m[12] * m[2] * m[11] - m[12] * m[3] * m[10];
out[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] + m[8] * m[1] * m[15] - m[8] * m[3] * m[13] - m[12] * m[1] * m[11] + m[12] * m[3] * m[9];
out[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] - m[8] * m[1] * m[14] + m[8] * m[2] * m[13] + m[12] * m[1] * m[10] - m[12] * m[2] * m[9];
out[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] - m[5] * m[2] * m[15] + m[5] * m[3] * m[14] + m[13] * m[2] * m[7] - m[13] * m[3] * m[6];
out[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] + m[4] * m[2] * m[15] - m[4] * m[3] * m[14] - m[12] * m[2] * m[7] + m[12] * m[3] * m[6];
out[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] - m[4] * m[1] * m[15] + m[4] * m[3] * m[13] + m[12] * m[1] * m[7] - m[12] * m[3] * m[5];
out[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] + m[4] * m[1] * m[14] - m[4] * m[2] * m[13] - m[12] * m[1] * m[6] + m[12] * m[2] * m[5];
out[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] + m[5] * m[2] * m[11] - m[5] * m[3] * m[10] - m[9] * m[2] * m[7] + m[9] * m[3] * m[6];
out[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] - m[4] * m[2] * m[11] + m[4] * m[3] * m[10] + m[8] * m[2] * m[7] - m[8] * m[3] * m[6];
out[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] + m[4] * m[1] * m[11] - m[4] * m[3] * m[9] - m[8] * m[1] * m[7] + m[8] * m[3] * m[5];
out[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] - m[4] * m[1] * m[10] + m[4] * m[2] * m[9] + m[8] * m[1] * m[6] - m[8] * m[2] * m[5];
float det = m[0] * out[0] + m[1] * out[4] + m[2] * out[8] + m[3] * out[12];
det = 1.0f / det;
for (unsigned int i = 0; i < 16; i++) out[i] = out[i] * det;
}
inline void lookAtSelection(const ImVec3& pivotPos, const float pivotDistance, const int selection, float* viewMatrix, const CoordinateSystem coordinateSystem)
{
const int selectedAxis = selection % 3;
const float selectedSign = selection < 3 ? 1.0f : -1.0f;
const std::array<int, 3> coordinateAxes = axes(coordinateSystem);
const int upAxis = coordinateAxes[1];
const int referenceAxis = coordinateAxes[2];
const float referenceSign = isRightHanded(coordinateSystem) ? -selectedSign : selectedSign;
const ImVec3 eye = pivotPos + axisVector(selectedAxis, selectedSign * pivotDistance);
const ImVec3 up = selectedAxis == upAxis ? axisVector(referenceAxis, referenceSign) : axisVector(upAxis);
lookAt(eye, pivotPos, up, viewMatrix, coordinateSystem);
}
inline bool orbitView(float* const viewMatrix, const ImVec2 mouseDelta, const float pivotDistance, const CoordinateSystem coordinateSystem)
{
if (pivotDistance <= 0.0f || (mouseDelta.x == 0.0f && mouseDelta.y == 0.0f)) return false;
float modelMat[16];
invert4x4(viewMatrix, modelMat);
const ImVec3 eye = ImVec3{ &modelMat[12] };
const ImVec3 pivotPos = eye - ImVec3{ &modelMat[8] } * pivotDistance;
ImVec3 offset = eye - pivotPos;
ImVec3 cameraUp = ImVec3{ &modelMat[4] };
ImVec3 cameraRight = ImVec3{ &modelMat[0] };
const ImVec3 orbitUp = axisVector(axes(coordinateSystem)[1]);
offset = rotateAroundAxis(offset, orbitUp, -mouseDelta.x);
cameraUp = rotateAroundAxis(cameraUp, orbitUp, -mouseDelta.x);
cameraRight = rotateAroundAxis(cameraRight, orbitUp, -mouseDelta.x);
offset = rotateAroundAxis(offset, cameraRight, -mouseDelta.y);
cameraUp = rotateAroundAxis(cameraUp, cameraRight, -mouseDelta.y);
lookAt(pivotPos + offset, pivotPos, cameraUp, viewMatrix, coordinateSystem);
return true;
}
}
static struct Config {
// in relation to half the rect size
float lineThicknessScale = 0.017f;
float axisLengthScale = 0.33f;
float positiveRadiusScale = 0.075f;
float negativeRadiusScale = 0.05f;
float hoverCircleRadiusScale = 0.88f;
// Mouse movement in pixels before a click becomes an orbit drag.
float dragThreshold = 3.0f;
float dragSensitivity = 0.01f;
bool drag = true;
bool click = true;
ImU32 xCircleFrontColor = IM_COL32(255, 54, 83, 255);
ImU32 xCircleBackColor = IM_COL32(154, 57, 71, 255);
ImU32 yCircleFrontColor = IM_COL32(138, 219, 0, 255);
ImU32 yCircleBackColor = IM_COL32(98, 138, 34, 255);
ImU32 zCircleFrontColor = IM_COL32(44, 143, 255, 255);
ImU32 zCircleBackColor = IM_COL32(52, 100, 154, 255);
ImU32 hoverCircleColor = IM_COL32(100, 100, 100, 130);
} config;
inline void SetRect(const float x, const float y, const float size)
{
internal::config.mX = x;
internal::config.mY = y;
internal::config.mSize = size;
}
inline void SetDrawList(ImDrawList* drawlist = nullptr)
{
internal::config.mDrawList = drawlist ? drawlist : ImGui::GetWindowDrawList();
}
inline void BeginFrame(const bool background = false)
{
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoBringToFrontOnFocus | ((background != true) ? ImGuiWindowFlags_NoBackground : ImGuiWindowFlags_None);
ImGui::SetNextWindowPos({ internal::config.mX, internal::config.mY }, ImGuiCond_Always);
ImGui::SetNextWindowSize({ internal::config.mSize, internal::config.mSize });
ImGui::Begin("imoguizmo", nullptr, flags);
SetDrawList(internal::config.mDrawList);
ImGui::End();
}
inline bool DrawGizmo(float* const viewMatrix, const float* const projectionMatrix, const float pivotDistance = 0.0f, const CoordinateSystem coordinateSystem = CoordinateSystem::XYZ) {
const float size = internal::config.mSize;
const float hSize = size * 0.5f;
const auto center = ImVec2{ internal::config.mX + hSize, internal::config.mY + hSize };
float viewProjection[16];
internal::multiply(viewMatrix, projectionMatrix, viewProjection);
// correction for non-square aspect ratio
{
const float aspectRatio = projectionMatrix[5] / projectionMatrix[0];
viewProjection[0] *= aspectRatio; viewProjection[8] *= aspectRatio;
}
// axis
const float axisLength = size * config.axisLengthScale;
const ImVec4 xAxis = internal::multiply(viewProjection, ImVec4{ axisLength, 0, 0, 0 });
const ImVec4 yAxis = internal::multiply(viewProjection, ImVec4{ 0, axisLength, 0, 0 });
const ImVec4 zAxis = internal::multiply(viewProjection, ImVec4{ 0, 0, axisLength, 0 });
const bool interactive = pivotDistance > 0.0f;
const ImVec2 mousePos = ImGui::GetIO().MousePos;
const float hoverCircleRadius = hSize * config.hoverCircleRadiusScale;
const bool hovered = interactive && internal::checkInsideCircle(center, hoverCircleRadius, mousePos);
const bool dragging = internal::interaction.dragging;
SetDrawList(internal::config.mDrawList);
if (config.hoverCircleColor != 0 && (hovered || dragging)) internal::config.mDrawList->AddCircleFilled(center, hoverCircleRadius, config.hoverCircleColor);
const float positiveRadius = size * config.positiveRadiusScale;
const float negativeRadius = size * config.negativeRadiusScale;
const bool xPositiveCloser = 0.0f >= xAxis.w;
const bool yPositiveCloser = 0.0f >= yAxis.w;
const bool zPositiveCloser = 0.0f >= zAxis.w;
// sort axis based on distance
// 0 : +x axis, 1 : +y axis, 2 : +z axis, 3 : -x axis, 4 : -y axis, 5 : -z axis
std::array<std::pair<int, float>, 6> pairs = { { {0, xAxis.w}, {1, yAxis.w}, {2, zAxis.w}, {3, -xAxis.w}, {4, -yAxis.w}, {5, -zAxis.w} } };
sort(pairs.begin(), pairs.end(), [=](const std::pair<int, float>& aA, const std::pair<int, float>& aB) { return aA.second > aB.second; });
// find selection, front to back
int selection = -1;
for (auto it = pairs.crbegin(); it != pairs.crend() && selection == -1 && interactive; ++it) {
switch (it->first) {
case 0: // +x axis
if (internal::checkInsideCircle(ImVec2{ center.x + xAxis.x, center.y - xAxis.y }, positiveRadius, mousePos)) selection = 0;
break;
case 1: // +y axis
if (internal::checkInsideCircle(ImVec2{ center.x + yAxis.x, center.y - yAxis.y }, positiveRadius, mousePos)) selection = 1;
break;
case 2: // +z axis
if (internal::checkInsideCircle(ImVec2{ center.x + zAxis.x, center.y - zAxis.y }, positiveRadius, mousePos)) selection = 2;
break;
case 3: // -x axis
if (internal::checkInsideCircle(ImVec2{ center.x - xAxis.x, center.y + xAxis.y }, negativeRadius, mousePos)) selection = 3;
break;
case 4: // -y axis
if (internal::checkInsideCircle(ImVec2{ center.x - yAxis.x, center.y + yAxis.y }, negativeRadius, mousePos)) selection = 4;
break;
case 5: // -z axis
if (internal::checkInsideCircle(ImVec2{ center.x - zAxis.x, center.y + zAxis.y }, negativeRadius, mousePos)) selection = 5;
break;
default: break;
}
}
// draw back first
const float lineThickness = size * config.lineThicknessScale;
for (const auto& [fst, snd] : pairs) {
switch (fst) {
case 0: // +x axis
internal::drawPositiveLine(center, ImVec2{ xAxis.x, -xAxis.y }, xPositiveCloser ? config.xCircleFrontColor : config.xCircleBackColor, positiveRadius, lineThickness, "X", !dragging && selection == 0);
continue;
case 1: // +y axis
internal::drawPositiveLine(center, ImVec2{ yAxis.x, -yAxis.y }, yPositiveCloser ? config.yCircleFrontColor : config.yCircleBackColor, positiveRadius, lineThickness, "Y", !dragging && selection == 1);
continue;
case 2: // +z axis
internal::drawPositiveLine(center, ImVec2{ zAxis.x, -zAxis.y }, zPositiveCloser ? config.zCircleFrontColor : config.zCircleBackColor, positiveRadius, lineThickness, "Z", !dragging && selection == 2);
continue;
case 3: // -x axis
internal::drawNegativeLine(center, ImVec2{ xAxis.x, -xAxis.y }, !xPositiveCloser ? config.xCircleFrontColor : config.xCircleBackColor, negativeRadius, !dragging && selection == 3);
continue;
case 4: // -y axis
internal::drawNegativeLine(center, ImVec2{ yAxis.x, -yAxis.y }, !yPositiveCloser ? config.yCircleFrontColor : config.yCircleBackColor, negativeRadius, !dragging && selection == 4);
continue;
case 5: // -z axis
internal::drawNegativeLine(center, ImVec2{ zAxis.x, -zAxis.y }, !zPositiveCloser ? config.zCircleFrontColor : config.zCircleBackColor, negativeRadius, !dragging && selection == 5);
continue;
default: break;
}
}
internal::config.mDrawList = nullptr;
if (config.drag) {
if (internal::interaction.dragging && ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
const ImVec2 d = mousePos - internal::interaction.clickMousePos;
const float dragThresholdSq = config.dragThreshold * config.dragThreshold;
if (d.x * d.x + d.y * d.y > dragThresholdSq) {
internal::interaction.dragged = true;
return internal::orbitView(viewMatrix, ImGui::GetIO().MouseDelta * config.dragSensitivity, pivotDistance, coordinateSystem);
}
}
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left)) {
internal::interaction.dragging = hovered;
internal::interaction.clickMousePos = mousePos;
}
if (ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
bool wasDragged = internal::interaction.dragged;
internal::interaction = {};
if(wasDragged) return false;
}
}
if (config.click && selection != -1 && ImGui::IsMouseReleased(ImGuiMouseButton_Left)) {
float modelMat[16];
internal::invert4x4(viewMatrix, modelMat);
const internal::ImVec3 pivotPos = internal::ImVec3{ &modelMat[12] } - internal::ImVec3{ &modelMat[8] } *pivotDistance;
internal::lookAtSelection(pivotPos, pivotDistance, selection, viewMatrix, coordinateSystem);
return true;
}
return false;
}
}