Audio plugin host https://kx.studio/carla
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.

758 lines
31KB

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