Add scroll wheel support to input hex fields

This commit is contained in:
scawful
2024-01-25 20:59:39 -05:00
parent f45e115176
commit 7231999cf3
4 changed files with 39 additions and 17 deletions

View File

@@ -17,7 +17,6 @@ static inline ImGuiInputTextFlags InputScalar_DefaultCharsFilter(
? ImGuiInputTextFlags_CharsHexadecimal
: ImGuiInputTextFlags_CharsDecimal;
}
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,
@@ -70,6 +69,27 @@ bool InputScalarLeft(const char* label, ImGuiDataType data_type, void* p_data,
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
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;
}
}
// Step buttons
const ImVec2 backup_frame_padding = style.FramePadding;
style.FramePadding.x = style.FramePadding.y;
@@ -129,7 +149,7 @@ bool InputHexWord(const char* label, uint16_t* data, float input_width) {
bool InputHexByte(const char* label, uint8_t* data, uint8_t step,
float input_width) {
return ImGui::InputScalarLeft(label, ImGuiDataType_U8, data, &step,
return ImGui::InputScalarLeft(label, ImGuiDataType_U8, data, &kStepOneHex,
&kStepFastHex, "%02X", input_width,
ImGuiInputTextFlags_CharsHexadecimal);
}

View File

@@ -16,6 +16,10 @@ namespace gui {
constexpr ImVec2 kDefaultModalSize = ImVec2(200, 0);
constexpr ImVec2 kZeroPos = ImVec2(0, 0);
IMGUI_API bool InputHexWithScrollwheel(const char* label, uint32_t* data,
uint32_t step = 0x01,
float input_width = 50.f);
IMGUI_API bool InputHex(const char* label, uint64_t* data);
IMGUI_API bool InputHexShort(const char* label, uint32_t* data);
IMGUI_API bool InputHexWord(const char* label, uint16_t* data,