1
0
mirror of https://github.com/ocornut/imgui.git synced 2024-12-01 02:37:24 +01:00

SliderFloat() fast-path when power=1.0f (no powf() calls) also makes code easier to read

This commit is contained in:
ocornut 2015-03-13 13:28:30 +00:00
parent aaca73de15
commit 110d96034b

View File

@ -4763,6 +4763,7 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id); const bool tab_focus_requested = window->FocusItemRegister(g.ActiveId == id);
const bool is_unbound = v_min == -FLT_MAX || v_min == FLT_MAX || v_max == -FLT_MAX || v_max == FLT_MAX; const bool is_unbound = v_min == -FLT_MAX || v_min == FLT_MAX || v_max == -FLT_MAX || v_max == FLT_MAX;
const bool is_logarithmic = abs(power - 1.0f) > 0.0001f;
const float grab_size_in_units = 1.0f; // In 'v' units. Probably needs to be parametrized, based on a 'v_step' value? decimal precision? const float grab_size_in_units = 1.0f; // In 'v' units. Probably needs to be parametrized, based on a 'v_step' value? decimal precision?
float grab_size_in_pixels; float grab_size_in_pixels;
@ -4830,11 +4831,10 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
{ {
const float normalized_pos = ImClamp((g.IO.MousePos.x - slider_effective_x1) / slider_effective_w, 0.0f, 1.0f); const float normalized_pos = ImClamp((g.IO.MousePos.x - slider_effective_x1) / slider_effective_w, 0.0f, 1.0f);
// Linear slider
//float new_value = ImLerp(v_min, v_max, normalized_pos);
// Account for logarithmic scale on both sides of the zero
float new_value; float new_value;
if (is_logarithmic)
{
// Account for logarithmic scale on both sides of the zero
if (normalized_pos < linear_zero_pos) if (normalized_pos < linear_zero_pos)
{ {
// Negative: rescale to the negative range before powering // Negative: rescale to the negative range before powering
@ -4853,6 +4853,12 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
a = powf(a, power); a = powf(a, power);
new_value = ImLerp(ImMax(v_min,0.0f), v_max, a); new_value = ImLerp(ImMax(v_min,0.0f), v_max, a);
} }
}
else
{
// Linear slider
new_value = ImLerp(v_min, v_max, normalized_pos);
}
// Round past decimal precision // Round past decimal precision
// 0->1, 1->0.1, 2->0.01, etc. // 0->1, 1->0.1, 2->0.01, etc.
@ -4879,11 +4885,10 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
if (!is_unbound) if (!is_unbound)
{ {
// Linear slider
// const float grab_t = (ImClamp(*v, v_min, v_max) - v_min) / (v_max - v_min);
// Calculate slider grab positioning // Calculate slider grab positioning
float grab_t; float grab_t;
if (is_logarithmic)
{
float v_clamped = ImClamp(*v, v_min, v_max); float v_clamped = ImClamp(*v, v_min, v_max);
if (v_clamped < 0.0f) if (v_clamped < 0.0f)
{ {
@ -4895,6 +4900,12 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
const float f = (v_clamped - ImMax(0.0f,v_min)) / (v_max - ImMax(0.0f,v_min)); const float f = (v_clamped - ImMax(0.0f,v_min)) / (v_max - ImMax(0.0f,v_min));
grab_t = linear_zero_pos + powf(f, 1.0f/power) * (1.0f - linear_zero_pos); grab_t = linear_zero_pos + powf(f, 1.0f/power) * (1.0f - linear_zero_pos);
} }
}
else
{
// Linear slider
grab_t = (ImClamp(*v, v_min, v_max) - v_min) / (v_max - v_min);
}
// Draw // Draw
const float grab_x = ImLerp(slider_effective_x1, slider_effective_x2, grab_t); const float grab_x = ImLerp(slider_effective_x1, slider_effective_x2, grab_t);