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 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?
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);
// 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;
if (is_logarithmic)
{
// Account for logarithmic scale on both sides of the zero
if (normalized_pos < linear_zero_pos)
{
// 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);
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
// 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)
{
// Linear slider
// const float grab_t = (ImClamp(*v, v_min, v_max) - v_min) / (v_max - v_min);
// Calculate slider grab positioning
float grab_t;
if (is_logarithmic)
{
float v_clamped = ImClamp(*v, v_min, v_max);
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));
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
const float grab_x = ImLerp(slider_effective_x1, slider_effective_x2, grab_t);

Loading…
Cancel
Save