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