MCPcopy Create free account
hub / github.com/KebsCS/KBotExt / SliderBehaviorT

Method SliderBehaviorT

KBotExt/imgui/imgui_widgets.cpp:2767–2938  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2765// FIXME: Try to move more of the code into shared SliderBehavior()
2766template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>
2767bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType data_type, TYPE* v, const TYPE v_min, const TYPE v_max, const char* format, ImGuiSliderFlags flags, ImRect* out_grab_bb)
2768{
2769 ImGuiContext& g = *GImGui;
2770 const ImGuiStyle& style = g.Style;
2771
2772 const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X;
2773 const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0;
2774 const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);
2775 const float v_range_f = (float)(v_min < v_max ? v_max - v_min : v_min - v_max); // We don't need high precision for what we do with it.
2776
2777 // Calculate bounds
2778 const float grab_padding = 2.0f; // FIXME: Should be part of style.
2779 const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f;
2780 float grab_sz = style.GrabMinSize;
2781 if (!is_floating_point && v_range_f >= 0.0f) // v_range_f < 0 may happen on integer overflows
2782 grab_sz = ImMax(slider_sz / (v_range_f + 1), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit
2783 grab_sz = ImMin(grab_sz, slider_sz);
2784 const float slider_usable_sz = slider_sz - grab_sz;
2785 const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f;
2786 const float slider_usable_pos_max = bb.Max[axis] - grab_padding - grab_sz * 0.5f;
2787
2788 float logarithmic_zero_epsilon = 0.0f; // Only valid when is_logarithmic is true
2789 float zero_deadzone_halfsize = 0.0f; // Only valid when is_logarithmic is true
2790 if (is_logarithmic)
2791 {
2792 // When using logarithmic sliders, we need to clamp to avoid hitting zero, but our choice of clamp value greatly affects slider precision. We attempt to use the specified precision to estimate a good lower bound.
2793 const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 1;
2794 logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision);
2795 zero_deadzone_halfsize = (style.LogSliderDeadzone * 0.5f) / ImMax(slider_usable_sz, 1.0f);
2796 }
2797
2798 // Process interacting with the slider
2799 bool value_changed = false;
2800 if (g.ActiveId == id)
2801 {
2802 bool set_new_value = false;
2803 float clicked_t = 0.0f;
2804 if (g.ActiveIdSource == ImGuiInputSource_Mouse)
2805 {
2806 if (!g.IO.MouseDown[0])
2807 {
2808 ClearActiveID();
2809 }
2810 else
2811 {
2812 const float mouse_abs_pos = g.IO.MousePos[axis];
2813 if (g.ActiveIdIsJustActivated)
2814 {
2815 float grab_t = ScaleRatioFromValueT<TYPE, SIGNEDTYPE, FLOATTYPE>(data_type, *v, v_min, v_max, is_logarithmic, logarithmic_zero_epsilon, zero_deadzone_halfsize);
2816 if (axis == ImGuiAxis_Y)
2817 grab_t = 1.0f - grab_t;
2818 const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t);
2819 const bool clicked_around_grab = (mouse_abs_pos >= grab_pos - grab_sz * 0.5f - 1.0f) && (mouse_abs_pos <= grab_pos + grab_sz * 0.5f + 1.0f); // No harm being extra generous here.
2820 g.SliderGrabClickOffset = (clicked_around_grab && is_floating_point) ? mouse_abs_pos - grab_pos : 0.0f;
2821 }
2822 if (slider_usable_sz > 0.0f)
2823 clicked_t = ImSaturate((mouse_abs_pos - g.SliderGrabClickOffset - slider_usable_pos_min) / slider_usable_sz);
2824 if (axis == ImGuiAxis_Y)

Callers

nothing calls this directly

Calls 7

ImMaxFunction · 0.85
ImMinFunction · 0.85
ImParseFormatPrecisionFunction · 0.85
ImPowFunction · 0.85
ImLerpFunction · 0.85
ImSaturateFunction · 0.85
ImRectFunction · 0.70

Tested by

no test coverage detected