-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHierarchyWindow.cpp
More file actions
173 lines (144 loc) · 5.21 KB
/
Copy pathHierarchyWindow.cpp
File metadata and controls
173 lines (144 loc) · 5.21 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
//
// Created by Alex on 12/22/2020.
//
#include "HierarchyWindow.hpp"
#include "../engine/core/Log.hpp"
#include "payloads/GameObjectPayload.hpp"
#include "EditorStats.hpp"
#include "../engine/core/Serializer.hpp"
using RPG::HierarchyWindow;
struct HierarchyWindow::Internal {
bool isOpened;
std::shared_ptr<RPG::Hierarchy> hierarchy;
std::string selectedGuid;
std::shared_ptr<RPG::GameObject> selectedGameObject;
RPG::Action<std::shared_ptr<RPG::GameObject>> selectedGameObjectAction = {};
Internal() : isOpened(true),
selectedGuid(""),
selectedGameObject(nullptr) {}
void Render(ImGuiID dockID) {
if (!isOpened) return;
ImGui::SetNextWindowDockID(dockID, ImGuiCond_FirstUseEver);
ImGui::Begin("Hierarchy", &isOpened);
for (std::shared_ptr<RPG::GameObject> gameObject : hierarchy->GetHierarchy()) {
RenderGameObject(gameObject);
}
if (ImGui::BeginPopupContextWindow("Context Menu")) {
if (ImGui::Button("New GameObject")) {
hierarchy->Add(std::make_unique<RPG::GameObject>(RPG::GameObject()));
ImGui::CloseCurrentPopup();
}
auto selected = RPG::EditorStats::GetInstance().GetSelectedGameObject();
if (selected != nullptr) {
std::string s = "Duplicate \"" + selected->GetName() + "\"";
if (ImGui::Button(s.c_str())) {
auto j = RPG::Serializer::GetInstance().SaveGameObject(selectedGameObject);
auto go = RPG::Serializer::GetInstance().LoadGameObject(j, true);
auto parent = selectedGameObject->GetParent();
if (parent != nullptr) {
go->SetParent(go, parent);
} else {
hierarchy->Add(go);
}
ImGui::CloseCurrentPopup();
}
}
ImGui::EndPopup();
}
ImGui::Separator();
//Drop - payload
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("GameObject")) {
IM_ASSERT(payload->DataSize == sizeof(RPG::GameObjectPayload));
RPG::Log("Hierarchy Window" , "Payload for a GameObject arrived");
RPG::GameObjectPayload p = *(const RPG::GameObjectPayload*)payload->Data;
if (!p.gameObject->HasParent()) {
hierarchy->Remove(p.gameObject);
}
p.gameObject->SetParent(p.gameObject, nullptr);
hierarchy->Add(p.gameObject);
}
ImGui::EndDragDropTarget();
}
ImGui::End();
}
void RenderGameObject(std::shared_ptr<RPG::GameObject> gameObject) {
bool isSelected = selectedGuid == gameObject->GetGuid();
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_None;
if (!gameObject->HasChildren()) {
flags |= ImGuiTreeNodeFlags_Leaf;
} else {
flags |= ImGuiTreeNodeFlags_OpenOnArrow;
}
//Selected goes here
if (isSelected) {
flags |= ImGuiTreeNodeFlags_Selected;
}
bool isOpen = ImGui::TreeNodeEx(gameObject->GetGuid().c_str(), flags, gameObject->GetName().c_str());
//Check click
if (ImGui::IsItemClicked() && (ImGui::GetMousePos().x - ImGui::GetItemRectMin().x) > ImGui::GetTreeNodeToLabelSpacing()) {
selectedGuid = isSelected ? "" : gameObject->GetGuid();
selectedGameObject = isSelected ? nullptr : gameObject;
//Fire off our selected GameObject
if (selectedGameObjectAction.GetListenerCount() > 0) {
selectedGameObjectAction.Invoke(selectedGameObject);
}
}
//Drag - payload
if (ImGui::BeginDragDropSource()) {
RPG::GameObjectPayload payload = RPG::GameObjectPayload();
payload.guid = gameObject->GetGuid();
payload.gameObject = gameObject;
ImGui::SetDragDropPayload("GameObject", &payload, sizeof(RPG::GameObjectPayload));
std::string str = "GameObject: " + gameObject->GetName();
ImGui::Text(str.c_str());
ImGui::EndDragDropSource();
}
//Drop - payload
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("GameObject")) {
IM_ASSERT(payload->DataSize == sizeof(RPG::GameObjectPayload));
RPG::Log("Hierarchy Window" , "Payload for a GameObject arrived");
RPG::GameObjectPayload p = *(const RPG::GameObjectPayload*)payload->Data;
if (gameObject->GetGuid() != p.guid) {
if (!p.gameObject->HasParent()) {
hierarchy->Remove(p.gameObject);
}
p.gameObject->SetParent(p.gameObject, gameObject);
}
}
ImGui::EndDragDropTarget();
}
//Render out children
if (isOpen) {
for (std::shared_ptr<RPG::GameObject> childGameObject : gameObject->GetChildren()) {
RenderGameObject(childGameObject);
}
ImGui::TreePop();
}
}
};
HierarchyWindow::HierarchyWindow() : internal(RPG::MakeInternalPointer<Internal>()) {}
void HierarchyWindow::Render(ImGuiID dockID) {
internal->Render(dockID);
}
RPG::Action<>::Callback HierarchyWindow::ToggleIsOpen() {
return [this] () {
internal->isOpened = !internal->isOpened;
};
}
RPG::Action<>::Func<bool> HierarchyWindow::IsOpen() {
return [this] () -> bool {
return internal->isOpened;
};
}
void HierarchyWindow::SetHierarchy(std::shared_ptr<RPG::Hierarchy> hierarchy) {
internal->hierarchy = hierarchy;
}
void HierarchyWindow::SetSelectedGameObject(std::shared_ptr<RPG::GameObject> gameObject) {
internal->selectedGameObject = gameObject;
internal->selectedGuid = gameObject == nullptr ? "" : gameObject->GetGuid();
}
void HierarchyWindow::SelectedGameObjectAction(RPG::Action<std::shared_ptr<RPG::GameObject>>::Callback callback) {
internal->selectedGameObjectAction += callback;
}