Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Gizmo.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_EDITOR_GIZMO_H_
34#define DVD_EDITOR_GIZMO_H_
35
38
39#include <imgui.h>
40#include <ImGuizmo.h>
41
42struct ImGuiContext;
43
44namespace Divide {
45 class Editor;
46 class Camera;
47 class DisplayWindow;
48 class SceneGraphNode;
49 namespace GFX {
50 class CommandBuffer;
51 }
52 namespace Attorney {
53 class GizmoEditor;
54 }
56 ImGuizmo::OPERATION currentGizmoOperation = ImGuizmo::TRANSLATE;
57 ImGuizmo::MODE currentGizmoMode = ImGuizmo::WORLD;
58 F32 snapTranslation[3] = { 1.f, 1.f, 1.f };
59 F32 snapScale[3] = { 1.f, 1.f, 1.f };
60 F32 snapRotation[3] = { 1.f, 1.f, 1.f };
61 U8 previousAxisSelected[3] = { 0u, 0u, 0u }; //0 = all, 1 = x, 2 = y, 3 = z
63 bool useSnap = false;
64 };
65
66 [[nodiscard]] FORCE_INLINE bool IsTranslationOperation(const TransformSettings& settings) {
67 return settings.currentGizmoOperation == ImGuizmo::OPERATION::TRANSLATE ||
68 settings.currentGizmoOperation == ImGuizmo::OPERATION::TRANSLATE_X ||
69 settings.currentGizmoOperation == ImGuizmo::OPERATION::TRANSLATE_Y ||
70 settings.currentGizmoOperation == ImGuizmo::OPERATION::TRANSLATE_Z;
71 }
72
73 [[nodiscard]] FORCE_INLINE bool IsRotationOperation(const TransformSettings& settings) {
74 return settings.currentGizmoOperation == ImGuizmo::OPERATION::ROTATE ||
75 settings.currentGizmoOperation == ImGuizmo::OPERATION::ROTATE_X ||
76 settings.currentGizmoOperation == ImGuizmo::OPERATION::ROTATE_Y ||
77 settings.currentGizmoOperation == ImGuizmo::OPERATION::ROTATE_Z;
78 }
79
80 [[nodiscard]] FORCE_INLINE bool IsScaleOperation(const TransformSettings& settings) {
81 return settings.currentGizmoOperation == ImGuizmo::OPERATION::SCALE ||
82 settings.currentGizmoOperation == ImGuizmo::OPERATION::SCALE_X ||
83 settings.currentGizmoOperation == ImGuizmo::OPERATION::SCALE_Y ||
84 settings.currentGizmoOperation == ImGuizmo::OPERATION::SCALE_Z;
85 }
86
87 class Gizmo {
89
90 public:
91 explicit Gizmo(Editor& parent, ImGuiContext* targetContext);
92 ~Gizmo();
93
94 [[nodiscard]] ImGuiContext& getContext() noexcept;
95 [[nodiscard]] const ImGuiContext& getContext() const noexcept;
96
97 [[nodiscard]] bool needsMouse() const;
98 [[nodiscard]] bool hovered() const noexcept;
99 void enable(bool state) noexcept;
100 [[nodiscard]] bool enabled() const noexcept;
101 [[nodiscard]] bool active() const noexcept;
102
103 void onMouseButton(bool pressed) noexcept;
104 [[nodiscard]] bool onKey(bool pressed, const Input::KeyEvent& key);
105
106 protected:
107 void update(U64 deltaTimeUS);
108 void render(const Camera* camera, const Rect<I32>& targetViewport, GFX::CommandBuffer& bufferInOut, GFX::MemoryBarrierCommand& memCmdInOut);
109 void renderSingleSelection(const Camera* camera);
110 void renderMultipleSelections(const Camera* camera);
111 void updateSelections(const vector<SceneGraphNode*>& nodes);
112 void setTransformSettings(const TransformSettings& settings) noexcept;
113 [[nodiscard]] const TransformSettings& getTransformSettings() const noexcept;
114 void onSceneFocus(bool state) noexcept;
115
116 private:
118 TransformComponent* tComp = nullptr;
120 };
121 void applyTransforms(const SelectedNode& node, const vec3<F32>& position, const vec3<Angle::DEGREES<F32>>& euler, const vec3<F32>& scale);
122
123 private:
125 bool _enabled = false;
126 bool _wasUsed = false;
129 ImGuiContext* _imguiContext = nullptr;
131
135 };
136
138
139 namespace Attorney {
141 static void render(Gizmo* gizmo, const Camera* camera, const Rect<I32>& targetViewport, GFX::CommandBuffer& bufferInOut, GFX::MemoryBarrierCommand& memCmdInOut) {
142 gizmo->render(camera, targetViewport, bufferInOut, memCmdInOut);
143 }
144
145 static void updateSelection(Gizmo* gizmo, const vector<SceneGraphNode*>& nodes) {
146 gizmo->updateSelections(nodes);
147 }
148
149 static void update(Gizmo* gizmo, const U64 deltaTimeUS) {
150 gizmo->update(deltaTimeUS);
151 }
152
153 static void setTransformSettings(Gizmo* gizmo, const TransformSettings& settings) noexcept {
154 gizmo->setTransformSettings(settings);
155 }
156
157 static const TransformSettings& getTransformSettings(const Gizmo* gizmo) noexcept {
158 return gizmo->getTransformSettings();
159 }
160
161 static void onSceneFocus(Gizmo* gizmo, const bool state) noexcept {
162 gizmo->onSceneFocus(state);
163 }
164 friend class Divide::Editor;
165 };
166 }
167} //namespace Divide
168
169#endif //DVD_EDITOR_GIZMO_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
#define FORCE_INLINE
static void updateSelection(Gizmo *gizmo, const vector< SceneGraphNode * > &nodes)
Definition: Gizmo.h:145
static void setTransformSettings(Gizmo *gizmo, const TransformSettings &settings) noexcept
Definition: Gizmo.h:153
static void render(Gizmo *gizmo, const Camera *camera, const Rect< I32 > &targetViewport, GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut)
Definition: Gizmo.h:141
static void onSceneFocus(Gizmo *gizmo, const bool state) noexcept
Definition: Gizmo.h:161
static void update(Gizmo *gizmo, const U64 deltaTimeUS)
Definition: Gizmo.h:149
static const TransformSettings & getTransformSettings(const Gizmo *gizmo) noexcept
Definition: Gizmo.h:157
mat4< F32 > _deltaMatrix
Definition: Gizmo.h:134
void applyTransforms(const SelectedNode &node, const vec3< F32 > &position, const vec3< Angle::DEGREES< F32 > > &euler, const vec3< F32 > &scale)
Definition: Gizmo.cpp:176
void onSceneFocus(bool state) noexcept
Definition: Gizmo.cpp:354
Editor & _parent
Definition: Gizmo.h:124
ImGuiContext & getContext() noexcept
Definition: Gizmo.cpp:77
bool active() const noexcept
Definition: Gizmo.cpp:99
bool needsMouse() const
Definition: Gizmo.cpp:440
void updateSelections(const vector< SceneGraphNode * > &nodes)
Definition: Gizmo.cpp:286
void renderSingleSelection(const Camera *camera)
Definition: Gizmo.cpp:198
bool onKey(bool pressed, const Input::KeyEvent &key)
Definition: Gizmo.cpp:362
bool _shouldRegisterUndo
Definition: Gizmo.h:127
bool _enabled
Definition: Gizmo.h:125
void render(const Camera *camera, const Rect< I32 > &targetViewport, GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut)
Definition: Gizmo.cpp:114
bool enabled() const noexcept
Definition: Gizmo.cpp:94
void update(U64 deltaTimeUS)
Definition: Gizmo.cpp:104
bool _wasUsed
Definition: Gizmo.h:126
void setTransformSettings(const TransformSettings &settings) noexcept
Definition: Gizmo.cpp:344
vector< SelectedNode > _selectedNodes
Definition: Gizmo.h:128
void enable(bool state) noexcept
Definition: Gizmo.cpp:89
TransformSettings _transformSettings
Definition: Gizmo.h:130
const TransformSettings & getTransformSettings() const noexcept
Definition: Gizmo.cpp:349
mat4< F32 > _workMatrix
Definition: Gizmo.h:132
void renderMultipleSelections(const Camera *camera)
Definition: Gizmo.cpp:228
void onMouseButton(bool pressed) noexcept
Definition: Gizmo.cpp:427
ImGuiContext * _imguiContext
Definition: Gizmo.h:129
bool hovered() const noexcept
Definition: Gizmo.cpp:454
mat4< F32 > _localToWorldMatrix
Definition: Gizmo.h:133
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
FORCE_INLINE bool IsRotationOperation(const TransformSettings &settings)
Definition: Gizmo.h:73
int32_t I32
FORCE_INLINE bool IsTranslationOperation(const TransformSettings &settings)
Definition: Gizmo.h:66
uint8_t U8
FORCE_INLINE bool IsScaleOperation(const TransformSettings &settings)
Definition: Gizmo.h:80
eastl::vector< Type > vector
Definition: Vector.h:42
Project & parent
Definition: DefaultScene.h:41
uint64_t U64
TransformValues _initialValues
Definition: Gizmo.h:119
U8 previousAxisSelected[3]
Definition: Gizmo.h:61
ImGuizmo::OPERATION currentGizmoOperation
Definition: Gizmo.h:56
ImGuizmo::MODE currentGizmoMode
Definition: Gizmo.h:57