yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
style.h
Go to the documentation of this file.
1#ifndef YAZE_APP_CORE_STYLE_H
2#define YAZE_APP_CORE_STYLE_H
3
4#include <functional>
5#include <string>
6#include <vector>
7
8#include "absl/strings/string_view.h"
9#include "app/gfx/bitmap.h"
10#include "app/gui/color.h"
12#include "imgui/imgui.h"
13
14namespace yaze {
15namespace gui {
16
17void ColorsYaze();
18
20
21void DrawBitmapViewer(const std::vector<gfx::Bitmap> &bitmaps, float scale,
22 int &current_bitmap);
23
24void BeginWindowWithDisplaySettings(const char *id, bool *active,
25 const ImVec2 &size = ImVec2(0, 0),
26 ImGuiWindowFlags flags = 0);
27
29
30void BeginPadding(int i);
31void EndPadding();
32
33void BeginNoPadding();
34void EndNoPadding();
35
36void BeginChildWithScrollbar(const char *str_id);
37void BeginChildWithScrollbar(const char *str_id, ImVec2 content_size);
38
39void BeginChildBothScrollbars(int id);
40
41// Table canvas management helpers for GUI elements that need proper sizing
42void BeginTableCanvas(const char* table_id, int columns, ImVec2 canvas_size = ImVec2(0, 0));
43void EndTableCanvas();
44void SetupCanvasTableColumn(const char* label, float width_ratio = 0.0f);
45void BeginCanvasTableCell(ImVec2 min_size = ImVec2(0, 0));
46
47void DrawDisplaySettings(ImGuiStyle *ref = nullptr);
48void DrawDisplaySettingsForPopup(ImGuiStyle *ref = nullptr); // Popup-safe version
49
50void TextWithSeparators(const absl::string_view &text);
51
52void DrawFontManager();
53
54struct TextBox {
55 std::string text;
56 std::string buffer;
57 int cursor_pos = 0;
61 bool has_selection = false;
62 bool has_focus = false;
63 bool changed = false;
64 bool can_undo = false;
65
66 void Undo() {
67 text = buffer;
69 has_selection = false;
70 }
71 void clearUndo() { can_undo = false; }
72 void Copy() { ImGui::SetClipboardText(text.c_str()); }
73 void Cut() {
74 Copy();
77 has_selection = false;
78 changed = true;
79 }
80 void Paste() {
82 text.insert(selection_start, ImGui::GetClipboardText());
83 std::string str = ImGui::GetClipboardText();
84 cursor_pos = selection_start + str.size();
85 has_selection = false;
86 changed = true;
87 }
88 void clear() {
89 text.clear();
90 buffer.clear();
91 cursor_pos = 0;
93 selection_end = 0;
95 has_selection = false;
96 has_focus = false;
97 changed = false;
98 can_undo = false;
99 }
100 void SelectAll() {
101 selection_start = 0;
102 selection_end = text.size();
103 selection_length = text.size();
104 has_selection = true;
105 }
106 void Focus() { has_focus = true; }
107};
108
109// Generic multi-select component that can be used with different types of data
110template <typename T>
112 public:
113 // Callback function type for rendering an item
115 std::function<void(int index, const T &item, bool is_selected)>;
116
117 // Constructor with optional title and default flags
119 const char *title = "Selection",
120 ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape |
121 ImGuiMultiSelectFlags_BoxSelect1d)
122 : title_(title), flags_(flags), selection_() {}
123
124 // Set the items to display
125 void SetItems(const std::vector<T> &items) { items_ = items; }
126
127 // Set the renderer function for items
128 void SetItemRenderer(ItemRenderer renderer) { item_renderer_ = renderer; }
129
130 // Set the height of the selection area (in font size units)
131 void SetHeight(float height_in_font_units = 20.0f) {
132 height_in_font_units_ = height_in_font_units;
133 }
134
135 // Set the child window flags
136 void SetChildFlags(ImGuiChildFlags flags) { child_flags_ = flags; }
137
138 // Update and render the multi-select component
139 void Update() {
140 ImGui::Text("%s: %d/%d", title_, selection_.Size, items_.size());
141
142 if (ImGui::BeginChild(
143 "##MultiSelectChild",
144 ImVec2(-FLT_MIN, ImGui::GetFontSize() * height_in_font_units_),
145 child_flags_)) {
146 ImGuiMultiSelectIO *ms_io =
147 ImGui::BeginMultiSelect(flags_, selection_.Size, items_.size());
148 selection_.ApplyRequests(ms_io);
149
150 ImGuiListClipper clipper;
151 clipper.Begin(items_.size());
152 if (ms_io->RangeSrcItem != -1)
153 clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem);
154
155 while (clipper.Step()) {
156 for (int n = clipper.DisplayStart; n < clipper.DisplayEnd; n++) {
157 bool item_is_selected = selection_.Contains((ImGuiID)n);
158 ImGui::SetNextItemSelectionUserData(n);
159
160 if (item_renderer_) {
161 item_renderer_(n, items_[n], item_is_selected);
162 } else {
163 // Default rendering if no custom renderer is provided
164 char label[64];
165 snprintf(label, sizeof(label), "Item %d", n);
166 ImGui::Selectable(label, item_is_selected);
167 }
168 }
169 }
170
171 ms_io = ImGui::EndMultiSelect();
172 selection_.ApplyRequests(ms_io);
173 }
174 ImGui::EndChild();
175 }
176
177 // Get the selected indices
178 std::vector<int> GetSelectedIndices() const {
179 std::vector<int> indices;
180 for (int i = 0; i < items_.size(); i++) {
181 if (selection_.Contains((ImGuiID)i)) {
182 indices.push_back(i);
183 }
184 }
185 return indices;
186 }
187
188 // Clear the selection
189 void ClearSelection() { selection_.Clear(); }
190
191 private:
192 const char *title_;
193 ImGuiMultiSelectFlags flags_;
194 ImGuiSelectionBasicStorage selection_;
195 std::vector<T> items_;
198 ImGuiChildFlags child_flags_ =
199 ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY;
200};
201
202} // namespace gui
203} // namespace yaze
204
205#endif
void SetItemRenderer(ItemRenderer renderer)
Definition style.h:128
void SetItems(const std::vector< T > &items)
Definition style.h:125
ItemRenderer item_renderer_
Definition style.h:196
std::function< void(int index, const T &item, bool is_selected)> ItemRenderer
Definition style.h:115
void SetHeight(float height_in_font_units=20.0f)
Definition style.h:131
ImGuiMultiSelectFlags flags_
Definition style.h:193
MultiSelect(const char *title="Selection", ImGuiMultiSelectFlags flags=ImGuiMultiSelectFlags_ClearOnEscape|ImGuiMultiSelectFlags_BoxSelect1d)
Definition style.h:118
void SetChildFlags(ImGuiChildFlags flags)
Definition style.h:136
const char * title_
Definition style.h:192
ImGuiChildFlags child_flags_
Definition style.h:198
std::vector< T > items_
Definition style.h:195
ImGuiSelectionBasicStorage selection_
Definition style.h:194
std::vector< int > GetSelectedIndices() const
Definition style.h:178
float height_in_font_units_
Definition style.h:197
void DrawBitmapViewer(const std::vector< gfx::Bitmap > &bitmaps, float scale, int &current_bitmap_id)
Definition style.cc:131
void BeginCanvasTableCell(ImVec2 min_size)
Definition style.cc:343
void BeginTableCanvas(const char *table_id, int columns, ImVec2 canvas_size)
Definition style.cc:317
void DrawFontManager()
Definition style.cc:1286
void BeginPadding(int i)
Definition style.cc:272
void BeginChildBothScrollbars(int id)
Definition style.cc:309
void EndNoPadding()
Definition style.cc:282
TextEditor::LanguageDefinition GetAssemblyLanguageDef()
Definition style.cc:192
void DrawDisplaySettings(ImGuiStyle *ref)
Definition style.cc:360
void EndPadding()
Definition style.cc:276
void BeginNoPadding()
Definition style.cc:278
void SetupCanvasTableColumn(const char *label, float width_ratio)
Definition style.cc:335
void EndWindowWithDisplaySettings()
Definition style.cc:267
void DrawDisplaySettingsForPopup(ImGuiStyle *ref)
Definition style.cc:842
void EndTableCanvas()
Definition style.cc:331
void ColorsYaze()
Definition style.cc:32
void BeginWindowWithDisplaySettings(const char *id, bool *active, const ImVec2 &size, ImGuiWindowFlags flags)
Definition style.cc:247
void TextWithSeparators(const absl::string_view &text)
Definition style.cc:1280
void BeginChildWithScrollbar(const char *str_id)
Definition style.cc:284
Main namespace for the application.
int selection_start
Definition style.h:58
void SelectAll()
Definition style.h:100
std::string text
Definition style.h:55
void clearUndo()
Definition style.h:71
bool has_selection
Definition style.h:61
int selection_length
Definition style.h:60
std::string buffer
Definition style.h:56