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.

751 lines
30KB

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