diff --git a/engine/src/cubos/engine/tools/palette_editor/plugin.cpp b/engine/src/cubos/engine/tools/palette_editor/plugin.cpp index c785016b53..5f4db4a224 100644 --- a/engine/src/cubos/engine/tools/palette_editor/plugin.cpp +++ b/engine/src/cubos/engine/tools/palette_editor/plugin.cpp @@ -1,27 +1,32 @@ +#include + #include #include -#include #include #include #include #include -#include #include -#include "imgui.h" - using cubos::core::data::old::Debug; +using cubos::core::ecs::EventReader; using cubos::core::ecs::Read; using cubos::core::ecs::Write; using namespace cubos::engine; +using tools::AssetSelectedEvent; struct SelectedPaletteInfo { - VoxelPalette palette; + Asset paletteAsset; + VoxelPalette paletteData; // so we dont local copy every frame, but only when palette is selected + Asset assetLoad; + bool active; + bool modified; + std::pair modifiedMaterials; }; -static void checkAssetEventSystem(cubos::core::ecs::EventReader reader, Read assets, +static void checkAssetEventSystem(EventReader reader, Read assets, Write selectedPalette) { for (const auto& event : reader) @@ -30,38 +35,67 @@ static void checkAssetEventSystem(cubos::core::ecs::EventReader paletteAsset = AnyAsset(event.asset); - selectedPalette->palette = assets->read(paletteAsset).get(); + if (selectedPalette->paletteAsset.getId() == paletteAsset.getId()) + { + // FIXME: if user clicks on same palette 2 times this runs which shouldnt + selectedPalette->active = true; + } + else + { + selectedPalette->active = false; + } + selectedPalette->paletteAsset = paletteAsset; + selectedPalette->assetLoad = assets->load(event.asset); + selectedPalette->paletteData = assets->read(selectedPalette->assetLoad).get(); } } } -static void paletteEditor(Write renderer, Write selectedPalette) +static void paletteEditorSystem(Write assets, Write renderer, + Write selectedPalette) { - auto& palette = selectedPalette->palette; - - if (palette.data() == nullptr) + if (selectedPalette->paletteAsset.isNull() || selectedPalette->paletteData.data() == nullptr) { - return; // no data on the palette yet! + return; } ImGui::Begin("Palette Editor"); - for (uint16_t materialIndex = 1; materialIndex < palette.size(); ++materialIndex) + if (selectedPalette->active == true) { - const VoxelMaterial& material = palette.get(materialIndex); + ImGui::TextColored({0, 255, 0, 255}, "This is your current active palette!"); + } + + for (uint16_t i = 0; i < selectedPalette->paletteData.size(); ++i) + { + const uint16_t materialIndex = i + 1; + VoxelMaterial& material = (VoxelMaterial&)selectedPalette->paletteData.get(materialIndex); std::string label = "Material " + std::to_string(materialIndex); - if (ImGui::ColorEdit4(label.c_str(), (float*)&material.color.r)) + if (ImGui::ColorEdit4(label.c_str(), &material.color.r)) { - VoxelMaterial newMaterial = material; - palette.set(materialIndex, newMaterial); + CUBOS_INFO("Modified material ..."); + selectedPalette->modifiedMaterials = std::pair(materialIndex, material); // no need for a vector right? + selectedPalette->modified = true; } } - if (ImGui::Button("Set this Palette as default")) + if (selectedPalette->modified) + { + CUBOS_INFO("Storing as new asset because palette was modified ..."); + auto writePaletteAsset = assets->write(selectedPalette->assetLoad); + auto [modifiedMaterialIndex, modifiedMaterial] = selectedPalette->modifiedMaterials; + writePaletteAsset->set(modifiedMaterialIndex, modifiedMaterial); + // assets->store(assetLoad, writePaletteAsset.get()); // FIXME: crash + selectedPalette->modified = false; + } + + if (ImGui::Button("Make Active")) { - CUBOS_INFO("New palette set: {}", Debug(palette)); - (*renderer)->setPalette(palette); + CUBOS_INFO("New palette set: {}", Debug(selectedPalette->paletteData)); + (*renderer)->setPalette(selectedPalette->paletteData); + selectedPalette->active = true; + assets->save(selectedPalette->assetLoad); // FIXME: read only } ImGui::End(); @@ -69,11 +103,12 @@ static void paletteEditor(Write renderer, Write s void cubos::engine::tools::paletteEditorPlugin(Cubos& cubos) { + cubos.addPlugin(rendererPlugin); cubos.addPlugin(imguiPlugin); cubos.addPlugin(voxelsPlugin); cubos.addResource(); cubos.system(checkAssetEventSystem); - cubos.system(paletteEditor).tagged("cubos.imgui").after("cubos.renderer.init"); + cubos.system(paletteEditorSystem).tagged("cubos.imgui"); } diff --git a/tools/tesseratos/assets/main_copy.pal b/tools/tesseratos/assets/main_copy.pal new file mode 100644 index 0000000000..00b68f858f Binary files /dev/null and b/tools/tesseratos/assets/main_copy.pal differ diff --git a/tools/tesseratos/assets/main_copy.pal.meta b/tools/tesseratos/assets/main_copy.pal.meta new file mode 100644 index 0000000000..29583c01d2 --- /dev/null +++ b/tools/tesseratos/assets/main_copy.pal.meta @@ -0,0 +1,3 @@ +{ + "id": "6f42ae5a-59d1-5df3-1720-83b8df6dd536" +} \ No newline at end of file