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"
10#include "app/gui/core/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,
43 ImVec2 canvas_size = ImVec2(0, 0));
44void EndTableCanvas();
45void SetupCanvasTableColumn(const char* label, float width_ratio = 0.0f);
46void BeginCanvasTableCell(ImVec2 min_size = ImVec2(0, 0));
47
48void DrawDisplaySettings(ImGuiStyle* ref = nullptr);
50 ImGuiStyle* ref = nullptr); // Popup-safe version
51
52void TextWithSeparators(const absl::string_view& text);
53
54void DrawFontManager();
55
56struct TextBox {
57 std::string text;
58 std::string buffer;
59 int cursor_pos = 0;
63 bool has_selection = false;
64 bool has_focus = false;
65 bool changed = false;
66 bool can_undo = false;
67
68 void Undo() {
69 text = buffer;
71 has_selection = false;
72 }
73 void clearUndo() { can_undo = false; }
74 void Copy() { ImGui::SetClipboardText(text.c_str()); }
75 void Cut() {
76 Copy();
79 has_selection = false;
80 changed = true;
81 }
82 void Paste() {
84 text.insert(selection_start, ImGui::GetClipboardText());
85 std::string str = ImGui::GetClipboardText();
86 cursor_pos = selection_start + str.size();
87 has_selection = false;
88 changed = true;
89 }
90 void clear() {
91 text.clear();
92 buffer.clear();
93 cursor_pos = 0;
95 selection_end = 0;
97 has_selection = false;
98 has_focus = false;
99 changed = false;
100 can_undo = false;
101 }
102 void SelectAll() {
103 selection_start = 0;
104 selection_end = text.size();
105 selection_length = text.size();
106 has_selection = true;
107 }
108 void Focus() { has_focus = true; }
109};
110
111// Generic multi-select component that can be used with different types of data
112template <typename T>
114 public:
115 // Callback function type for rendering an item
117 std::function<void(int index, const T& item, bool is_selected)>;
118
119 // Constructor with optional title and default flags
121 const char* title = "Selection",
122 ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape |
123 ImGuiMultiSelectFlags_BoxSelect1d)
124 : title_(title), flags_(flags), selection_() {}
125
126 // Set the items to display
127 void SetItems(const std::vector<T>& items) { items_ = items; }
128
129 // Set the renderer function for items
130 void SetItemRenderer(ItemRenderer renderer) { item_renderer_ = renderer; }
131
132 // Set the height of the selection area (in font size units)
133 void SetHeight(float height_in_font_units = 20.0f) {
134 height_in_font_units_ = height_in_font_units;
135 }
136
137 // Set the child window flags
138 void SetChildFlags(ImGuiChildFlags flags) { child_flags_ = flags; }
139
140 // Update and render the multi-select component
141 void Update() {
142 ImGui::Text("%s: %d/%d", title_, selection_.Size, items_.size());
143
144 if (ImGui::BeginChild(
145 "##MultiSelectChild",
146 ImVec2(-FLT_MIN, ImGui::GetFontSize() * height_in_font_units_),
147 child_flags_)) {
148 ImGuiMultiSelectIO* ms_io =
149 ImGui::BeginMultiSelect(flags_, selection_.Size, items_.size());
150 selection_.ApplyRequests(ms_io);
151
152 ImGuiListClipper clipper;
153 clipper.Begin(items_.size());
154 if (ms_io->RangeSrcItem != -1)
155 clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem);
156
157 while (clipper.Step()) {
158 for (int n = clipper.DisplayStart; n < clipper.DisplayEnd; n++) {
159 bool item_is_selected = selection_.Contains((ImGuiID)n);
160 ImGui::SetNextItemSelectionUserData(n);
161
162 if (item_renderer_) {
163 item_renderer_(n, items_[n], item_is_selected);
164 } else {
165 // Default rendering if no custom renderer is provided
166 char label[64];
167 snprintf(label, sizeof(label), "Item %d", n);
168 ImGui::Selectable(label, item_is_selected);
169 }
170 }
171 }
172
173 ms_io = ImGui::EndMultiSelect();
174 selection_.ApplyRequests(ms_io);
175 }
176 ImGui::EndChild();
177 }
178
179 // Get the selected indices
180 std::vector<int> GetSelectedIndices() const {
181 std::vector<int> indices;
182 for (int i = 0; i < items_.size(); i++) {
183 if (selection_.Contains((ImGuiID)i)) {
184 indices.push_back(i);
185 }
186 }
187 return indices;
188 }
189
190 // Clear the selection
191 void ClearSelection() { selection_.Clear(); }
192
193 private:
194 const char* title_;
195 ImGuiMultiSelectFlags flags_;
196 ImGuiSelectionBasicStorage selection_;
197 std::vector<T> items_;
200 ImGuiChildFlags child_flags_ =
201 ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY;
202};
203
204} // namespace gui
205} // namespace yaze
206
207#endif
void SetItemRenderer(ItemRenderer renderer)
Definition style.h:130
void SetItems(const std::vector< T > &items)
Definition style.h:127
ItemRenderer item_renderer_
Definition style.h:198
void SetHeight(float height_in_font_units=20.0f)
Definition style.h:133
ImGuiMultiSelectFlags flags_
Definition style.h:195
std::function< void(int index, const T &item, bool is_selected)> ItemRenderer
Definition style.h:116
MultiSelect(const char *title="Selection", ImGuiMultiSelectFlags flags=ImGuiMultiSelectFlags_ClearOnEscape|ImGuiMultiSelectFlags_BoxSelect1d)
Definition style.h:120
void SetChildFlags(ImGuiChildFlags flags)
Definition style.h:138
const char * title_
Definition style.h:194
ImGuiChildFlags child_flags_
Definition style.h:200
std::vector< T > items_
Definition style.h:197
ImGuiSelectionBasicStorage selection_
Definition style.h:196
std::vector< int > GetSelectedIndices() const
Definition style.h:180
float height_in_font_units_
Definition style.h:199
void DrawBitmapViewer(const std::vector< gfx::Bitmap > &bitmaps, float scale, int &current_bitmap_id)
Definition style.cc:132
void BeginCanvasTableCell(ImVec2 min_size)
Definition style.cc:356
void BeginTableCanvas(const char *table_id, int columns, ImVec2 canvas_size)
Definition style.cc:327
void DrawFontManager()
Definition style.cc:1323
void BeginPadding(int i)
Definition style.cc:274
void BeginChildBothScrollbars(int id)
Definition style.cc:319
void EndNoPadding()
Definition style.cc:286
TextEditor::LanguageDefinition GetAssemblyLanguageDef()
Definition style.cc:193
void DrawDisplaySettings(ImGuiStyle *ref)
Definition style.cc:372
void EndPadding()
Definition style.cc:278
void BeginNoPadding()
Definition style.cc:282
void SetupCanvasTableColumn(const char *label, float width_ratio)
Definition style.cc:347
void EndWindowWithDisplaySettings()
Definition style.cc:269
void DrawDisplaySettingsForPopup(ImGuiStyle *ref)
Definition style.cc:868
void EndTableCanvas()
Definition style.cc:343
void ColorsYaze()
Definition style.cc:32
void BeginWindowWithDisplaySettings(const char *id, bool *active, const ImVec2 &size, ImGuiWindowFlags flags)
Definition style.cc:249
void TextWithSeparators(const absl::string_view &text)
Definition style.cc:1317
void BeginChildWithScrollbar(const char *str_id)
Definition style.cc:290
int selection_start
Definition style.h:60
void SelectAll()
Definition style.h:102
std::string text
Definition style.h:57
void clearUndo()
Definition style.h:73
bool has_selection
Definition style.h:63
int selection_length
Definition style.h:62
std::string buffer
Definition style.h:58