[nostalgia] Split PaletteEditor into Imgui and general files, other cleanup
This commit is contained in:
@@ -2,318 +2,104 @@
|
||||
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <nostalgia/core/gfx.hpp>
|
||||
#include <nostalgia/core/media.hpp>
|
||||
#include <ox/std/memory.hpp>
|
||||
|
||||
#include "paletteeditor.hpp"
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
enum class PaletteEditorCommandId {
|
||||
AddColor,
|
||||
RemoveColor,
|
||||
UpdateColor,
|
||||
MoveColor,
|
||||
};
|
||||
|
||||
|
||||
class AddColorCommand: public studio::UndoCommand {
|
||||
private:
|
||||
PaletteEditorImGui *m_editor = nullptr;
|
||||
Color16 m_color = 0;
|
||||
int m_idx = -1;
|
||||
|
||||
public:
|
||||
AddColorCommand(PaletteEditorImGui *editor, Color16 color, int idx) noexcept {
|
||||
m_editor = editor;
|
||||
m_color = color;
|
||||
m_idx = idx;
|
||||
}
|
||||
|
||||
~AddColorCommand() noexcept override = default;
|
||||
|
||||
[[nodiscard]]
|
||||
int commandId() const noexcept override {
|
||||
return static_cast<int>(PaletteEditorCommandId::AddColor);
|
||||
}
|
||||
|
||||
void redo() noexcept override {
|
||||
m_editor->addColor(m_idx, m_color);
|
||||
}
|
||||
|
||||
void undo() noexcept override {
|
||||
m_editor->rmColor(m_idx);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class RemoveColorCommand: public studio::UndoCommand {
|
||||
private:
|
||||
PaletteEditorImGui *m_editor = nullptr;
|
||||
Color16 m_color = 0;
|
||||
int m_idx = -1;
|
||||
|
||||
public:
|
||||
RemoveColorCommand(PaletteEditorImGui *editor, Color16 color, int idx) noexcept {
|
||||
m_editor = editor;
|
||||
m_color = color;
|
||||
m_idx = idx;
|
||||
}
|
||||
|
||||
~RemoveColorCommand() noexcept override = default;
|
||||
|
||||
[[nodiscard]]
|
||||
int commandId() const noexcept override {
|
||||
return static_cast<int>(PaletteEditorCommandId::RemoveColor);
|
||||
}
|
||||
|
||||
void redo() noexcept override {
|
||||
m_editor->rmColor(m_idx);
|
||||
}
|
||||
|
||||
void undo() noexcept override {
|
||||
m_editor->addColor(m_idx, m_color);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class UpdateColorCommand: public studio::UndoCommand {
|
||||
private:
|
||||
PaletteEditorImGui *m_editor = nullptr;
|
||||
Color16 m_oldColor = 0;
|
||||
Color16 m_newColor = 0;
|
||||
int m_idx = -1;
|
||||
|
||||
public:
|
||||
UpdateColorCommand(PaletteEditorImGui *editor, int idx, Color16 oldColor, Color16 newColor) noexcept {
|
||||
m_editor = editor;
|
||||
m_idx = idx;
|
||||
m_oldColor = oldColor;
|
||||
m_newColor = newColor;
|
||||
//setObsolete(m_oldColor == m_newColor);
|
||||
}
|
||||
|
||||
~UpdateColorCommand() noexcept override = default;
|
||||
|
||||
bool mergeWith(const UndoCommand *cmd) noexcept final {
|
||||
if (cmd->commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) {
|
||||
return false;
|
||||
}
|
||||
auto ucCmd = static_cast<const UpdateColorCommand*>(cmd);
|
||||
if (m_idx != ucCmd->m_idx) {
|
||||
return false;
|
||||
}
|
||||
m_newColor = ucCmd->m_newColor;
|
||||
return true;
|
||||
}
|
||||
|
||||
int commandId() const noexcept final {
|
||||
return static_cast<int>(PaletteEditorCommandId::UpdateColor);
|
||||
}
|
||||
|
||||
void redo() noexcept final {
|
||||
m_editor->updateColor(m_idx, m_newColor);
|
||||
}
|
||||
|
||||
void undo() noexcept final {
|
||||
m_editor->updateColor(m_idx, m_oldColor);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class MoveColorCommand: public studio::UndoCommand {
|
||||
private:
|
||||
PaletteEditorImGui *m_editor = nullptr;
|
||||
std::size_t m_idx = 0;
|
||||
int m_offset = 0;
|
||||
|
||||
public:
|
||||
MoveColorCommand(PaletteEditorImGui *editor, std::size_t idx, int offset) noexcept {
|
||||
m_editor = editor;
|
||||
m_idx = idx;
|
||||
m_offset = offset;
|
||||
}
|
||||
|
||||
~MoveColorCommand() noexcept override = default;
|
||||
|
||||
[[nodiscard]]
|
||||
int commandId() const noexcept override {
|
||||
return static_cast<int>(PaletteEditorCommandId::MoveColor);
|
||||
}
|
||||
|
||||
void redo() noexcept override {
|
||||
m_editor->moveColor(m_idx, m_offset);
|
||||
}
|
||||
|
||||
void undo() noexcept override {
|
||||
m_editor->moveColor(static_cast<int>(m_idx) + m_offset, -m_offset);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
ox::Result<PaletteEditorImGui*> PaletteEditorImGui::make(Context *ctx, const ox::String &path) noexcept {
|
||||
auto out = ox::UniquePtr<PaletteEditorImGui>(new PaletteEditorImGui);
|
||||
out->m_ctx = ctx;
|
||||
out->m_itemPath = path;
|
||||
const auto lastSlash = std::find(out->m_itemPath.rbegin(), out->m_itemPath.rend(), '/').offset();
|
||||
out->m_itemName = out->m_itemPath.substr(lastSlash + 1);
|
||||
oxRequire(pal, core::readObj<Palette>(out->m_ctx, out->m_itemPath));
|
||||
out->m_pal = *pal.get();
|
||||
out->undoStack()->changeTriggered.connect(out.get(), &PaletteEditorImGui::markUnsavedChanges);
|
||||
return out.release();
|
||||
AddColorCommand::AddColorCommand(Palette *pal, Color16 color, int idx) noexcept {
|
||||
m_pal = pal;
|
||||
m_color = color;
|
||||
m_idx = idx;
|
||||
}
|
||||
|
||||
const ox::String &PaletteEditorImGui::itemName() const {
|
||||
return m_itemPath;
|
||||
int AddColorCommand::commandId() const noexcept {
|
||||
return static_cast<int>(PaletteEditorCommandId::AddColor);
|
||||
}
|
||||
|
||||
const ox::String &PaletteEditorImGui::itemDisplayName() const noexcept {
|
||||
return m_itemName;
|
||||
void AddColorCommand::redo() noexcept {
|
||||
m_pal->colors.insert(static_cast<std::size_t>(m_idx), m_color);
|
||||
}
|
||||
|
||||
studio::UndoStack *PaletteEditorImGui::undoStack() noexcept {
|
||||
return &m_undoStack;
|
||||
void AddColorCommand::undo() noexcept {
|
||||
oxIgnoreError(m_pal->colors.erase(static_cast<std::size_t>(m_idx)));
|
||||
}
|
||||
|
||||
void PaletteEditorImGui::draw(core::Context*) noexcept {
|
||||
static constexpr auto flags = ImGuiTableFlags_RowBg;
|
||||
const auto paneSize = ImGui::GetContentRegionAvail();
|
||||
ImGui::BeginChild("PaletteEditor");
|
||||
{
|
||||
ImGui::BeginChild("Colors", ImVec2(paneSize.x - 200, paneSize.y), false);
|
||||
{
|
||||
const auto colorsSz = ImGui::GetContentRegionAvail();
|
||||
static constexpr auto toolbarHeight = 40;
|
||||
ImGui::BeginChild("Toolbar", ImVec2(colorsSz.x, toolbarHeight), true);
|
||||
{
|
||||
const auto sz = ImVec2(70, 24);
|
||||
if (ImGui::Button("Add", sz)) {
|
||||
m_undoStack.push(new AddColorCommand(this, 0, m_pal.colors.size()));
|
||||
}
|
||||
ImGui::SameLine();
|
||||
ImGui::BeginDisabled(m_selectedRow >= m_pal.colors.size());
|
||||
{
|
||||
if (ImGui::Button("Remove", sz)) {
|
||||
m_undoStack.push(new RemoveColorCommand(this, m_pal.colors[static_cast<std::size_t>(m_selectedRow)], m_selectedRow));
|
||||
m_selectedRow = ox::min(m_pal.colors.size() - 1, m_selectedRow);
|
||||
}
|
||||
ImGui::SameLine();
|
||||
ImGui::BeginDisabled(m_selectedRow <= 0);
|
||||
{
|
||||
if (ImGui::Button("Move Up", sz)) {
|
||||
m_undoStack.push(new MoveColorCommand(this, m_selectedRow, -1));
|
||||
--m_selectedRow;
|
||||
}
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
ImGui::SameLine();
|
||||
ImGui::BeginDisabled(m_selectedRow >= m_pal.colors.size() - 1);
|
||||
{
|
||||
if (ImGui::Button("Move Down", sz)) {
|
||||
m_undoStack.push(new MoveColorCommand(this, m_selectedRow, 1));
|
||||
++m_selectedRow;
|
||||
}
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
}
|
||||
ImGui::EndChild();
|
||||
ImGui::BeginTable("Colors", 5, flags, ImVec2(colorsSz.x, colorsSz.y - (toolbarHeight + 5)));
|
||||
{
|
||||
ImGui::TableSetupColumn("Idx", ImGuiTableColumnFlags_WidthFixed, 25);
|
||||
ImGui::TableSetupColumn("Red", ImGuiTableColumnFlags_WidthFixed, 50);
|
||||
ImGui::TableSetupColumn("Green", ImGuiTableColumnFlags_WidthFixed, 50);
|
||||
ImGui::TableSetupColumn("Blue", ImGuiTableColumnFlags_WidthFixed, 50);
|
||||
ImGui::TableSetupColumn("Color Preview", ImGuiTableColumnFlags_NoHide);
|
||||
ImGui::TableHeadersRow();
|
||||
for (auto i = 0u; const auto c : m_pal.colors) {
|
||||
ImGui::PushID(static_cast<int>(i));
|
||||
ImGui::TableNextRow();
|
||||
// Color No.
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", i);
|
||||
// Red
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", red16(c));
|
||||
// Green
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", green16(c));
|
||||
// Blue
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%d", blue16(c));
|
||||
// ColorPreview
|
||||
ImGui::TableNextColumn();
|
||||
const auto ic = ImGui::GetColorU32(ImVec4(redf(c), greenf(c), bluef(c), 1));
|
||||
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, ic);
|
||||
if (ImGui::Selectable("##ColorRow", i == m_selectedRow, ImGuiSelectableFlags_SpanAllColumns)) {
|
||||
m_selectedRow = i;
|
||||
}
|
||||
ImGui::PopID();
|
||||
++i;
|
||||
}
|
||||
}
|
||||
ImGui::EndTable();
|
||||
}
|
||||
ImGui::EndChild();
|
||||
if (m_selectedRow < m_pal.colors.size()) {
|
||||
ImGui::SameLine();
|
||||
ImGui::BeginChild("ColorEditor", ImVec2(200, paneSize.y), true);
|
||||
{
|
||||
const auto c = m_pal.colors[m_selectedRow];
|
||||
int r = red16(c);
|
||||
int g = green16(c);
|
||||
int b = blue16(c);
|
||||
int a = alpha16(c);
|
||||
ImGui::InputInt("Red", &r, 1, 5);
|
||||
ImGui::InputInt("Green", &g, 1, 5);
|
||||
ImGui::InputInt("Blue", &b, 1, 5);
|
||||
const auto newColor = color16(r, g, b, a);
|
||||
if (c != newColor) {
|
||||
m_undoStack.push(new UpdateColorCommand(this, m_selectedRow, c, newColor));
|
||||
}
|
||||
}
|
||||
ImGui::EndChild();
|
||||
}
|
||||
|
||||
RemoveColorCommand::RemoveColorCommand(Palette *pal, Color16 color, int idx) noexcept {
|
||||
m_pal = pal;
|
||||
m_color = color;
|
||||
m_idx = idx;
|
||||
}
|
||||
|
||||
int RemoveColorCommand::commandId() const noexcept {
|
||||
return static_cast<int>(PaletteEditorCommandId::RemoveColor);
|
||||
}
|
||||
|
||||
void RemoveColorCommand::redo() noexcept {
|
||||
oxIgnoreError(m_pal->colors.erase(static_cast<std::size_t>(m_idx)));
|
||||
}
|
||||
|
||||
void RemoveColorCommand::undo() noexcept {
|
||||
m_pal->colors.insert(static_cast<std::size_t>(m_idx), m_color);
|
||||
}
|
||||
|
||||
|
||||
UpdateColorCommand::UpdateColorCommand(Palette *pal, int idx, Color16 oldColor, Color16 newColor) noexcept {
|
||||
m_pal = pal;
|
||||
m_idx = idx;
|
||||
m_oldColor = oldColor;
|
||||
m_newColor = newColor;
|
||||
//setObsolete(m_oldColor == m_newColor);
|
||||
}
|
||||
|
||||
bool UpdateColorCommand::mergeWith(const UndoCommand *cmd) noexcept {
|
||||
if (cmd->commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) {
|
||||
return false;
|
||||
}
|
||||
ImGui::EndChild();
|
||||
auto ucCmd = static_cast<const UpdateColorCommand*>(cmd);
|
||||
if (m_idx != ucCmd->m_idx) {
|
||||
return false;
|
||||
}
|
||||
m_newColor = ucCmd->m_newColor;
|
||||
return true;
|
||||
}
|
||||
|
||||
void PaletteEditorImGui::addColor(int idx, Color16 c) noexcept {
|
||||
m_pal.colors.insert(static_cast<std::size_t>(idx), c);
|
||||
setUnsavedChanges(true);
|
||||
[[nodiscard]]
|
||||
int UpdateColorCommand::commandId() const noexcept {
|
||||
return static_cast<int>(PaletteEditorCommandId::UpdateColor);
|
||||
}
|
||||
|
||||
void PaletteEditorImGui::rmColor(int idx) noexcept {
|
||||
oxIgnoreError(m_pal.colors.erase(static_cast<std::size_t>(idx)));
|
||||
setUnsavedChanges(true);
|
||||
void UpdateColorCommand::redo() noexcept {
|
||||
m_pal->colors[static_cast<std::size_t>(m_idx)] = m_newColor;
|
||||
}
|
||||
|
||||
void PaletteEditorImGui::updateColor(int idx, Color16 c) noexcept {
|
||||
m_pal.colors[static_cast<std::size_t>(idx)] = c;
|
||||
setUnsavedChanges(true);
|
||||
void UpdateColorCommand::undo() noexcept {
|
||||
m_pal->colors[static_cast<std::size_t>(m_idx)] = m_oldColor;
|
||||
}
|
||||
|
||||
void PaletteEditorImGui::moveColor(int idx, int offset) noexcept {
|
||||
const auto c = m_pal.colors[static_cast<std::size_t>(idx)];
|
||||
oxIgnoreError(m_pal.colors.erase(static_cast<std::size_t>(idx)));
|
||||
m_pal.colors.insert(static_cast<std::size_t>(idx + offset), c);
|
||||
setUnsavedChanges(true);
|
||||
|
||||
MoveColorCommand::MoveColorCommand(Palette *pal, std::size_t idx, int offset) noexcept {
|
||||
m_pal = pal;
|
||||
m_idx = idx;
|
||||
m_offset = offset;
|
||||
}
|
||||
|
||||
void PaletteEditorImGui::saveItem() {
|
||||
//m_ctx->project->writeObj(m_itemPath, &m_pal);
|
||||
int MoveColorCommand::commandId() const noexcept {
|
||||
return static_cast<int>(PaletteEditorCommandId::MoveColor);
|
||||
}
|
||||
|
||||
ox::Error PaletteEditorImGui::markUnsavedChanges(int) noexcept {
|
||||
setUnsavedChanges(true);
|
||||
return OxError(0);
|
||||
void MoveColorCommand::redo() noexcept {
|
||||
moveColor(static_cast<int>(m_idx), m_offset);
|
||||
}
|
||||
|
||||
void MoveColorCommand::undo() noexcept {
|
||||
moveColor(static_cast<int>(m_idx) + m_offset, -m_offset);
|
||||
}
|
||||
|
||||
void MoveColorCommand::moveColor(int idx, int offset) noexcept {
|
||||
const auto c = m_pal->colors[static_cast<std::size_t>(idx)];
|
||||
oxIgnoreError(m_pal->colors.erase(static_cast<std::size_t>(idx)));
|
||||
m_pal->colors.insert(static_cast<std::size_t>(idx + offset), c);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user