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.

782 lines
31KB

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