Skip to content

Commit

Permalink
paletteEditorSystem improvements, still some todos and fixmes
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Oct 5, 2023
1 parent 9e9d2a6 commit 569acb6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 21 deletions.
77 changes: 56 additions & 21 deletions engine/src/cubos/engine/tools/palette_editor/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
#include <imgui.h>

#include <cubos/core/data/old/debug_serializer.hpp>
#include <cubos/core/log.hpp>

#include <cubos/engine/assets/asset.hpp>
#include <cubos/engine/imgui/plugin.hpp>
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/tools/asset_explorer/plugin.hpp>
#include <cubos/engine/tools/palette_editor/plugin.hpp>
#include <cubos/engine/voxels/palette.hpp>
#include <cubos/engine/voxels/plugin.hpp>

#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<VoxelPalette> paletteAsset;
VoxelPalette paletteData; // so we dont local copy every frame, but only when palette is selected
Asset<VoxelPalette> assetLoad;
bool active;
bool modified;
std::pair<uint16_t, VoxelMaterial> modifiedMaterials;
};

static void checkAssetEventSystem(cubos::core::ecs::EventReader<tools::AssetSelectedEvent> reader, Read<Assets> assets,
static void checkAssetEventSystem(EventReader<AssetSelectedEvent> reader, Read<Assets> assets,
Write<SelectedPaletteInfo> selectedPalette)
{
for (const auto& event : reader)
Expand All @@ -30,50 +35,80 @@ static void checkAssetEventSystem(cubos::core::ecs::EventReader<tools::AssetSele
{
CUBOS_INFO("Opening Palette Editor for {} ...", Debug(event.asset));
const Asset<VoxelPalette> 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> renderer, Write<SelectedPaletteInfo> selectedPalette)
static void paletteEditorSystem(Write<Assets> assets, Write<Renderer> renderer,
Write<SelectedPaletteInfo> 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();
}

void cubos::engine::tools::paletteEditorPlugin(Cubos& cubos)
{
cubos.addPlugin(rendererPlugin);
cubos.addPlugin(imguiPlugin);
cubos.addPlugin(voxelsPlugin);

cubos.addResource<SelectedPaletteInfo>();

cubos.system(checkAssetEventSystem);
cubos.system(paletteEditor).tagged("cubos.imgui").after("cubos.renderer.init");
cubos.system(paletteEditorSystem).tagged("cubos.imgui");
}
Binary file added tools/tesseratos/assets/main_copy.pal
Binary file not shown.
3 changes: 3 additions & 0 deletions tools/tesseratos/assets/main_copy.pal.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "6f42ae5a-59d1-5df3-1720-83b8df6dd536"
}

0 comments on commit 569acb6

Please sign in to comment.