Euphoria
imguizmo.cc
Go to the documentation of this file.
1 #include "window/imguizmo.h"
2 
3 #include "imgui/imgui.h"
4 #include "imguizmo/ImGuizmo.h"
5 
6 
8 {
9  void setup()
10  {
11  ImGuizmo::SetDrawlist(ImGui::GetForegroundDrawList());
12  }
13 
14 
15  void begin()
16  {
17  ImGuizmo::BeginFrame();
18  }
19 
20  namespace
21  {
22  std::optional<ImGuizmo::OPERATION>
23  combine
24  (
25  bool a, ImGuizmo::OPERATION ao,
26  bool b, ImGuizmo::OPERATION bo,
27  bool c, ImGuizmo::OPERATION co
28  )
29  {
30  // todo(Gustav): add a none value to ImGuizmo so we can remove the static cast
31  constexpr const auto none = static_cast<ImGuizmo::OPERATION>(0);
32 
33  auto r = none;
34 
35  if(a) { r = r | ao; }
36  if(b) { r = r | bo; }
37  if(c) { r = r | co; }
38 
39  if (r != none)
40  {
41  return r;
42  }
43  else
44  {
45  return std::nullopt;
46  }
47  }
48  }
49 
50 
51  bool
53  (
54  bool is_local,
55  const std::optional<vec3f>& snap,
56  const mat4f& camera_view,
57  const mat4f& camera_projection,
58  const mat4f& model,
59  bool tx, bool ty, bool tz,
60  vec3f* new_position
61  )
62  {
63  const auto current_gizmo_mode = is_local ? ImGuizmo::LOCAL : ImGuizmo::WORLD;
64 
65  const auto operation = combine
66  (
67  tx, ImGuizmo::TRANSLATE_X,
68  ty, ImGuizmo::TRANSLATE_Y,
69  tz, ImGuizmo::TRANSLATE_Z
70  );
71 
72  if (operation.has_value() == false)
73  {
74  return false;
75  }
76 
77  {
78  ImGuiIO& io = ImGui::GetIO();
79  ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
80  }
81 
82  auto model_to_modify = model;
83 
84  const auto was_modified = ImGuizmo::Manipulate
85  (
86  camera_view.get_column_major(),
87  camera_projection.get_column_major(),
88  *operation,
89  current_gizmo_mode,
90  model_to_modify.get_column_major(),
91  nullptr,
92  snap.has_value() ? &snap->x : nullptr
93  );
94 
95  if (was_modified)
96  {
97  float matrix_rotation[3];
98  float matrix_scale[3];
99 
100  ImGuizmo::DecomposeMatrixToComponents
101  (
102  model_to_modify.get_column_major(),
103  new_position->get_data_ptr(), matrix_rotation, matrix_scale
104  );
105  }
106 
107  return was_modified;
108  }
109 
110 
111  bool
113  (
114  bool is_local,
115  const std::optional<Angle>& snap,
116  const mat4f& camera_view,
117  const mat4f& camera_projection,
118  const mat4f& model,
119  bool rx, bool ry, bool rz,
120  quatf* new_rotation
121  )
122  {
123  const auto current_gizmo_mode = is_local ? ImGuizmo::LOCAL : ImGuizmo::WORLD;
124 
125  const auto operation = combine
126  (
127  rx, ImGuizmo::ROTATE_X,
128  ry, ImGuizmo::ROTATE_Y,
129  rz, ImGuizmo::ROTATE_Z
130  );
131 
132  if (operation.has_value() == false)
133  {
134  return false;
135  }
136 
137  {
138  ImGuiIO& io = ImGui::GetIO();
139  ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
140  }
141 
142  auto model_to_modify = model;
143 
144  const auto snap_degrees = snap.value_or(no_rotation).as_degrees();
145 
146  const auto was_modified = ImGuizmo::Manipulate
147  (
148  camera_view.get_column_major(),
149  camera_projection.get_column_major(),
150  *operation,
151  current_gizmo_mode,
152  model_to_modify.get_column_major(),
153  nullptr,
154  snap.has_value() ? &snap_degrees : nullptr
155  );
156 
157  if (was_modified)
158  {
159  float matrix_position[3];
160  float matrix_rotation[3];
161  float matrix_scale[3];
162 
163  ImGuizmo::DecomposeMatrixToComponents
164  (
165  model_to_modify.get_column_major(),
166  matrix_position, matrix_rotation, matrix_scale
167  );
168 
169  *new_rotation = quatf::from_ypr
170  (
171  Angle::from_degrees(matrix_rotation[2]),
172  Angle::from_degrees(matrix_rotation[1]),
173  Angle::from_degrees(matrix_rotation[0])
174  );
175  }
176 
177  return was_modified;
178  }
179 }
bool translate(bool is_local, const std::optional< vec3f > &snap, const mat4f &camera_view, const mat4f &camera_projection, const mat4f &model, bool tx, bool ty, bool tz, vec3f *new_position)
Definition: imguizmo.cc:53
bool rotate(bool is_local, const std::optional< Angle > &snap, const mat4f &camera_view, const mat4f &camera_projection, const mat4f &model, bool rx, bool ry, bool rz, quatf *new_rotation)
Definition: imguizmo.cc:113
constexpr Angle no_rotation
Definition: angle.h:90
constexpr static Angle from_degrees(float degrees)
Definition: angle.h:16
Definition: mat4.h:14
float * get_column_major()
Definition: mat4.cc:6
Definition: quat.h:15
static quatf from_ypr(const Angle &yaw, const Angle &pitch, const Angle &roll)
Definition: quat.cc:34
Definition: vec3.h:48
float * get_data_ptr()
Definition: vec3.cc:56