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.

783 lines
31KB

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