-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaterialMakerWindow.cpp
More file actions
241 lines (209 loc) · 10.6 KB
/
Copy pathMaterialMakerWindow.cpp
File metadata and controls
241 lines (209 loc) · 10.6 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// Created by Alex on 7/29/2021.
//
#include "MaterialMakerWindow.hpp"
#include "nlohmann/json.hpp"
#include "../engine/core/components/MeshComponent.hpp"
#include "../engine/core/Property.hpp"
#include "payloads/GeneralPayload.hpp"
#include "EditorStats.hpp"
#include "../engine/core/Serializer.hpp"
#include "../engine/core/Assets.hpp"
#include "../engine/core/Content.hpp"
using json = nlohmann::json;
using RPG::MaterialMakerWindow;
struct MaterialMakerWindow::Internal {
bool isOpened;
std::shared_ptr<RPG::OpenGLAssetManager> assetManager;
std::shared_ptr<RPG::Property> property;
std::string newMaterialName;
std::shared_ptr<RPG::FrameBuffer> frameBuffer;
std::shared_ptr<RPG::Material> material;
std::vector<std::shared_ptr<RPG::Property>> properties;
Internal(std::shared_ptr<RPG::OpenGLAssetManager> assetManager) : isOpened(true), assetManager(assetManager),
property(std::make_unique<RPG::Property>(std::string(""), "Material", "RPG::Resource::String", true, "Material")) {}
void SubmitFramebuffer(std::shared_ptr<RPG::FrameBuffer> frameBuffer) {
this->frameBuffer = frameBuffer;
}
void Render(ImGuiID dockID) {
if (!isOpened) return;
ImGui::SetNextWindowDockID(dockID, ImGuiCond_FirstUseEver);
ImGui::Begin("Material Maker", &isOpened);
std::any prop = property->GetProperty();
std::string v = std::any_cast<std::string>(prop);
bool hasMaterialLoaded = v.length() > 0;
if (!hasMaterialLoaded) {
ImGui::InputText("##MaterialName", &newMaterialName);
ImGui::SameLine();
if (newMaterialName.length() == 0) {
ImGui::Text("Create");
} else {
if (ImGui::Button("Create")) {
std::string path = "assets/materials/" + newMaterialName + ".mat";
material = std::make_unique<RPG::Material>(newMaterialName, 0, "default");
RPG::Serializer::GetInstance().SaveMaterial(material, path);
newMaterialName = "";
//Set and reload the property so we can display the material to the user
property->SetProperty(std::string(path));
prop = property->GetProperty();
v = std::any_cast<std::string>(prop);
auto shaderDefinitionFile = RPG::Assets::LoadTextFile("assets/shaders/definitions/" + material->GetShader() + ".shader");
GrabProperties(shaderDefinitionFile);
material->SetProperties(properties);
}
}
} else {
if (ImGui::Button("Unload")) {
newMaterialName = "";
property->SetProperty(std::string(""));
material = nullptr;
properties.clear();
}
//TODO: Render material in a scene
}
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.75, 0.75, 0.75, 1));
std::string id = "##" + property->GetGuid();
ImGui::InputText(id.c_str(), &v, ImGuiInputTextFlags_ReadOnly);
ImGui::PopStyleColor();
if (ImGui::IsItemHovered() && hasMaterialLoaded) {
ImGui::BeginTooltip();
ImGui::Text(v.c_str());
ImGui::EndTooltip();
}
//Dropable Payloads
if (property->AllowsDragAndDrop()) {
//Drop - payload
if (ImGui::BeginDragDropTarget()) {
if (ImGui::AcceptDragDropPayload(property->DragAndDropTag().c_str())) {
RPG::GeneralPayload p = RPG::EditorStats::GetInstance().GetPayload();
RPG::Log("Payload", p.path);
property->SetProperty(p.path);
material = assetManager->GetMaterial(p.path);// std::make_unique<RPG::Material>(RPG::Assets::LoadMaterial(p.path));
properties = material->GetProperties();
//Save this for when we actually change the Shader file
/*//Load Shader Definition file
auto shaderDefinitionFile = RPG::Assets::LoadTextFile("assets/shaders/definitions/" + material->GetShader() + ".shader");
GrabProperties(shaderDefinitionFile);*/
}
ImGui::EndDragDropTarget();
}
}
ImGui::SameLine();
ImGui::Text(property->GetName().c_str(), property->GetEditorName().c_str());
if (hasMaterialLoaded && material != nullptr && material->GetName() != "") {
ImGui::Image((void *) (intptr_t) frameBuffer->GetRenderTextureID(), ImVec2{16 * 10, 9 * 10}, ImVec2{0, 1}, ImVec2{1, 0});
int renderQueue = material->GetRenderQueue();
ImGui::InputInt("Render Queue", &renderQueue);
material->SetRenderQueue(renderQueue);
auto shader = material->GetShader();
ImGui::InputText("##MaterialName", &shader);
material->SetShader(shader);
ImGui::SameLine();
if (ImGui::Button("Reload")) {
properties.clear();
auto shaderDefinitionFile = RPG::Assets::LoadTextFile("assets/shaders/definitions/" + material->GetShader() + ".shader");
GrabProperties(shaderDefinitionFile);
material->SetProperties(properties);
}
if (properties.size() > 0) {
if (ImGui::CollapsingHeader("Properties", true)) {
for (auto p : properties) {
//RPG::Log("Material Window", p->GetType());
std::any prop = p->GetProperty();
if (p->GetType() == "float") {
float v = std::any_cast<float>(prop);
if (ImGui::DragFloat(p->GetEditorName().c_str(), (float *) &v, 0.05f)) {
p->SetProperty(v);
}
} else if (p->GetType() == "glm::vec2") {
glm::vec2 v = std::any_cast<glm::vec2>(prop);
if (ImGui::DragFloat2(p->GetEditorName().c_str(), (float *) &v, 0.05f)) {
p->SetProperty(v);
}
} else if (p->GetType() == "glm::vec3") {
glm::vec3 v = std::any_cast<glm::vec3>(prop);
if (ImGui::DragFloat3(p->GetEditorName().c_str(), (float *) &v, 0.05f)) {
p->SetProperty(v);
}
} else if (p->GetType() == "glm::vec4") {
glm::vec4 v = std::any_cast<glm::vec4>(prop);
if (ImGui::ColorPicker4(p->GetEditorName().c_str(), (float *) &v, 0.05f)) {
p->SetProperty(v);
}
} else if (p->GetType() == "RPG::Resource::String") {
std::any prop = p->GetProperty();
std::string v = std::any_cast<std::string>(prop);
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.75, 0.75, 0.75, 1));
std::string id = "##" + p->GetGuid();
ImGui::InputText(id.c_str(), &v, ImGuiInputTextFlags_ReadOnly);
ImGui::PopStyleColor();
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text(v.c_str());
ImGui::EndTooltip();
}
//Dropable Payloads
if (p->AllowsDragAndDrop()) {
//Drop - payload
if (ImGui::BeginDragDropTarget()) {
if (ImGui::AcceptDragDropPayload(p->DragAndDropTag().c_str())) {
RPG::GeneralPayload generalPayload = RPG::EditorStats::GetInstance().GetPayload();
RPG::Log("Payload", generalPayload.path);
p->SetProperty(generalPayload.path);
}
ImGui::EndDragDropTarget();
}
}
ImGui::SameLine();
ImGui::Text(p->GetName().c_str(), p->GetEditorName().c_str());
if (p->DragAndDropTag() == "Texture") {
std::string texturePath = std::any_cast<std::string>(p->GetProperty());
uint32_t id = RPG::Content::GetInstance().GetTextureID(texturePath);
if (id != -1) {
ImGui::Image((void *) (intptr_t) id, ImVec2{50,50}, ImVec2{0, 1}, ImVec2{1, 0}, ImVec4{1,1,1,1}, ImVec4(1,1,1,1));
}
}
}
}
}
}
ImGui::Spacing();
if (ImGui::Button("Save")) {
material->SetProperties(properties);
RPG::Serializer::GetInstance().SaveMaterial(material, std::any_cast<std::string>(property->GetProperty()));
}
}
ImGui::End();
}
void GrabProperties(std::string data) {
properties.clear();
json j = json::parse(data);
for (auto [key, value] : j["Properties"].items()) {
auto property = RPG::Assets::LoadProperty(value);
if (property != nullptr) {
properties.push_back(property);
}
}
RPG::Log("Material Maker", "Was able to load (" + std::to_string(properties.size()) + ") properties");
}
};
MaterialMakerWindow::MaterialMakerWindow(std::shared_ptr<RPG::OpenGLAssetManager> assetManager) : internal(RPG::MakeInternalPointer<Internal>(assetManager)) {}
void MaterialMakerWindow::Render(ImGuiID dockID) {
internal->Render(dockID);
}
RPG::Action<>::Callback MaterialMakerWindow::ToggleIsOpen() {
return [this] () {
internal->isOpened = !internal->isOpened;
};
}
RPG::Action<>::Func<bool> MaterialMakerWindow::IsOpen() {
return [this] () -> bool {
return internal->isOpened;
};
}
void MaterialMakerWindow::SubmitFramebuffer(std::shared_ptr<RPG::FrameBuffer> frameBuffer) {
internal->SubmitFramebuffer(frameBuffer);
}
std::shared_ptr<RPG::Material> MaterialMakerWindow::GetCurrentMaterial() {
return internal->material;
}