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.

781 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.containsInternal (relativePos))
  64. return comp.getComponentAtInternal (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. : lastScreenPos);
  78. }
  79. void setScreenPosition (Point<float> p)
  80. {
  81. MouseInputSource::setRawMousePosition (ScalingHelpers::scaledScreenPosToUnscaled (p));
  82. }
  83. bool isPressureValid() const noexcept { return pressure >= 0.0f && pressure <= 1.0f; }
  84. bool isOrientationValid() const noexcept { return orientation >= 0.0f && orientation <= MathConstants<float>::twoPi; }
  85. bool isRotationValid() const noexcept { return rotation >= 0.0f && rotation <= MathConstants<float>::twoPi; }
  86. bool isTiltValid (bool isX) const noexcept { return isX ? (tiltX >= -1.0f && tiltX <= 1.0f) : (tiltY >= -1.0f && tiltY <= 1.0f); }
  87. //==============================================================================
  88. #if JUCE_DUMP_MOUSE_EVENTS
  89. #define JUCE_MOUSE_EVENT_DBG(desc) DBG ("Mouse " << desc << " #" << index \
  90. << ": " << screenPosToLocalPos (comp, screenPos).toString() \
  91. << " - Comp: " << String::toHexString ((pointer_sized_int) &comp));
  92. #else
  93. #define JUCE_MOUSE_EVENT_DBG(desc)
  94. #endif
  95. void sendMouseEnter (Component& comp, Point<float> screenPos, Time time)
  96. {
  97. JUCE_MOUSE_EVENT_DBG ("enter")
  98. comp.internalMouseEnter (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  99. }
  100. void sendMouseExit (Component& comp, Point<float> screenPos, Time time)
  101. {
  102. JUCE_MOUSE_EVENT_DBG ("exit")
  103. comp.internalMouseExit (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  104. }
  105. void sendMouseMove (Component& comp, Point<float> screenPos, Time time)
  106. {
  107. JUCE_MOUSE_EVENT_DBG ("move")
  108. comp.internalMouseMove (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  109. }
  110. void sendMouseDown (Component& comp, Point<float> screenPos, Time time)
  111. {
  112. JUCE_MOUSE_EVENT_DBG ("down")
  113. comp.internalMouseDown (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, pressure, orientation, rotation, tiltX, tiltY);
  114. }
  115. void sendMouseDrag (Component& comp, Point<float> screenPos, Time time)
  116. {
  117. JUCE_MOUSE_EVENT_DBG ("drag")
  118. comp.internalMouseDrag (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, pressure, orientation, rotation, tiltX, tiltY);
  119. }
  120. void sendMouseUp (Component& comp, Point<float> screenPos, Time time, ModifierKeys oldMods)
  121. {
  122. JUCE_MOUSE_EVENT_DBG ("up")
  123. comp.internalMouseUp (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, oldMods, pressure, orientation, rotation, tiltX, tiltY);
  124. }
  125. void sendMouseWheel (Component& comp, Point<float> screenPos, Time time, const MouseWheelDetails& wheel)
  126. {
  127. JUCE_MOUSE_EVENT_DBG ("wheel")
  128. comp.internalMouseWheel (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, wheel);
  129. }
  130. void sendMagnifyGesture (Component& comp, Point<float> screenPos, Time time, float amount)
  131. {
  132. JUCE_MOUSE_EVENT_DBG ("magnify")
  133. comp.internalMagnifyGesture (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, amount);
  134. }
  135. //==============================================================================
  136. // (returns true if the button change caused a modal event loop)
  137. bool setButtons (Point<float> screenPos, Time time, ModifierKeys newButtonState)
  138. {
  139. if (buttonState == newButtonState)
  140. return false;
  141. // (avoid sending a spurious mouse-drag when we receive a mouse-up)
  142. if (! (isDragging() && ! newButtonState.isAnyMouseButtonDown()))
  143. setScreenPos (screenPos, time, false);
  144. // (ignore secondary clicks when there's already a button down)
  145. if (buttonState.isAnyMouseButtonDown() == newButtonState.isAnyMouseButtonDown())
  146. {
  147. buttonState = newButtonState;
  148. return false;
  149. }
  150. auto lastCounter = mouseEventCounter;
  151. if (buttonState.isAnyMouseButtonDown())
  152. {
  153. if (auto* current = getComponentUnderMouse())
  154. {
  155. auto oldMods = getCurrentModifiers();
  156. buttonState = newButtonState; // must change this before calling sendMouseUp, in case it runs a modal loop
  157. sendMouseUp (*current, screenPos + unboundedMouseOffset, time, oldMods);
  158. if (lastCounter != mouseEventCounter)
  159. return true; // if a modal loop happened, then newButtonState is no longer valid.
  160. }
  161. enableUnboundedMouseMovement (false, false);
  162. }
  163. buttonState = newButtonState;
  164. if (buttonState.isAnyMouseButtonDown())
  165. {
  166. Desktop::getInstance().incrementMouseClickCounter();
  167. if (auto* current = getComponentUnderMouse())
  168. {
  169. registerMouseDown (screenPos, time, *current, buttonState,
  170. inputType == MouseInputSource::InputSourceType::touch);
  171. sendMouseDown (*current, screenPos, time);
  172. }
  173. }
  174. return lastCounter != mouseEventCounter;
  175. }
  176. void setComponentUnderMouse (Component* newComponent, Point<float> screenPos, Time time)
  177. {
  178. auto* current = getComponentUnderMouse();
  179. if (newComponent != current)
  180. {
  181. WeakReference<Component> safeNewComp (newComponent);
  182. auto originalButtonState = buttonState;
  183. if (current != nullptr)
  184. {
  185. WeakReference<Component> safeOldComp (current);
  186. setButtons (screenPos, time, ModifierKeys());
  187. if (auto oldComp = safeOldComp.get())
  188. {
  189. componentUnderMouse = safeNewComp;
  190. sendMouseExit (*oldComp, screenPos, time);
  191. }
  192. buttonState = originalButtonState;
  193. }
  194. componentUnderMouse = safeNewComp.get();
  195. current = safeNewComp.get();
  196. if (current != nullptr)
  197. sendMouseEnter (*current, screenPos, time);
  198. revealCursor (false);
  199. setButtons (screenPos, time, originalButtonState);
  200. }
  201. }
  202. void setPeer (ComponentPeer& newPeer, Point<float> screenPos, Time time)
  203. {
  204. if (&newPeer != lastPeer)
  205. {
  206. setComponentUnderMouse (nullptr, screenPos, time);
  207. lastPeer = &newPeer;
  208. setComponentUnderMouse (findComponentAt (screenPos), screenPos, time);
  209. }
  210. }
  211. void setScreenPos (Point<float> newScreenPos, Time time, bool forceUpdate)
  212. {
  213. if (! isDragging())
  214. setComponentUnderMouse (findComponentAt (newScreenPos), newScreenPos, time);
  215. if (newScreenPos != lastScreenPos || forceUpdate)
  216. {
  217. cancelPendingUpdate();
  218. if (newScreenPos != MouseInputSource::offscreenMousePos)
  219. lastScreenPos = newScreenPos;
  220. if (auto* current = getComponentUnderMouse())
  221. {
  222. if (isDragging())
  223. {
  224. registerMouseDrag (newScreenPos);
  225. sendMouseDrag (*current, newScreenPos + unboundedMouseOffset, time);
  226. if (isUnboundedMouseModeOn)
  227. handleUnboundedDrag (*current);
  228. }
  229. else
  230. {
  231. sendMouseMove (*current, newScreenPos, time);
  232. }
  233. }
  234. revealCursor (false);
  235. }
  236. }
  237. //==============================================================================
  238. void handleEvent (ComponentPeer& newPeer, Point<float> positionWithinPeer, Time time,
  239. const ModifierKeys newMods, float newPressure, float newOrientation, PenDetails pen)
  240. {
  241. lastTime = time;
  242. const bool pressureChanged = (pressure != newPressure);
  243. pressure = newPressure;
  244. const bool orientationChanged = (orientation != newOrientation);
  245. orientation = newOrientation;
  246. const bool rotationChanged = (rotation != pen.rotation);
  247. rotation = pen.rotation;
  248. const bool tiltChanged = (tiltX != pen.tiltX || tiltY != pen.tiltY);
  249. tiltX = pen.tiltX;
  250. tiltY = pen.tiltY;
  251. const bool shouldUpdate = (pressureChanged || orientationChanged || rotationChanged || tiltChanged);
  252. ++mouseEventCounter;
  253. auto screenPos = newPeer.localToGlobal (positionWithinPeer);
  254. if (isDragging() && newMods.isAnyMouseButtonDown())
  255. {
  256. setScreenPos (screenPos, time, shouldUpdate);
  257. }
  258. else
  259. {
  260. setPeer (newPeer, screenPos, time);
  261. if (auto* peer = getPeer())
  262. {
  263. if (setButtons (screenPos, time, newMods))
  264. return; // some modal events have been dispatched, so the current event is now out-of-date
  265. peer = getPeer();
  266. if (peer != nullptr)
  267. setScreenPos (screenPos, time, shouldUpdate);
  268. }
  269. }
  270. }
  271. Component* getTargetForGesture (ComponentPeer& peer, Point<float> positionWithinPeer,
  272. Time time, Point<float>& screenPos)
  273. {
  274. lastTime = time;
  275. ++mouseEventCounter;
  276. screenPos = peer.localToGlobal (positionWithinPeer);
  277. setPeer (peer, screenPos, time);
  278. setScreenPos (screenPos, 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. setScreenPos (lastScreenPos, 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 (lastScreenPos)));
  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 (lastScreenPos))
  368. {
  369. auto componentCentre = current.getScreenBounds().toFloat().getCentre();
  370. unboundedMouseOffset += (lastScreenPos - ScalingHelpers::scaledScreenPosToUnscaled (componentCentre));
  371. setScreenPosition (componentCentre);
  372. }
  373. else if (isCursorVisibleUntilOffscreen
  374. && (! unboundedMouseOffset.isOrigin())
  375. && componentScreenBounds.contains (lastScreenPos + unboundedMouseOffset))
  376. {
  377. MouseInputSource::setRawMousePosition (lastScreenPos + 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> lastScreenPos, unboundedMouseOffset; // NB: these are unscaled coords
  410. ModifierKeys buttonState;
  411. float pressure = 0;
  412. float orientation = 0;
  413. float rotation = 0;
  414. float tiltX = 0;
  415. float tiltY = 0;
  416. bool isUnboundedMouseModeOn = false, isCursorVisibleUntilOffscreen = false;
  417. private:
  418. WeakReference<Component> componentUnderMouse, lastNonInertialWheelTarget;
  419. ComponentPeer* lastPeer = nullptr;
  420. void* currentCursorHandle = nullptr;
  421. int mouseEventCounter = 0;
  422. struct RecentMouseDown
  423. {
  424. RecentMouseDown() = default;
  425. Point<float> position;
  426. Time time;
  427. ModifierKeys buttons;
  428. uint32 peerID = 0;
  429. bool isTouch = false;
  430. bool canBePartOfMultipleClickWith (const RecentMouseDown& other, int maxTimeBetweenMs) const noexcept
  431. {
  432. return time - other.time < RelativeTime::milliseconds (maxTimeBetweenMs)
  433. && std::abs (position.x - other.position.x) < (float) getPositionToleranceForInputType()
  434. && std::abs (position.y - other.position.y) < (float) getPositionToleranceForInputType()
  435. && buttons == other.buttons
  436. && peerID == other.peerID;
  437. }
  438. int getPositionToleranceForInputType() const noexcept { return isTouch ? 25 : 8; }
  439. };
  440. RecentMouseDown mouseDowns[4];
  441. Time lastTime;
  442. bool movedSignificantly = false;
  443. void registerMouseDown (Point<float> screenPos, Time time, Component& component,
  444. const ModifierKeys modifiers, bool isTouchSource) noexcept
  445. {
  446. for (int i = numElementsInArray (mouseDowns); --i > 0;)
  447. mouseDowns[i] = mouseDowns[i - 1];
  448. mouseDowns[0].position = screenPos;
  449. mouseDowns[0].time = time;
  450. mouseDowns[0].buttons = modifiers.withOnlyMouseButtons();
  451. mouseDowns[0].isTouch = isTouchSource;
  452. if (auto* peer = component.getPeer())
  453. mouseDowns[0].peerID = peer->getUniqueID();
  454. else
  455. mouseDowns[0].peerID = 0;
  456. movedSignificantly = false;
  457. lastNonInertialWheelTarget = nullptr;
  458. }
  459. void registerMouseDrag (Point<float> screenPos) noexcept
  460. {
  461. movedSignificantly = movedSignificantly || mouseDowns[0].position.getDistanceFrom (screenPos) >= 4;
  462. }
  463. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseInputSourceInternal)
  464. };
  465. //==============================================================================
  466. MouseInputSource::MouseInputSource (MouseInputSourceInternal* s) noexcept : pimpl (s) {}
  467. MouseInputSource::MouseInputSource (const MouseInputSource& other) noexcept : pimpl (other.pimpl) {}
  468. MouseInputSource::~MouseInputSource() noexcept {}
  469. MouseInputSource& MouseInputSource::operator= (const MouseInputSource& other) noexcept
  470. {
  471. pimpl = other.pimpl;
  472. return *this;
  473. }
  474. MouseInputSource::InputSourceType MouseInputSource::getType() const noexcept { return pimpl->inputType; }
  475. bool MouseInputSource::isMouse() const noexcept { return (getType() == MouseInputSource::InputSourceType::mouse); }
  476. bool MouseInputSource::isTouch() const noexcept { return (getType() == MouseInputSource::InputSourceType::touch); }
  477. bool MouseInputSource::isPen() const noexcept { return (getType() == MouseInputSource::InputSourceType::pen); }
  478. bool MouseInputSource::canHover() const noexcept { return ! isTouch(); }
  479. bool MouseInputSource::hasMouseWheel() const noexcept { return ! isTouch(); }
  480. int MouseInputSource::getIndex() const noexcept { return pimpl->index; }
  481. bool MouseInputSource::isDragging() const noexcept { return pimpl->isDragging(); }
  482. Point<float> MouseInputSource::getScreenPosition() const noexcept { return pimpl->getScreenPosition(); }
  483. Point<float> MouseInputSource::getRawScreenPosition() const noexcept { return pimpl->getRawScreenPosition(); }
  484. ModifierKeys MouseInputSource::getCurrentModifiers() const noexcept { return pimpl->getCurrentModifiers(); }
  485. float MouseInputSource::getCurrentPressure() const noexcept { return pimpl->pressure; }
  486. bool MouseInputSource::isPressureValid() const noexcept { return pimpl->isPressureValid(); }
  487. float MouseInputSource::getCurrentOrientation() const noexcept { return pimpl->orientation; }
  488. bool MouseInputSource::isOrientationValid() const noexcept { return pimpl->isOrientationValid(); }
  489. float MouseInputSource::getCurrentRotation() const noexcept { return pimpl->rotation; }
  490. bool MouseInputSource::isRotationValid() const noexcept { return pimpl->isRotationValid(); }
  491. float MouseInputSource::getCurrentTilt (bool tiltX) const noexcept { return tiltX ? pimpl->tiltX : pimpl->tiltY; }
  492. bool MouseInputSource::isTiltValid (bool isX) const noexcept { return pimpl->isTiltValid (isX); }
  493. Component* MouseInputSource::getComponentUnderMouse() const { return pimpl->getComponentUnderMouse(); }
  494. void MouseInputSource::triggerFakeMove() const { pimpl->triggerFakeMove(); }
  495. int MouseInputSource::getNumberOfMultipleClicks() const noexcept { return pimpl->getNumberOfMultipleClicks(); }
  496. Time MouseInputSource::getLastMouseDownTime() const noexcept { return pimpl->getLastMouseDownTime(); }
  497. Point<float> MouseInputSource::getLastMouseDownPosition() const noexcept { return pimpl->getLastMouseDownPosition(); }
  498. bool MouseInputSource::isLongPressOrDrag() const noexcept { return pimpl->isLongPressOrDrag(); }
  499. bool MouseInputSource::hasMovedSignificantlySincePressed() const noexcept { return pimpl->hasMovedSignificantlySincePressed(); }
  500. bool MouseInputSource::canDoUnboundedMovement() const noexcept { return ! isTouch(); }
  501. void MouseInputSource::enableUnboundedMouseMovement (bool isEnabled, bool keepCursorVisibleUntilOffscreen) const
  502. { pimpl->enableUnboundedMouseMovement (isEnabled, keepCursorVisibleUntilOffscreen); }
  503. bool MouseInputSource::isUnboundedMouseMovementEnabled() const { return pimpl->isUnboundedMouseModeOn; }
  504. bool MouseInputSource::hasMouseCursor() const noexcept { return ! isTouch(); }
  505. void MouseInputSource::showMouseCursor (const MouseCursor& cursor) { pimpl->showMouseCursor (cursor, false); }
  506. void MouseInputSource::hideCursor() { pimpl->hideCursor(); }
  507. void MouseInputSource::revealCursor() { pimpl->revealCursor (false); }
  508. void MouseInputSource::forceMouseCursorUpdate() { pimpl->revealCursor (true); }
  509. void MouseInputSource::setScreenPosition (Point<float> p) { pimpl->setScreenPosition (p); }
  510. void MouseInputSource::handleEvent (ComponentPeer& peer, Point<float> pos, int64 time, ModifierKeys mods,
  511. float pressure, float orientation, const PenDetails& penDetails)
  512. {
  513. pimpl->handleEvent (peer, pos, Time (time), mods.withOnlyMouseButtons(), pressure, orientation, penDetails);
  514. }
  515. void MouseInputSource::handleWheel (ComponentPeer& peer, Point<float> pos, int64 time, const MouseWheelDetails& wheel)
  516. {
  517. pimpl->handleWheel (peer, pos, Time (time), wheel);
  518. }
  519. void MouseInputSource::handleMagnifyGesture (ComponentPeer& peer, Point<float> pos, int64 time, float scaleFactor)
  520. {
  521. pimpl->handleMagnifyGesture (peer, pos, Time (time), scaleFactor);
  522. }
  523. const float MouseInputSource::invalidPressure = 0.0f;
  524. const float MouseInputSource::invalidOrientation = 0.0f;
  525. const float MouseInputSource::invalidRotation = 0.0f;
  526. const float MouseInputSource::invalidTiltX = 0.0f;
  527. const float MouseInputSource::invalidTiltY = 0.0f;
  528. const Point<float> MouseInputSource::offscreenMousePos { -10.0f, -10.0f };
  529. // Deprecated method
  530. bool MouseInputSource::hasMouseMovedSignificantlySincePressed() const noexcept { return pimpl->hasMouseMovedSignificantlySincePressed(); }
  531. //==============================================================================
  532. struct MouseInputSource::SourceList : public Timer
  533. {
  534. SourceList()
  535. {
  536. #if JUCE_ANDROID || JUCE_IOS
  537. auto mainMouseInputType = MouseInputSource::InputSourceType::touch;
  538. #else
  539. auto mainMouseInputType = MouseInputSource::InputSourceType::mouse;
  540. #endif
  541. addSource (0, mainMouseInputType);
  542. }
  543. bool addSource();
  544. bool canUseTouch();
  545. MouseInputSource* addSource (int index, MouseInputSource::InputSourceType type)
  546. {
  547. auto* s = new MouseInputSourceInternal (index, type);
  548. sources.add (s);
  549. sourceArray.add (MouseInputSource (s));
  550. return &sourceArray.getReference (sourceArray.size() - 1);
  551. }
  552. MouseInputSource* getMouseSource (int index) noexcept
  553. {
  554. return isPositiveAndBelow (index, sourceArray.size()) ? &sourceArray.getReference (index)
  555. : nullptr;
  556. }
  557. MouseInputSource* getOrCreateMouseInputSource (MouseInputSource::InputSourceType type, int touchIndex = 0)
  558. {
  559. if (type == MouseInputSource::InputSourceType::mouse || type == MouseInputSource::InputSourceType::pen)
  560. {
  561. for (auto& m : sourceArray)
  562. if (type == m.getType())
  563. return &m;
  564. addSource (0, type);
  565. }
  566. else if (type == MouseInputSource::InputSourceType::touch)
  567. {
  568. jassert (touchIndex >= 0 && touchIndex < 100); // sanity-check on number of fingers
  569. for (auto& m : sourceArray)
  570. if (type == m.getType() && touchIndex == m.getIndex())
  571. return &m;
  572. if (canUseTouch())
  573. return addSource (touchIndex, type);
  574. }
  575. return nullptr;
  576. }
  577. int getNumDraggingMouseSources() const noexcept
  578. {
  579. int num = 0;
  580. for (auto* s : sources)
  581. if (s->isDragging())
  582. ++num;
  583. return num;
  584. }
  585. MouseInputSource* getDraggingMouseSource (int index) noexcept
  586. {
  587. int num = 0;
  588. for (auto& s : sourceArray)
  589. {
  590. if (s.isDragging())
  591. {
  592. if (index == num)
  593. return &s;
  594. ++num;
  595. }
  596. }
  597. return nullptr;
  598. }
  599. void beginDragAutoRepeat (int interval)
  600. {
  601. if (interval > 0)
  602. {
  603. if (getTimerInterval() != interval)
  604. startTimer (interval);
  605. }
  606. else
  607. {
  608. stopTimer();
  609. }
  610. }
  611. void timerCallback() override
  612. {
  613. bool anyDragging = false;
  614. for (auto* s : sources)
  615. {
  616. // NB: when doing auto-repeat, we need to force an update of the current position and button state,
  617. // because on some OSes the queue can get overloaded with messages so that mouse-events don't get through..
  618. if (s->isDragging() && ComponentPeer::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  619. {
  620. s->lastScreenPos = s->getRawScreenPosition();
  621. s->triggerFakeMove();
  622. anyDragging = true;
  623. }
  624. }
  625. if (! anyDragging)
  626. stopTimer();
  627. }
  628. OwnedArray<MouseInputSourceInternal> sources;
  629. Array<MouseInputSource> sourceArray;
  630. };
  631. } // namespace juce