MCPcopy Create free account
hub / github.com/clementgallet/libTAS / DragBehaviorT

Method DragBehaviorT

src/external/imgui/imgui_widgets.cpp:2244–2360  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 4

ImParseFormatPrecisionFunction · 0.85
ImMaxFunction · 0.85
ImPowFunction · 0.85

Tested by

no test coverage detected