mirror of
https://github.com/Drezil/imgui.git
synced 2025-04-21 02:24:02 +00:00
Slider internals: removed unnecessary slider_bb parameter
This commit is contained in:
parent
acbd58627f
commit
731b003c9d
25
imgui.cpp
25
imgui.cpp
@ -5138,7 +5138,7 @@ static inline float RoundScalar(float value, int decimal_precision)
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool SliderBehavior(const ImRect& frame_bb, const ImRect& slider_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, bool horizontal)
|
static bool SliderScalarBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, bool horizontal)
|
||||||
{
|
{
|
||||||
ImGuiState& g = *GImGui;
|
ImGuiState& g = *GImGui;
|
||||||
ImGuiWindow* window = GetCurrentWindow();
|
ImGuiWindow* window = GetCurrentWindow();
|
||||||
@ -5149,15 +5149,16 @@ static bool SliderBehavior(const ImRect& frame_bb, const ImRect& slider_bb, ImGu
|
|||||||
|
|
||||||
const bool is_non_linear = fabsf(power - 1.0f) > 0.0001f;
|
const bool is_non_linear = fabsf(power - 1.0f) > 0.0001f;
|
||||||
|
|
||||||
const float slider_sz = horizontal ? slider_bb.GetWidth() : slider_bb.GetHeight();
|
const float padding = horizontal ? style.FramePadding.x : style.FramePadding.y;
|
||||||
|
const float slider_sz = horizontal ? (frame_bb.GetWidth() - padding * 2.0f) : (frame_bb.GetHeight() - padding * 2.0f);
|
||||||
float grab_sz;
|
float grab_sz;
|
||||||
if (decimal_precision > 0)
|
if (decimal_precision > 0)
|
||||||
grab_sz = ImMin(style.GrabMinSize, slider_sz);
|
grab_sz = ImMin(style.GrabMinSize, slider_sz);
|
||||||
else
|
else
|
||||||
grab_sz = ImMin(ImMax(1.0f * (slider_sz / (v_max-v_min+1.0f)), style.GrabMinSize), slider_sz); // Integer sliders, if possible have the grab size represent 1 unit
|
grab_sz = ImMin(ImMax(1.0f * (slider_sz / (v_max-v_min+1.0f)), style.GrabMinSize), slider_sz); // Integer sliders, if possible have the grab size represent 1 unit
|
||||||
const float slider_usable_sz = slider_sz - grab_sz;
|
const float slider_usable_sz = slider_sz - grab_sz;
|
||||||
const float slider_usable_pos_min = (horizontal ? slider_bb.Min.x : slider_bb.Min.y) + grab_sz*0.5f;
|
const float slider_usable_pos_min = (horizontal ? frame_bb.Min.x : frame_bb.Min.y) + padding + grab_sz*0.5f;
|
||||||
const float slider_usable_pos_max = (horizontal ? slider_bb.Max.x : slider_bb.Max.y) - grab_sz*0.5f;
|
const float slider_usable_pos_max = (horizontal ? frame_bb.Max.x : frame_bb.Max.y) - padding - grab_sz*0.5f;
|
||||||
|
|
||||||
bool value_changed = false;
|
bool value_changed = false;
|
||||||
|
|
||||||
@ -5195,7 +5196,7 @@ static bool SliderBehavior(const ImRect& frame_bb, const ImRect& slider_bb, ImGu
|
|||||||
// Negative: rescale to the negative range before powering
|
// Negative: rescale to the negative range before powering
|
||||||
float a = 1.0f - (normalized_pos / linear_zero_pos);
|
float a = 1.0f - (normalized_pos / linear_zero_pos);
|
||||||
a = powf(a, power);
|
a = powf(a, power);
|
||||||
new_value = ImLerp(ImMin(v_max,0.f), v_min, a);
|
new_value = ImLerp(ImMin(v_max,0.0f), v_min, a);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -5284,7 +5285,6 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
|
|||||||
|
|
||||||
const ImVec2 label_size = CalcTextSize(label, NULL, true);
|
const ImVec2 label_size = CalcTextSize(label, NULL, true);
|
||||||
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f);
|
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y) + style.FramePadding*2.0f);
|
||||||
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
|
|
||||||
const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
|
const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
|
||||||
|
|
||||||
// NB- we don't call ItemSize() yet because we may turn into a text edit box below
|
// NB- we don't call ItemSize() yet because we may turn into a text edit box below
|
||||||
@ -5324,16 +5324,16 @@ bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, c
|
|||||||
ItemSize(total_bb, style.FramePadding.y);
|
ItemSize(total_bb, style.FramePadding.y);
|
||||||
|
|
||||||
// Actual slider behavior + render grab
|
// Actual slider behavior + render grab
|
||||||
const bool value_changed = SliderBehavior(frame_bb, inner_bb, id, v, v_min, v_max, power, decimal_precision, true);
|
const bool value_changed = SliderScalarBehavior(frame_bb, id, v, v_min, v_max, power, decimal_precision, true);
|
||||||
|
|
||||||
// Display 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];
|
||||||
const char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
|
const 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);
|
||||||
RenderTextClipped(ImVec2(ImMax(frame_bb.Min.x + style.FramePadding.x, inner_bb.GetCenter().x - value_text_size.x*0.5f), frame_bb.Min.y + style.FramePadding.y), value_buf, value_buf_end, &value_text_size, frame_bb.Max);
|
RenderTextClipped(ImVec2(ImMax(frame_bb.Min.x + style.FramePadding.x, frame_bb.GetCenter().x - value_text_size.x*0.5f), frame_bb.Min.y + style.FramePadding.y), value_buf, value_buf_end, &value_text_size, frame_bb.Max);
|
||||||
|
|
||||||
if (label_size.x > 0.0f)
|
if (label_size.x > 0.0f)
|
||||||
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label);
|
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
|
||||||
|
|
||||||
return value_changed;
|
return value_changed;
|
||||||
}
|
}
|
||||||
@ -5350,7 +5350,6 @@ bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float
|
|||||||
|
|
||||||
const ImVec2 label_size = CalcTextSize(label, NULL, true);
|
const ImVec2 label_size = CalcTextSize(label, NULL, true);
|
||||||
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size);
|
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + size);
|
||||||
const ImRect slider_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
|
|
||||||
const ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
|
const ImRect bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
|
||||||
|
|
||||||
ItemSize(bb, style.FramePadding.y);
|
ItemSize(bb, style.FramePadding.y);
|
||||||
@ -5373,17 +5372,17 @@ bool ImGui::VSliderFloat(const char* label, const ImVec2& size, float* v, float
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Actual slider behavior + render grab
|
// Actual slider behavior + render grab
|
||||||
bool value_changed = SliderBehavior(frame_bb, slider_bb, id, v, v_min, v_max, power, decimal_precision, false);
|
bool value_changed = SliderScalarBehavior(frame_bb, id, v, v_min, v_max, power, decimal_precision, false);
|
||||||
|
|
||||||
// Display 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.
|
||||||
// For the vertical slider we allow centered text to overlap the frame padding
|
// For the vertical slider we allow centered text to overlap the frame padding
|
||||||
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);
|
||||||
RenderTextClipped(ImVec2(ImMax(frame_bb.Min.x, slider_bb.GetCenter().x - value_text_size.x*0.5f), frame_bb.Min.y + style.FramePadding.y), value_buf, value_buf_end, &value_text_size, frame_bb.Max);
|
RenderTextClipped(ImVec2(ImMax(frame_bb.Min.x, frame_bb.GetCenter().x - value_text_size.x*0.5f), frame_bb.Min.y + style.FramePadding.y), value_buf, value_buf_end, &value_text_size, frame_bb.Max);
|
||||||
|
|
||||||
if (label_size.x > 0.0f)
|
if (label_size.x > 0.0f)
|
||||||
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, slider_bb.Min.y), label);
|
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y), label);
|
||||||
|
|
||||||
return value_changed;
|
return value_changed;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user