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