-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneWindow.cpp
More file actions
176 lines (137 loc) · 5.54 KB
/
Copy pathSceneWindow.cpp
File metadata and controls
176 lines (137 loc) · 5.54 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
//
// Created by Alex on 12/20/2020.
//
#include "SceneWindow.hpp"
#include "../engine/application/ApplicationStats.hpp"
#include "ImGuizmoWrapper.hpp"
#include "../engine/core/SceneManager.hpp"
#include "../engine/core/GLMWrapper.hpp"
#include "EditorStats.hpp"
#include "../engine/core/Log.hpp"
#include "payloads/ModelPayload.hpp"
#include "payloads/GeneralPayload.hpp"
#include "../engine/core/Input/InputManager.hpp"
#include "EditorManager.hpp"
using RPG::SceneWindow;
struct SceneWindow::Internal {
uint32_t frameBufferID;
bool isOpened = true;
glm::mat4 identity;
std::shared_ptr<RPG::GameObject> selectedGameObject;
RPG::Action<std::shared_ptr<RPG::GameObject>> selectedGameObjectAction = {};
Internal() : identity(glm::mat4{1.0f}),
selectedGameObject(nullptr) {}
void Render(ImGuiID dockID) {
if (!isOpened) return;
ImGui::SetNextWindowDockID(dockID, ImGuiCond_FirstUseEver);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::Begin("Scene", &isOpened);
auto windowPosition = ImGui::GetWindowPos();
auto windowSize = ImGui::GetWindowSize();
RPG::EditorStats::GetInstance().SetSceneScreenRect(glm::vec4{windowPosition.x, windowPosition.y, windowSize.x, windowSize.y});
ImVec2 size = ImGui::GetContentRegionAvail();
ImVec2 contentSize = CalculateContentSize(size);
ImGui::Image((void *) (intptr_t) frameBufferID, contentSize, ImVec2{0, 1}, ImVec2{1, 0});
std::shared_ptr<RPG::CameraComponent> camera = RPG::SceneManager::GetInstance().GetCurrentScene()->GetCamera();
int gizmoTool = RPG::EditorStats::GetInstance().GetGizmoTool();
// Yellow is content region min/max
ImVec2 vMin = ImGui::GetWindowContentRegionMin();
ImVec2 vMax;
vMin.x += ImGui::GetWindowPos().x;
vMin.y += ImGui::GetWindowPos().y;
vMax.x = vMin.x + contentSize.x;
vMax.y = vMin.y + contentSize.y;
//For Testing
//ImGui::GetForegroundDrawList()->AddRect( vMin, vMax, IM_COL32( 255, 255, 0, 255 ) );
ImGuizmo::SetOrthographic(false);
ImGuizmo::SetDrawlist();
ImGuizmo::SetRect(vMin.x, vMin.y, contentSize.x, contentSize.y);
if (selectedGameObject != nullptr && gizmoTool != -1) {
auto transform = selectedGameObject->GetTransform();
glm::mat4 modelMatrix = transform->GetTransformMatrix();
bool snap = RPG::InputManager::GetInstance().IsKeyDown(RPG::Input::Key::LeftAlt);
float snapValue = 0.5f;
if (gizmoTool == ImGuizmo::OPERATION::ROTATE) {
snapValue = 45.0f;
}
float snapValues[3] = { snapValue, snapValue, snapValue };
float tmpMatrix[16];
ImGuizmo::RecomposeMatrixFromComponents(glm::value_ptr(transform->GetPosition()), glm::value_ptr(transform->GetRotation()), glm::value_ptr(transform->GetScale()), tmpMatrix);
ImGuizmo::Manipulate(glm::value_ptr(camera->GetViewMatrix()), glm::value_ptr(camera->GetProjectionMatrix()), (ImGuizmo::OPERATION)gizmoTool, ImGuizmo::MODE::LOCAL, tmpMatrix);
if (ImGuizmo::IsUsing()) {
auto transform = selectedGameObject->GetTransform();
float matrixTranslation[3], matrixRotation[3], matrixScale[3];
ImGuizmo::DecomposeMatrixToComponents(tmpMatrix, matrixTranslation, matrixRotation, matrixScale);
switch (gizmoTool) {
case ImGuizmo::OPERATION::TRANSLATE:
transform->SetPosition(glm::vec3{matrixTranslation[0], matrixTranslation[1], matrixTranslation[2]});
break;
case ImGuizmo::OPERATION::ROTATE:
transform->SetRotation(glm::vec3{matrixRotation[0], matrixRotation[1], matrixRotation[2]});
break;
case ImGuizmo::OPERATION::SCALE:
transform->SetScale(glm::vec3{matrixScale[0], matrixScale[1], matrixScale[2]});
break;
default:
break;
}
}
}
//Drop - payload Model
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("Scene")) {
RPG::GeneralPayload p = RPG::EditorStats::GetInstance().GetPayload();
RPG::Log("Payload", p.path);
RPG::SceneManager::GetInstance().LoadScene(p.path);
//Set the current inspector selected back to null
selectedGameObject = nullptr;
if (selectedGameObjectAction.GetListenerCount() > 0) {
selectedGameObjectAction.Invoke(nullptr);
}
}
ImGui::EndDragDropTarget();
}
ImGui::End();
ImGui::PopStyleVar();
}
ImVec2 CalculateContentSize(ImVec2 availableSize) {
auto size = RPG::ApplicationStats::GetInstance().GetRenderingSize();
ImVec2 frameBufferSize{size.x, size.y};
float width = frameBufferSize.x;
float height = frameBufferSize.y;
float ratio = frameBufferSize.x / frameBufferSize.y;
float reciprocal = 1 / ratio;
height = availableSize.x * reciprocal;
if (height > availableSize.y) {
height = availableSize.y;
}
width = height * ratio;
return ImVec2{width, height};
}
void SetFrameBuffer(uint32_t id) {
frameBufferID = id;
}
};
SceneWindow::SceneWindow() : internal(RPG::MakeInternalPointer<Internal>()) {}
void SceneWindow::Render(ImGuiID dockID) {
internal->Render(dockID);
}
RPG::Action<>::Callback SceneWindow::ToggleIsOpen() {
return [this] () {
internal->isOpened = !internal->isOpened;
};
}
RPG::Action<>::Func<bool> SceneWindow::IsOpen() {
return [this] () -> bool {
return internal->isOpened;
};
}
void SceneWindow::SetFrameBuffer(uint32_t frameBufferID) {
internal->SetFrameBuffer(frameBufferID);
}
void SceneWindow::SetSelectedGameObject(std::shared_ptr<RPG::GameObject> gameObject) {
internal->selectedGameObject = gameObject;
}
void SceneWindow::SelectedGameObjectAction(RPG::Action<std::shared_ptr<RPG::GameObject>>::Callback callback) {
internal->selectedGameObjectAction += callback;
}