Update SnesColorButton and PaletteEditor

This commit is contained in:
scawful
2024-02-02 23:58:52 -05:00
parent 427b7f6f41
commit 626d38ec0d
8 changed files with 79 additions and 39 deletions

View File

@@ -106,7 +106,7 @@ void ResourceLabelManager::SelectableLabelWithNameEdit(
ImGui::Selectable(label.c_str(), selected,
ImGuiSelectableFlags_AllowDoubleClick);
std::string label_id = type + "_" + key;
if (ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Right)) {
ImGui::OpenPopup(label_id.c_str());
}
@@ -118,8 +118,6 @@ void ResourceLabelManager::SelectableLabelWithNameEdit(
}
ImGui::EndPopup();
}
// ImGui::PopID();
}
std::string ResourceLabelManager::CreateOrGetLabel(

View File

@@ -35,6 +35,8 @@ class ResourceLabelManager {
const std::string& defaultValue);
std::string CreateOrGetLabel(const std::string& type, const std::string& key,
const std::string& defaultValue);
std::string CreateOrGetLabel(const std::string& type, const std::string& key,
const absl::string_view& defaultValue);
private:
bool labels_loaded_ = false;

View File

@@ -35,6 +35,15 @@ namespace app {
namespace editor {
absl::Status PaletteEditor::Update() {
if (rom()->is_loaded()) {
// Initialize the labels
for (int i = 0; i < kNumPalettes; i++) {
rom()->resource_label()->CreateOrGetLabel(
"Palette Group Name", std::to_string(i),
std::string(kPaletteGroupNames[i]));
}
}
if (ImGui::BeginTable("paletteEditorTable", 2,
ImGuiTableFlags_Reorderable |
ImGuiTableFlags_Resizable |
@@ -55,7 +64,9 @@ absl::Status PaletteEditor::Update() {
}
}
ImGui::TableNextColumn();
ImGui::Text("Test Column");
if (gui::SnesColorEdit4("Color Picker", current_color_,
ImGuiColorEditFlags_NoAlpha)) {
}
ImGui::EndTable();
}
@@ -97,38 +108,39 @@ absl::Status PaletteEditor::DrawPaletteGroup(int category) {
const auto size =
rom()->palette_group(kPaletteGroupNames[category].data()).size();
auto palettes = rom()->palette_group(kPaletteGroupNames[category].data());
auto palettes =
rom()->mutable_palette_group(kPaletteGroupNames[category].data());
static bool edit_color = false;
for (int j = 0; j < size; j++) {
ImGui::Text("%d", j);
auto palette = palettes[j];
auto pal_size = palette.size();
// ImGui::Text("%d", j);
rom()->resource_label()->SelectableLabelWithNameEdit(
false, "Palette Group Name", std::to_string(j),
std::string(kPaletteGroupNames[category]));
auto palette = palettes->mutable_palette(j);
auto pal_size = palette->size();
for (int n = 0; n < pal_size; n++) {
ImGui::PushID(n);
if ((n % 8) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
if ((n % 7) != 0) ImGui::SameLine(0.0f, ImGui::GetStyle().ItemSpacing.y);
auto popup_id =
absl::StrCat(kPaletteCategoryNames[category].data(), j, "_", n);
// Small icon of the color in the palette
if (gui::SNESColorButton(popup_id, palette[n], palette_button_flags)) {
edit_color = true;
if (gui::SnesColorButton(popup_id, *palette->mutable_color(n),
palette_button_flags)) {
current_color_ = palette->GetColor(n);
// EditColorInPalette(*palette, n);
}
if (ImGui::BeginPopupContextItem(popup_id.c_str())) {
RETURN_IF_ERROR(HandleColorPopup(palette, category, j, n))
RETURN_IF_ERROR(HandleColorPopup(*palette, category, j, n))
}
if (edit_color) {
// The color button was clicked, open the popup
if (ImGui::ColorEdit4(popup_id.c_str(),
gfx::ToFloatArray(palette[n]).data(),
palette_button_flags)) {
EditColorInPalette(palette, n);
}
}
// if (gui::SnesColorEdit4(popup_id.c_str(), (*palette)[n],
// palette_button_flags)) {
// EditColorInPalette(*palette, n);
// }
ImGui::PopID();
}
@@ -139,13 +151,12 @@ absl::Status PaletteEditor::DrawPaletteGroup(int category) {
absl::Status PaletteEditor::HandleColorPopup(gfx::SnesPalette& palette, int i,
int j, int n) {
auto col = gfx::ToFloatArray(palette[n]);
if (ImGui::ColorEdit4("Edit Color", col.data(), color_popup_flags)) {
if (gui::SnesColorEdit4("Edit Color", palette[n], color_popup_flags)) {
RETURN_IF_ERROR(rom()->UpdatePaletteColor(kPaletteGroupNames[i].data(), j,
n, palette[n]))
}
if (ImGui::Button("Copy as..", ImVec2(-1, 0))) ImGui::OpenPopup("Copy");
if (ImGui::BeginPopup("Copy")) {
int cr = IM_F32_TO_INT8_SAT(col[0]);
int cg = IM_F32_TO_INT8_SAT(col[1]);

View File

@@ -26,11 +26,11 @@ static constexpr absl::string_view kPaletteGroupNames[] = {
"ow_mini_map", "3d_object", "3d_object"};
struct PaletteChange {
std::string groupName;
size_t paletteIndex;
size_t colorIndex;
gfx::SnesColor originalColor;
gfx::SnesColor newColor;
std::string group_name;
size_t palette_index;
size_t color_index;
gfx::SnesColor original_color;
gfx::SnesColor new_color;
};
class PaletteEditorHistory {
@@ -59,10 +59,10 @@ class PaletteEditorHistory {
size_t paletteIndex,
size_t colorIndex) const {
for (const auto& change : recentChanges) {
if (change.groupName == groupName &&
change.paletteIndex == paletteIndex &&
change.colorIndex == colorIndex) {
return change.originalColor;
if (change.group_name == groupName &&
change.palette_index == paletteIndex &&
change.color_index == colorIndex) {
return change.original_color;
}
}
// Handle error or return default (this is just an example,
@@ -104,12 +104,11 @@ class PaletteEditor : public SharedROM {
PaletteEditorHistory history_;
ImVec4 saved_palette_[256] = {};
ImVec4 current_color_;
gfx::SnesColor current_color_;
ImGuiColorEditFlags color_popup_flags =
ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoAlpha;
ImGuiColorEditFlags palette_button_flags =
ImGuiColorEditFlags_NoAlpha | ImGuiColorEditFlags_NoTooltip;
ImGuiColorEditFlags palette_button_flags = ImGuiColorEditFlags_NoAlpha;
ImGuiColorEditFlags palette_button_flags_2 = ImGuiColorEditFlags_NoAlpha |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip;

View File

@@ -29,7 +29,6 @@ std::vector<char> Convert(const std::vector<snes_color>& palette);
class SnesColor {
public:
SnesColor() : rgb_(0.f, 0.f, 0.f, 0.f), snes_(0) {}
explicit SnesColor(const ImVec4 val) : rgb_(val) {
snes_color color;
color.red = val.x / 255;
@@ -43,6 +42,16 @@ class SnesColor {
snes_(ConvertRGBtoSNES(val)),
rom_color_(val) {}
SnesColor(uint8_t r, uint8_t g, uint8_t b) {
rgb_ = ImVec4(r, g, b, 255.f);
snes_color color;
color.red = r;
color.green = g;
color.blue = b;
snes_ = ConvertRGBtoSNES(color);
rom_color_ = color;
}
ImVec4 rgb() const { return rgb_; }
void set_rgb(const ImVec4 val) {
rgb_.x = val.x / 255;

View File

@@ -21,7 +21,7 @@ ImVec4 ConvertSNESColorToImVec4(const SnesColor& color) {
);
}
IMGUI_API bool SNESColorButton(absl::string_view id, SnesColor& color,
IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color,
ImGuiColorEditFlags flags,
const ImVec2& size_arg) {
// Convert the SNES color values to ImGui color values (normalized to 0-1
@@ -34,6 +34,24 @@ IMGUI_API bool SNESColorButton(absl::string_view id, SnesColor& color,
return pressed;
}
IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor& color,
ImGuiColorEditFlags flags) {
// Convert the SNES color values to ImGui color values (normalized to 0-1
// range)
ImVec4 displayColor = ConvertSNESColorToImVec4(color);
// Call the original ImGui::ColorEdit4 with the converted color
bool pressed = ImGui::ColorEdit4(label.data(), (float*)&displayColor, flags);
// Convert the ImGui color values back to SNES color values (normalized to
// 0-255 range)
color = SnesColor(static_cast<uint8_t>(displayColor.x * 255.0f),
static_cast<uint8_t>(displayColor.y * 255.0f),
static_cast<uint8_t>(displayColor.z * 255.0f));
return pressed;
}
void DisplayPalette(app::gfx::SnesPalette& palette, bool loaded) {
static ImVec4 color = ImVec4(0, 0, 0, 255.f);
ImGuiColorEditFlags misc_flags = ImGuiColorEditFlags_AlphaPreview |

View File

@@ -20,10 +20,13 @@ using gfx::SnesColor;
ImVec4 ConvertSNESColorToImVec4(const SnesColor& color);
// The wrapper function for ImGui::ColorButton that takes a SnesColor reference
IMGUI_API bool SNESColorButton(absl::string_view id, SnesColor& color,
IMGUI_API bool SnesColorButton(absl::string_view id, SnesColor& color,
ImGuiColorEditFlags flags = 0,
const ImVec2& size_arg = ImVec2(0, 0));
IMGUI_API bool SnesColorEdit4(absl::string_view label, SnesColor& color,
ImGuiColorEditFlags flags = 0);
void DisplayPalette(app::gfx::SnesPalette& palette, bool loaded);
} // namespace gui

View File

@@ -43,7 +43,7 @@ void SelectablePalettePipeline(uint64_t& palette_id, bool& refresh_graphics,
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 2.0f);
}
if (gui::SNESColorButton("##palette", palette[n],
if (gui::SnesColorButton("##palette", palette[n],
ImGuiColorEditFlags_NoAlpha |
ImGuiColorEditFlags_NoPicker |
ImGuiColorEditFlags_NoTooltip,