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

Method DragBehaviorT

KBotExt/imgui/imgui_widgets.cpp:2231–2347  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2229// This is called by DragBehavior() when the widget is active (held by mouse or being manipulated with Nav controls)
2230template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>
2231bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const TYPE v_min, const TYPE v_max, const char* format, ImGuiSliderFlags flags)
2232{
2233 ImGuiContext& g = *GImGui;
2234 const ImGuiAxis axis = (flags & ImGuiSliderFlags_Vertical) ? ImGuiAxis_Y : ImGuiAxis_X;
2235 const bool is_clamped = (v_min < v_max);
2236 const bool is_logarithmic = (flags & ImGuiSliderFlags_Logarithmic) != 0;
2237 const bool is_floating_point = (data_type == ImGuiDataType_Float) || (data_type == ImGuiDataType_Double);
2238
2239 // Default tweak speed
2240 if (v_speed == 0.0f && is_clamped && (v_max - v_min < FLT_MAX))
2241 v_speed = (float)((v_max - v_min) * g.DragSpeedDefaultRatio);
2242
2243 // Inputs accumulates into g.DragCurrentAccum, which is flushed into the current value as soon as it makes a difference with our precision settings
2244 float adjust_delta = 0.0f;
2245 if (g.ActiveIdSource == ImGuiInputSource_Mouse && IsMousePosValid() && IsMouseDragPastThreshold(0, g.IO.MouseDragThreshold * DRAG_MOUSE_THRESHOLD_FACTOR))
2246 {
2247 adjust_delta = g.IO.MouseDelta[axis];
2248 if (g.IO.KeyAlt)
2249 adjust_delta *= 1.0f / 100.0f;
2250 if (g.IO.KeyShift)
2251 adjust_delta *= 10.0f;
2252 }
2253 else if (g.ActiveIdSource == ImGuiInputSource_Keyboard || g.ActiveIdSource == ImGuiInputSource_Gamepad)
2254 {
2255 const int decimal_precision = is_floating_point ? ImParseFormatPrecision(format, 3) : 0;
2256 const bool tweak_slow = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakSlow : ImGuiKey_NavKeyboardTweakSlow);
2257 const bool tweak_fast = IsKeyDown((g.NavInputSource == ImGuiInputSource_Gamepad) ? ImGuiKey_NavGamepadTweakFast : ImGuiKey_NavKeyboardTweakFast);
2258 const float tweak_factor = tweak_slow ? 1.0f / 1.0f : tweak_fast ? 10.0f : 1.0f;
2259 adjust_delta = GetNavTweakPressedAmount(axis) * tweak_factor;
2260 v_speed = ImMax(v_speed, GetMinimumStepAtDecimalPrecision(decimal_precision));
2261 }
2262 adjust_delta *= v_speed;
2263
2264 // For vertical drag we currently assume that Up=higher value (like we do with vertical sliders). This may become a parameter.
2265 if (axis == ImGuiAxis_Y)
2266 adjust_delta = -adjust_delta;
2267
2268 // For logarithmic use our range is effectively 0..1 so scale the delta into that range
2269 if (is_logarithmic && (v_max - v_min < FLT_MAX) && ((v_max - v_min) > 0.000001f)) // Epsilon to avoid /0
2270 adjust_delta /= (float)(v_max - v_min);
2271
2272 // Clear current value on activation
2273 // Avoid altering values and clamping when we are _already_ past the limits and heading in the same direction, so e.g. if range is 0..255, current value is 300 and we are pushing to the right side, keep the 300.
2274 bool is_just_activated = g.ActiveIdIsJustActivated;
2275 bool is_already_past_limits_and_pushing_outward = is_clamped && ((*v >= v_max && adjust_delta > 0.0f) || (*v <= v_min && adjust_delta < 0.0f));
2276 if (is_just_activated || is_already_past_limits_and_pushing_outward)
2277 {
2278 g.DragCurrentAccum = 0.0f;
2279 g.DragCurrentAccumDirty = false;
2280 }
2281 else if (adjust_delta != 0.0f)
2282 {
2283 g.DragCurrentAccum += adjust_delta;
2284 g.DragCurrentAccumDirty = true;
2285 }
2286
2287 if (!g.DragCurrentAccumDirty)
2288 return false;

Callers

nothing calls this directly

Calls 4

ImParseFormatPrecisionFunction · 0.85
ImMaxFunction · 0.85
ImPowFunction · 0.85

Tested by

no test coverage detected