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.

751 lines
30KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class MouseInputSourceInternal : private AsyncUpdater
  18. {
  19. public:
  20. //==============================================================================
  21. MouseInputSourceInternal (const int i, const MouseInputSource::InputSourceType type)
  22. : index (i), inputType (type), pressure (0.0f), orientation (0.0f), rotation (0.0f), tiltX (0.0f), tiltY (0.0f),
  23. isUnboundedMouseModeOn (false), isCursorVisibleUntilOffscreen (false), lastPeer (nullptr),
  24. currentCursorHandle (nullptr), mouseEventCounter (0), mouseMovedSignificantlySincePressed (false)
  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::getCurrentModifiers().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 (ComponentPeer* const peer = comp.getPeer())
  49. {
  50. pos = peer->globalToLocal (pos);
  51. Component& 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 (ComponentPeer* const peer = getPeer())
  59. {
  60. Point<float> relativePos (ScalingHelpers::unscaledScreenPosToScaled (peer->getComponent(),
  61. peer->globalToLocal (screenPos)));
  62. Component& comp = peer->getComponent();
  63. const Point<int> 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
  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
  75. (unboundedMouseOffset + (inputType != MouseInputSource::InputSourceType::touch ? MouseInputSource::getCurrentRawMousePosition()
  76. : lastScreenPos));
  77. }
  78. void setScreenPosition (Point<float> p)
  79. {
  80. MouseInputSource::setRawMousePosition (ScalingHelpers::scaledScreenPosToUnscaled (p));
  81. }
  82. bool isPressureValid() const noexcept { return pressure >= 0.0f && pressure <= 1.0f; }
  83. bool isOrientationValid() const noexcept { return orientation >= 0.0f && orientation <= 2.0f * float_Pi; }
  84. bool isRotationValid() const noexcept { return rotation >= 0.0f && rotation <= 2.0f * float_Pi; }
  85. bool isTiltValid (bool isX) const noexcept { return isX ? (tiltX >= -1.0f && tiltX <= 1.0f) : (tiltY >= -1.0f && tiltY <= 1.0f); }
  86. //==============================================================================
  87. #if JUCE_DUMP_MOUSE_EVENTS
  88. #define JUCE_MOUSE_EVENT_
  89. (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, const 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, const 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, const 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. const int lastCounter = mouseEventCounter;
  151. if (buttonState.isAnyMouseButtonDown())
  152. {
  153. if (Component* const current = getComponentUnderMouse())
  154. {
  155. const ModifierKeys 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 (Component* const current = getComponentUnderMouse())
  168. {
  169. registerMouseDown (screenPos, time, *current, buttonState);
  170. sendMouseDown (*current, screenPos, time);
  171. }
  172. }
  173. return lastCounter != mouseEventCounter;
  174. }
  175. void setComponentUnderMouse (Component* const newComponent, Point<float> screenPos, Time time)
  176. {
  177. Component* current = getComponentUnderMouse();
  178. if (newComponent != current)
  179. {
  180. WeakReference<Component> safeNewComp (newComponent);
  181. const ModifierKeys originalButtonState (buttonState);
  182. if (current != nullptr)
  183. {
  184. WeakReference<Component> safeOldComp (current);
  185. setButtons (screenPos, time, ModifierKeys());
  186. if (safeOldComp != nullptr)
  187. {
  188. componentUnderMouse = safeNewComp;
  189. sendMouseExit (*safeOldComp, screenPos, time);
  190. }
  191. buttonState = originalButtonState;
  192. }
  193. current = componentUnderMouse = safeNewComp;
  194. if (current != nullptr)
  195. sendMouseEnter (*current, screenPos, time);
  196. revealCursor (false);
  197. setButtons (screenPos, time, originalButtonState);
  198. }
  199. }
  200. void setPeer (ComponentPeer& newPeer, Point<float> screenPos, Time time)
  201. {
  202. ModifierKeys::updateCurrentModifiers();
  203. if (&newPeer != lastPeer)
  204. {
  205. setComponentUnderMouse (nullptr, screenPos, time);
  206. lastPeer = &newPeer;
  207. setComponentUnderMouse (findComponentAt (screenPos), screenPos, time);
  208. }
  209. }
  210. void setScreenPos (Point<float> newScreenPos, Time time, const bool forceUpdate)
  211. {
  212. if (! isDragging())
  213. setComponentUnderMouse (findComponentAt (newScreenPos), newScreenPos, time);
  214. if (newScreenPos != lastScreenPos || forceUpdate)
  215. {
  216. cancelPendingUpdate();
  217. lastScreenPos = newScreenPos;
  218. if (Component* const current = getComponentUnderMouse())
  219. {
  220. if (isDragging())
  221. {
  222. registerMouseDrag (newScreenPos);
  223. sendMouseDrag (*current, newScreenPos + unboundedMouseOffset, time);
  224. if (isUnboundedMouseModeOn)
  225. handleUnboundedDrag (*current);
  226. }
  227. else
  228. {
  229. sendMouseMove (*current, newScreenPos, time);
  230. }
  231. }
  232. revealCursor (false);
  233. }
  234. }
  235. //==============================================================================
  236. void handleEvent (ComponentPeer& newPeer, Point<float> positionWithinPeer, Time time,
  237. const ModifierKeys newMods, float newPressure, float newOrientation, PenDetails pen)
  238. {
  239. lastTime = time;
  240. const bool pressureChanged = (pressure != newPressure);
  241. pressure = newPressure;
  242. const bool orientationChanged = (orientation != newOrientation);
  243. orientation = newOrientation;
  244. const bool rotationChanged = (rotation != pen.rotation);
  245. rotation = pen.rotation;
  246. const bool tiltChanged = (tiltX != pen.tiltX || tiltY != pen.tiltY);
  247. tiltX = pen.tiltX;
  248. tiltY = pen.tiltY;
  249. const bool shouldUpdate = (pressureChanged || orientationChanged || rotationChanged || tiltChanged);
  250. ++mouseEventCounter;
  251. const Point<float> screenPos (newPeer.localToGlobal (positionWithinPeer));
  252. if (isDragging() && newMods.isAnyMouseButtonDown())
  253. {
  254. setScreenPos (screenPos, time, shouldUpdate);
  255. }
  256. else
  257. {
  258. setPeer (newPeer, screenPos, time);
  259. if (ComponentPeer* peer = getPeer())
  260. {
  261. if (setButtons (screenPos, time, newMods))
  262. return; // some modal events have been dispatched, so the current event is now out-of-date
  263. peer = getPeer();
  264. if (peer != nullptr)
  265. setScreenPos (screenPos, time, shouldUpdate);
  266. }
  267. }
  268. }
  269. Component* getTargetForGesture (ComponentPeer& peer, Point<float> positionWithinPeer,
  270. Time time, Point<float>& screenPos)
  271. {
  272. lastTime = time;
  273. ++mouseEventCounter;
  274. screenPos = peer.localToGlobal (positionWithinPeer);
  275. setPeer (peer, screenPos, time);
  276. setScreenPos (screenPos, time, false);
  277. triggerFakeMove();
  278. return getComponentUnderMouse();
  279. }
  280. void handleWheel (ComponentPeer& peer, Point<float> positionWithinPeer,
  281. Time time, const MouseWheelDetails& wheel)
  282. {
  283. Desktop::getInstance().incrementMouseWheelCounter();
  284. Point<float> screenPos;
  285. // This will make sure that when the wheel spins in its inertial phase, any events
  286. // continue to be sent to the last component that the mouse was over when it was being
  287. // actively controlled by the user. This avoids confusion when scrolling through nested
  288. // scrollable components.
  289. if (lastNonInertialWheelTarget == nullptr || ! wheel.isInertial)
  290. lastNonInertialWheelTarget = getTargetForGesture (peer, positionWithinPeer, time, screenPos);
  291. else
  292. screenPos = peer.localToGlobal (positionWithinPeer);
  293. if (Component* target = lastNonInertialWheelTarget)
  294. sendMouseWheel (*target, screenPos, time, wheel);
  295. }
  296. void handleMagnifyGesture (ComponentPeer& peer, Point<float> positionWithinPeer,
  297. Time time, const float scaleFactor)
  298. {
  299. Point<float> screenPos;
  300. if (Component* current = getTargetForGesture (peer, positionWithinPeer, time, screenPos))
  301. sendMagnifyGesture (*current, screenPos, time, scaleFactor);
  302. }
  303. //==============================================================================
  304. Time getLastMouseDownTime() const noexcept { return mouseDowns[0].time; }
  305. Point<float> getLastMouseDownPosition() const noexcept { return ScalingHelpers::unscaledScreenPosToScaled (mouseDowns[0].position); }
  306. int getNumberOfMultipleClicks() const noexcept
  307. {
  308. int numClicks = 1;
  309. if (! hasMouseMovedSignificantlySincePressed())
  310. {
  311. for (int i = 1; i < numElementsInArray (mouseDowns); ++i)
  312. {
  313. if (mouseDowns[0].canBePartOfMultipleClickWith (mouseDowns[i], MouseEvent::getDoubleClickTimeout() * jmin (i, 2)))
  314. ++numClicks;
  315. else
  316. break;
  317. }
  318. }
  319. return numClicks;
  320. }
  321. bool hasMouseMovedSignificantlySincePressed() const noexcept
  322. {
  323. return mouseMovedSignificantlySincePressed
  324. || lastTime > mouseDowns[0].time + RelativeTime::milliseconds (300);
  325. }
  326. //==============================================================================
  327. void triggerFakeMove()
  328. {
  329. triggerAsyncUpdate();
  330. }
  331. void handleAsyncUpdate() override
  332. {
  333. setScreenPos (lastScreenPos, jmax (lastTime, Time::getCurrentTime()), true);
  334. }
  335. //==============================================================================
  336. void enableUnboundedMouseMovement (bool enable, bool keepCursorVisibleUntilOffscreen)
  337. {
  338. enable = enable && isDragging();
  339. isCursorVisibleUntilOffscreen = keepCursorVisibleUntilOffscreen;
  340. if (enable != isUnboundedMouseModeOn)
  341. {
  342. if ((! enable) && ((! isCursorVisibleUntilOffscreen) || ! unboundedMouseOffset.isOrigin()))
  343. {
  344. // when released, return the mouse to within the component's bounds
  345. if (Component* current = getComponentUnderMouse())
  346. setScreenPosition (current->getScreenBounds().toFloat()
  347. .getConstrainedPoint (ScalingHelpers::unscaledScreenPosToScaled (lastScreenPos)));
  348. }
  349. isUnboundedMouseModeOn = enable;
  350. unboundedMouseOffset = Point<float>();
  351. revealCursor (true);
  352. }
  353. }
  354. void handleUnboundedDrag (Component& current)
  355. {
  356. const Rectangle<float> componentScreenBounds
  357. = ScalingHelpers::scaledScreenPosToUnscaled (current.getParentMonitorArea().reduced (2, 2).toFloat());
  358. if (! componentScreenBounds.contains (lastScreenPos))
  359. {
  360. const Point<float> componentCentre (current.getScreenBounds().toFloat().getCentre());
  361. unboundedMouseOffset += (lastScreenPos - ScalingHelpers::scaledScreenPosToUnscaled (componentCentre));
  362. setScreenPosition (componentCentre);
  363. }
  364. else if (isCursorVisibleUntilOffscreen
  365. && (! unboundedMouseOffset.isOrigin())
  366. && componentScreenBounds.contains (lastScreenPos + unboundedMouseOffset))
  367. {
  368. MouseInputSource::setRawMousePosition (lastScreenPos + unboundedMouseOffset);
  369. unboundedMouseOffset = Point<float>();
  370. }
  371. }
  372. //==============================================================================
  373. void showMouseCursor (MouseCursor cursor, bool forcedUpdate)
  374. {
  375. if (isUnboundedMouseModeOn && ((! unboundedMouseOffset.isOrigin()) || ! isCursorVisibleUntilOffscreen))
  376. {
  377. cursor = MouseCursor::NoCursor;
  378. forcedUpdate = true;
  379. }
  380. if (forcedUpdate || cursor.getHandle() != currentCursorHandle)
  381. {
  382. currentCursorHandle = cursor.getHandle();
  383. cursor.showInWindow (getPeer());
  384. }
  385. }
  386. void hideCursor()
  387. {
  388. showMouseCursor (MouseCursor::NoCursor, true);
  389. }
  390. void revealCursor (bool forcedUpdate)
  391. {
  392. MouseCursor mc (MouseCursor::NormalCursor);
  393. if (Component* current = getComponentUnderMouse())
  394. mc = current->getLookAndFeel().getMouseCursorFor (*current);
  395. showMouseCursor (mc, forcedUpdate);
  396. }
  397. //==============================================================================
  398. const int index;
  399. const MouseInputSource::InputSourceType inputType;
  400. Point<float> lastScreenPos, unboundedMouseOffset; // NB: these are unscaled coords
  401. ModifierKeys buttonState;
  402. float pressure;
  403. float orientation;
  404. float rotation;
  405. float tiltX;
  406. float tiltY;
  407. bool isUnboundedMouseModeOn, isCursorVisibleUntilOffscreen;
  408. private:
  409. WeakReference<Component> componentUnderMouse, lastNonInertialWheelTarget;
  410. ComponentPeer* lastPeer;
  411. void* currentCursorHandle;
  412. int mouseEventCounter;
  413. struct RecentMouseDown
  414. {
  415. RecentMouseDown() noexcept : peerID (0) {}
  416. Point<float> position;
  417. Time time;
  418. ModifierKeys buttons;
  419. uint32 peerID;
  420. bool canBePartOfMultipleClickWith (const RecentMouseDown& other, const int maxTimeBetweenMs) const
  421. {
  422. return time - other.time < RelativeTime::milliseconds (maxTimeBetweenMs)
  423. && std::abs (position.x - other.position.x) < 8
  424. && std::abs (position.y - other.position.y) < 8
  425. && buttons == other.buttons
  426. && peerID == other.peerID;
  427. }
  428. };
  429. RecentMouseDown mouseDowns[4];
  430. Time lastTime;
  431. bool mouseMovedSignificantlySincePressed;
  432. void registerMouseDown (Point<float> screenPos, Time time,
  433. Component& component, const ModifierKeys modifiers) noexcept
  434. {
  435. for (int i = numElementsInArray (mouseDowns); --i > 0;)
  436. mouseDowns[i] = mouseDowns[i - 1];
  437. mouseDowns[0].position = screenPos;
  438. mouseDowns[0].time = time;
  439. mouseDowns[0].buttons = modifiers.withOnlyMouseButtons();
  440. if (ComponentPeer* const peer = component.getPeer())
  441. mouseDowns[0].peerID = peer->getUniqueID();
  442. else
  443. mouseDowns[0].peerID = 0;
  444. mouseMovedSignificantlySincePressed = false;
  445. lastNonInertialWheelTarget = nullptr;
  446. }
  447. void registerMouseDrag (Point<float> screenPos) noexcept
  448. {
  449. mouseMovedSignificantlySincePressed = mouseMovedSignificantlySincePressed
  450. || mouseDowns[0].position.getDistanceFrom (screenPos) >= 4;
  451. }
  452. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseInputSourceInternal)
  453. };
  454. //==============================================================================
  455. MouseInputSource::MouseInputSource (MouseInputSourceInternal* s) noexcept : pimpl (s) {}
  456. MouseInputSource::MouseInputSource (const MouseInputSource& other) noexcept : pimpl (other.pimpl) {}
  457. MouseInputSource::~MouseInputSource() noexcept {}
  458. MouseInputSource& MouseInputSource::operator= (const MouseInputSource& other) noexcept
  459. {
  460. pimpl = other.pimpl;
  461. return *this;
  462. }
  463. MouseInputSource::InputSourceType MouseInputSource::getType() const noexcept { return pimpl->inputType; }
  464. bool MouseInputSource::isMouse() const noexcept { return (getType() == MouseInputSource::InputSourceType::mouse); }
  465. bool MouseInputSource::isTouch() const noexcept { return (getType() == MouseInputSource::InputSourceType::touch); }
  466. bool MouseInputSource::isPen() const noexcept { return (getType() == MouseInputSource::InputSourceType::pen); }
  467. bool MouseInputSource::canHover() const noexcept { return ! isTouch(); }
  468. bool MouseInputSource::hasMouseWheel() const noexcept { return ! isTouch(); }
  469. int MouseInputSource::getIndex() const noexcept { return pimpl->index; }
  470. bool MouseInputSource::isDragging() const noexcept { return pimpl->isDragging(); }
  471. Point<float> MouseInputSource::getScreenPosition() const noexcept { return pimpl->getScreenPosition(); }
  472. ModifierKeys MouseInputSource::getCurrentModifiers() const noexcept { return pimpl->getCurrentModifiers(); }
  473. float MouseInputSource::getCurrentPressure() const noexcept { return pimpl->pressure; }
  474. bool MouseInputSource::isPressureValid() const noexcept { return pimpl->isPressureValid(); }
  475. float MouseInputSource::getCurrentOrientation() const noexcept { return pimpl->orientation; }
  476. bool MouseInputSource::isOrientationValid() const noexcept { return pimpl->isOrientationValid(); }
  477. float MouseInputSource::getCurrentRotation() const noexcept { return pimpl->rotation; }
  478. bool MouseInputSource::isRotationValid() const noexcept { return pimpl->isRotationValid(); }
  479. float MouseInputSource::getCurrentTilt (bool tiltX) const noexcept { return tiltX ? pimpl->tiltX : pimpl->tiltY; }
  480. bool MouseInputSource::isTiltValid (bool isX) const noexcept { return pimpl->isTiltValid (isX); }
  481. Component* MouseInputSource::getComponentUnderMouse() const { return pimpl->getComponentUnderMouse(); }
  482. void MouseInputSource::triggerFakeMove() const { pimpl->triggerFakeMove(); }
  483. int MouseInputSource::getNumberOfMultipleClicks() const noexcept { return pimpl->getNumberOfMultipleClicks(); }
  484. Time MouseInputSource::getLastMouseDownTime() const noexcept { return pimpl->getLastMouseDownTime(); }
  485. Point<float> MouseInputSource::getLastMouseDownPosition() const noexcept { return pimpl->getLastMouseDownPosition(); }
  486. bool MouseInputSource::hasMouseMovedSignificantlySincePressed() const noexcept { return pimpl->hasMouseMovedSignificantlySincePressed(); }
  487. bool MouseInputSource::canDoUnboundedMovement() const noexcept { return ! isTouch(); }
  488. void MouseInputSource::enableUnboundedMouseMovement (bool isEnabled, bool keepCursorVisibleUntilOffscreen) const
  489. { pimpl->enableUnboundedMouseMovement (isEnabled, keepCursorVisibleUntilOffscreen); }
  490. bool MouseInputSource::isUnboundedMouseMovementEnabled() const { return pimpl->isUnboundedMouseModeOn; }
  491. bool MouseInputSource::hasMouseCursor() const noexcept { return ! isTouch(); }
  492. void MouseInputSource::showMouseCursor (const MouseCursor& cursor) { pimpl->showMouseCursor (cursor, false); }
  493. void MouseInputSource::hideCursor() { pimpl->hideCursor(); }
  494. void MouseInputSource::revealCursor() { pimpl->revealCursor (false); }
  495. void MouseInputSource::forceMouseCursorUpdate() { pimpl->revealCursor (true); }
  496. void MouseInputSource::setScreenPosition (Point<float> p) { pimpl->setScreenPosition (p); }
  497. void MouseInputSource::handleEvent (ComponentPeer& peer, Point<float> pos, int64 time, ModifierKeys mods,
  498. float pressure, float orientation, const PenDetails& pen)
  499. {
  500. pimpl->handleEvent (peer, pos, Time (time), mods.withOnlyMouseButtons(), pressure, orientation, pen);
  501. }
  502. void MouseInputSource::handleWheel (ComponentPeer& peer, Point<float> pos, int64 time, const MouseWheelDetails& wheel)
  503. {
  504. pimpl->handleWheel (peer, pos, Time (time), wheel);
  505. }
  506. void MouseInputSource::handleMagnifyGesture (ComponentPeer& peer, Point<float> pos, int64 time, float scaleFactor)
  507. {
  508. pimpl->handleMagnifyGesture (peer, pos, Time (time), scaleFactor);
  509. }
  510. const float MouseInputSource::invalidPressure = 0.0f;
  511. const float MouseInputSource::invalidOrientation = 0.0f;
  512. const float MouseInputSource::invalidRotation = 0.0f;
  513. const float MouseInputSource::invalidTiltX = 0.0f;
  514. const float MouseInputSource::invalidTiltY = 0.0f;
  515. //==============================================================================
  516. struct MouseInputSource::SourceList : public Timer
  517. {
  518. SourceList()
  519. {
  520. addSource (0, MouseInputSource::InputSourceType::mouse);
  521. }
  522. bool addSource();
  523. bool canUseTouch();
  524. MouseInputSource* addSource (int index, MouseInputSource::InputSourceType type)
  525. {
  526. auto* s = new MouseInputSourceInternal (index, type);
  527. sources.add (s);
  528. sourceArray.add (MouseInputSource (s));
  529. return &sourceArray.getReference (sourceArray.size() - 1);
  530. }
  531. MouseInputSource* getMouseSource (int index) const noexcept
  532. {
  533. return isPositiveAndBelow (index, sourceArray.size()) ? &sourceArray.getReference (index)
  534. : nullptr;
  535. }
  536. MouseInputSource* getOrCreateMouseInputSource (MouseInputSource::InputSourceType type, int touchIndex = 0)
  537. {
  538. if (type == MouseInputSource::InputSourceType::mouse || type == MouseInputSource::InputSourceType::pen)
  539. {
  540. for (auto& m : sourceArray)
  541. if (type == m.getType())
  542. return &m;
  543. addSource (0, type);
  544. }
  545. else if (type == MouseInputSource::InputSourceType::touch)
  546. {
  547. jassert (touchIndex >= 0 && touchIndex < 100); // sanity-check on number of fingers
  548. for (auto& m : sourceArray)
  549. if (type == m.getType() && touchIndex == m.getIndex())
  550. return &m;
  551. if (canUseTouch())
  552. return addSource (touchIndex, type);
  553. }
  554. return nullptr;
  555. }
  556. int getNumDraggingMouseSources() const noexcept
  557. {
  558. int num = 0;
  559. for (int i = 0; i < sources.size(); ++i)
  560. if (sources.getUnchecked(i)->isDragging())
  561. ++num;
  562. return num;
  563. }
  564. MouseInputSource* getDraggingMouseSource (int index) const noexcept
  565. {
  566. int num = 0;
  567. for (int i = 0; i < sources.size(); ++i)
  568. {
  569. MouseInputSource* const mi = &(sourceArray.getReference(i));
  570. if (mi->isDragging())
  571. {
  572. if (index == num)
  573. return mi;
  574. ++num;
  575. }
  576. }
  577. return nullptr;
  578. }
  579. void beginDragAutoRepeat (const int interval)
  580. {
  581. if (interval > 0)
  582. {
  583. if (getTimerInterval() != interval)
  584. startTimer (interval);
  585. }
  586. else
  587. {
  588. stopTimer();
  589. }
  590. }
  591. void timerCallback() override
  592. {
  593. int numMiceDown = 0;
  594. for (int i = 0; i < sources.size(); ++i)
  595. {
  596. MouseInputSourceInternal* const mi = sources.getUnchecked(i);
  597. if (mi->isDragging())
  598. {
  599. mi->triggerFakeMove();
  600. ++numMiceDown;
  601. }
  602. }
  603. if (numMiceDown == 0)
  604. stopTimer();
  605. }
  606. OwnedArray<MouseInputSourceInternal> sources;
  607. Array<MouseInputSource> sourceArray;
  608. };