mirror of
https://github.com/Drezil/imgui.git
synced 2024-11-15 01:17:00 +00:00
Slider code syntax tweaks
This commit is contained in:
parent
eff466d77d
commit
72855becab
85
imgui.cpp
85
imgui.cpp
@ -4827,56 +4827,53 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
|
|||||||
// Process clicking on the slider
|
// Process clicking on the slider
|
||||||
if (g.ActiveId == id)
|
if (g.ActiveId == id)
|
||||||
{
|
{
|
||||||
if (g.IO.MouseDown[0])
|
if (!is_unbound && g.IO.MouseDown[0])
|
||||||
{
|
{
|
||||||
if (!is_unbound)
|
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);
|
|
||||||
|
|
||||||
float new_value;
|
float new_value;
|
||||||
if (is_logarithmic)
|
if (is_logarithmic)
|
||||||
|
{
|
||||||
|
// Account for logarithmic scale on both sides of the zero
|
||||||
|
if (normalized_pos < linear_zero_pos)
|
||||||
{
|
{
|
||||||
// Account for logarithmic scale on both sides of the zero
|
// Negative: rescale to the negative range before powering
|
||||||
if (normalized_pos < linear_zero_pos)
|
float a = 1.0f - (normalized_pos / linear_zero_pos);
|
||||||
{
|
a = powf(a, power);
|
||||||
// Negative: rescale to the negative range before powering
|
new_value = ImLerp(ImMin(v_max,0.f), v_min, a);
|
||||||
float a = 1.0f - (normalized_pos / linear_zero_pos);
|
}
|
||||||
a = powf(a, power);
|
else
|
||||||
new_value = ImLerp(ImMin(v_max,0.f), v_min, a);
|
{
|
||||||
}
|
// 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
|
else
|
||||||
{
|
a = normalized_pos;
|
||||||
// Positive: rescale to the positive range before powering
|
a = powf(a, power);
|
||||||
float a;
|
new_value = ImLerp(ImMax(v_min,0.0f), v_max, 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
|
|
||||||
{
|
|
||||||
// Linear slider
|
|
||||||
new_value = ImLerp(v_min, v_max, normalized_pos);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
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.
|
||||||
// So when our value is 1.99999 with a precision of 0.001 we'll end up rounding to 2.0
|
// So when our value is 1.99999 with a precision of 0.001 we'll end up rounding to 2.0
|
||||||
const float min_step = 1.0f / powf(10.0f, (float)decimal_precision);
|
const float min_step = 1.0f / powf(10.0f, (float)decimal_precision);
|
||||||
const float remainder = fmodf(new_value, min_step);
|
const float remainder = fmodf(new_value, min_step);
|
||||||
if (remainder <= min_step*0.5f)
|
if (remainder <= min_step*0.5f)
|
||||||
new_value -= remainder;
|
new_value -= remainder;
|
||||||
else
|
else
|
||||||
new_value += (min_step - remainder);
|
new_value += (min_step - remainder);
|
||||||
|
|
||||||
if (*v != new_value)
|
if (*v != new_value)
|
||||||
{
|
{
|
||||||
*v = new_value;
|
*v = new_value;
|
||||||
value_changed = true;
|
value_changed = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -4915,7 +4912,7 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
|
|||||||
window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, window->Color(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab));
|
window->DrawList->AddRectFilled(grab_bb.Min, grab_bb.Max, window->Color(g.ActiveId == id ? ImGuiCol_SliderGrabActive : ImGuiCol_SliderGrab));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw value using user-provided display format so user can add prefix/suffix/decorations to the value.
|
// Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
|
||||||
char value_buf[64];
|
char value_buf[64];
|
||||||
char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
|
char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
|
||||||
const ImVec2 value_text_size = CalcTextSize(value_buf, value_buf_end, true);
|
const ImVec2 value_text_size = CalcTextSize(value_buf, value_buf_end, true);
|
||||||
|
Loading…
Reference in New Issue
Block a user