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.

746 lines
29KB

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