yaze 0.2.0
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 <functional>
4#include <optional>
5#include <string>
6
7#include "ImGuiFileDialog/ImGuiFileDialog.h"
8#include "absl/strings/string_view.h"
9#include "app/gfx/bitmap.h"
11#include "app/gui/canvas.h"
12#include "app/gui/color.h"
13#include "imgui/imgui.h"
14#include "imgui/imgui_internal.h"
15#include "imgui/misc/cpp/imgui_stdlib.h"
16
17namespace ImGui {
18
19static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter(
20 ImGuiDataType data_type, const char* format) {
21 if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double)
22 return ImGuiInputTextFlags_CharsScientific;
23 const char format_last_char = format[0] ? format[strlen(format) - 1] : 0;
24 return (format_last_char == 'x' || format_last_char == 'X')
25 ? ImGuiInputTextFlags_CharsHexadecimal
26 : ImGuiInputTextFlags_CharsDecimal;
27}
28bool InputScalarLeft(const char* label, ImGuiDataType data_type, void* p_data,
29 const void* p_step, const void* p_step_fast,
30 const char* format, float input_width,
31 ImGuiInputTextFlags flags, bool no_step = false) {
32 ImGuiWindow* window = ImGui::GetCurrentWindow();
33 if (window->SkipItems) return false;
34
35 ImGuiContext& g = *GImGui;
36 ImGuiStyle& style = g.Style;
37
38 if (format == NULL) format = DataTypeGetInfo(data_type)->PrintFmt;
39
40 char buf[64];
41 DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format);
42
43 if (g.ActiveId == 0 && (flags & (ImGuiInputTextFlags_CharsDecimal |
44 ImGuiInputTextFlags_CharsHexadecimal |
45 ImGuiInputTextFlags_CharsScientific)) == 0)
46 flags |= InputScalar_DefaultCharsFilter(data_type, format);
47 flags |= ImGuiInputTextFlags_AutoSelectAll;
48 flags |= ImGuiInputTextFlags_NoMarkEdited;
49
50 bool value_changed = false;
51 // if (p_step == NULL) {
52 // ImGui::SetNextItemWidth(input_width);
53 // if (InputText("", buf, IM_ARRAYSIZE(buf), flags))
54 // value_changed = DataTypeApplyFromText(buf, data_type, p_data, format);
55 // } else {
56 const float button_size = GetFrameHeight();
57 AlignTextToFramePadding();
58 Text("%s", label);
59 SameLine();
60 BeginGroup(); // The only purpose of the group here is to allow the caller
61 // to query item data e.g. IsItemActive()
62 PushID(label);
63 SetNextItemWidth(ImMax(
64 1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));
65
66 // Place the label on the left of the input field
67 PushStyleVar(ImGuiStyleVar_ItemSpacing,
68 ImVec2{style.ItemSpacing.x, style.ItemSpacing.y});
69 PushStyleVar(ImGuiStyleVar_FramePadding,
70 ImVec2{style.FramePadding.x, style.FramePadding.y});
71
72 SetNextItemWidth(input_width);
73 if (InputText("", buf, IM_ARRAYSIZE(buf),
74 flags)) // PushId(label) + "" gives us the expected ID
75 // from outside point of view
76 value_changed = DataTypeApplyFromText(buf, data_type, p_data, format);
77 IMGUI_TEST_ENGINE_ITEM_INFO(
78 g.LastItemData.ID, label,
79 g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable);
80
81 // Mouse wheel support
82 if (IsItemHovered() && g.IO.MouseWheel != 0.0f) {
83 float scroll_amount = g.IO.MouseWheel;
84 float scroll_speed = 0.25f; // Adjust the scroll speed as needed
85
86 if (g.IO.KeyCtrl && p_step_fast)
87 scroll_amount *= *(const float*)p_step_fast;
88 else
89 scroll_amount *= *(const float*)p_step;
90
91 if (scroll_amount > 0.0f) {
92 scroll_amount *= scroll_speed; // Adjust the scroll speed as needed
93 DataTypeApplyOp(data_type, '+', p_data, p_data, &scroll_amount);
94 value_changed = true;
95 } else if (scroll_amount < 0.0f) {
96 scroll_amount *= -scroll_speed; // Adjust the scroll speed as needed
97 DataTypeApplyOp(data_type, '-', p_data, p_data, &scroll_amount);
98 value_changed = true;
99 }
100 }
101
102 // Step buttons
103 if (!no_step) {
104 const ImVec2 backup_frame_padding = style.FramePadding;
105 style.FramePadding.x = style.FramePadding.y;
106 ImGuiButtonFlags button_flags =
107 ImGuiButtonFlags_Repeat | ImGuiButtonFlags_DontClosePopups;
108 if (flags & ImGuiInputTextFlags_ReadOnly) BeginDisabled();
109 SameLine(0, style.ItemInnerSpacing.x);
110 if (ButtonEx("-", ImVec2(button_size, button_size), button_flags)) {
111 DataTypeApplyOp(data_type, '-', p_data, p_data,
112 g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
113 value_changed = true;
114 }
115 SameLine(0, style.ItemInnerSpacing.x);
116 if (ButtonEx("+", ImVec2(button_size, button_size), button_flags)) {
117 DataTypeApplyOp(data_type, '+', p_data, p_data,
118 g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
119 value_changed = true;
120 }
121
122 if (flags & ImGuiInputTextFlags_ReadOnly) EndDisabled();
123
124 style.FramePadding = backup_frame_padding;
125 }
126 PopID();
127 EndGroup();
128 ImGui::PopStyleVar(2);
129
130 if (value_changed) MarkItemEdited(g.LastItemData.ID);
131
132 return value_changed;
133}
134} // namespace ImGui
135
136namespace yaze {
137namespace app {
138namespace gui {
139
140const int kStepOneHex = 0x01;
141const int kStepFastHex = 0x0F;
142
143bool InputHex(const char* label, uint64_t* data) {
144 return ImGui::InputScalar(label, ImGuiDataType_U64, data, &kStepOneHex,
145 &kStepFastHex, "%06X",
146 ImGuiInputTextFlags_CharsHexadecimal);
147}
148
149bool InputHex(const char* label, int* data, int num_digits, float input_width) {
150 const std::string format = "%0" + std::to_string(num_digits) + "X";
151 return ImGui::InputScalarLeft(label, ImGuiDataType_S32, data, &kStepOneHex,
152 &kStepFastHex, format.c_str(), input_width,
153 ImGuiInputTextFlags_CharsHexadecimal);
154}
155
156bool InputHexShort(const char* label, uint32_t* data) {
157 return ImGui::InputScalar(label, ImGuiDataType_U32, data, &kStepOneHex,
158 &kStepFastHex, "%06X",
159 ImGuiInputTextFlags_CharsHexadecimal);
160}
161
162bool InputHexWord(const char* label, uint16_t* data, float input_width,
163 bool no_step) {
164 return ImGui::InputScalarLeft(label, ImGuiDataType_U16, data, &kStepOneHex,
165 &kStepFastHex, "%04X", input_width,
166 ImGuiInputTextFlags_CharsHexadecimal, no_step);
167}
168
169bool InputHexWord(const char* label, int16_t* data, float input_width,
170 bool no_step) {
171 return ImGui::InputScalarLeft(label, ImGuiDataType_S16, data, &kStepOneHex,
172 &kStepFastHex, "%04X", input_width,
173 ImGuiInputTextFlags_CharsHexadecimal, no_step);
174}
175
176bool InputHexByte(const char* label, uint8_t* data, float input_width,
177 bool no_step) {
178 return ImGui::InputScalarLeft(label, ImGuiDataType_U8, data, &kStepOneHex,
179 &kStepFastHex, "%02X", input_width,
180 ImGuiInputTextFlags_CharsHexadecimal, no_step);
181}
182
183bool InputHexByte(const char* label, uint8_t* data, uint8_t max_value,
184 float input_width, bool no_step) {
185 if (ImGui::InputScalarLeft(label, ImGuiDataType_U8, data, &kStepOneHex,
186 &kStepFastHex, "%02X", input_width,
187 ImGuiInputTextFlags_CharsHexadecimal, no_step)) {
188 if (*data > max_value) {
189 *data = max_value;
190 }
191 return true;
192 }
193 return false;
194}
195
196void ItemLabel(absl::string_view title, ItemLabelFlags flags) {
197 ImGuiWindow* window = ImGui::GetCurrentWindow();
198 const ImVec2 lineStart = ImGui::GetCursorScreenPos();
199 const ImGuiStyle& style = ImGui::GetStyle();
200 float fullWidth = ImGui::GetContentRegionAvail().x;
201 float itemWidth = ImGui::CalcItemWidth() + style.ItemSpacing.x;
202 ImVec2 textSize = ImGui::CalcTextSize(title.begin(), title.end());
203 ImRect textRect;
204 textRect.Min = ImGui::GetCursorScreenPos();
205 if (flags & ItemLabelFlag::Right) textRect.Min.x = textRect.Min.x + itemWidth;
206 textRect.Max = textRect.Min;
207 textRect.Max.x += fullWidth - itemWidth;
208 textRect.Max.y += textSize.y;
209
210 ImGui::SetCursorScreenPos(textRect.Min);
211
212 ImGui::AlignTextToFramePadding();
213 // Adjust text rect manually because we render it directly into a drawlist
214 // instead of using public functions.
215 textRect.Min.y += window->DC.CurrLineTextBaseOffset;
216 textRect.Max.y += window->DC.CurrLineTextBaseOffset;
217
218 ImGui::ItemSize(textRect);
219 if (ImGui::ItemAdd(
220 textRect, window->GetID(title.data(), title.data() + title.size()))) {
221 ImGui::RenderTextEllipsis(
222 ImGui::GetWindowDrawList(), textRect.Min, textRect.Max, textRect.Max.x,
223 textRect.Max.x, title.data(), title.data() + title.size(), &textSize);
224
225 if (textRect.GetWidth() < textSize.x && ImGui::IsItemHovered())
226 ImGui::SetTooltip("%.*s", (int)title.size(), title.data());
227 }
228 if (flags & ItemLabelFlag::Left) {
229 ImVec2 result;
230 auto other = ImVec2{0, textSize.y + window->DC.CurrLineTextBaseOffset};
231 result.x = textRect.Max.x - other.x;
232 result.y = textRect.Max.y - other.y;
233 ImGui::SetCursorScreenPos(result);
234 ImGui::SameLine();
235 } else if (flags & ItemLabelFlag::Right)
236 ImGui::SetCursorScreenPos(lineStart);
237}
238
239bool ListBox(const char* label, int* current_item,
240 const std::vector<std::string>& items, int height_in_items) {
241 std::vector<const char*> items_ptr;
242 items_ptr.reserve(items.size());
243 for (const auto& item : items) {
244 items_ptr.push_back(item.c_str());
245 }
246 int items_count = static_cast<int>(items.size());
247 return ImGui::ListBox(label, current_item, items_ptr.data(), items_count,
248 height_in_items);
249}
250
251ImGuiID GetID(const std::string& id) { return ImGui::GetID(id.c_str()); }
252
253void FileDialogPipeline(absl::string_view display_key,
254 absl::string_view file_extensions,
255 std::optional<absl::string_view> button_text,
256 std::function<void()> callback) {
257 if (button_text.has_value() && ImGui::Button(button_text->data())) {
258 ImGuiFileDialog::Instance()->OpenDialog(display_key.data(), "Choose File",
259 file_extensions.data(), ".");
260 }
261
262 if (ImGuiFileDialog::Instance()->Display(
263 display_key.data(), ImGuiWindowFlags_NoCollapse, ImVec2(600, 400))) {
264 if (ImGuiFileDialog::Instance()->IsOk()) {
265 callback();
266 }
267 ImGuiFileDialog::Instance()->Close();
268 }
269}
270
271} // namespace gui
272} // namespace app
273} // namespace yaze
Definition input.cc:17
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:28
bool InputHexByte(const char *label, uint8_t *data, float input_width, bool no_step)
Definition input.cc:176
void ItemLabel(absl::string_view title, ItemLabelFlags flags)
Definition input.cc:196
const int kStepFastHex
Definition input.cc:141
bool InputHex(const char *label, uint64_t *data)
Definition input.cc:143
void FileDialogPipeline(absl::string_view display_key, absl::string_view file_extensions, std::optional< absl::string_view > button_text, std::function< void()> callback)
Definition input.cc:253
bool InputHexShort(const char *label, uint32_t *data)
Definition input.cc:156
ImGuiID GetID(const std::string &id)
Definition input.cc:251
const int kStepOneHex
Definition input.cc:140
enum ItemLabelFlag { Left=1u<< 0u, Right=1u<< 1u, Default=Left, } ItemLabelFlags
Definition input.h:48
bool ListBox(const char *label, int *current_item, const std::vector< std::string > &items, int height_in_items)
Definition input.cc:239
bool InputHexWord(const char *label, uint16_t *data, float input_width, bool no_step)
Definition input.cc:162
Definition common.cc:21