MCPcopy Create free account
hub / github.com/Wemino/MarkerPatch / SliderBehaviorT

Method SliderBehaviorT

include/imgui/imgui_widgets.cpp:3100–3271  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3098// FIXME: Try to move more of the code into shared SliderBehavior()
3099template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>
3100bool 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)
3101{
3102 ImGuiContext& g = *GImGui;
3103 const ImGuiStyle& style = g.Style;
3104
3105 const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X;
3106 const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0;
3107 const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);
3108 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.
3109
3110 // Calculate bounds
3111 const float grab_padding = 2.0f; // FIXME: Should be part of style.
3112 const float slider_sz = (bb.Max[axis] - bb.Min[axis]) - grab_padding * 2.0f;
3113 float grab_sz = style.GrabMinSize;
3114 if (!is_floating_point && v_range_f >= 0.0f) // v_range_f < 0 may happen on integer overflows
3115 grab_sz = ImMax(slider_sz / (v_range_f + 1), style.GrabMinSize); // For integer sliders: if possible have the grab size represent 1 unit
3116 grab_sz = ImMin(grab_sz, slider_sz);
3117 const float slider_usable_sz = slider_sz - grab_sz;
3118 const float slider_usable_pos_min = bb.Min[axis] + grab_padding + grab_sz * 0.5f;
3119 const float slider_usable_pos_max = bb.Max[axis] - grab_padding - grab_sz * 0.5f;
3120
3121 float logarithmic_zero_epsilon = 0.0f; // Only valid when is_logarithmic is true
3122 float zero_deadzone_halfsize = 0.0f; // Only valid when is_logarithmic is true
3123 if (is_logarithmic)
3124 {
3125 // 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.
3126 const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 1;
3127 logarithmic_zero_epsilon = ImPow(0.1f, (float)decimal_precision);
3128 zero_deadzone_halfsize = (style.LogSliderDeadzone * 0.5f) / ImMax(slider_usable_sz, 1.0f);
3129 }
3130
3131 // Process interacting with the slider
3132 bool value_changed = false;
3133 if (g.ActiveId == id)
3134 {
3135 bool set_new_value = false;
3136 float clicked_t = 0.0f;
3137 if (g.ActiveIdSource == ImGuiInputSource_Mouse)
3138 {
3139 if (!g.IO.MouseDown[0])
3140 {
3141 ClearActiveID();
3142 }
3143 else
3144 {
3145 const float mouse_abs_pos = g.IO.MousePos[axis];
3146 if (g.ActiveIdIsJustActivated)
3147 {
3148 float grab_t = ScaleRatioFromValueT<TYPE, SIGNEDTYPE, FLOATTYPE>(data_type, *v, v_min, v_max, logarithmic_zero_epsilon, zero_deadzone_halfsize);
3149 if (axis == ImGuiAxis_Y)
3150 grab_t = 1.0f - grab_t;
3151 const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t);
3152 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.
3153 g.SliderGrabClickOffset = (clicked_around_grab && is_floating_point) ? mouse_abs_pos - grab_pos : 0.0f;
3154 }
3155 if (slider_usable_sz > 0.0f)
3156 clicked_t = ImSaturate((mouse_abs_pos - g.SliderGrabClickOffset - slider_usable_pos_min) / slider_usable_sz);
3157 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