Skip to content

Commit

Permalink
address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Oct 15, 2023
1 parent 54b9a64 commit c182413
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
64 changes: 33 additions & 31 deletions engine/src/cubos/engine/tools/voxel_palette_editor/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,54 @@ using tools::AssetSelectedEvent;

struct SelectedPaletteInfo
{
Asset<VoxelPalette> paletteAsset;
Asset<VoxelPalette> asset;
VoxelPalette paletteCopy;
AnyAsset selectedPaletteAnyAsset;
std::pair<uint16_t, VoxelMaterial> modifiedMaterial;
bool modified;
Asset<VoxelPalette> next;
};

static void savePaletteUiGuard(Write<Assets> assets, Write<SelectedPaletteInfo> 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();
ImGui::SameLine();

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();
Expand All @@ -83,17 +83,17 @@ static void checkAssetEventSystem(EventReader<AssetSelectedEvent> 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();
}
}
}
Expand All @@ -105,14 +105,15 @@ static void checkAssetEventSystem(EventReader<AssetSelectedEvent> reader, Write<
static void voxelPaletteEditorSystem(Write<Assets> assets, Write<Renderer> renderer,
Write<SelectedPaletteInfo> 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<uint16_t, VoxelMaterial> modifiedMaterial;

for (uint16_t i = 0; i < selectedPalette->paletteCopy.size(); ++i)
{
Expand All @@ -122,17 +123,17 @@ static void voxelPaletteEditorSystem(Write<Assets> assets, Write<Renderer> 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;
}

Expand All @@ -156,8 +157,8 @@ static void voxelPaletteEditorSystem(Write<Assets> assets, Write<Renderer> 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;
}

Expand All @@ -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<SelectedPaletteInfo>();
Expand Down

0 comments on commit c182413

Please sign in to comment.