From c182413d9e9a323beb105e7d393190ea86d31508 Mon Sep 17 00:00:00 2001 From: roby2014 Date: Sun, 15 Oct 2023 19:49:29 +0100 Subject: [PATCH] address requested changes --- .../tools/voxel_palette_editor/plugin.hpp | 2 +- .../tools/voxel_palette_editor/plugin.cpp | 64 ++++++++++--------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/engine/include/cubos/engine/tools/voxel_palette_editor/plugin.hpp b/engine/include/cubos/engine/tools/voxel_palette_editor/plugin.hpp index e5fec8d251..e7baf52b1d 100644 --- a/engine/include/cubos/engine/tools/voxel_palette_editor/plugin.hpp +++ b/engine/include/cubos/engine/tools/voxel_palette_editor/plugin.hpp @@ -17,6 +17,6 @@ namespace cubos::engine::tools /// @brief Plugin entry function. /// @param cubos @b CUBOS. main class - /// @ingroup palette-editor-tool-plugin + /// @ingroup voxel-palette-editor-tool-plugin void voxelPaletteEditorPlugin(Cubos& cubos); } // namespace cubos::engine::tools diff --git a/engine/src/cubos/engine/tools/voxel_palette_editor/plugin.cpp b/engine/src/cubos/engine/tools/voxel_palette_editor/plugin.cpp index 32e16c3e9d..059432be2c 100644 --- a/engine/src/cubos/engine/tools/voxel_palette_editor/plugin.cpp +++ b/engine/src/cubos/engine/tools/voxel_palette_editor/plugin.cpp @@ -22,37 +22,37 @@ using tools::AssetSelectedEvent; struct SelectedPaletteInfo { - Asset paletteAsset; + Asset asset; VoxelPalette paletteCopy; - AnyAsset selectedPaletteAnyAsset; - std::pair modifiedMaterial; bool modified; + Asset next; }; static void savePaletteUiGuard(Write assets, Write selectedPalette) { - if (selectedPalette->selectedPaletteAnyAsset.isNull()) + // The popup only shows when we've already opened a palette and want to show another one. + // Thus if we haven't set the next asset variable we can just stop here. + if (selectedPalette->next.isNull()) { return; } - bool optionSelected = false; - ImVec2 center = ImGui::GetMainViewport()->GetCenter(); ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5F, 0.5F)); if (ImGui::BeginPopupModal("Save Palette?", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { + bool optionSelected = false; + ImGui::Text("Do you want to save the modified palette?"); ImGui::Separator(); if (ImGui::Button("Yes", ImVec2(120, 0))) { - CUBOS_INFO("Saving palette asset {} ...", Debug(selectedPalette->selectedPaletteAnyAsset)); - assets->store(selectedPalette->paletteAsset, selectedPalette->paletteCopy); - assets->save(selectedPalette->paletteAsset); + CUBOS_INFO("Saving palette asset {} modificaations", Debug(selectedPalette->next)); + assets->store(selectedPalette->asset, selectedPalette->paletteCopy); + assets->save(selectedPalette->asset); optionSelected = true; - ImGui::CloseCurrentPopup(); } ImGui::SetItemDefaultFocus(); @@ -60,16 +60,16 @@ static void savePaletteUiGuard(Write assets, Write if (ImGui::Button("No", ImVec2(120, 0))) { - CUBOS_DEBUG("NOT saving palette asset {} ...", Debug(selectedPalette->selectedPaletteAnyAsset)); + CUBOS_DEBUG("Discarding palette asset {} modifications", Debug(selectedPalette->next)); optionSelected = true; - ImGui::CloseCurrentPopup(); } if (optionSelected) { - selectedPalette->paletteAsset = assets->load(selectedPalette->selectedPaletteAnyAsset); - selectedPalette->paletteCopy = assets->read(selectedPalette->paletteAsset).get(); + selectedPalette->asset = assets->load(selectedPalette->next); + selectedPalette->paletteCopy = assets->read(selectedPalette->asset).get(); selectedPalette->modified = false; + ImGui::CloseCurrentPopup(); } ImGui::EndPopup(); @@ -83,17 +83,17 @@ static void checkAssetEventSystem(EventReader reader, Write< { if (assets->type(event.asset) == typeid(VoxelPalette)) { - CUBOS_INFO("Loading palette asset {} ...", Debug(event.asset)); - if (!selectedPalette->paletteAsset.isNull() && selectedPalette->modified) + CUBOS_INFO("Opening palette asset {}", Debug(event.asset)); + if (!selectedPalette->asset.isNull() && selectedPalette->modified) { - CUBOS_DEBUG("Opening save palette ui guard ..."); + CUBOS_DEBUG("Opening save palette UI guard"); ImGui::OpenPopup("Save Palette?"); - selectedPalette->selectedPaletteAnyAsset = event.asset; + selectedPalette->next = event.asset; } else { - selectedPalette->paletteAsset = assets->load(event.asset); - selectedPalette->paletteCopy = assets->read(selectedPalette->paletteAsset).get(); + selectedPalette->asset = assets->load(event.asset); + selectedPalette->paletteCopy = assets->read(selectedPalette->asset).get(); } } } @@ -105,14 +105,15 @@ static void checkAssetEventSystem(EventReader reader, Write< static void voxelPaletteEditorSystem(Write assets, Write renderer, Write selectedPalette) { - if (assets->status(selectedPalette->paletteAsset) != Assets::Status::Loaded) + if (assets->status(selectedPalette->asset) != Assets::Status::Loaded) { return; } ImGui::Begin("Palette Editor"); - bool materialModified = false; + bool wasMaterialModified = false; + std::pair modifiedMaterial; for (uint16_t i = 0; i < selectedPalette->paletteCopy.size(); ++i) { @@ -122,17 +123,17 @@ static void voxelPaletteEditorSystem(Write assets, Write rende std::string label = "Material " + std::to_string(materialIndex); if (ImGui::ColorEdit4(label.c_str(), &material.color.r)) { - CUBOS_DEBUG("Modified material ..."); - selectedPalette->modifiedMaterial = std::pair(materialIndex, material); - materialModified = true; + CUBOS_DEBUG("Modified material"); + modifiedMaterial = std::pair(materialIndex, material); + wasMaterialModified = true; } } - if (materialModified) + if (wasMaterialModified) { - CUBOS_DEBUG("Storing as new asset because palette was modified ..."); - auto [modifiedMaterialIndex, modifiedMaterial] = selectedPalette->modifiedMaterial; - selectedPalette->paletteCopy.set(modifiedMaterialIndex, modifiedMaterial); + CUBOS_DEBUG("Storing as new asset because palette was modified"); + auto [idx, material] = modifiedMaterial; + selectedPalette->paletteCopy.set(idx, material); selectedPalette->modified = true; } @@ -156,8 +157,8 @@ static void voxelPaletteEditorSystem(Write assets, Write rende if (ImGui::Button("Save")) { - assets->store(selectedPalette->paletteAsset, selectedPalette->paletteCopy); - assets->save(selectedPalette->paletteAsset); + assets->store(selectedPalette->asset, selectedPalette->paletteCopy); + assets->save(selectedPalette->asset); selectedPalette->modified = false; } @@ -168,6 +169,7 @@ void cubos::engine::tools::voxelPaletteEditorPlugin(Cubos& cubos) { cubos.addPlugin(rendererPlugin); cubos.addPlugin(imguiPlugin); + cubos.addPlugin(assetExplorerPlugin); cubos.addPlugin(voxelsPlugin); cubos.addResource();