From 509201784d6525fc26345e55a56ab81e4a7616b3 Mon Sep 17 00:00:00 2001 From: hnOsmium0001 Date: Fri, 15 Apr 2022 20:30:39 -0700 Subject: Work on Material system --- source/EditorResources.cpp | 83 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 6 deletions(-) (limited to 'source/EditorResources.cpp') diff --git a/source/EditorResources.cpp b/source/EditorResources.cpp index c6d4d09..be7cefb 100644 --- a/source/EditorResources.cpp +++ b/source/EditorResources.cpp @@ -3,9 +3,18 @@ #include "EditorCore.hpp" #include "EditorNotification.hpp" #include "EditorUtils.hpp" +#include "Macros.hpp" +#include "ScopeGuard.hpp" #include "Shader.hpp" #include +#include +#include +#include +#include +#include + +using namespace std::literals; EditorContentBrowser::EditorContentBrowser(EditorInstance* editor) : mEditor{ editor } { @@ -37,6 +46,9 @@ void EditorContentBrowser::Show(bool* open) { if (ImGui::Selectable("Shaders", mPane == P_Shader)) { mPane = P_Shader; } + if (ImGui::Selectable("Materials", mPane == P_Material)) { + mPane = P_Material; + } } ImGui::EndChild(); @@ -49,23 +61,82 @@ void EditorContentBrowser::Show(bool* open) { } break; case P_Shader: { - // TODO reload shaders while keeping existing references working - // if (ImGui::Button("Reload from disk")) { - // ShaderManager::instance->DiscoverShaders(); - // } + if (ImGui::Button("Refresh")) { + // TODO reload shaders while keeping existing references working + } + ImGui::SameLine(); + if (ImGui::Button("Save all")) { + auto& shaders = ShaderManager::instance->GetShaders(); + for (auto&& [DISCARD, shader] : shaders) { + auto info = shader->GetInfo(); + if (info) { + info->SaveToFile(shader->GetDesignatedMetadataPath()); + } + } + } auto& shaders = ShaderManager::instance->GetShaders(); for (auto it = shaders.begin(); it != shaders.end(); ++it) { - auto name = it->first; auto shader = it->second.Get(); + auto& name = shader->GetName(); shader->GatherInfoIfAbsent(); auto details = shader->GetInfo(); bool selected = mEditor->GetSelectedItPtr() == shader; - if (ImGui::Selectable(name.data(), selected)) { + if (ImGui::Selectable(name.c_str(), selected)) { mEditor->SelectIt(shader, EditorInstance::ITT_Shader); } + + if (ImGui::BeginDragDropSource()) { + // Reason: intentionally using pointer as Fpayload + ImGui::SetDragDropPayload(BRUSSEL_DRAG_DROP_SHADER, &shader, sizeof(shader)); // NOLINT(bugprone-sizeof-expression) + ImGui::Text("Shader '%s'", name.c_str()); + ImGui::EndDragDropSource(); + } + } + } break; + + case P_Material: { + if (ImGui::Button("New")) { + int n = std::rand(); + auto mat = new Material(nullptr, "Unnamed Material " + std::to_string(n)); + auto guard = GuardDeletion(mat); + auto [DISCARD, inserted] = MaterialManager::instance->SaveMaterial(mat); + if (inserted) { + guard.Dismiss(); + } else { + ImGui::AddNotification(ImGuiToast(ImGuiToastType_Error, "Failed to create material.")); + } + } + ImGui::SameLine(); + if (ImGui::Button("Refresh")) { + // TODO + } + ImGui::SameLine(); + if (ImGui::Button("Save all")) { + auto& mats = MaterialManager::instance->GetMaterials(); + for (auto&& [DISCARD, mat] : mats) { + mat->SaveToFile(mat->GetDesignatedPath()); + } + } + + auto& mats = MaterialManager::instance->GetMaterials(); + for (auto it = mats.begin(); it != mats.end(); ++it) { + auto mat = it->second.Get(); + auto& name = mat->GetName(); + + bool selected = mEditor->GetSelectedItPtr() == mat; + if (ImGui::Selectable(name.c_str(), selected)) { + mEditor->SelectIt(mat, EditorInstance::ITT_Material); + } + + if (ImGui::BeginDragDropSource()) { + // Reason: intentionally using pointer as payload + ImGui::SetDragDropPayload(BRUSSEL_DRAG_DROP_MATERIAL, &mat, sizeof(mat)); // NOLINT(bugprone-sizeof-expression) + ImGui::Text("Material '%s'", name.c_str()); + ImGui::EndDragDropSource(); + } } } break; } -- cgit v1.2.3-70-g09d2