Drags, Sliders: Support ImGuiSliderFlags_Logarithmic flag with integers. Because why not? (#3786)

Renamed is_decimal to is_floating_point.
This commit is contained in:
ocornut 2021-03-15 12:21:10 +01:00
parent 287bd9b984
commit d94644261d
3 changed files with 15 additions and 17 deletions

View File

@ -86,6 +86,7 @@ Other Changes:
format specifier (e.g. using "%f123" as a format string). [@rokups] format specifier (e.g. using "%f123" as a format string). [@rokups]
- Drags, Sliders: Fixed a bug where using custom formatting flags (',$,_) supported by stb_sprintf.h - Drags, Sliders: Fixed a bug where using custom formatting flags (',$,_) supported by stb_sprintf.h
would cause incorrect value to be displayed. (#3604) [@rokups] would cause incorrect value to be displayed. (#3604) [@rokups]
- Drags, Sliders: Support ImGuiSliderFlags_Logarithmic flag with integers. Because why not? (#3786)
- Tables: Fixed unaligned accesses when using TableSetBgColor(ImGuiTableBgTarget_CellBg). (#3872) - Tables: Fixed unaligned accesses when using TableSetBgColor(ImGuiTableBgTarget_CellBg). (#3872)
- IsItemHovered(): fixed return value false positive when used after EndChild(), EndGroup() or widgets using - IsItemHovered(): fixed return value false positive when used after EndChild(), EndGroup() or widgets using
either of them, when the hovered location is located within a child window, e.g. InputTextMultiline(). either of them, when the hovered location is located within a child window, e.g. InputTextMultiline().

View File

@ -81,9 +81,6 @@ You may use the [docking](https://github.com/ocornut/imgui/tree/docking) branch
Many projects are using this branch and it is kept in sync with master regularly. Many projects are using this branch and it is kept in sync with master regularly.
You may merge in the [tables](https://github.com/ocornut/imgui/tree/tables) branch which includes:
- [Table features](https://github.com/ocornut/imgui/issues/2957)
##### [Return to Index](#index) ##### [Return to Index](#index)
---- ----

View File

@ -2143,9 +2143,9 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X;
const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);
const bool is_clamped = (v_min < v_max); const bool is_clamped = (v_min < v_max);
const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) && is_decimal; const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0;
const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);
// Default tweak speed // Default tweak speed
if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX)) if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX))
@ -2163,7 +2163,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const
} }
else if (g.ActiveIdSource == ImGuiInputSource_Nav) else if (g.ActiveIdSource == ImGuiInputSource_Nav)
{ {
int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0;
adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis]; adjust_delta = GetNavInputAmount2d(ImGuiNavDirSourceFlags_Keyboard | ImGuiNavDirSourceFlags_PadDPad, ImGuiInputReadMode_RepeatFast, 1.0f / 10.0f, 10.0f)[axis];
v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision)); v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision));
} }
@ -2203,7 +2203,7 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const
if (is_logarithmic) if (is_logarithmic)
{ {
// When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound. // When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound.
const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 1; const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 1;
logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision); logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision);
// Convert to parametric space, apply delta, convert back // Convert to parametric space, apply delta, convert back
@ -2241,9 +2241,9 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const
// Clamp values (+ handle overflow/wrap-around for integer types) // Clamp values (+ handle overflow/wrap-around for integer types)
if (*v != v_cur && is_clamped) if (*v != v_cur && is_clamped)
{ {
if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_decimal)) if (v_cur < v_min || (v_cur > *v && adjust_delta < 0.0f && !is_floating_point))
v_cur = v_min; v_cur = v_min;
if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_decimal)) if (v_cur > v_max || (v_cur < *v && adjust_delta > 0.0f && !is_floating_point))
v_cur = v_max; v_cur = v_max;
} }
@ -2634,7 +2634,7 @@ TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, T
{ {
if (v_min == v_max) if (v_min == v_max)
return v_min; return v_min;
const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);
TYPE result; TYPE result;
if (is_logarithmic) if (is_logarithmic)
@ -2682,7 +2682,7 @@ TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, T
else else
{ {
// Linear slider // Linear slider
if (is_decimal) if (is_floating_point)
{ {
result = ImLerp(v_min, v_max, t); result = ImLerp(v_min, v_max, t);
} }
@ -2715,14 +2715,14 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X; const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X;
const bool is_decimal = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double); const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0;
const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) && is_decimal; const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);
const float grab_padding = 2.0f; const float grab_padding = 2.0f;
const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f; const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f;
float grab_sz = style.GrabMinSize; float grab_sz = style.GrabMinSize;
SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max); SIGNEDTYPE v_range = (v_min < v_max ? v_max - v_min : v_min - v_max);
if (!is_decimal && v_range >= 0) // v_range < 0 may happen on integer overflows if (!is_floating_point && v_range >= 0) // v_range < 0 may happen on integer overflows
grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit grab_sz = ImMax((float)(slider_sz / (v_range + 1)), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit
grab_sz = ImMin(grab_sz, slider_sz); grab_sz = ImMin(grab_sz, slider_sz);
const float slider_usable_sz = slider_sz - grab_sz; const float slider_usable_sz = slider_sz - grab_sz;
@ -2734,7 +2734,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ
if (is_logarithmic) if (is_logarithmic)
{ {
// When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound. // When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound.
const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 1; const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 1;
logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision); logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision);
zero_deadzone_halfsize = (style.LogSliderDeadzone * 0.5f) / ImMax(slider_usable_sz, 1.0f); zero_deadzone_halfsize = (style.LogSliderDeadzone * 0.5f) / ImMax(slider_usable_sz, 1.0f);
} }
@ -2772,7 +2772,7 @@ bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_typ
float input_delta = (axis == ImGuiAxis_X) ? input_delta2.x : -input_delta2.y; float input_delta = (axis == ImGuiAxis_X) ? input_delta2.x : -input_delta2.y;
if (input_delta != 0.0f) if (input_delta != 0.0f)
{ {
const int decimal_precision = is_decimal ? ImParseFormatPrecision(format, 3) : 0; const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0;
if (decimal_precision > 0) if (decimal_precision > 0)
{ {
input_delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds input_delta /= 100.0f; // Gamepad/keyboard tweak speeds in % of slider bounds