Add a parameter to disable +/- buttons for gui input hex

This commit is contained in:
scawful
2024-02-08 09:19:45 -05:00
parent dc74483f2c
commit f11ec52466
2 changed files with 69 additions and 57 deletions

View File

@@ -20,7 +20,7 @@ static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter(
bool InputScalarLeft(const char* label, ImGuiDataType data_type, void* p_data, bool InputScalarLeft(const char* label, ImGuiDataType data_type, void* p_data,
const void* p_step, const void* p_step_fast, const void* p_step, const void* p_step_fast,
const char* format, float input_width, const char* format, float input_width,
ImGuiInputTextFlags flags) { ImGuiInputTextFlags flags, bool no_step = false) {
ImGuiWindow* window = ImGui::GetCurrentWindow(); ImGuiWindow* window = ImGui::GetCurrentWindow();
if (window->SkipItems) return false; 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; flags |= ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_NoMarkEdited;
bool value_changed = false; bool value_changed = false;
if (p_step == NULL) { // if (p_step == NULL) {
ImGui::SetNextItemWidth(input_width); // ImGui::SetNextItemWidth(input_width);
if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) // if (InputText("", buf, IM_ARRAYSIZE(buf), flags))
value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); // value_changed = DataTypeApplyFromText(buf, data_type, p_data, format);
} else { // } else {
const float button_size = GetFrameHeight(); const float button_size = GetFrameHeight();
ImGui::AlignTextToFramePadding(); ImGui::AlignTextToFramePadding();
ImGui::Text("%s", label); ImGui::Text("%s", label);
ImGui::SameLine(); ImGui::SameLine();
BeginGroup(); // The only purpose of the group here is to allow the caller BeginGroup(); // The only purpose of the group here is to allow the caller
// to query item data e.g. IsItemActive() // to query item data e.g. IsItemActive()
PushID(label); PushID(label);
SetNextItemWidth(ImMax( SetNextItemWidth(ImMax(
1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); 1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));
// Place the label on the left of the input field // Place the label on the left of the input field
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing,
ImVec2{style.ItemSpacing.x, style.ItemSpacing.y}); ImVec2{style.ItemSpacing.x, style.ItemSpacing.y});
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImGui::PushStyleVar(ImGuiStyleVar_FramePadding,
ImVec2{style.FramePadding.x, style.FramePadding.y}); ImVec2{style.FramePadding.x, style.FramePadding.y});
ImGui::SetNextItemWidth(input_width); ImGui::SetNextItemWidth(input_width);
if (InputText("", buf, IM_ARRAYSIZE(buf), if (InputText("", buf, IM_ARRAYSIZE(buf),
flags)) // PushId(label) + "" gives us the expected ID flags)) // PushId(label) + "" gives us the expected ID
// from outside point of view // from outside point of view
value_changed = DataTypeApplyFromText(buf, data_type, p_data, format); value_changed = DataTypeApplyFromText(buf, data_type, p_data, format);
IMGUI_TEST_ENGINE_ITEM_INFO( IMGUI_TEST_ENGINE_ITEM_INFO(
g.LastItemData.ID, label, g.LastItemData.ID, label,
g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable); g.LastItemData.StatusFlags | ImGuiItemStatusFlags_Inputable);
// Mouse wheel support // Mouse wheel support
if (IsItemHovered() && g.IO.MouseWheel != 0.0f) { if (IsItemHovered() && g.IO.MouseWheel != 0.0f) {
float scroll_amount = g.IO.MouseWheel; float scroll_amount = g.IO.MouseWheel;
float scroll_speed = 0.25f; // Adjust the scroll speed as needed float scroll_speed = 0.25f; // Adjust the scroll speed as needed
if (g.IO.KeyCtrl && p_step_fast) if (g.IO.KeyCtrl && p_step_fast)
scroll_amount *= *(const float*)p_step_fast; scroll_amount *= *(const float*)p_step_fast;
else else
scroll_amount *= *(const float*)p_step; scroll_amount *= *(const float*)p_step;
if (scroll_amount > 0.0f) { if (scroll_amount > 0.0f) {
scroll_amount *= scroll_speed; // Adjust the scroll speed as needed scroll_amount *= scroll_speed; // Adjust the scroll speed as needed
DataTypeApplyOp(data_type, '+', p_data, p_data, &scroll_amount); DataTypeApplyOp(data_type, '+', p_data, p_data, &scroll_amount);
value_changed = true; value_changed = true;
} else if (scroll_amount < 0.0f) { } else if (scroll_amount < 0.0f) {
scroll_amount *= -scroll_speed; // Adjust the scroll speed as needed scroll_amount *= -scroll_speed; // Adjust the scroll speed as needed
DataTypeApplyOp(data_type, '-', p_data, p_data, &scroll_amount); DataTypeApplyOp(data_type, '-', p_data, p_data, &scroll_amount);
value_changed = true; value_changed = true;
}
} }
}
// Step buttons // Step buttons
if (!no_step) {
const ImVec2 backup_frame_padding = style.FramePadding; const ImVec2 backup_frame_padding = style.FramePadding;
style.FramePadding.x = style.FramePadding.y; style.FramePadding.x = style.FramePadding.y;
ImGuiButtonFlags button_flags = 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); g.IO.KeyCtrl && p_step_fast ? p_step_fast : p_step);
value_changed = true; value_changed = true;
} }
if (flags & ImGuiInputTextFlags_ReadOnly) EndDisabled(); if (flags & ImGuiInputTextFlags_ReadOnly) EndDisabled();
style.FramePadding = backup_frame_padding; style.FramePadding = backup_frame_padding;
PopID();
EndGroup();
ImGui::PopStyleVar(2);
} }
PopID();
EndGroup();
ImGui::PopStyleVar(2);
if (value_changed) MarkItemEdited(g.LastItemData.ID); if (value_changed) MarkItemEdited(g.LastItemData.ID);
return value_changed; return value_changed;
@@ -148,17 +150,25 @@ bool InputHexShort(const char* label, uint32_t* data) {
ImGuiInputTextFlags_CharsHexadecimal); 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, return ImGui::InputScalarLeft(label, ImGuiDataType_U16, data, &kStepOneHex,
&kStepFastHex, "%04X", input_width, &kStepFastHex, "%04X", input_width,
ImGuiInputTextFlags_CharsHexadecimal); ImGuiInputTextFlags_CharsHexadecimal, no_step);
} }
bool InputHexByte(const char* label, uint8_t* data, uint8_t step, bool InputHexWord(const char* label, int16_t* data, float input_width,
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, return ImGui::InputScalarLeft(label, ImGuiDataType_U8, data, &kStepOneHex,
&kStepFastHex, "%02X", input_width, &kStepFastHex, "%02X", input_width,
ImGuiInputTextFlags_CharsHexadecimal); ImGuiInputTextFlags_CharsHexadecimal, no_step);
} }
void ItemLabel(absl::string_view title, ItemLabelFlags flags) { void ItemLabel(absl::string_view title, ItemLabelFlags flags) {

View File

@@ -25,9 +25,11 @@ IMGUI_API bool InputHex(const char* label, int* data, int num_digits = 4,
float input_width = 50.f); float input_width = 50.f);
IMGUI_API bool InputHexShort(const char* label, uint32_t* data); IMGUI_API bool InputHexShort(const char* label, uint32_t* data);
IMGUI_API bool InputHexWord(const char* label, uint16_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, 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, IMGUI_API bool ListBox(const char* label, int* current_item,
const std::vector<std::string>& items, const std::vector<std::string>& items,