yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
input.cc
Go to the documentation of this file.
1#include "input.h"
2
3#include <algorithm>
4#include <functional>
5#include <limits>
6#include <string>
7#include <variant>
8
9#include "absl/strings/string_view.h"
11#include "imgui/imgui.h"
12#include "imgui/imgui_internal.h"
14
15template <class... Ts>
16struct overloaded : Ts... {
17 using Ts::operator()...;
18};
19template <class... Ts>
20overloaded(Ts...) -> overloaded<Ts...>;
21
22namespace ImGui {
23
24static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter(
25 ImGuiDataType data_type, const char* format) {
26 if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double)
27 return ImGuiInputTextFlags_CharsScientific;
28 const char format_last_char = format[0] ? format[strlen(format) - 1] : 0;
29 return (format_last_char == 'x' || format_last_char == 'X')
30 ? ImGuiInputTextFlags_CharsHexadecimal
31 : ImGuiInputTextFlags_CharsDecimal;
32}
33
34// Helper: returns true if label is "invisible" (starts with "##")
35static inline bool IsInvisibleLabel(const char* label) {
36 return label && label[0] == '#' && label[1] == '#';
37}
38
39// Result struct for extended input functions
41 bool changed; // Any change occurred
42 bool immediate; // Change was from button/wheel (apply immediately)
43 bool text_changed; // Change was from text input
44 bool text_committed; // Text input was committed (deactivated after edit)
45};
46
47bool InputScalarLeft(const char* label, ImGuiDataType data_type, void* p_data,
48 const void* p_step, const void* p_step_fast,
49 const char* format, float input_width,
50 ImGuiInputTextFlags flags, bool no_step = false) {
51 InputScalarResult result = {};
52 // Call extended version and return simple bool
53 // (implementation below handles both)
54
55 ImGuiWindow* window = ImGui::GetCurrentWindow();
56 if (window->SkipItems)
57 return false;
58
59 ImGuiContext& g = *GImGui;
60 ImGuiStyle& style = g.Style;
61
62 if (format == NULL)
63 format = DataTypeGetInfo(data_type)->PrintFmt;
64
65 char buf[64];
66 DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format);
67
68 if (g.ActiveId == 0 && (flags & (ImGuiInputTextFlags_CharsDecimal |
69 ImGuiInputTextFlags_CharsHexadecimal |
70 ImGuiInputTextFlags_CharsScientific)) == 0)
71 flags |= InputScalar_DefaultCharsFilter(data_type, format);
72 flags |= ImGuiInputTextFlags_AutoSelectAll;
73
74 bool value_changed = false;
75 const float button_size = GetFrameHeight();
76
77 // Support invisible labels (##) by not rendering the label, but still using
78 // it for ID
79 bool invisible_label = IsInvisibleLabel(label);
80
81 if (!invisible_label) {
82 AlignTextToFramePadding();
83 Text("%s", label);
84 SameLine();
85 }
86
87 BeginGroup(); // The only purpose of the group here is to allow the caller
88 // to query item data e.g. IsItemActive()
89 PushID(label);
90 SetNextItemWidth(ImMax(
91 1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));
92
93 // Place the label on the left of the input field, unless invisible
94 PushStyleVar(ImGuiStyleVar_ItemSpacing,
95 ImVec2{style.ItemSpacing.x, style.ItemSpacing.y});
96 PushStyleVar(ImGuiStyleVar_FramePadding,
97 ImVec2{style.FramePadding.x, style.FramePadding.y});
98
99 SetNextItemWidth(input_width);
100 if (InputText("", buf, IM_ARRAYSIZE(buf),
101 flags)) // PushId(label) + "" gives us the expected ID
102 // from outside point of view
103 value_changed = DataTypeApplyFromText(buf, data_type, p_data, format);
104 IMGUI_TEST_ENGINE_ITEM_INFO(
105 g.LastItemData.ID, label,
106 g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable);
107
108 // Mouse wheel support
109 if (IsItemHovered() && g.IO.MouseWheel != 0.0f) {
110 float scroll_amount = g.IO.MouseWheel;
111 float scroll_speed = 0.25f; // Adjust the scroll speed as needed
112
113 if (g.IO.KeyCtrl && p_step_fast)
114 scroll_amount *= *(const float*)p_step_fast;
115 else
116 scroll_amount *= *(const float*)p_step;
117
118 if (scroll_amount > 0.0f) {
119 scroll_amount *= scroll_speed; // Adjust the scroll speed as needed
120 DataTypeApplyOp(data_type, '+', p_data, p_data, &scroll_amount);
121 value_changed = true;
122 } else if (scroll_amount < 0.0f) {
123 scroll_amount *= -scroll_speed; // Adjust the scroll speed as needed
124 DataTypeApplyOp(data_type, '-', p_data, p_data, &scroll_amount);
125 value_changed = true;
126 }
127 }
128
129 // Step buttons
130 if (!no_step) {
131 const ImVec2 backup_frame_padding = style.FramePadding;
132 style.FramePadding.x = style.FramePadding.y;
133 ImGuiButtonFlags button_flags = ImGuiButtonFlags_PressedOnClick;
134 if (flags & ImGuiInputTextFlags_ReadOnly)
135 BeginDisabled();
136 SameLine(0, style.ItemInnerSpacing.x);
137 if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) {
138 DataTypeApplyOp(data_type, '-', p_data, p_data,
139 g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
140 value_changed = true;
141 }
142 SameLine(0, style.ItemInnerSpacing.x);
143 if (ButtonEx("+", ImVec2(button_size, button_size), button_flags)) {
144 DataTypeApplyOp(data_type, '+', p_data, p_data,
145 g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
146 value_changed = true;
147 }
148
149 if (flags & ImGuiInputTextFlags_ReadOnly)
150 EndDisabled();
151
152 style.FramePadding = backup_frame_padding;
153 }
154 PopID();
155 EndGroup();
156 ImGui::PopStyleVar(2);
157
158 if (value_changed)
159 MarkItemEdited(g.LastItemData.ID);
160
161 return value_changed;
162}
163
164// Extended version that tracks change source
165InputScalarResult InputScalarLeftEx(const char* label, ImGuiDataType data_type,
166 void* p_data, const void* p_step,
167 const void* p_step_fast, const char* format,
168 float input_width, ImGuiInputTextFlags flags,
169 bool no_step = false) {
170 InputScalarResult result = {false, false, false, false};
171
172 ImGuiWindow* window = ImGui::GetCurrentWindow();
173 if (window->SkipItems)
174 return result;
175
176 ImGuiContext& g = *GImGui;
177 ImGuiStyle& style = g.Style;
178
179 if (format == NULL)
180 format = DataTypeGetInfo(data_type)->PrintFmt;
181
182 char buf[64];
183 DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format);
184
185 if (g.ActiveId == 0 && (flags & (ImGuiInputTextFlags_CharsDecimal |
186 ImGuiInputTextFlags_CharsHexadecimal |
187 ImGuiInputTextFlags_CharsScientific)) == 0)
188 flags |= InputScalar_DefaultCharsFilter(data_type, format);
189 flags |= ImGuiInputTextFlags_AutoSelectAll;
190
191 const float button_size = GetFrameHeight();
192
193 // Support invisible labels (##) by not rendering the label, but still using
194 // it for ID
195 bool invisible_label = IsInvisibleLabel(label);
196
197 if (!invisible_label) {
198 AlignTextToFramePadding();
199 Text("%s", label);
200 SameLine();
201 }
202
203 BeginGroup();
204 PushID(label);
205 SetNextItemWidth(ImMax(
206 1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));
207
208 PushStyleVar(ImGuiStyleVar_ItemSpacing,
209 ImVec2{style.ItemSpacing.x, style.ItemSpacing.y});
210 PushStyleVar(ImGuiStyleVar_FramePadding,
211 ImVec2{style.FramePadding.x, style.FramePadding.y});
212
213 SetNextItemWidth(input_width);
214 if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) {
215 if (DataTypeApplyFromText(buf, data_type, p_data, format)) {
216 result.text_changed = true;
217 result.changed = true;
218 }
219 }
220
221 // Check if text input was committed (deactivated after edit)
222 if (IsItemDeactivatedAfterEdit()) {
223 result.text_committed = true;
224 }
225
226 IMGUI_TEST_ENGINE_ITEM_INFO(
227 g.LastItemData.ID, label,
228 g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable);
229
230 // Mouse wheel support - immediate change
231 if (IsItemHovered() && g.IO.MouseWheel != 0.0f) {
232 float scroll_amount = g.IO.MouseWheel;
233 float scroll_speed = 0.25f;
234
235 if (g.IO.KeyCtrl && p_step_fast)
236 scroll_amount *= *(const float*)p_step_fast;
237 else
238 scroll_amount *= *(const float*)p_step;
239
240 if (scroll_amount > 0.0f) {
241 scroll_amount *= scroll_speed;
242 DataTypeApplyOp(data_type, '+', p_data, p_data, &scroll_amount);
243 result.changed = true;
244 result.immediate = true;
245 } else if (scroll_amount < 0.0f) {
246 scroll_amount *= -scroll_speed;
247 DataTypeApplyOp(data_type, '-', p_data, p_data, &scroll_amount);
248 result.changed = true;
249 result.immediate = true;
250 }
251 }
252
253 // Step buttons - immediate change
254 if (!no_step) {
255 const ImVec2 backup_frame_padding = style.FramePadding;
256 style.FramePadding.x = style.FramePadding.y;
257 ImGuiButtonFlags button_flags = ImGuiButtonFlags_PressedOnClick;
258 if (flags & ImGuiInputTextFlags_ReadOnly)
259 BeginDisabled();
260 SameLine(0, style.ItemInnerSpacing.x);
261 if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) {
262 DataTypeApplyOp(data_type, '-', p_data, p_data,
263 g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
264 result.changed = true;
265 result.immediate = true;
266 }
267 SameLine(0, style.ItemInnerSpacing.x);
268 if (ButtonEx("+", ImVec2(button_size, button_size), button_flags)) {
269 DataTypeApplyOp(data_type, '+', p_data, p_data,
270 g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
271 result.changed = true;
272 result.immediate = true;
273 }
274
275 if (flags & ImGuiInputTextFlags_ReadOnly)
276 EndDisabled();
277
278 style.FramePadding = backup_frame_padding;
279 }
280 PopID();
281 EndGroup();
282 ImGui::PopStyleVar(2);
283
284 if (result.changed)
285 MarkItemEdited(g.LastItemData.ID);
286
287 return result;
288}
289} // namespace ImGui
290
291namespace yaze {
292namespace gui {
293
294namespace {
295
296template <typename T>
297bool ApplyHexMouseWheel(T* data, T min_value, T max_value) {
298 if (!ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByPopup)) {
299 return false;
300 }
301
302 const float wheel = ImGui::GetIO().MouseWheel;
303 if (wheel == 0.0f) {
304 return false;
305 }
306
307 using Numeric = long long;
308 Numeric new_value =
309 static_cast<Numeric>(*data) + (wheel > 0.0f ? 1 : -1);
310 new_value = std::clamp(new_value, static_cast<Numeric>(min_value),
311 static_cast<Numeric>(max_value));
312 if (static_cast<T>(new_value) != *data) {
313 *data = static_cast<T>(new_value);
314 ImGui::ClearActiveID();
315 return true;
316 }
317 return false;
318}
319
320} // namespace
321
322const int kStepOneHex = 0x01;
323const int kStepFastHex = 0x0F;
324
325bool InputHex(const char* label, uint64_t* data) {
326 return ImGui::InputScalar(label, ImGuiDataType_U64, data, &kStepOneHex,
327 &kStepFastHex, "%06X",
328 ImGuiInputTextFlags_CharsHexadecimal);
329}
330
331bool InputHex(const char* label, int* data, int num_digits, float input_width) {
332 const std::string format = "%0" + std::to_string(num_digits) + "X";
333 return ImGui::InputScalarLeft(label, ImGuiDataType_S32, data, &kStepOneHex,
334 &kStepFastHex, format.c_str(), input_width,
335 ImGuiInputTextFlags_CharsHexadecimal);
336}
337
338bool InputHexShort(const char* label, uint32_t* data) {
339 return ImGui::InputScalar(label, ImGuiDataType_U32, data, &kStepOneHex,
340 &kStepFastHex, "%06X",
341 ImGuiInputTextFlags_CharsHexadecimal);
342}
343
344bool InputHexWord(const char* label, uint16_t* data, float input_width,
345 bool no_step) {
346 bool changed = ImGui::InputScalarLeft(label, ImGuiDataType_U16, data,
347 &kStepOneHex, &kStepFastHex, "%04X",
348 input_width,
349 ImGuiInputTextFlags_CharsHexadecimal,
350 no_step);
351 bool wheel_changed =
352 ApplyHexMouseWheel<uint16_t>(data, 0u,
353 std::numeric_limits<uint16_t>::max());
354 return changed || wheel_changed;
355}
356
357bool InputHexWord(const char* label, int16_t* data, float input_width,
358 bool no_step) {
359 bool changed = ImGui::InputScalarLeft(label, ImGuiDataType_S16, data,
360 &kStepOneHex, &kStepFastHex, "%04X",
361 input_width,
362 ImGuiInputTextFlags_CharsHexadecimal,
363 no_step);
364 bool wheel_changed = ApplyHexMouseWheel<int16_t>(
365 data, std::numeric_limits<int16_t>::min(),
366 std::numeric_limits<int16_t>::max());
367 return changed || wheel_changed;
368}
369
370bool InputHexByte(const char* label, uint8_t* data, float input_width,
371 bool no_step) {
372 bool changed = ImGui::InputScalarLeft(label, ImGuiDataType_U8, data,
373 &kStepOneHex, &kStepFastHex, "%02X",
374 input_width,
375 ImGuiInputTextFlags_CharsHexadecimal,
376 no_step);
377 bool wheel_changed = ApplyHexMouseWheel<uint8_t>(
378 data, 0u, std::numeric_limits<uint8_t>::max());
379 return changed || wheel_changed;
380}
381
382bool InputHexByte(const char* label, uint8_t* data, uint8_t max_value,
383 float input_width, bool no_step) {
384 bool changed = ImGui::InputScalarLeft(label, ImGuiDataType_U8, data,
385 &kStepOneHex, &kStepFastHex, "%02X",
386 input_width,
387 ImGuiInputTextFlags_CharsHexadecimal,
388 no_step);
389 if (changed && *data > max_value) {
390 *data = max_value;
391 }
392 bool wheel_changed = ApplyHexMouseWheel<uint8_t>(data, 0u, max_value);
393 return changed || wheel_changed;
394}
395
396// Extended versions that properly track change source
397InputHexResult InputHexByteEx(const char* label, uint8_t* data,
398 float input_width, bool no_step) {
399 auto result = ImGui::InputScalarLeftEx(label, ImGuiDataType_U8, data,
400 &kStepOneHex, &kStepFastHex, "%02X",
401 input_width,
402 ImGuiInputTextFlags_CharsHexadecimal,
403 no_step);
404 InputHexResult hex_result;
405 hex_result.changed = result.changed;
406 hex_result.immediate = result.immediate;
407 hex_result.text_committed = result.text_committed;
408 return hex_result;
409}
410
411InputHexResult InputHexByteEx(const char* label, uint8_t* data,
412 uint8_t max_value, float input_width,
413 bool no_step) {
414 auto result = ImGui::InputScalarLeftEx(label, ImGuiDataType_U8, data,
415 &kStepOneHex, &kStepFastHex, "%02X",
416 input_width,
417 ImGuiInputTextFlags_CharsHexadecimal,
418 no_step);
419 if (result.changed && *data > max_value) {
420 *data = max_value;
421 }
422 InputHexResult hex_result;
423 hex_result.changed = result.changed;
424 hex_result.immediate = result.immediate;
425 hex_result.text_committed = result.text_committed;
426 return hex_result;
427}
428
429InputHexResult InputHexWordEx(const char* label, uint16_t* data,
430 float input_width, bool no_step) {
431 auto result = ImGui::InputScalarLeftEx(label, ImGuiDataType_U16, data,
432 &kStepOneHex, &kStepFastHex, "%04X",
433 input_width,
434 ImGuiInputTextFlags_CharsHexadecimal,
435 no_step);
436 InputHexResult hex_result;
437 hex_result.changed = result.changed;
438 hex_result.immediate = result.immediate;
439 hex_result.text_committed = result.text_committed;
440 return hex_result;
441}
442
443void Paragraph(const std::string& text) {
444 ImGui::TextWrapped("%s", text.c_str());
445}
446
447// TODO: Setup themes and text/clickable colors
448bool ClickableText(const std::string& text) {
449 ImGui::BeginGroup();
450 ImGui::PushID(text.c_str());
451
452 // Calculate text size
453 ImVec2 text_size = ImGui::CalcTextSize(text.c_str());
454
455 // Get cursor position for hover detection
456 ImVec2 pos = ImGui::GetCursorScreenPos();
457 ImRect bb(pos, ImVec2(pos.x + text_size.x, pos.y + text_size.y));
458
459 // Add item
460 const ImGuiID id = ImGui::GetID(text.c_str());
461 bool result = false;
462 if (ImGui::ItemAdd(bb, id)) {
463 bool hovered = ImGui::IsItemHovered();
464 bool clicked = ImGui::IsItemClicked();
465
466 // Render text with high-contrast appropriate color
467 ImVec4 link_color = ImGui::GetStyleColorVec4(ImGuiCol_TextLink);
468 ImVec4 bg_color = ImGui::GetStyleColorVec4(ImGuiCol_WindowBg);
469
470 // Ensure good contrast against background
471 float contrast_factor =
472 (bg_color.x + bg_color.y + bg_color.z) < 1.5f ? 1.0f : 0.3f;
473
474 ImVec4 color;
475 if (hovered) {
476 // Brighter color on hover for better visibility
477 color = ImVec4(std::min(1.0f, link_color.x + 0.3f),
478 std::min(1.0f, link_color.y + 0.3f),
479 std::min(1.0f, link_color.z + 0.3f), 1.0f);
480 } else {
481 // Ensure link color has good contrast
482 color = ImVec4(std::max(contrast_factor, link_color.x),
483 std::max(contrast_factor, link_color.y),
484 std::max(contrast_factor, link_color.z), 1.0f);
485 }
486
487 ImGui::GetWindowDrawList()->AddText(
488 pos, ImGui::ColorConvertFloat4ToU32(color), text.c_str());
489
490 result = clicked;
491 }
492
493 ImGui::PopID();
494
495 // Advance cursor past the text
496 ImGui::Dummy(text_size);
497 ImGui::EndGroup();
498
499 return result;
500}
501
502void ItemLabel(absl::string_view title, ItemLabelFlags flags) {
503 ImGuiWindow* window = ImGui::GetCurrentWindow();
504 const ImVec2 lineStart = ImGui::GetCursorScreenPos();
505 const ImGuiStyle& style = ImGui::GetStyle();
506 float fullWidth = ImGui::GetContentRegionAvail().x;
507 float itemWidth = ImGui::CalcItemWidth() + style.ItemSpacing.x;
508 ImVec2 textSize =
509 ImGui::CalcTextSize(title.data(), title.data() + title.size());
510 ImRect textRect;
511 textRect.Min = ImGui::GetCursorScreenPos();
512 if (flags & ItemLabelFlag::Right)
513 textRect.Min.x = textRect.Min.x + itemWidth;
514 textRect.Max = textRect.Min;
515 textRect.Max.x += fullWidth - itemWidth;
516 textRect.Max.y += textSize.y;
517
518 ImGui::SetCursorScreenPos(textRect.Min);
519
520 ImGui::AlignTextToFramePadding();
521 // Adjust text rect manually because we render it directly into a drawlist
522 // instead of using public functions.
523 textRect.Min.y += window->DC.CurrLineTextBaseOffset;
524 textRect.Max.y += window->DC.CurrLineTextBaseOffset;
525
526 ImGui::ItemSize(textRect);
527 if (ImGui::ItemAdd(
528 textRect, window->GetID(title.data(), title.data() + title.size()))) {
529 ImGui::RenderTextEllipsis(ImGui::GetWindowDrawList(), textRect.Min,
530 textRect.Max, textRect.Max.x, title.data(),
531 title.data() + title.size(), &textSize);
532
533 if (textRect.GetWidth() < textSize.x && ImGui::IsItemHovered())
534 ImGui::SetTooltip("%.*s", (int)title.size(), title.data());
535 }
536 if (flags & ItemLabelFlag::Left) {
537 ImVec2 result;
538 auto other = ImVec2{0, textSize.y + window->DC.CurrLineTextBaseOffset};
539 result.x = textRect.Max.x - other.x;
540 result.y = textRect.Max.y - other.y;
541 ImGui::SetCursorScreenPos(result);
542 ImGui::SameLine();
543 } else if (flags & ItemLabelFlag::Right)
544 ImGui::SetCursorScreenPos(lineStart);
545}
546
547bool ListBox(const char* label, int* current_item,
548 const std::vector<std::string>& items, int height_in_items) {
549 std::vector<const char*> items_ptr;
550 items_ptr.reserve(items.size());
551 for (const auto& item : items) {
552 items_ptr.push_back(item.c_str());
553 }
554 int items_count = static_cast<int>(items.size());
555 return ImGui::ListBox(label, current_item, items_ptr.data(), items_count,
556 height_in_items);
557}
558
559bool InputTileInfo(const char* label, gfx::TileInfo* tile_info) {
560 ImGui::PushID(label);
561 ImGui::BeginGroup();
562 bool changed = false;
563 changed |= InputHexWord(label, &tile_info->id_);
564 changed |= InputHexByte("Palette", &tile_info->palette_);
565 changed |= ImGui::Checkbox("Priority", &tile_info->over_);
566 changed |= ImGui::Checkbox("Vertical Flip", &tile_info->vertical_mirror_);
567 changed |= ImGui::Checkbox("Horizontal Flip", &tile_info->horizontal_mirror_);
568 ImGui::EndGroup();
569 ImGui::PopID();
570 return changed;
571}
572
573ImGuiID GetID(const std::string& id) {
574 return ImGui::GetID(id.c_str());
575}
576
577ImGuiKey MapKeyToImGuiKey(char key) {
578 switch (key) {
579 case 'A':
580 return ImGuiKey_A;
581 case 'B':
582 return ImGuiKey_B;
583 case 'C':
584 return ImGuiKey_C;
585 case 'D':
586 return ImGuiKey_D;
587 case 'E':
588 return ImGuiKey_E;
589 case 'F':
590 return ImGuiKey_F;
591 case 'G':
592 return ImGuiKey_G;
593 case 'H':
594 return ImGuiKey_H;
595 case 'I':
596 return ImGuiKey_I;
597 case 'J':
598 return ImGuiKey_J;
599 case 'K':
600 return ImGuiKey_K;
601 case 'L':
602 return ImGuiKey_L;
603 case 'M':
604 return ImGuiKey_M;
605 case 'N':
606 return ImGuiKey_N;
607 case 'O':
608 return ImGuiKey_O;
609 case 'P':
610 return ImGuiKey_P;
611 case 'Q':
612 return ImGuiKey_Q;
613 case 'R':
614 return ImGuiKey_R;
615 case 'S':
616 return ImGuiKey_S;
617 case 'T':
618 return ImGuiKey_T;
619 case 'U':
620 return ImGuiKey_U;
621 case 'V':
622 return ImGuiKey_V;
623 case 'W':
624 return ImGuiKey_W;
625 case 'X':
626 return ImGuiKey_X;
627 case 'Y':
628 return ImGuiKey_Y;
629 case 'Z':
630 return ImGuiKey_Z;
631 case '/':
632 return ImGuiKey_Slash;
633 case '-':
634 return ImGuiKey_Minus;
635 default:
636 return ImGuiKey_COUNT;
637 }
638}
639
640void AddTableColumn(Table& table, const std::string& label,
641 GuiElement element) {
642 table.column_labels.push_back(label);
643 table.column_contents.push_back(element);
644}
645
646void DrawTable(Table& params) {
647 if (ImGui::BeginTable(params.id, params.num_columns, params.flags,
648 params.size)) {
649 for (int i = 0; i < params.num_columns; ++i)
650 ImGui::TableSetupColumn(params.column_labels[i].c_str());
651
652 for (int i = 0; i < params.num_columns; ++i) {
653 ImGui::TableNextColumn();
654 switch (params.column_contents[i].index()) {
655 case 0:
656 std::get<0>(params.column_contents[i])();
657 break;
658 case 1:
659 ImGui::Text("%s", std::get<1>(params.column_contents[i]).c_str());
660 break;
661 }
662 }
663 ImGui::EndTable();
664 }
665}
666
667bool OpenUrl(const std::string& url) {
668 // if iOS
669#ifdef __APPLE__
670#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
671 // no system call on iOS
672 return false;
673#else
674 return system(("open " + url).c_str()) == 0;
675#endif
676#endif
677
678#ifdef __linux__
679 return system(("xdg-open " + url).c_str()) == 0;
680#endif
681
682#ifdef __windows__
683 return system(("start " + url).c_str()) == 0;
684#endif
685
686 return false;
687}
688
689void MemoryEditorPopup(const std::string& label, std::span<uint8_t> memory) {
690 static bool open = false;
691 static yaze::gui::MemoryEditorWidget editor;
692 if (ImGui::Button("View Data")) {
693 open = true;
694 }
695 if (open) {
696 ImGui::Begin(label.c_str(), &open);
697 editor.DrawContents(memory.data(), memory.size());
698 ImGui::End();
699 }
700}
701
702// Custom hex input functions that properly respect width
703bool InputHexByteCustom(const char* label, uint8_t* data, float input_width) {
704 ImGui::PushID(label);
705
706 // Create a simple hex input that respects width
707 char buf[8];
708 snprintf(buf, sizeof(buf), "%02X", *data);
709
710 ImGui::SetNextItemWidth(input_width);
711 bool changed = ImGui::InputText(
712 label, buf, sizeof(buf),
713 ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_AutoSelectAll);
714
715 if (changed) {
716 unsigned int temp;
717 if (sscanf(buf, "%X", &temp) == 1) {
718 *data = static_cast<uint8_t>(temp & 0xFF);
719 }
720 }
721
722 ImGui::PopID();
723 return changed;
724}
725
726bool InputHexWordCustom(const char* label, uint16_t* data, float input_width) {
727 ImGui::PushID(label);
728
729 // Create a simple hex input that respects width
730 char buf[8];
731 snprintf(buf, sizeof(buf), "%04X", *data);
732
733 ImGui::SetNextItemWidth(input_width);
734 bool changed = ImGui::InputText(
735 label, buf, sizeof(buf),
736 ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_AutoSelectAll);
737
738 if (changed) {
739 unsigned int temp;
740 if (sscanf(buf, "%X", &temp) == 1) {
741 *data = static_cast<uint16_t>(temp & 0xFFFF);
742 }
743 }
744
745 ImGui::PopID();
746 return changed;
747}
748
749bool SliderFloatWheel(const char* label, float* v, float v_min, float v_max,
750 const char* format, float wheel_step,
751 ImGuiSliderFlags flags) {
752 bool changed = ImGui::SliderFloat(label, v, v_min, v_max, format, flags);
753
754 // Handle mouse wheel when hovering
755 if (ImGui::IsItemHovered()) {
756 float wheel = ImGui::GetIO().MouseWheel;
757 if (wheel != 0.0f) {
758 *v = std::clamp(*v + wheel * wheel_step, v_min, v_max);
759 changed = true;
760 }
761 }
762 return changed;
763}
764
765bool SliderIntWheel(const char* label, int* v, int v_min, int v_max,
766 const char* format, int wheel_step, ImGuiSliderFlags flags) {
767 bool changed = ImGui::SliderInt(label, v, v_min, v_max, format, flags);
768
769 // Handle mouse wheel when hovering
770 if (ImGui::IsItemHovered()) {
771 float wheel = ImGui::GetIO().MouseWheel;
772 if (wheel != 0.0f) {
773 int delta = static_cast<int>(wheel) * wheel_step;
774 *v = std::clamp(*v + delta, v_min, v_max);
775 changed = true;
776 }
777 }
778 return changed;
779}
780
781} // namespace gui
782} // namespace yaze
SNES 16-bit tile metadata container.
Definition snes_tile.h:50
overloaded(Ts...) -> overloaded< Ts... >
Definition input.cc:22
InputScalarResult InputScalarLeftEx(const char *label, ImGuiDataType data_type, void *p_data, const void *p_step, const void *p_step_fast, const char *format, float input_width, ImGuiInputTextFlags flags, bool no_step=false)
Definition input.cc:165
bool InputScalarLeft(const char *label, ImGuiDataType data_type, void *p_data, const void *p_step, const void *p_step_fast, const char *format, float input_width, ImGuiInputTextFlags flags, bool no_step=false)
Definition input.cc:47
bool ApplyHexMouseWheel(T *data, T min_value, T max_value)
Definition input.cc:297
bool InputHexByteCustom(const char *label, uint8_t *data, float input_width)
Definition input.cc:703
bool ClickableText(const std::string &text)
Definition input.cc:448
bool SliderIntWheel(const char *label, int *v, int v_min, int v_max, const char *format, int wheel_step, ImGuiSliderFlags flags)
Definition input.cc:765
void Paragraph(const std::string &text)
Definition input.cc:443
void ItemLabel(absl::string_view title, ItemLabelFlags flags)
Definition input.cc:502
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
Definition input.cc:344
bool ListBox(const char *label, int *current_item, const std::vector< std::string > &items, int height_in_items)
Definition input.cc:547
bool SliderFloatWheel(const char *label, float *v, float v_min, float v_max, const char *format, float wheel_step, ImGuiSliderFlags flags)
Definition input.cc:749
bool InputHexShort(const char *label, uint32_t *data)
Definition input.cc:338
void AddTableColumn(Table &table, const std::string &label, GuiElement element)
Definition input.cc:640
enum ItemLabelFlag { Left=1u<< 0u, Right=1u<< 1u, Default=Left, } ItemLabelFlags
Definition input.h:82
void MemoryEditorPopup(const std::string &label, std::span< uint8_t > memory)
Definition input.cc:689
const int kStepOneHex
Definition input.cc:322
void DrawTable(Table &params)
Definition input.cc:646
bool OpenUrl(const std::string &url)
Definition input.cc:667
bool InputHexWordCustom(const char *label, uint16_t *data, float input_width)
Definition input.cc:726
bool InputHex(const char *label, uint64_t *data)
Definition input.cc:325
bool InputTileInfo(const char *label, gfx::TileInfo *tile_info)
Definition input.cc:559
std::variant< std::function< void()>, std::string > GuiElement
Definition input.h:94
InputHexResult InputHexByteEx(const char *label, uint8_t *data, float input_width, bool no_step)
Definition input.cc:397
const int kStepFastHex
Definition input.cc:323
ImGuiID GetID(const std::string &id)
Definition input.cc:573
ImGuiKey MapKeyToImGuiKey(char key)
Definition input.cc:577
bool InputHexByte(const char *label, uint8_t *data, float input_width, bool no_step)
Definition input.cc:370
InputHexResult InputHexWordEx(const char *label, uint16_t *data, float input_width, bool no_step)
Definition input.cc:429
void DrawContents(void *mem_data_void, size_t mem_size, size_t base_display_addr=0x0000)
std::vector< std::string > column_labels
Definition input.h:101
std::vector< GuiElement > column_contents
Definition input.h:102
ImVec2 size
Definition input.h:100
int num_columns
Definition input.h:98
const char * id
Definition input.h:97
ImGuiTableFlags flags
Definition input.h:99