From 9a680310bdf9103f6784c35016b2399bc2472254 Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 29 Dec 2024 16:03:11 -0500 Subject: [PATCH] Add table handling functions: implement AddTableColumn and DrawTable for improved GUI layout --- src/app/gui/input.cc | 30 ++++++++++++++++++++++++------ src/app/gui/input.h | 44 ++++++++++++++++++++++++++------------------ 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/app/gui/input.cc b/src/app/gui/input.cc index 53be057c..7b0871df 100644 --- a/src/app/gui/input.cc +++ b/src/app/gui/input.cc @@ -6,14 +6,9 @@ #include "ImGuiFileDialog/ImGuiFileDialog.h" #include "absl/strings/string_view.h" -#include "app/gfx/bitmap.h" -#include "app/gfx/snes_palette.h" #include "app/gfx/snes_tile.h" -#include "app/gui/canvas.h" -#include "app/gui/color.h" #include "imgui/imgui.h" #include "imgui/imgui_internal.h" -#include "imgui/misc/cpp/imgui_stdlib.h" namespace ImGui { @@ -279,7 +274,30 @@ void FileDialogPipeline(absl::string_view display_key, ImGuiFileDialog::Instance()->Close(); } } +void AddTableColumn(Table &table, const std::string &label, GuiElement element) { + table.column_labels.push_back(label); + table.column_contents.push_back(element); +} + +void DrawTable(Table& params) { + if (ImGui::BeginTable(params.id, params.num_columns, params.flags, params.size)) { + for (int i = 0; i < params.num_columns; ++i) + ImGui::TableSetupColumn(params.column_labels[i].c_str()); + + for (int i = 0; i < params.num_columns; ++i) { + ImGui::TableNextColumn(); + switch (params.column_contents[i].index()) { + case 0: + std::get<0>(params.column_contents[i])(); + break; + case 1: + ImGui::Text("%s", std::get<1>(params.column_contents[i]).c_str()); + break; + } + } + ImGui::EndTable(); + } +} } // namespace gui - } // namespace yaze diff --git a/src/app/gui/input.h b/src/app/gui/input.h index e04aa3bf..6cccbe6b 100644 --- a/src/app/gui/input.h +++ b/src/app/gui/input.h @@ -11,14 +11,8 @@ #include #include "absl/strings/string_view.h" -#include "app/gfx/bitmap.h" -#include "app/gfx/snes_palette.h" #include "app/gfx/snes_tile.h" -#include "app/gui/canvas.h" -#include "app/gui/color.h" #include "imgui/imgui.h" -#include "imgui/misc/cpp/imgui_stdlib.h" -#include "imgui_memory_editor.h" namespace yaze { namespace gui { @@ -26,26 +20,26 @@ namespace gui { constexpr ImVec2 kDefaultModalSize = ImVec2(200, 0); constexpr ImVec2 kZeroPos = ImVec2(0, 0); -IMGUI_API bool InputHex(const char* label, uint64_t* data); -IMGUI_API bool InputHex(const char* label, int* data, int num_digits = 4, +IMGUI_API bool InputHex(const char *label, uint64_t *data); +IMGUI_API bool InputHex(const char *label, int *data, int num_digits = 4, float input_width = 50.f); -IMGUI_API bool InputHexShort(const char* label, uint32_t* data); -IMGUI_API bool InputHexWord(const char* label, uint16_t* data, +IMGUI_API bool InputHexShort(const char *label, uint32_t *data); +IMGUI_API bool InputHexWord(const char *label, uint16_t *data, float input_width = 50.f, bool no_step = false); -IMGUI_API bool InputHexWord(const char* label, int16_t* data, +IMGUI_API bool InputHexWord(const char *label, int16_t *data, float input_width = 50.f, bool no_step = false); -IMGUI_API bool InputHexByte(const char* label, uint8_t* data, +IMGUI_API bool InputHexByte(const char *label, uint8_t *data, float input_width = 50.f, bool no_step = false); -IMGUI_API bool InputHexByte(const char* label, uint8_t* data, uint8_t max_value, +IMGUI_API bool InputHexByte(const char *label, uint8_t *data, uint8_t max_value, float input_width = 50.f, bool no_step = false); -IMGUI_API bool ListBox(const char* label, int* current_item, - const std::vector& items, +IMGUI_API bool ListBox(const char *label, int *current_item, + const std::vector &items, int height_in_items = -1); -bool InputTileInfo(const char* label, gfx::TileInfo* tile_info); +bool InputTileInfo(const char *label, gfx::TileInfo *tile_info); using ItemLabelFlags = enum ItemLabelFlag { Left = 1u << 0u, @@ -55,15 +49,29 @@ using ItemLabelFlags = enum ItemLabelFlag { IMGUI_API void ItemLabel(absl::string_view title, ItemLabelFlags flags); -IMGUI_API ImGuiID GetID(const std::string& id); +IMGUI_API ImGuiID GetID(const std::string &id); void FileDialogPipeline(absl::string_view display_key, absl::string_view file_extensions, std::optional button_text, std::function callback); -} // namespace gui +using GuiElement = std::variant, std::string>; +struct Table { + const char *id; + int num_columns; + ImGuiTableFlags flags; + ImVec2 size; + std::vector column_labels; + std::vector column_contents; +}; + +void AddTableColumn(Table &table, const std::string &label, GuiElement element); + +void DrawTable(Table ¶ms); + +} // namespace gui } // namespace yaze #endif