yaze 0.2.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
41
42absl::StatusOr<Theme> LoadTheme(const std::string &filename);
43absl::Status SaveTheme(const Theme &theme);
44void ApplyTheme(const Theme &theme);
45
46void ColorsYaze();
47
49
50void DrawBitmapViewer(const std::vector<gfx::Bitmap> &bitmaps, float scale,
51 int &current_bitmap);
52
53void BeginWindowWithDisplaySettings(const char *id, bool *active,
54 const ImVec2 &size = ImVec2(0, 0),
55 ImGuiWindowFlags flags = 0);
56
58
59void BeginPadding(int i);
60void EndPadding();
61
62void BeginNoPadding();
63void EndNoPadding();
64
65void BeginChildWithScrollbar(const char *str_id);
66
67void BeginChildBothScrollbars(int id);
68
69void DrawDisplaySettings(ImGuiStyle *ref = nullptr);
70
71void TextWithSeparators(const absl::string_view &text);
72
73void DrawFontManager();
74
75struct TextBox {
76 std::string text;
77 std::string buffer;
78 int cursor_pos = 0;
82 bool has_selection = false;
83 bool has_focus = false;
84 bool changed = false;
85 bool can_undo = false;
86
87 void Undo() {
88 text = buffer;
90 has_selection = false;
91 }
92 void clearUndo() { can_undo = false; }
93 void Copy() { ImGui::SetClipboardText(text.c_str()); }
94 void Cut() {
95 Copy();
98 has_selection = false;
99 changed = true;
100 }
101 void Paste() {
103 text.insert(selection_start, ImGui::GetClipboardText());
104 std::string str = ImGui::GetClipboardText();
105 cursor_pos = selection_start + str.size();
106 has_selection = false;
107 changed = true;
108 }
109 void clear() {
110 text.clear();
111 buffer.clear();
112 cursor_pos = 0;
113 selection_start = 0;
114 selection_end = 0;
116 has_selection = false;
117 has_focus = false;
118 changed = false;
119 can_undo = false;
120 }
121 void SelectAll() {
122 selection_start = 0;
123 selection_end = text.size();
124 selection_length = text.size();
125 has_selection = true;
126 }
127 void Focus() { has_focus = true; }
128};
129
130// Generic multi-select component that can be used with different types of data
131template <typename T>
133 public:
134 // Callback function type for rendering an item
136 std::function<void(int index, const T &item, bool is_selected)>;
137
138 // Constructor with optional title and default flags
140 const char *title = "Selection",
141 ImGuiMultiSelectFlags flags = ImGuiMultiSelectFlags_ClearOnEscape |
142 ImGuiMultiSelectFlags_BoxSelect1d)
143 : title_(title), flags_(flags), selection_() {}
144
145 // Set the items to display
146 void SetItems(const std::vector<T> &items) { items_ = items; }
147
148 // Set the renderer function for items
149 void SetItemRenderer(ItemRenderer renderer) { item_renderer_ = renderer; }
150
151 // Set the height of the selection area (in font size units)
152 void SetHeight(float height_in_font_units = 20.0f) {
153 height_in_font_units_ = height_in_font_units;
154 }
155
156 // Set the child window flags
157 void SetChildFlags(ImGuiChildFlags flags) { child_flags_ = flags; }
158
159 // Update and render the multi-select component
160 void Update() {
161 ImGui::Text("%s: %d/%d", title_, selection_.Size, items_.size());
162
163 if (ImGui::BeginChild(
164 "##MultiSelectChild",
165 ImVec2(-FLT_MIN, ImGui::GetFontSize() * height_in_font_units_),
166 child_flags_)) {
167 ImGuiMultiSelectIO *ms_io =
168 ImGui::BeginMultiSelect(flags_, selection_.Size, items_.size());
169 selection_.ApplyRequests(ms_io);
170
171 ImGuiListClipper clipper;
172 clipper.Begin(items_.size());
173 if (ms_io->RangeSrcItem != -1)
174 clipper.IncludeItemByIndex((int)ms_io->RangeSrcItem);
175
176 while (clipper.Step()) {
177 for (int n = clipper.DisplayStart; n < clipper.DisplayEnd; n++) {
178 bool item_is_selected = selection_.Contains((ImGuiID)n);
179 ImGui::SetNextItemSelectionUserData(n);
180
181 if (item_renderer_) {
182 item_renderer_(n, items_[n], item_is_selected);
183 } else {
184 // Default rendering if no custom renderer is provided
185 char label[64];
186 snprintf(label, sizeof(label), "Item %d", n);
187 ImGui::Selectable(label, item_is_selected);
188 }
189 }
190 }
191
192 ms_io = ImGui::EndMultiSelect();
193 selection_.ApplyRequests(ms_io);
194 }
195 ImGui::EndChild();
196 }
197
198 // Get the selected indices
199 std::vector<int> GetSelectedIndices() const {
200 std::vector<int> indices;
201 for (int i = 0; i < items_.size(); i++) {
202 if (selection_.Contains((ImGuiID)i)) {
203 indices.push_back(i);
204 }
205 }
206 return indices;
207 }
208
209 // Clear the selection
210 void ClearSelection() { selection_.Clear(); }
211
212 private:
213 const char *title_;
214 ImGuiMultiSelectFlags flags_;
215 ImGuiSelectionBasicStorage selection_;
216 std::vector<T> items_;
219 ImGuiChildFlags child_flags_ =
220 ImGuiChildFlags_FrameStyle | ImGuiChildFlags_ResizeY;
221};
222
223} // namespace gui
224} // namespace yaze
225
226#endif
void SetItemRenderer(ItemRenderer renderer)
Definition style.h:149
void SetItems(const std::vector< T > &items)
Definition style.h:146
ItemRenderer item_renderer_
Definition style.h:217
void SetHeight(float height_in_font_units=20.0f)
Definition style.h:152
ImGuiMultiSelectFlags flags_
Definition style.h:214
MultiSelect(const char *title="Selection", ImGuiMultiSelectFlags flags=ImGuiMultiSelectFlags_ClearOnEscape|ImGuiMultiSelectFlags_BoxSelect1d)
Definition style.h:139
std::function< void(int index, const T &item, bool is_selected)> ItemRenderer
Definition style.h:135
void SetChildFlags(ImGuiChildFlags flags)
Definition style.h:157
const char * title_
Definition style.h:213
ImGuiChildFlags child_flags_
Definition style.h:219
std::vector< T > items_
Definition style.h:216
ImGuiSelectionBasicStorage selection_
Definition style.h:215
std::vector< int > GetSelectedIndices() const
Definition style.h:199
float height_in_font_units_
Definition style.h:218
Graphical User Interface (GUI) components for the application.
Definition canvas.cc:17
void DrawBitmapViewer(const std::vector< gfx::Bitmap > &bitmaps, float scale, int &current_bitmap_id)
Definition style.cc:231
void DrawFontManager()
Definition style.cc:759
absl::StatusOr< Theme > LoadTheme(const std::string &filename)
Definition style.cc:48
void BeginPadding(int i)
Definition style.cc:372
void BeginChildBothScrollbars(int id)
Definition style.cc:389
void EndNoPadding()
Definition style.cc:382
TextEditor::LanguageDefinition GetAssemblyLanguageDef()
Definition style.cc:292
void DrawDisplaySettings(ImGuiStyle *ref)
Definition style.cc:396
void EndPadding()
Definition style.cc:376
absl::Status SaveTheme(const Theme &theme)
Definition style.cc:75
void BeginNoPadding()
Definition style.cc:378
void EndWindowWithDisplaySettings()
Definition style.cc:367
void ColorsYaze()
Definition style.cc:132
void BeginWindowWithDisplaySettings(const char *id, bool *active, const ImVec2 &size, ImGuiWindowFlags flags)
Definition style.cc:347
void TextWithSeparators(const absl::string_view &text)
Definition style.cc:753
void BeginChildWithScrollbar(const char *str_id)
Definition style.cc:384
void ApplyTheme(const Theme &theme)
Definition style.cc:107
Main namespace for the application.
Definition controller.cc:18
int selection_start
Definition style.h:79
void SelectAll()
Definition style.h:121
std::string text
Definition style.h:76
void clearUndo()
Definition style.h:92
bool has_selection
Definition style.h:82
int selection_length
Definition style.h:81
std::string buffer
Definition style.h:77
Color button_active
Definition style.h:36
Color tab_hovered
Definition style.h:31
Color title_bar_bg
Definition style.h:21
Color header
Definition style.h:23
Color menu_bar_bg
Definition style.h:20
std::string name
Definition style.h:18
Color clickable_text_hovered
Definition style.h:39
Color title_bg_collapsed
Definition style.h:28
Color header_active
Definition style.h:25
Color header_hovered
Definition style.h:24
Color title_bg_active
Definition style.h:27
Color tab_active
Definition style.h:32
Color clickable_text
Definition style.h:38
Color button
Definition style.h:34
Color button_hovered
Definition style.h:35