From f11ec524661de109483a684c102666a8ea7ecf89 Mon Sep 17 00:00:00 2001 From: scawful Date: Thu, 8 Feb 2024 09:19:45 -0500 Subject: [PATCH] Add a parameter to disable +/- buttons for gui input hex --- src/app/gui/input.cc | 120 +++++++++++++++++++++++-------------------- src/app/gui/input.h | 6 ++- 2 files changed, 69 insertions(+), 57 deletions(-) diff --git a/src/app/gui/input.cc b/src/app/gui/input.cc index 63cfdb72..4da0965a 100644 --- a/src/app/gui/input.cc +++ b/src/app/gui/input.cc @@ -20,7 +20,7 @@ static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter( 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) { + ImGuiInputTextFlags flags, bool no_step = false) { ImGuiWindow* window = ImGui::GetCurrentWindow(); if (window->SkipItems) return false; @@ -39,58 +39,59 @@ bool InputScalarLeft(const char* label, ImGuiDataType data_type, void* p_data, flags |= ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited; bool value_changed = false; - if (p_step == NULL) { - ImGui::SetNextItemWidth(input_width); - if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) - value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); - } else { - const float button_size = GetFrameHeight(); - ImGui::AlignTextToFramePadding(); - ImGui::Text("%s", label); - ImGui::SameLine(); - BeginGroup(); // The only purpose of the group here is to allow the caller - // to query item data e.g. IsItemActive() - PushID(label); - SetNextItemWidth(ImMax( - 1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); + // if (p_step == NULL) { + // ImGui::SetNextItemWidth(input_width); + // if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) + // value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); + // } else { + const float button_size = GetFrameHeight(); + ImGui::AlignTextToFramePadding(); + ImGui::Text("%s", label); + ImGui::SameLine(); + BeginGroup(); // The only purpose of the group here is to allow the caller + // to query item data e.g. IsItemActive() + PushID(label); + SetNextItemWidth(ImMax( + 1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); - // Place the label on the left of the input field - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, - ImVec2{style.ItemSpacing.x, style.ItemSpacing.y}); - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, - ImVec2{style.FramePadding.x, style.FramePadding.y}); + // Place the label on the left of the input field + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, + ImVec2{style.ItemSpacing.x, style.ItemSpacing.y}); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, + ImVec2{style.FramePadding.x, style.FramePadding.y}); - ImGui::SetNextItemWidth(input_width); - if (InputText("", buf, IM_ARRAYSIZE(buf), - flags)) // PushId(label) + "" gives us the expected ID - // from outside point of view - value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); - IMGUI_TEST_ENGINE_ITEM_INFO( - g.LastItemData.ID, label, - g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable); + ImGui::SetNextItemWidth(input_width); + if (InputText("", buf, IM_ARRAYSIZE(buf), + flags)) // PushId(label) + "" gives us the expected ID + // from outside point of view + value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); + IMGUI_TEST_ENGINE_ITEM_INFO( + g.LastItemData.ID, label, + g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable); - // Mouse wheel support - if (IsItemHovered() && g.IO.MouseWheel != 0.0f) { - float scroll_amount = g.IO.MouseWheel; - float scroll_speed = 0.25f; // Adjust the scroll speed as needed + // Mouse wheel support + if (IsItemHovered() && g.IO.MouseWheel != 0.0f) { + float scroll_amount = g.IO.MouseWheel; + float scroll_speed = 0.25f; // Adjust the scroll speed as needed - if (g.IO.KeyCtrl && p_step_fast) - scroll_amount *= *(const float*)p_step_fast; - else - scroll_amount *= *(const float*)p_step; + if (g.IO.KeyCtrl && p_step_fast) + scroll_amount *= *(const float*)p_step_fast; + else + scroll_amount *= *(const float*)p_step; - if (scroll_amount > 0.0f) { - scroll_amount *= scroll_speed; // Adjust the scroll speed as needed - DataTypeApplyOp(data_type, '+', p_data, p_data, &scroll_amount); - value_changed = true; - } else if (scroll_amount < 0.0f) { - scroll_amount *= -scroll_speed; // Adjust the scroll speed as needed - DataTypeApplyOp(data_type, '-', p_data, p_data, &scroll_amount); - value_changed = true; - } + if (scroll_amount > 0.0f) { + scroll_amount *= scroll_speed; // Adjust the scroll speed as needed + DataTypeApplyOp(data_type, '+', p_data, p_data, &scroll_amount); + value_changed = true; + } else if (scroll_amount < 0.0f) { + scroll_amount *= -scroll_speed; // Adjust the scroll speed as needed + DataTypeApplyOp(data_type, '-', p_data, p_data, &scroll_amount); + value_changed = true; } + } - // Step buttons + // Step buttons + if (!no_step) { const ImVec2 backup_frame_padding = style.FramePadding; style.FramePadding.x = style.FramePadding.y; ImGuiButtonFlags button_flags = @@ -108,14 +109,15 @@ bool InputScalarLeft(const char* label, ImGuiDataType data_type, void* p_data, g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step); value_changed = true; } + if (flags & ImGuiInputTextFlags_ReadOnly) EndDisabled(); style.FramePadding = backup_frame_padding; - - PopID(); - EndGroup(); - ImGui::PopStyleVar(2); } + PopID(); + EndGroup(); + ImGui::PopStyleVar(2); + if (value_changed) MarkItemEdited(g.LastItemData.ID); return value_changed; @@ -148,17 +150,25 @@ bool InputHexShort(const char* label, uint32_t* data) { ImGuiInputTextFlags_CharsHexadecimal); } -bool InputHexWord(const char* label, uint16_t* data, float input_width) { +bool InputHexWord(const char* label, uint16_t* data, float input_width, + bool no_step) { return ImGui::InputScalarLeft(label, ImGuiDataType_U16, data, &kStepOneHex, &kStepFastHex, "%04X", input_width, - ImGuiInputTextFlags_CharsHexadecimal); + ImGuiInputTextFlags_CharsHexadecimal, no_step); } -bool InputHexByte(const char* label, uint8_t* data, uint8_t step, - float input_width) { +bool InputHexWord(const char* label, int16_t* data, float input_width, + bool no_step) { + return ImGui::InputScalarLeft(label, ImGuiDataType_S16, data, &kStepOneHex, + &kStepFastHex, "%04X", input_width, + ImGuiInputTextFlags_CharsHexadecimal, no_step); +} + +bool InputHexByte(const char* label, uint8_t* data, float input_width, + bool no_step) { return ImGui::InputScalarLeft(label, ImGuiDataType_U8, data, &kStepOneHex, &kStepFastHex, "%02X", input_width, - ImGuiInputTextFlags_CharsHexadecimal); + ImGuiInputTextFlags_CharsHexadecimal, no_step); } void ItemLabel(absl::string_view title, ItemLabelFlags flags) { diff --git a/src/app/gui/input.h b/src/app/gui/input.h index 5466adcd..823b4a1c 100644 --- a/src/app/gui/input.h +++ b/src/app/gui/input.h @@ -25,9 +25,11 @@ IMGUI_API bool InputHex(const char* label, int* data, int num_digits = 4, float input_width = 50.f); IMGUI_API bool InputHexShort(const char* label, uint32_t* data); IMGUI_API bool InputHexWord(const char* label, uint16_t* data, - float input_width = 50.f); + float input_width = 50.f, bool no_step = false); +IMGUI_API bool InputHexWord(const char* label, int16_t* data, + float input_width = 50.f, bool no_step = false); IMGUI_API bool InputHexByte(const char* label, uint8_t* data, - uint8_t step = 0x01, float input_width = 50.f); + float input_width = 50.f, bool no_step = false); IMGUI_API bool ListBox(const char* label, int* current_item, const std::vector& items,