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

docking
ocornut 10 years ago
parent aaca73de15
commit 110d96034b

@ -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,28 +4831,33 @@ 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 (normalized_pos < linear_zero_pos) if (is_logarithmic)
{ {
// Negative: rescale to the negative range before powering // Account for logarithmic scale on both sides of the zero
float a = 1.0f - (normalized_pos / linear_zero_pos); if (normalized_pos < linear_zero_pos)
a = powf(a, power); {
new_value = ImLerp(ImMin(v_max,0.f), v_min, a); // Negative: rescale to the negative range before powering
float a = 1.0f - (normalized_pos / linear_zero_pos);
a = powf(a, power);
new_value = ImLerp(ImMin(v_max,0.f), v_min, a);
}
else
{
// Positive: rescale to the positive range before powering
float a;
if (fabsf(linear_zero_pos - 1.0f) > 1.e-6)
a = (normalized_pos - linear_zero_pos) / (1.0f - linear_zero_pos);
else
a = normalized_pos;
a = powf(a, power);
new_value = ImLerp(ImMax(v_min,0.0f), v_max, a);
}
} }
else else
{ {
// Positive: rescale to the positive range before powering // Linear slider
float a; new_value = ImLerp(v_min, v_max, normalized_pos);
if (fabsf(linear_zero_pos - 1.0f) > 1.e-6)
a = (normalized_pos - linear_zero_pos) / (1.0f - linear_zero_pos);
else
a = normalized_pos;
a = powf(a, power);
new_value = ImLerp(ImMax(v_min,0.0f), v_max, a);
} }
// Round past decimal precision // Round past decimal precision
@ -4879,21 +4885,26 @@ 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;
float v_clamped = ImClamp(*v, v_min, v_max); if (is_logarithmic)
if (v_clamped < 0.0f)
{ {
const float f = 1.0f - (v_clamped - v_min) / (ImMin(0.0f,v_max) - v_min); float v_clamped = ImClamp(*v, v_min, v_max);
grab_t = (1.0f - powf(f, 1.0f/power)) * linear_zero_pos; if (v_clamped < 0.0f)
{
const float f = 1.0f - (v_clamped - v_min) / (ImMin(0.0f,v_max) - v_min);
grab_t = (1.0f - powf(f, 1.0f/power)) * linear_zero_pos;
}
else
{
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);
}
} }
else else
{ {
const float f = (v_clamped - ImMax(0.0f,v_min)) / (v_max - ImMax(0.0f,v_min)); // Linear slider
grab_t = linear_zero_pos + powf(f, 1.0f/power) * (1.0f - linear_zero_pos); grab_t = (ImClamp(*v, v_min, v_max) - v_min) / (v_max - v_min);
} }
// Draw // Draw

Loading…
Cancel
Save