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.

751 lines
30KB

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