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.

753 lines
30KB

  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. class MouseInputSourceInternal : private AsyncUpdater
  20. {
  21. public:
  22. //==============================================================================
  23. MouseInputSourceInternal (const int i, const MouseInputSource::InputSourceType type)
  24. : index (i), inputType (type), pressure (0.0f), orientation (0.0f), rotation (0.0f), tiltX (0.0f), tiltY (0.0f),
  25. isUnboundedMouseModeOn (false), isCursorVisibleUntilOffscreen (false), lastPeer (nullptr),
  26. currentCursorHandle (nullptr), mouseEventCounter (0), mouseMovedSignificantlySincePressed (false)
  27. {
  28. }
  29. //==============================================================================
  30. bool isDragging() const noexcept
  31. {
  32. return buttonState.isAnyMouseButtonDown();
  33. }
  34. Component* getComponentUnderMouse() const noexcept
  35. {
  36. return componentUnderMouse.get();
  37. }
  38. ModifierKeys getCurrentModifiers() const noexcept
  39. {
  40. return ModifierKeys::getCurrentModifiers().withoutMouseButtons().withFlags (buttonState.getRawFlags());
  41. }
  42. ComponentPeer* getPeer() noexcept
  43. {
  44. if (! ComponentPeer::isValidPeer (lastPeer))
  45. lastPeer = nullptr;
  46. return lastPeer;
  47. }
  48. static Point<float> screenPosToLocalPos (Component& comp, Point<float> pos)
  49. {
  50. if (ComponentPeer* const peer = comp.getPeer())
  51. {
  52. pos = peer->globalToLocal (pos);
  53. Component& peerComp = peer->getComponent();
  54. return comp.getLocalPoint (&peerComp, ScalingHelpers::unscaledScreenPosToScaled (peerComp, pos));
  55. }
  56. return comp.getLocalPoint (nullptr, ScalingHelpers::unscaledScreenPosToScaled (comp, pos));
  57. }
  58. Component* findComponentAt (Point<float> screenPos)
  59. {
  60. if (ComponentPeer* const peer = getPeer())
  61. {
  62. Point<float> relativePos (ScalingHelpers::unscaledScreenPosToScaled (peer->getComponent(),
  63. peer->globalToLocal (screenPos)));
  64. Component& comp = peer->getComponent();
  65. const Point<int> pos (relativePos.roundToInt());
  66. // (the contains() call is needed to test for overlapping desktop windows)
  67. if (comp.contains (pos))
  68. return comp.getComponentAt (pos);
  69. }
  70. return nullptr;
  71. }
  72. Point<float> getScreenPosition() const
  73. {
  74. // This needs to return the live position if possible, but it mustn't update the lastScreenPos
  75. // value, because that can cause continuity problems.
  76. return ScalingHelpers::unscaledScreenPosToScaled
  77. (unboundedMouseOffset + (inputType != MouseInputSource::InputSourceType::touch ? MouseInputSource::getCurrentRawMousePosition()
  78. : lastScreenPos));
  79. }
  80. void setScreenPosition (Point<float> p)
  81. {
  82. MouseInputSource::setRawMousePosition (ScalingHelpers::scaledScreenPosToUnscaled (p));
  83. }
  84. bool isPressureValid() const noexcept { return pressure >= 0.0f && pressure <= 1.0f; }
  85. bool isOrientationValid() const noexcept { return orientation >= 0.0f && orientation <= 2.0f * float_Pi; }
  86. bool isRotationValid() const noexcept { return rotation >= 0.0f && rotation <= 2.0f * float_Pi; }
  87. bool isTiltValid (bool isX) const noexcept { return isX ? (tiltX >= -1.0f && tiltX <= 1.0f) : (tiltY >= -1.0f && tiltY <= 1.0f); }
  88. //==============================================================================
  89. #if JUCE_DUMP_MOUSE_EVENTS
  90. #define JUCE_MOUSE_EVENT_
  91. (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, const 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, const 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, const 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. const int lastCounter = mouseEventCounter;
  153. if (buttonState.isAnyMouseButtonDown())
  154. {
  155. if (Component* const current = getComponentUnderMouse())
  156. {
  157. const ModifierKeys 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 (Component* const current = getComponentUnderMouse())
  170. {
  171. registerMouseDown (screenPos, time, *current, buttonState);
  172. sendMouseDown (*current, screenPos, time);
  173. }
  174. }
  175. return lastCounter != mouseEventCounter;
  176. }
  177. void setComponentUnderMouse (Component* const newComponent, Point<float> screenPos, Time time)
  178. {
  179. Component* current = getComponentUnderMouse();
  180. if (newComponent != current)
  181. {
  182. WeakReference<Component> safeNewComp (newComponent);
  183. const ModifierKeys originalButtonState (buttonState);
  184. if (current != nullptr)
  185. {
  186. WeakReference<Component> safeOldComp (current);
  187. setButtons (screenPos, time, ModifierKeys());
  188. if (safeOldComp != nullptr)
  189. {
  190. componentUnderMouse = safeNewComp;
  191. sendMouseExit (*safeOldComp, screenPos, time);
  192. }
  193. buttonState = originalButtonState;
  194. }
  195. current = componentUnderMouse = safeNewComp;
  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. ModifierKeys::updateCurrentModifiers();
  205. if (&newPeer != lastPeer)
  206. {
  207. setComponentUnderMouse (nullptr, screenPos, time);
  208. lastPeer = &newPeer;
  209. setComponentUnderMouse (findComponentAt (screenPos), screenPos, time);
  210. }
  211. }
  212. void setScreenPos (Point<float> newScreenPos, Time time, const bool forceUpdate)
  213. {
  214. if (! isDragging())
  215. setComponentUnderMouse (findComponentAt (newScreenPos), newScreenPos, time);
  216. if (newScreenPos != lastScreenPos || forceUpdate)
  217. {
  218. cancelPendingUpdate();
  219. lastScreenPos = newScreenPos;
  220. if (Component* const 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. const Point<float> 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 (ComponentPeer* 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 (Component* target = lastNonInertialWheelTarget)
  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 (Component* 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 (! hasMouseMovedSignificantlySincePressed())
  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 hasMouseMovedSignificantlySincePressed() const noexcept
  324. {
  325. return mouseMovedSignificantlySincePressed
  326. || lastTime > mouseDowns[0].time + RelativeTime::milliseconds (300);
  327. }
  328. //==============================================================================
  329. void triggerFakeMove()
  330. {
  331. triggerAsyncUpdate();
  332. }
  333. void handleAsyncUpdate() override
  334. {
  335. setScreenPos (lastScreenPos, jmax (lastTime, Time::getCurrentTime()), true);
  336. }
  337. //==============================================================================
  338. void enableUnboundedMouseMovement (bool enable, bool keepCursorVisibleUntilOffscreen)
  339. {
  340. enable = enable && isDragging();
  341. isCursorVisibleUntilOffscreen = keepCursorVisibleUntilOffscreen;
  342. if (enable != isUnboundedMouseModeOn)
  343. {
  344. if ((! enable) && ((! isCursorVisibleUntilOffscreen) || ! unboundedMouseOffset.isOrigin()))
  345. {
  346. // when released, return the mouse to within the component's bounds
  347. if (Component* current = getComponentUnderMouse())
  348. setScreenPosition (current->getScreenBounds().toFloat()
  349. .getConstrainedPoint (ScalingHelpers::unscaledScreenPosToScaled (lastScreenPos)));
  350. }
  351. isUnboundedMouseModeOn = enable;
  352. unboundedMouseOffset = Point<float>();
  353. revealCursor (true);
  354. }
  355. }
  356. void handleUnboundedDrag (Component& current)
  357. {
  358. const Rectangle<float> componentScreenBounds
  359. = ScalingHelpers::scaledScreenPosToUnscaled (current.getParentMonitorArea().reduced (2, 2).toFloat());
  360. if (! componentScreenBounds.contains (lastScreenPos))
  361. {
  362. const Point<float> componentCentre (current.getScreenBounds().toFloat().getCentre());
  363. unboundedMouseOffset += (lastScreenPos - ScalingHelpers::scaledScreenPosToUnscaled (componentCentre));
  364. setScreenPosition (componentCentre);
  365. }
  366. else if (isCursorVisibleUntilOffscreen
  367. && (! unboundedMouseOffset.isOrigin())
  368. && componentScreenBounds.contains (lastScreenPos + unboundedMouseOffset))
  369. {
  370. MouseInputSource::setRawMousePosition (lastScreenPos + unboundedMouseOffset);
  371. unboundedMouseOffset = Point<float>();
  372. }
  373. }
  374. //==============================================================================
  375. void showMouseCursor (MouseCursor cursor, bool forcedUpdate)
  376. {
  377. if (isUnboundedMouseModeOn && ((! unboundedMouseOffset.isOrigin()) || ! isCursorVisibleUntilOffscreen))
  378. {
  379. cursor = MouseCursor::NoCursor;
  380. forcedUpdate = true;
  381. }
  382. if (forcedUpdate || cursor.getHandle() != currentCursorHandle)
  383. {
  384. currentCursorHandle = cursor.getHandle();
  385. cursor.showInWindow (getPeer());
  386. }
  387. }
  388. void hideCursor()
  389. {
  390. showMouseCursor (MouseCursor::NoCursor, true);
  391. }
  392. void revealCursor (bool forcedUpdate)
  393. {
  394. MouseCursor mc (MouseCursor::NormalCursor);
  395. if (Component* current = getComponentUnderMouse())
  396. mc = current->getLookAndFeel().getMouseCursorFor (*current);
  397. showMouseCursor (mc, forcedUpdate);
  398. }
  399. //==============================================================================
  400. const int index;
  401. const MouseInputSource::InputSourceType inputType;
  402. Point<float> lastScreenPos, unboundedMouseOffset; // NB: these are unscaled coords
  403. ModifierKeys buttonState;
  404. float pressure;
  405. float orientation;
  406. float rotation;
  407. float tiltX;
  408. float tiltY;
  409. bool isUnboundedMouseModeOn, isCursorVisibleUntilOffscreen;
  410. private:
  411. WeakReference<Component> componentUnderMouse, lastNonInertialWheelTarget;
  412. ComponentPeer* lastPeer;
  413. void* currentCursorHandle;
  414. int mouseEventCounter;
  415. struct RecentMouseDown
  416. {
  417. RecentMouseDown() noexcept : peerID (0) {}
  418. Point<float> position;
  419. Time time;
  420. ModifierKeys buttons;
  421. uint32 peerID;
  422. bool canBePartOfMultipleClickWith (const RecentMouseDown& other, const int maxTimeBetweenMs) const
  423. {
  424. return time - other.time < RelativeTime::milliseconds (maxTimeBetweenMs)
  425. && std::abs (position.x - other.position.x) < 8
  426. && std::abs (position.y - other.position.y) < 8
  427. && buttons == other.buttons
  428. && peerID == other.peerID;
  429. }
  430. };
  431. RecentMouseDown mouseDowns[4];
  432. Time lastTime;
  433. bool mouseMovedSignificantlySincePressed;
  434. void registerMouseDown (Point<float> screenPos, Time time,
  435. Component& component, const ModifierKeys modifiers) noexcept
  436. {
  437. for (int i = numElementsInArray (mouseDowns); --i > 0;)
  438. mouseDowns[i] = mouseDowns[i - 1];
  439. mouseDowns[0].position = screenPos;
  440. mouseDowns[0].time = time;
  441. mouseDowns[0].buttons = modifiers.withOnlyMouseButtons();
  442. if (ComponentPeer* const peer = component.getPeer())
  443. mouseDowns[0].peerID = peer->getUniqueID();
  444. else
  445. mouseDowns[0].peerID = 0;
  446. mouseMovedSignificantlySincePressed = false;
  447. lastNonInertialWheelTarget = nullptr;
  448. }
  449. void registerMouseDrag (Point<float> screenPos) noexcept
  450. {
  451. mouseMovedSignificantlySincePressed = mouseMovedSignificantlySincePressed
  452. || mouseDowns[0].position.getDistanceFrom (screenPos) >= 4;
  453. }
  454. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseInputSourceInternal)
  455. };
  456. //==============================================================================
  457. MouseInputSource::MouseInputSource (MouseInputSourceInternal* s) noexcept : pimpl (s) {}
  458. MouseInputSource::MouseInputSource (const MouseInputSource& other) noexcept : pimpl (other.pimpl) {}
  459. MouseInputSource::~MouseInputSource() noexcept {}
  460. MouseInputSource& MouseInputSource::operator= (const MouseInputSource& other) noexcept
  461. {
  462. pimpl = other.pimpl;
  463. return *this;
  464. }
  465. MouseInputSource::InputSourceType MouseInputSource::getType() const noexcept { return pimpl->inputType; }
  466. bool MouseInputSource::isMouse() const noexcept { return (getType() == MouseInputSource::InputSourceType::mouse); }
  467. bool MouseInputSource::isTouch() const noexcept { return (getType() == MouseInputSource::InputSourceType::touch); }
  468. bool MouseInputSource::isPen() const noexcept { return (getType() == MouseInputSource::InputSourceType::pen); }
  469. bool MouseInputSource::canHover() const noexcept { return ! isTouch(); }
  470. bool MouseInputSource::hasMouseWheel() const noexcept { return ! isTouch(); }
  471. int MouseInputSource::getIndex() const noexcept { return pimpl->index; }
  472. bool MouseInputSource::isDragging() const noexcept { return pimpl->isDragging(); }
  473. Point<float> MouseInputSource::getScreenPosition() const noexcept { return pimpl->getScreenPosition(); }
  474. ModifierKeys MouseInputSource::getCurrentModifiers() const noexcept { return pimpl->getCurrentModifiers(); }
  475. float MouseInputSource::getCurrentPressure() const noexcept { return pimpl->pressure; }
  476. bool MouseInputSource::isPressureValid() const noexcept { return pimpl->isPressureValid(); }
  477. float MouseInputSource::getCurrentOrientation() const noexcept { return pimpl->orientation; }
  478. bool MouseInputSource::isOrientationValid() const noexcept { return pimpl->isOrientationValid(); }
  479. float MouseInputSource::getCurrentRotation() const noexcept { return pimpl->rotation; }
  480. bool MouseInputSource::isRotationValid() const noexcept { return pimpl->isRotationValid(); }
  481. float MouseInputSource::getCurrentTilt (bool tiltX) const noexcept { return tiltX ? pimpl->tiltX : pimpl->tiltY; }
  482. bool MouseInputSource::isTiltValid (bool isX) const noexcept { return pimpl->isTiltValid (isX); }
  483. Component* MouseInputSource::getComponentUnderMouse() const { return pimpl->getComponentUnderMouse(); }
  484. void MouseInputSource::triggerFakeMove() const { pimpl->triggerFakeMove(); }
  485. int MouseInputSource::getNumberOfMultipleClicks() const noexcept { return pimpl->getNumberOfMultipleClicks(); }
  486. Time MouseInputSource::getLastMouseDownTime() const noexcept { return pimpl->getLastMouseDownTime(); }
  487. Point<float> MouseInputSource::getLastMouseDownPosition() const noexcept { return pimpl->getLastMouseDownPosition(); }
  488. bool MouseInputSource::hasMouseMovedSignificantlySincePressed() const noexcept { return pimpl->hasMouseMovedSignificantlySincePressed(); }
  489. bool MouseInputSource::canDoUnboundedMovement() const noexcept { return ! isTouch(); }
  490. void MouseInputSource::enableUnboundedMouseMovement (bool isEnabled, bool keepCursorVisibleUntilOffscreen) const
  491. { pimpl->enableUnboundedMouseMovement (isEnabled, keepCursorVisibleUntilOffscreen); }
  492. bool MouseInputSource::isUnboundedMouseMovementEnabled() const { return pimpl->isUnboundedMouseModeOn; }
  493. bool MouseInputSource::hasMouseCursor() const noexcept { return ! isTouch(); }
  494. void MouseInputSource::showMouseCursor (const MouseCursor& cursor) { pimpl->showMouseCursor (cursor, false); }
  495. void MouseInputSource::hideCursor() { pimpl->hideCursor(); }
  496. void MouseInputSource::revealCursor() { pimpl->revealCursor (false); }
  497. void MouseInputSource::forceMouseCursorUpdate() { pimpl->revealCursor (true); }
  498. void MouseInputSource::setScreenPosition (Point<float> p) { pimpl->setScreenPosition (p); }
  499. void MouseInputSource::handleEvent (ComponentPeer& peer, Point<float> pos, int64 time, ModifierKeys mods,
  500. float pressure, float orientation, const PenDetails& pen)
  501. {
  502. pimpl->handleEvent (peer, pos, Time (time), mods.withOnlyMouseButtons(), pressure, orientation, pen);
  503. }
  504. void MouseInputSource::handleWheel (ComponentPeer& peer, Point<float> pos, int64 time, const MouseWheelDetails& wheel)
  505. {
  506. pimpl->handleWheel (peer, pos, Time (time), wheel);
  507. }
  508. void MouseInputSource::handleMagnifyGesture (ComponentPeer& peer, Point<float> pos, int64 time, float scaleFactor)
  509. {
  510. pimpl->handleMagnifyGesture (peer, pos, Time (time), scaleFactor);
  511. }
  512. const float MouseInputSource::invalidPressure = 0.0f;
  513. const float MouseInputSource::invalidOrientation = 0.0f;
  514. const float MouseInputSource::invalidRotation = 0.0f;
  515. const float MouseInputSource::invalidTiltX = 0.0f;
  516. const float MouseInputSource::invalidTiltY = 0.0f;
  517. //==============================================================================
  518. struct MouseInputSource::SourceList : public Timer
  519. {
  520. SourceList()
  521. {
  522. addSource (0, MouseInputSource::InputSourceType::mouse);
  523. }
  524. bool addSource();
  525. bool canUseTouch();
  526. MouseInputSource* addSource (int index, MouseInputSource::InputSourceType type)
  527. {
  528. auto* s = new MouseInputSourceInternal (index, type);
  529. sources.add (s);
  530. sourceArray.add (MouseInputSource (s));
  531. return &sourceArray.getReference (sourceArray.size() - 1);
  532. }
  533. MouseInputSource* getMouseSource (int index) const noexcept
  534. {
  535. return isPositiveAndBelow (index, sourceArray.size()) ? &sourceArray.getReference (index)
  536. : nullptr;
  537. }
  538. MouseInputSource* getOrCreateMouseInputSource (MouseInputSource::InputSourceType type, int touchIndex = 0)
  539. {
  540. if (type == MouseInputSource::InputSourceType::mouse || type == MouseInputSource::InputSourceType::pen)
  541. {
  542. for (auto& m : sourceArray)
  543. if (type == m.getType())
  544. return &m;
  545. addSource (0, type);
  546. }
  547. else if (type == MouseInputSource::InputSourceType::touch)
  548. {
  549. jassert (touchIndex >= 0 && touchIndex < 100); // sanity-check on number of fingers
  550. for (auto& m : sourceArray)
  551. if (type == m.getType() && touchIndex == m.getIndex())
  552. return &m;
  553. if (canUseTouch())
  554. return addSource (touchIndex, type);
  555. }
  556. return nullptr;
  557. }
  558. int getNumDraggingMouseSources() const noexcept
  559. {
  560. int num = 0;
  561. for (int i = 0; i < sources.size(); ++i)
  562. if (sources.getUnchecked(i)->isDragging())
  563. ++num;
  564. return num;
  565. }
  566. MouseInputSource* getDraggingMouseSource (int index) const noexcept
  567. {
  568. int num = 0;
  569. for (int i = 0; i < sources.size(); ++i)
  570. {
  571. MouseInputSource* const mi = &(sourceArray.getReference(i));
  572. if (mi->isDragging())
  573. {
  574. if (index == num)
  575. return mi;
  576. ++num;
  577. }
  578. }
  579. return nullptr;
  580. }
  581. void beginDragAutoRepeat (const int interval)
  582. {
  583. if (interval > 0)
  584. {
  585. if (getTimerInterval() != interval)
  586. startTimer (interval);
  587. }
  588. else
  589. {
  590. stopTimer();
  591. }
  592. }
  593. void timerCallback() override
  594. {
  595. int numMiceDown = 0;
  596. for (int i = 0; i < sources.size(); ++i)
  597. {
  598. MouseInputSourceInternal* const mi = sources.getUnchecked(i);
  599. if (mi->isDragging())
  600. {
  601. mi->triggerFakeMove();
  602. ++numMiceDown;
  603. }
  604. }
  605. if (numMiceDown == 0)
  606. stopTimer();
  607. }
  608. OwnedArray<MouseInputSourceInternal> sources;
  609. Array<MouseInputSource> sourceArray;
  610. };