载入中...
搜索中...
未找到
GizmoManager.cpp
浏览该文件的文档.
2
3#include <cmath>
4
5namespace eve::editor {
6
8 gizmo_.setMode("translate");
9 positionEnabled_ = true;
10}
11
12void GizmoManager::attach() { attached_ = true; }
13
15 attached_ = false;
16 gizmo_.endDrag();
17}
18
19GizmoManager::Hit GizmoManager::pickMode(const std::string &mode, float ox, float oy, float oz,
20 float dx, float dy, float dz) {
21 Hit h;
22 h.mode = mode;
23 std::string prev = gizmo_.getMode();
24 gizmo_.setMode(mode);
25 h.axis = gizmo_.pick(ox, oy, oz, dx, dy, dz);
26 // Approximate depth: if miss, huge t; if hit, use distance from origin to gizmo pos as proxy
27 // Re-pick stores hover; we use a second geometric estimate via part origins when hit.
28 if (h.axis.empty()) {
29 h.t = 1e30f;
30 } else {
31 // Prefer closer hits: distance from ray origin to gizmo position
32 float px = gizmo_.getPositionX() - ox;
33 float py = gizmo_.getPositionY() - oy;
34 float pz = gizmo_.getPositionZ() - oz;
35 h.t = std::sqrt(px * px + py * py + pz * pz);
36 // Slight bias so translate planes don't always win over rings at same center
37 if (mode == "rotate") h.t -= 0.001f;
38 if (mode == "scale") h.t -= 0.002f;
39 if (mode == "bound") h.t -= 0.003f;
40 }
41 gizmo_.setMode(prev);
42 return h;
43}
44
45std::string GizmoManager::pick(float ox, float oy, float oz, float dx, float dy, float dz) {
46 if (!attached_) return "";
47
48 Hit best;
49 best.t = 1e30f;
50
51 auto consider = [&](bool enabled, const char *mode) {
52 if (!enabled) return;
53 Hit h = pickMode(mode, ox, oy, oz, dx, dy, dz);
54 if (!h.axis.empty() && h.t < best.t) best = h;
55 };
56
57 consider(positionEnabled_, "translate");
58 consider(rotationEnabled_, "rotate");
59 consider(scaleEnabled_, "scale");
60 consider(boundEnabled_, "bound");
61
62 if (best.axis.empty()) {
63 gizmo_.pick(ox, oy, oz, dx, dy, dz); // clear hover via empty pick on current mode
64 return "";
65 }
66
67 gizmo_.setMode(best.mode);
68 return gizmo_.pick(ox, oy, oz, dx, dy, dz);
69}
70
71bool GizmoManager::beginDrag(const std::string &axis, float ox, float oy, float oz, float dx,
72 float dy, float dz) {
73 if (!attached_ || axis.empty()) return false;
74 return gizmo_.beginDrag(axis, ox, oy, oz, dx, dy, dz);
75}
76
77bool GizmoManager::updateDrag(float ox, float oy, float oz, float dx, float dy, float dz) {
78 if (!attached_) return false;
79 return gizmo_.updateDrag(ox, oy, oz, dx, dy, dz);
80}
81
82void GizmoManager::endDrag() { gizmo_.endDrag(); }
83
84} // namespace eve::editor
float py
float pz
int h
std::vector< Colorf > px
float dz
float dy
float dx
std::map< Cell, int > best
double oy
double ox
GizmoManager()
Gizmo manager.
std::string pick(float ox, float oy, float oz, float dx, float dy, float dz)
Pick across enabled modes; sets gizmo mode to the winning tool.
bool beginDrag(const std::string &axis, float ox, float oy, float oz, float dx, float dy, float dz)
Begins drag.
bool updateDrag(float ox, float oy, float oz, float dx, float dy, float dz)
Updates drag.
float getPositionY() const
Returns the position y.
std::string pick(float ox, float oy, float oz, float dx, float dy, float dz)
Ray pick against active mode handles. Returns axis id: "x"|"y"|"z"|"xy"|"yz"|"xz"|"xyz"|"bx"|…|"bz"|"...
std::string getMode() const
Returns the mode.
bool beginDrag(const std::string &axis, float ox, float oy, float oz, float dx, float dy, float dz)
Begins drag.
float getPositionZ() const
Returns the position z.
bool updateDrag(float ox, float oy, float oz, float dx, float dy, float dz)
Updates drag.
void setMode(const std::string &mode)
Sets the mode.
float getPositionX() const
Returns the position x.
bool enabled