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,
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) {