The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

770 lines
31KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class MouseInputSourceInternal : private AsyncUpdater
  21. {
  22. public:
  23. MouseInputSourceInternal (int i, MouseInputSource::InputSourceType type) : index (i), inputType (type)
  24. {
  25. }
  26. //==============================================================================
  27. bool isDragging() const noexcept
  28. {
  29. return buttonState.isAnyMouseButtonDown();
  30. }
  31. Component* getComponentUnderMouse() const noexcept
  32. {
  33. return componentUnderMouse.get();
  34. }
  35. ModifierKeys getCurrentModifiers() const noexcept
  36. {
  37. return ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (buttonState.getRawFlags());
  38. }
  39. ComponentPeer* getPeer() noexcept
  40. {
  41. if (! ComponentPeer::isValidPeer (lastPeer))
  42. lastPeer = nullptr;
  43. return lastPeer;
  44. }
  45. static Point<float> screenPosToLocalPos (Component& comp, Point<float> pos)
  46. {
  47. if (auto* peer = comp.getPeer())
  48. {
  49. pos = peer->globalToLocal (pos);
  50. auto& peerComp = peer->getComponent();
  51. return comp.getLocalPoint (&peerComp, ScalingHelpers::unscaledScreenPosToScaled (peerComp, pos));
  52. }
  53. return comp.getLocalPoint (nullptr, ScalingHelpers::unscaledScreenPosToScaled (comp, pos));
  54. }
  55. Component* findComponentAt (Point<float> screenPos)
  56. {
  57. if (auto* peer = getPeer())
  58. {
  59. auto relativePos = ScalingHelpers::unscaledScreenPosToScaled (peer->getComponent(),
  60. peer->globalToLocal (screenPos));
  61. auto& comp = peer->getComponent();
  62. // (the contains() call is needed to test for overlapping desktop windows)
  63. if (comp.contains (relativePos))
  64. return comp.getComponentAt (relativePos);
  65. }
  66. return nullptr;
  67. }
  68. Point<float> getScreenPosition() const noexcept
  69. {
  70. // This needs to return the live position if possible, but it mustn't update the lastScreenPos
  71. // value, because that can cause continuity problems.
  72. return ScalingHelpers::unscaledScreenPosToScaled (getRawScreenPosition());
  73. }
  74. Point<float> getRawScreenPosition() const noexcept
  75. {
  76. return unboundedMouseOffset + (inputType != MouseInputSource::InputSourceType::touch ? MouseInputSource::getCurrentRawMousePosition()
  77. : lastPointerState.position);
  78. }
  79. void setScreenPosition (Point<float> p)
  80. {
  81. MouseInputSource::setRawMousePosition (ScalingHelpers::scaledScreenPosToUnscaled (p));
  82. }
  83. //==============================================================================
  84. #if JUCE_DUMP_MOUSE_EVENTS
  85. #define JUCE_MOUSE_EVENT_DBG(desc, screenPos) DBG ("Mouse " << desc << " #" << index \
  86. << ": " << screenPosToLocalPos (comp, screenPos).toString() \
  87. << " - Comp: " << String::toHexString ((pointer_sized_int) &comp));
  88. #else
  89. #define JUCE_MOUSE_EVENT_DBG(desc, screenPos)
  90. #endif
  91. void sendMouseEnter (Component& comp, const PointerState& pointerState, Time time)
  92. {
  93. JUCE_MOUSE_EVENT_DBG ("enter", pointerState.position)
  94. comp.internalMouseEnter (MouseInputSource (this), screenPosToLocalPos (comp, pointerState.position), time);
  95. }
  96. void sendMouseExit (Component& comp, const PointerState& pointerState, Time time)
  97. {
  98. JUCE_MOUSE_EVENT_DBG ("exit", pointerState.position)
  99. comp.internalMouseExit (MouseInputSource (this), screenPosToLocalPos (comp, pointerState.position), time);
  100. }
  101. void sendMouseMove (Component& comp, const PointerState& pointerState, Time time)
  102. {
  103. JUCE_MOUSE_EVENT_DBG ("move", pointerState.position)
  104. comp.internalMouseMove (MouseInputSource (this), screenPosToLocalPos (comp, pointerState.position), time);
  105. }
  106. void sendMouseDown (Component& comp, const PointerState& pointerState, Time time)
  107. {
  108. JUCE_MOUSE_EVENT_DBG ("down", pointerState.position)
  109. comp.internalMouseDown (MouseInputSource (this),
  110. pointerState.withPosition (screenPosToLocalPos (comp, pointerState.position)),
  111. time);
  112. }
  113. void sendMouseDrag (Component& comp, const PointerState& pointerState, Time time)
  114. {
  115. JUCE_MOUSE_EVENT_DBG ("drag", pointerState.position)
  116. comp.internalMouseDrag (MouseInputSource (this),
  117. pointerState.withPosition (screenPosToLocalPos (comp, pointerState.position)),
  118. time);
  119. }
  120. void sendMouseUp (Component& comp, const PointerState& pointerState, Time time, ModifierKeys oldMods)
  121. {
  122. JUCE_MOUSE_EVENT_DBG ("up", pointerState.position)
  123. comp.internalMouseUp (MouseInputSource (this),
  124. pointerState.withPosition (screenPosToLocalPos (comp, pointerState.position)),
  125. time,
  126. oldMods);
  127. }
  128. void sendMouseWheel (Component& comp, Point<float> screenPos, Time time, const MouseWheelDetails& wheel)
  129. {
  130. JUCE_MOUSE_EVENT_DBG ("wheel", screenPos)
  131. comp.internalMouseWheel (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, wheel);
  132. }
  133. void sendMagnifyGesture (Component& comp, Point<float> screenPos, Time time, float amount)
  134. {
  135. JUCE_MOUSE_EVENT_DBG ("magnify", screenPos)
  136. comp.internalMagnifyGesture (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, amount);
  137. }
  138. //==============================================================================
  139. // (returns true if the button change caused a modal event loop)
  140. bool setButtons (const PointerState& pointerState, Time time, ModifierKeys newButtonState)
  141. {
  142. if (buttonState == newButtonState)
  143. return false;
  144. // (avoid sending a spurious mouse-drag when we receive a mouse-up)
  145. if (! (isDragging() && ! newButtonState.isAnyMouseButtonDown()))
  146. setPointerState (pointerState, time, false);
  147. // (ignore secondary clicks when there's already a button down)
  148. if (buttonState.isAnyMouseButtonDown() == newButtonState.isAnyMouseButtonDown())
  149. {
  150. buttonState = newButtonState;
  151. return false;
  152. }
  153. auto lastCounter = mouseEventCounter;
  154. if (buttonState.isAnyMouseButtonDown())
  155. {
  156. if (auto* current = getComponentUnderMouse())
  157. {
  158. auto oldMods = getCurrentModifiers();
  159. buttonState = newButtonState; // must change this before calling sendMouseUp, in case it runs a modal loop
  160. sendMouseUp (*current, pointerState.withPositionOffset (unboundedMouseOffset), time, oldMods);
  161. if (lastCounter != mouseEventCounter)
  162. return true; // if a modal loop happened, then newButtonState is no longer valid.
  163. }
  164. enableUnboundedMouseMovement (false, false);
  165. }
  166. buttonState = newButtonState;
  167. if (buttonState.isAnyMouseButtonDown())
  168. {
  169. Desktop::getInstance().incrementMouseClickCounter();
  170. if (auto* current = getComponentUnderMouse())
  171. {
  172. registerMouseDown (pointerState.position, time, *current, buttonState,
  173. inputType == MouseInputSource::InputSourceType::touch);
  174. sendMouseDown (*current, pointerState, time);
  175. }
  176. }
  177. return lastCounter != mouseEventCounter;
  178. }
  179. void setComponentUnderMouse (Component* newComponent, const PointerState& pointerState, Time time)
  180. {
  181. auto* current = getComponentUnderMouse();
  182. if (newComponent != current)
  183. {
  184. WeakReference<Component> safeNewComp (newComponent);
  185. auto originalButtonState = buttonState;
  186. if (current != nullptr)
  187. {
  188. WeakReference<Component> safeOldComp (current);
  189. setButtons (pointerState, time, ModifierKeys());
  190. if (auto oldComp = safeOldComp.get())
  191. {
  192. componentUnderMouse = safeNewComp;
  193. sendMouseExit (*oldComp, pointerState, time);
  194. }
  195. buttonState = originalButtonState;
  196. }
  197. componentUnderMouse = safeNewComp.get();
  198. current = safeNewComp.get();
  199. if (current != nullptr)
  200. sendMouseEnter (*current, pointerState, time);
  201. revealCursor (false);
  202. setButtons (pointerState, time, originalButtonState);
  203. }
  204. }
  205. void setPeer (ComponentPeer& newPeer, const PointerState& pointerState, Time time)
  206. {
  207. if (&newPeer != lastPeer)
  208. {
  209. setComponentUnderMouse (nullptr, pointerState, time);
  210. lastPeer = &newPeer;
  211. setComponentUnderMouse (findComponentAt (pointerState.position), pointerState, time);
  212. }
  213. }
  214. void setPointerState (const PointerState& newPointerState, Time time, bool forceUpdate)
  215. {
  216. const auto& newScreenPos = newPointerState.position;
  217. if (! isDragging())
  218. setComponentUnderMouse (findComponentAt (newScreenPos), newPointerState, time);
  219. if ((newPointerState != lastPointerState) || forceUpdate)
  220. {
  221. cancelPendingUpdate();
  222. if (newPointerState.position != MouseInputSource::offscreenMousePos)
  223. lastPointerState = newPointerState;
  224. if (auto* current = getComponentUnderMouse())
  225. {
  226. if (isDragging())
  227. {
  228. registerMouseDrag (newScreenPos);
  229. sendMouseDrag (*current, newPointerState.withPositionOffset (unboundedMouseOffset), time);
  230. if (isUnboundedMouseModeOn)
  231. handleUnboundedDrag (*current);
  232. }
  233. else
  234. {
  235. sendMouseMove (*current, newPointerState, time);
  236. }
  237. }
  238. revealCursor (false);
  239. }
  240. }
  241. //==============================================================================
  242. void handleEvent (ComponentPeer& newPeer, Point<float> positionWithinPeer, Time time,
  243. const ModifierKeys newMods, float newPressure, float newOrientation, PenDetails pen)
  244. {
  245. lastTime = time;
  246. ++mouseEventCounter;
  247. const auto pointerState = PointerState().withPosition (newPeer.localToGlobal (positionWithinPeer))
  248. .withPressure (newPressure)
  249. .withOrientation (newOrientation)
  250. .withRotation (MouseInputSource::defaultRotation)
  251. .withTiltX (pen.tiltX)
  252. .withTiltY (pen.tiltY);
  253. if (isDragging() && newMods.isAnyMouseButtonDown())
  254. {
  255. setPointerState (pointerState, time, false);
  256. }
  257. else
  258. {
  259. setPeer (newPeer, pointerState, time);
  260. if (auto* peer = getPeer())
  261. {
  262. if (setButtons (pointerState, time, newMods))
  263. return; // some modal events have been dispatched, so the current event is now out-of-date
  264. peer = getPeer();
  265. if (peer != nullptr)
  266. setPointerState (pointerState, time, false);
  267. }
  268. }
  269. }
  270. Component* getTargetForGesture (ComponentPeer& peer, Point<float> positionWithinPeer,
  271. Time time, Point<float>& screenPos)
  272. {
  273. lastTime = time;
  274. ++mouseEventCounter;
  275. screenPos = peer.localToGlobal (positionWithinPeer);
  276. const auto pointerState = lastPointerState.withPosition (screenPos);
  277. setPeer (peer, pointerState, time);
  278. setPointerState (pointerState, time, false);
  279. triggerFakeMove();
  280. return getComponentUnderMouse();
  281. }
  282. void handleWheel (ComponentPeer& peer, Point<float> positionWithinPeer,
  283. Time time, const MouseWheelDetails& wheel)
  284. {
  285. Desktop::getInstance().incrementMouseWheelCounter();
  286. Point<float> screenPos;
  287. // This will make sure that when the wheel spins in its inertial phase, any events
  288. // continue to be sent to the last component that the mouse was over when it was being
  289. // actively controlled by the user. This avoids confusion when scrolling through nested
  290. // scrollable components.
  291. if (lastNonInertialWheelTarget == nullptr || ! wheel.isInertial)
  292. lastNonInertialWheelTarget = getTargetForGesture (peer, positionWithinPeer, time, screenPos);
  293. else
  294. screenPos = peer.localToGlobal (positionWithinPeer);
  295. if (auto target = lastNonInertialWheelTarget.get())
  296. sendMouseWheel (*target, screenPos, time, wheel);
  297. }
  298. void handleMagnifyGesture (ComponentPeer& peer, Point<float> positionWithinPeer,
  299. Time time, const float scaleFactor)
  300. {
  301. Point<float> screenPos;
  302. if (auto* current = getTargetForGesture (peer, positionWithinPeer, time, screenPos))
  303. sendMagnifyGesture (*current, screenPos, time, scaleFactor);
  304. }
  305. //==============================================================================
  306. Time getLastMouseDownTime() const noexcept { return mouseDowns[0].time; }
  307. Point<float> getLastMouseDownPosition() const noexcept { return ScalingHelpers::unscaledScreenPosToScaled (mouseDowns[0].position); }
  308. int getNumberOfMultipleClicks() const noexcept
  309. {
  310. int numClicks = 1;
  311. if (! isLongPressOrDrag())
  312. {
  313. for (int i = 1; i < numElementsInArray (mouseDowns); ++i)
  314. {
  315. if (mouseDowns[0].canBePartOfMultipleClickWith (mouseDowns[i], MouseEvent::getDoubleClickTimeout() * jmin (i, 2)))
  316. ++numClicks;
  317. else
  318. break;
  319. }
  320. }
  321. return numClicks;
  322. }
  323. bool isLongPressOrDrag() const noexcept
  324. {
  325. return movedSignificantly || lastTime > mouseDowns[0].time + RelativeTime::milliseconds (300);
  326. }
  327. bool hasMovedSignificantlySincePressed() const noexcept
  328. {
  329. return movedSignificantly;
  330. }
  331. // Deprecated method
  332. bool hasMouseMovedSignificantlySincePressed() const noexcept
  333. {
  334. return isLongPressOrDrag();
  335. }
  336. //==============================================================================
  337. void triggerFakeMove()
  338. {
  339. triggerAsyncUpdate();
  340. }
  341. void handleAsyncUpdate() override
  342. {
  343. setPointerState (lastPointerState, jmax (lastTime, Time::getCurrentTime()), true);
  344. }
  345. //==============================================================================
  346. void enableUnboundedMouseMovement (bool enable, bool keepCursorVisibleUntilOffscreen)
  347. {
  348. enable = enable && isDragging();
  349. isCursorVisibleUntilOffscreen = keepCursorVisibleUntilOffscreen;
  350. if (enable != isUnboundedMouseModeOn)
  351. {
  352. if ((! enable) && ((! isCursorVisibleUntilOffscreen) || ! unboundedMouseOffset.isOrigin()))
  353. {
  354. // when released, return the mouse to within the component's bounds
  355. if (auto* current = getComponentUnderMouse())
  356. setScreenPosition (current->getScreenBounds().toFloat()
  357. .getConstrainedPoint (ScalingHelpers::unscaledScreenPosToScaled (lastPointerState.position)));
  358. }
  359. isUnboundedMouseModeOn = enable;
  360. unboundedMouseOffset = {};
  361. revealCursor (true);
  362. }
  363. }
  364. void handleUnboundedDrag (Component& current)
  365. {
  366. auto componentScreenBounds = ScalingHelpers::scaledScreenPosToUnscaled (current.getParentMonitorArea().reduced (2, 2).toFloat());
  367. if (! componentScreenBounds.contains (lastPointerState.position))
  368. {
  369. auto componentCentre = current.getScreenBounds().toFloat().getCentre();
  370. unboundedMouseOffset += (lastPointerState.position - ScalingHelpers::scaledScreenPosToUnscaled (componentCentre));
  371. setScreenPosition (componentCentre);
  372. }
  373. else if (isCursorVisibleUntilOffscreen
  374. && (! unboundedMouseOffset.isOrigin())
  375. && componentScreenBounds.contains (lastPointerState.position + unboundedMouseOffset))
  376. {
  377. MouseInputSource::setRawMousePosition (lastPointerState.position + unboundedMouseOffset);
  378. unboundedMouseOffset = {};
  379. }
  380. }
  381. //==============================================================================
  382. void showMouseCursor (MouseCursor cursor, bool forcedUpdate)
  383. {
  384. if (isUnboundedMouseModeOn && ((! unboundedMouseOffset.isOrigin()) || ! isCursorVisibleUntilOffscreen))
  385. {
  386. cursor = MouseCursor::NoCursor;
  387. forcedUpdate = true;
  388. }
  389. if (forcedUpdate || cursor.getHandle() != currentCursorHandle)
  390. {
  391. currentCursorHandle = cursor.getHandle();
  392. cursor.showInWindow (getPeer());
  393. }
  394. }
  395. void hideCursor()
  396. {
  397. showMouseCursor (MouseCursor::NoCursor, true);
  398. }
  399. void revealCursor (bool forcedUpdate)
  400. {
  401. MouseCursor mc (MouseCursor::NormalCursor);
  402. if (auto* current = getComponentUnderMouse())
  403. mc = current->getLookAndFeel().getMouseCursorFor (*current);
  404. showMouseCursor (mc, forcedUpdate);
  405. }
  406. //==============================================================================
  407. const int index;
  408. const MouseInputSource::InputSourceType inputType;
  409. Point<float> unboundedMouseOffset; // NB: these are unscaled coords
  410. PointerState lastPointerState;
  411. ModifierKeys buttonState;
  412. bool isUnboundedMouseModeOn = false, isCursorVisibleUntilOffscreen = false;
  413. private:
  414. WeakReference<Component> componentUnderMouse, lastNonInertialWheelTarget;
  415. ComponentPeer* lastPeer = nullptr;
  416. void* currentCursorHandle = nullptr;
  417. int mouseEventCounter = 0;
  418. struct RecentMouseDown
  419. {
  420. RecentMouseDown() = default;
  421. Point<float> position;
  422. Time time;
  423. ModifierKeys buttons;
  424. uint32 peerID = 0;
  425. bool isTouch = false;
  426. bool canBePartOfMultipleClickWith (const RecentMouseDown& other, int maxTimeBetweenMs) const noexcept
  427. {
  428. return time - other.time < RelativeTime::milliseconds (maxTimeBetweenMs)
  429. && std::abs (position.x - other.position.x) < (float) getPositionToleranceForInputType()
  430. && std::abs (position.y - other.position.y) < (float) getPositionToleranceForInputType()
  431. && buttons == other.buttons
  432. && peerID == other.peerID;
  433. }
  434. int getPositionToleranceForInputType() const noexcept { return isTouch ? 25 : 8; }
  435. };
  436. RecentMouseDown mouseDowns[4];
  437. Time lastTime;
  438. bool movedSignificantly = false;
  439. void registerMouseDown (Point<float> screenPos, Time time, Component& component,
  440. const ModifierKeys modifiers, bool isTouchSource) noexcept
  441. {
  442. for (int i = numElementsInArray (mouseDowns); --i > 0;)
  443. mouseDowns[i] = mouseDowns[i - 1];
  444. mouseDowns[0].position = screenPos;
  445. mouseDowns[0].time = time;
  446. mouseDowns[0].buttons = modifiers.withOnlyMouseButtons();
  447. mouseDowns[0].isTouch = isTouchSource;
  448. if (auto* peer = component.getPeer())
  449. mouseDowns[0].peerID = peer->getUniqueID();
  450. else
  451. mouseDowns[0].peerID = 0;
  452. movedSignificantly = false;
  453. lastNonInertialWheelTarget = nullptr;
  454. }
  455. void registerMouseDrag (Point<float> screenPos) noexcept
  456. {
  457. movedSignificantly = movedSignificantly || mouseDowns[0].position.getDistanceFrom (screenPos) >= 4;
  458. }
  459. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseInputSourceInternal)
  460. };
  461. //==============================================================================
  462. MouseInputSource::MouseInputSource (MouseInputSourceInternal* s) noexcept : pimpl (s) {}
  463. MouseInputSource::MouseInputSource (const MouseInputSource& other) noexcept : pimpl (other.pimpl) {}
  464. MouseInputSource::~MouseInputSource() noexcept {}
  465. MouseInputSource& MouseInputSource::operator= (const MouseInputSource& other) noexcept
  466. {
  467. pimpl = other.pimpl;
  468. return *this;
  469. }
  470. MouseInputSource::InputSourceType MouseInputSource::getType() const noexcept { return pimpl->inputType; }
  471. bool MouseInputSource::isMouse() const noexcept { return (getType() == MouseInputSource::InputSourceType::mouse); }
  472. bool MouseInputSource::isTouch() const noexcept { return (getType() == MouseInputSource::InputSourceType::touch); }
  473. bool MouseInputSource::isPen() const noexcept { return (getType() == MouseInputSource::InputSourceType::pen); }
  474. bool MouseInputSource::canHover() const noexcept { return ! isTouch(); }
  475. bool MouseInputSource::hasMouseWheel() const noexcept { return ! isTouch(); }
  476. int MouseInputSource::getIndex() const noexcept { return pimpl->index; }
  477. bool MouseInputSource::isDragging() const noexcept { return pimpl->isDragging(); }
  478. Point<float> MouseInputSource::getScreenPosition() const noexcept { return pimpl->getScreenPosition(); }
  479. Point<float> MouseInputSource::getRawScreenPosition() const noexcept { return pimpl->getRawScreenPosition(); }
  480. ModifierKeys MouseInputSource::getCurrentModifiers() const noexcept { return pimpl->getCurrentModifiers(); }
  481. float MouseInputSource::getCurrentPressure() const noexcept { return pimpl->lastPointerState.pressure; }
  482. bool MouseInputSource::isPressureValid() const noexcept { return pimpl->lastPointerState.isPressureValid(); }
  483. float MouseInputSource::getCurrentOrientation() const noexcept { return pimpl->lastPointerState.orientation; }
  484. bool MouseInputSource::isOrientationValid() const noexcept { return pimpl->lastPointerState.isOrientationValid(); }
  485. float MouseInputSource::getCurrentRotation() const noexcept { return pimpl->lastPointerState.rotation; }
  486. bool MouseInputSource::isRotationValid() const noexcept { return pimpl->lastPointerState.isRotationValid(); }
  487. float MouseInputSource::getCurrentTilt (bool tiltX) const noexcept { return tiltX ? pimpl->lastPointerState.tiltX : pimpl->lastPointerState.tiltY; }
  488. bool MouseInputSource::isTiltValid (bool isX) const noexcept { return pimpl->lastPointerState.isTiltValid (isX); }
  489. Component* MouseInputSource::getComponentUnderMouse() const { return pimpl->getComponentUnderMouse(); }
  490. void MouseInputSource::triggerFakeMove() const { pimpl->triggerFakeMove(); }
  491. int MouseInputSource::getNumberOfMultipleClicks() const noexcept { return pimpl->getNumberOfMultipleClicks(); }
  492. Time MouseInputSource::getLastMouseDownTime() const noexcept { return pimpl->getLastMouseDownTime(); }
  493. Point<float> MouseInputSource::getLastMouseDownPosition() const noexcept { return pimpl->getLastMouseDownPosition(); }
  494. bool MouseInputSource::isLongPressOrDrag() const noexcept { return pimpl->isLongPressOrDrag(); }
  495. bool MouseInputSource::hasMovedSignificantlySincePressed() const noexcept { return pimpl->hasMovedSignificantlySincePressed(); }
  496. bool MouseInputSource::canDoUnboundedMovement() const noexcept { return ! isTouch(); }
  497. void MouseInputSource::enableUnboundedMouseMovement (bool isEnabled, bool keepCursorVisibleUntilOffscreen) const
  498. { pimpl->enableUnboundedMouseMovement (isEnabled, keepCursorVisibleUntilOffscreen); }
  499. bool MouseInputSource::isUnboundedMouseMovementEnabled() const { return pimpl->isUnboundedMouseModeOn; }
  500. bool MouseInputSource::hasMouseCursor() const noexcept { return ! isTouch(); }
  501. void MouseInputSource::showMouseCursor (const MouseCursor& cursor) { pimpl->showMouseCursor (cursor, false); }
  502. void MouseInputSource::hideCursor() { pimpl->hideCursor(); }
  503. void MouseInputSource::revealCursor() { pimpl->revealCursor (false); }
  504. void MouseInputSource::forceMouseCursorUpdate() { pimpl->revealCursor (true); }
  505. void MouseInputSource::setScreenPosition (Point<float> p) { pimpl->setScreenPosition (p); }
  506. void MouseInputSource::handleEvent (ComponentPeer& peer, Point<float> pos, int64 time, ModifierKeys mods,
  507. float pressure, float orientation, const PenDetails& penDetails)
  508. {
  509. pimpl->handleEvent (peer, pos, Time (time), mods.withOnlyMouseButtons(), pressure, orientation, penDetails);
  510. }
  511. void MouseInputSource::handleWheel (ComponentPeer& peer, Point<float> pos, int64 time, const MouseWheelDetails& wheel)
  512. {
  513. pimpl->handleWheel (peer, pos, Time (time), wheel);
  514. }
  515. void MouseInputSource::handleMagnifyGesture (ComponentPeer& peer, Point<float> pos, int64 time, float scaleFactor)
  516. {
  517. pimpl->handleMagnifyGesture (peer, pos, Time (time), scaleFactor);
  518. }
  519. const float MouseInputSource::invalidPressure = 0.0f;
  520. const float MouseInputSource::invalidOrientation = 0.0f;
  521. const float MouseInputSource::invalidRotation = 0.0f;
  522. const float MouseInputSource::invalidTiltX = 0.0f;
  523. const float MouseInputSource::invalidTiltY = 0.0f;
  524. const Point<float> MouseInputSource::offscreenMousePos { -10.0f, -10.0f };
  525. // Deprecated method
  526. bool MouseInputSource::hasMouseMovedSignificantlySincePressed() const noexcept { return pimpl->hasMouseMovedSignificantlySincePressed(); }
  527. //==============================================================================
  528. struct MouseInputSource::SourceList : public Timer
  529. {
  530. SourceList()
  531. {
  532. #if JUCE_ANDROID || JUCE_IOS
  533. auto mainMouseInputType = MouseInputSource::InputSourceType::touch;
  534. #else
  535. auto mainMouseInputType = MouseInputSource::InputSourceType::mouse;
  536. #endif
  537. addSource (0, mainMouseInputType);
  538. }
  539. bool addSource();
  540. bool canUseTouch();
  541. MouseInputSource* addSource (int index, MouseInputSource::InputSourceType type)
  542. {
  543. auto* s = new MouseInputSourceInternal (index, type);
  544. sources.add (s);
  545. sourceArray.add (MouseInputSource (s));
  546. return &sourceArray.getReference (sourceArray.size() - 1);
  547. }
  548. MouseInputSource* getMouseSource (int index) noexcept
  549. {
  550. return isPositiveAndBelow (index, sourceArray.size()) ? &sourceArray.getReference (index)
  551. : nullptr;
  552. }
  553. MouseInputSource* getOrCreateMouseInputSource (MouseInputSource::InputSourceType type, int touchIndex = 0)
  554. {
  555. if (type == MouseInputSource::InputSourceType::mouse || type == MouseInputSource::InputSourceType::pen)
  556. {
  557. for (auto& m : sourceArray)
  558. if (type == m.getType())
  559. return &m;
  560. addSource (0, type);
  561. }
  562. else if (type == MouseInputSource::InputSourceType::touch)
  563. {
  564. jassert (0 <= touchIndex && touchIndex < 100); // sanity-check on number of fingers
  565. for (auto& m : sourceArray)
  566. if (type == m.getType() && touchIndex == m.getIndex())
  567. return &m;
  568. if (canUseTouch())
  569. return addSource (touchIndex, type);
  570. }
  571. return nullptr;
  572. }
  573. int getNumDraggingMouseSources() const noexcept
  574. {
  575. int num = 0;
  576. for (auto* s : sources)
  577. if (s->isDragging())
  578. ++num;
  579. return num;
  580. }
  581. MouseInputSource* getDraggingMouseSource (int index) noexcept
  582. {
  583. int num = 0;
  584. for (auto& s : sourceArray)
  585. {
  586. if (s.isDragging())
  587. {
  588. if (index == num)
  589. return &s;
  590. ++num;
  591. }
  592. }
  593. return nullptr;
  594. }
  595. void beginDragAutoRepeat (int interval)
  596. {
  597. if (interval > 0)
  598. {
  599. if (getTimerInterval() != interval)
  600. startTimer (interval);
  601. }
  602. else
  603. {
  604. stopTimer();
  605. }
  606. }
  607. void timerCallback() override
  608. {
  609. bool anyDragging = false;
  610. for (auto* s : sources)
  611. {
  612. // NB: when doing auto-repeat, we need to force an update of the current position and button state,
  613. // because on some OSes the queue can get overloaded with messages so that mouse-events don't get through..
  614. if (s->isDragging() && ComponentPeer::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  615. {
  616. s->lastPointerState.position = s->getRawScreenPosition();
  617. s->triggerFakeMove();
  618. anyDragging = true;
  619. }
  620. }
  621. if (! anyDragging)
  622. stopTimer();
  623. }
  624. OwnedArray<MouseInputSourceInternal> sources;
  625. Array<MouseInputSource> sourceArray;
  626. };
  627. } // namespace juce