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.

juce_MouseInputSource.cpp 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class MouseInputSourceInternal : private AsyncUpdater
  18. {
  19. public:
  20. //==============================================================================
  21. MouseInputSourceInternal (const int i, const bool isMouse)
  22. : index (i), isMouseDevice (isMouse),
  23. isUnboundedMouseModeOn (false), isCursorVisibleUntilOffscreen (false),
  24. lastPeer (nullptr), currentCursorHandle (nullptr),
  25. mouseEventCounter (0), mouseMovedSignificantlySincePressed (false)
  26. {
  27. }
  28. //==============================================================================
  29. bool isDragging() const noexcept
  30. {
  31. return buttonState.isAnyMouseButtonDown();
  32. }
  33. Component* getComponentUnderMouse() const
  34. {
  35. return componentUnderMouse.get();
  36. }
  37. ModifierKeys getCurrentModifiers() const
  38. {
  39. return ModifierKeys::getCurrentModifiers().withoutMouseButtons().withFlags (buttonState.getRawFlags());
  40. }
  41. ComponentPeer* getPeer()
  42. {
  43. if (! ComponentPeer::isValidPeer (lastPeer))
  44. lastPeer = nullptr;
  45. return lastPeer;
  46. }
  47. static Point<int> screenPosToLocalPos (Component& comp, Point<int> pos)
  48. {
  49. if (ComponentPeer* const peer = comp.getPeer())
  50. {
  51. pos = peer->globalToLocal (pos);
  52. Component& peerComp = peer->getComponent();
  53. return comp.getLocalPoint (&peerComp, ScalingHelpers::unscaledScreenPosToScaled (peerComp, pos));
  54. }
  55. return comp.getLocalPoint (nullptr, ScalingHelpers::unscaledScreenPosToScaled (comp, pos));
  56. }
  57. Component* findComponentAt (Point<int> screenPos)
  58. {
  59. if (ComponentPeer* const peer = getPeer())
  60. {
  61. Point<int> relativePos (ScalingHelpers::unscaledScreenPosToScaled (peer->getComponent(),
  62. peer->globalToLocal (screenPos)));
  63. Component& comp = peer->getComponent();
  64. // (the contains() call is needed to test for overlapping desktop windows)
  65. if (comp.contains (relativePos))
  66. return comp.getComponentAt (relativePos);
  67. }
  68. return nullptr;
  69. }
  70. Point<int> getScreenPosition() const
  71. {
  72. // This needs to return the live position if possible, but it mustn't update the lastScreenPos
  73. // value, because that can cause continuity problems.
  74. return ScalingHelpers::unscaledScreenPosToScaled
  75. (unboundedMouseOffset + (isMouseDevice ? MouseInputSource::getCurrentRawMousePosition()
  76. : lastScreenPos));
  77. }
  78. void setScreenPosition (Point<int> p)
  79. {
  80. MouseInputSource::setRawMousePosition (ScalingHelpers::scaledScreenPosToUnscaled (p));
  81. }
  82. //==============================================================================
  83. #if JUCE_DUMP_MOUSE_EVENTS
  84. #define JUCE_MOUSE_EVENT_DBG(desc) DBG ("Mouse " desc << " #" << source.getIndex() \
  85. << ": " << screenPosToLocalPos (comp, screenPos).toString() \
  86. << " - Comp: " << String::toHexString ((int) &comp));
  87. #else
  88. #define JUCE_MOUSE_EVENT_DBG(desc)
  89. #endif
  90. void sendMouseEnter (Component& comp, Point<int> screenPos, Time time)
  91. {
  92. JUCE_MOUSE_EVENT_DBG ("enter")
  93. comp.internalMouseEnter (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  94. }
  95. void sendMouseExit (Component& comp, Point<int> screenPos, Time time)
  96. {
  97. JUCE_MOUSE_EVENT_DBG ("exit")
  98. comp.internalMouseExit (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  99. }
  100. void sendMouseMove (Component& comp, Point<int> screenPos, Time time)
  101. {
  102. JUCE_MOUSE_EVENT_DBG ("move")
  103. comp.internalMouseMove (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  104. }
  105. void sendMouseDown (Component& comp, Point<int> screenPos, Time time)
  106. {
  107. JUCE_MOUSE_EVENT_DBG ("down")
  108. comp.internalMouseDown (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  109. }
  110. void sendMouseDrag (Component& comp, Point<int> screenPos, Time time)
  111. {
  112. JUCE_MOUSE_EVENT_DBG ("drag")
  113. comp.internalMouseDrag (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time);
  114. }
  115. void sendMouseUp (Component& comp, Point<int> screenPos, Time time, const ModifierKeys oldMods)
  116. {
  117. JUCE_MOUSE_EVENT_DBG ("up")
  118. comp.internalMouseUp (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, oldMods);
  119. }
  120. void sendMouseWheel (Component& comp, Point<int> screenPos, Time time, const MouseWheelDetails& wheel)
  121. {
  122. JUCE_MOUSE_EVENT_DBG ("wheel")
  123. comp.internalMouseWheel (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, wheel);
  124. }
  125. void sendMagnifyGesture (Component& comp, Point<int> screenPos, Time time, const float amount)
  126. {
  127. JUCE_MOUSE_EVENT_DBG ("magnify")
  128. comp.internalMagnifyGesture (MouseInputSource (this), screenPosToLocalPos (comp, screenPos), time, amount);
  129. }
  130. //==============================================================================
  131. // (returns true if the button change caused a modal event loop)
  132. bool setButtons (Point<int> screenPos, Time time, const ModifierKeys newButtonState)
  133. {
  134. if (buttonState == newButtonState)
  135. return false;
  136. // (avoid sending a spurious mouse-drag when we receive a mouse-up)
  137. if (! (isDragging() && ! newButtonState.isAnyMouseButtonDown()))
  138. setScreenPos (screenPos, time, false);
  139. // (ignore secondary clicks when there's already a button down)
  140. if (buttonState.isAnyMouseButtonDown() == newButtonState.isAnyMouseButtonDown())
  141. {
  142. buttonState = newButtonState;
  143. return false;
  144. }
  145. const int lastCounter = mouseEventCounter;
  146. if (buttonState.isAnyMouseButtonDown())
  147. {
  148. if (Component* const current = getComponentUnderMouse())
  149. {
  150. const ModifierKeys oldMods (getCurrentModifiers());
  151. buttonState = newButtonState; // must change this before calling sendMouseUp, in case it runs a modal loop
  152. sendMouseUp (*current, screenPos + unboundedMouseOffset, time, oldMods);
  153. if (lastCounter != mouseEventCounter)
  154. return true; // if a modal loop happened, then newButtonState is no longer valid.
  155. }
  156. enableUnboundedMouseMovement (false, false);
  157. }
  158. buttonState = newButtonState;
  159. if (buttonState.isAnyMouseButtonDown())
  160. {
  161. Desktop::getInstance().incrementMouseClickCounter();
  162. if (Component* const current = getComponentUnderMouse())
  163. {
  164. registerMouseDown (screenPos, time, *current, buttonState);
  165. sendMouseDown (*current, screenPos, time);
  166. }
  167. }
  168. return lastCounter != mouseEventCounter;
  169. }
  170. void setComponentUnderMouse (Component* const newComponent, Point<int> screenPos, Time time)
  171. {
  172. Component* current = getComponentUnderMouse();
  173. if (newComponent != current)
  174. {
  175. WeakReference<Component> safeNewComp (newComponent);
  176. const ModifierKeys originalButtonState (buttonState);
  177. if (current != nullptr)
  178. {
  179. WeakReference<Component> safeOldComp (current);
  180. setButtons (screenPos, time, ModifierKeys());
  181. if (safeOldComp != nullptr)
  182. {
  183. componentUnderMouse = safeNewComp;
  184. sendMouseExit (*safeOldComp, screenPos, time);
  185. }
  186. buttonState = originalButtonState;
  187. }
  188. current = componentUnderMouse = safeNewComp;
  189. if (current != nullptr)
  190. sendMouseEnter (*current, screenPos, time);
  191. revealCursor (false);
  192. setButtons (screenPos, time, originalButtonState);
  193. }
  194. }
  195. void setPeer (ComponentPeer& newPeer, Point<int> screenPos, Time time)
  196. {
  197. ModifierKeys::updateCurrentModifiers();
  198. if (&newPeer != lastPeer)
  199. {
  200. setComponentUnderMouse (nullptr, screenPos, time);
  201. lastPeer = &newPeer;
  202. setComponentUnderMouse (findComponentAt (screenPos), screenPos, time);
  203. }
  204. }
  205. void setScreenPos (Point<int> newScreenPos, Time time, const bool forceUpdate)
  206. {
  207. if (! isDragging())
  208. setComponentUnderMouse (findComponentAt (newScreenPos), newScreenPos, time);
  209. if (newScreenPos != lastScreenPos || forceUpdate)
  210. {
  211. cancelPendingUpdate();
  212. lastScreenPos = newScreenPos;
  213. if (Component* const current = getComponentUnderMouse())
  214. {
  215. if (isDragging())
  216. {
  217. registerMouseDrag (newScreenPos);
  218. sendMouseDrag (*current, newScreenPos + unboundedMouseOffset, time);
  219. if (isUnboundedMouseModeOn)
  220. handleUnboundedDrag (current);
  221. }
  222. else
  223. {
  224. sendMouseMove (*current, newScreenPos, time);
  225. }
  226. }
  227. revealCursor (false);
  228. }
  229. }
  230. //==============================================================================
  231. void handleEvent (ComponentPeer& newPeer, Point<int> positionWithinPeer, Time time, const ModifierKeys newMods)
  232. {
  233. lastTime = time;
  234. ++mouseEventCounter;
  235. const Point<int> screenPos (newPeer.localToGlobal (positionWithinPeer));
  236. if (isDragging() && newMods.isAnyMouseButtonDown())
  237. {
  238. setScreenPos (screenPos, time, false);
  239. }
  240. else
  241. {
  242. setPeer (newPeer, screenPos, time);
  243. if (ComponentPeer* peer = getPeer())
  244. {
  245. if (setButtons (screenPos, time, newMods))
  246. return; // some modal events have been dispatched, so the current event is now out-of-date
  247. peer = getPeer();
  248. if (peer != nullptr)
  249. setScreenPos (screenPos, time, false);
  250. }
  251. }
  252. }
  253. Component* getTargetForGesture (ComponentPeer& peer, Point<int> positionWithinPeer,
  254. Time time, Point<int>& screenPos)
  255. {
  256. lastTime = time;
  257. ++mouseEventCounter;
  258. screenPos = peer.localToGlobal (positionWithinPeer);
  259. setPeer (peer, screenPos, time);
  260. setScreenPos (screenPos, time, false);
  261. triggerFakeMove();
  262. return isDragging() ? nullptr : getComponentUnderMouse();
  263. }
  264. void handleWheel (ComponentPeer& peer, Point<int> positionWithinPeer,
  265. Time time, const MouseWheelDetails& wheel)
  266. {
  267. Desktop::getInstance().incrementMouseWheelCounter();
  268. Point<int> screenPos;
  269. if (Component* current = getTargetForGesture (peer, positionWithinPeer, time, screenPos))
  270. sendMouseWheel (*current, screenPos, time, wheel);
  271. }
  272. void handleMagnifyGesture (ComponentPeer& peer, Point<int> positionWithinPeer,
  273. Time time, const float scaleFactor)
  274. {
  275. Point<int> screenPos;
  276. if (Component* current = getTargetForGesture (peer, positionWithinPeer, time, screenPos))
  277. sendMagnifyGesture (*current, screenPos, time, scaleFactor);
  278. }
  279. //==============================================================================
  280. Time getLastMouseDownTime() const noexcept { return mouseDowns[0].time; }
  281. Point<int> getLastMouseDownPosition() const noexcept { return ScalingHelpers::unscaledScreenPosToScaled (mouseDowns[0].position); }
  282. int getNumberOfMultipleClicks() const noexcept
  283. {
  284. int numClicks = 0;
  285. if (mouseDowns[0].time != Time())
  286. {
  287. if (! mouseMovedSignificantlySincePressed)
  288. ++numClicks;
  289. for (int i = 1; i < numElementsInArray (mouseDowns); ++i)
  290. {
  291. if (mouseDowns[0].canBePartOfMultipleClickWith (mouseDowns[i], MouseEvent::getDoubleClickTimeout() * jmin (i, 2)))
  292. ++numClicks;
  293. else
  294. break;
  295. }
  296. }
  297. return numClicks;
  298. }
  299. bool hasMouseMovedSignificantlySincePressed() const noexcept
  300. {
  301. return mouseMovedSignificantlySincePressed
  302. || lastTime > mouseDowns[0].time + RelativeTime::milliseconds (300);
  303. }
  304. //==============================================================================
  305. void triggerFakeMove()
  306. {
  307. triggerAsyncUpdate();
  308. }
  309. void handleAsyncUpdate() override
  310. {
  311. setScreenPos (lastScreenPos, jmax (lastTime, Time::getCurrentTime()), true);
  312. }
  313. //==============================================================================
  314. void enableUnboundedMouseMovement (bool enable, bool keepCursorVisibleUntilOffscreen)
  315. {
  316. enable = enable && isDragging();
  317. isCursorVisibleUntilOffscreen = keepCursorVisibleUntilOffscreen;
  318. if (enable != isUnboundedMouseModeOn)
  319. {
  320. if ((! enable) && ((! isCursorVisibleUntilOffscreen) || ! unboundedMouseOffset.isOrigin()))
  321. {
  322. // when released, return the mouse to within the component's bounds
  323. if (Component* current = getComponentUnderMouse())
  324. Desktop::setMousePosition (current->getScreenBounds()
  325. .getConstrainedPoint (lastScreenPos));
  326. }
  327. isUnboundedMouseModeOn = enable;
  328. unboundedMouseOffset = Point<int>();
  329. revealCursor (true);
  330. }
  331. }
  332. void handleUnboundedDrag (Component* current)
  333. {
  334. const Rectangle<int> screenArea (current->getParentMonitorArea().expanded (-2, -2));
  335. if (! screenArea.contains (lastScreenPos))
  336. {
  337. const Point<int> componentCentre (current->getScreenBounds().getCentre());
  338. unboundedMouseOffset += (lastScreenPos - componentCentre);
  339. Desktop::setMousePosition (componentCentre);
  340. }
  341. else if (isCursorVisibleUntilOffscreen
  342. && (! unboundedMouseOffset.isOrigin())
  343. && screenArea.contains (lastScreenPos + unboundedMouseOffset))
  344. {
  345. Desktop::setMousePosition (lastScreenPos + unboundedMouseOffset);
  346. unboundedMouseOffset = Point<int>();
  347. }
  348. }
  349. //==============================================================================
  350. void showMouseCursor (MouseCursor cursor, bool forcedUpdate)
  351. {
  352. if (isUnboundedMouseModeOn && ((! unboundedMouseOffset.isOrigin()) || ! isCursorVisibleUntilOffscreen))
  353. {
  354. cursor = MouseCursor::NoCursor;
  355. forcedUpdate = true;
  356. }
  357. if (forcedUpdate || cursor.getHandle() != currentCursorHandle)
  358. {
  359. currentCursorHandle = cursor.getHandle();
  360. cursor.showInWindow (getPeer());
  361. }
  362. }
  363. void hideCursor()
  364. {
  365. showMouseCursor (MouseCursor::NoCursor, true);
  366. }
  367. void revealCursor (bool forcedUpdate)
  368. {
  369. MouseCursor mc (MouseCursor::NormalCursor);
  370. if (Component* current = getComponentUnderMouse())
  371. mc = current->getLookAndFeel().getMouseCursorFor (*current);
  372. showMouseCursor (mc, forcedUpdate);
  373. }
  374. //==============================================================================
  375. const int index;
  376. const bool isMouseDevice;
  377. Point<int> lastScreenPos;
  378. ModifierKeys buttonState;
  379. Point<int> unboundedMouseOffset;
  380. bool isUnboundedMouseModeOn, isCursorVisibleUntilOffscreen;
  381. private:
  382. WeakReference<Component> componentUnderMouse;
  383. ComponentPeer* lastPeer;
  384. void* currentCursorHandle;
  385. int mouseEventCounter;
  386. struct RecentMouseDown
  387. {
  388. RecentMouseDown() noexcept : peerID (0) {}
  389. Point<int> position;
  390. Time time;
  391. ModifierKeys buttons;
  392. uint32 peerID;
  393. bool canBePartOfMultipleClickWith (const RecentMouseDown& other, const int maxTimeBetweenMs) const
  394. {
  395. return time - other.time < RelativeTime::milliseconds (maxTimeBetweenMs)
  396. && abs (position.x - other.position.x) < 8
  397. && abs (position.y - other.position.y) < 8
  398. && buttons == other.buttons
  399. && peerID == other.peerID;
  400. }
  401. };
  402. RecentMouseDown mouseDowns[4];
  403. Time lastTime;
  404. bool mouseMovedSignificantlySincePressed;
  405. void registerMouseDown (Point<int> screenPos, Time time,
  406. Component& component, const ModifierKeys modifiers) noexcept
  407. {
  408. for (int i = numElementsInArray (mouseDowns); --i > 0;)
  409. mouseDowns[i] = mouseDowns[i - 1];
  410. mouseDowns[0].position = screenPos;
  411. mouseDowns[0].time = time;
  412. mouseDowns[0].buttons = modifiers.withOnlyMouseButtons();
  413. if (ComponentPeer* const peer = component.getPeer())
  414. mouseDowns[0].peerID = peer->getUniqueID();
  415. else
  416. mouseDowns[0].peerID = 0;
  417. mouseMovedSignificantlySincePressed = false;
  418. }
  419. void registerMouseDrag (Point<int> screenPos) noexcept
  420. {
  421. mouseMovedSignificantlySincePressed = mouseMovedSignificantlySincePressed
  422. || mouseDowns[0].position.getDistanceFrom (screenPos) >= 4;
  423. }
  424. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseInputSourceInternal)
  425. };
  426. //==============================================================================
  427. MouseInputSource::MouseInputSource (MouseInputSourceInternal* s) noexcept : pimpl (s) {}
  428. MouseInputSource::MouseInputSource (const MouseInputSource& other) noexcept : pimpl (other.pimpl) {}
  429. MouseInputSource::~MouseInputSource() noexcept {}
  430. MouseInputSource& MouseInputSource::operator= (const MouseInputSource& other) noexcept
  431. {
  432. pimpl = other.pimpl;
  433. return *this;
  434. }
  435. bool MouseInputSource::isMouse() const { return pimpl->isMouseDevice; }
  436. bool MouseInputSource::isTouch() const { return ! isMouse(); }
  437. bool MouseInputSource::canHover() const { return isMouse(); }
  438. bool MouseInputSource::hasMouseWheel() const { return isMouse(); }
  439. int MouseInputSource::getIndex() const { return pimpl->index; }
  440. bool MouseInputSource::isDragging() const { return pimpl->isDragging(); }
  441. Point<int> MouseInputSource::getScreenPosition() const { return pimpl->getScreenPosition(); }
  442. ModifierKeys MouseInputSource::getCurrentModifiers() const { return pimpl->getCurrentModifiers(); }
  443. Component* MouseInputSource::getComponentUnderMouse() const { return pimpl->getComponentUnderMouse(); }
  444. void MouseInputSource::triggerFakeMove() const { pimpl->triggerFakeMove(); }
  445. int MouseInputSource::getNumberOfMultipleClicks() const noexcept { return pimpl->getNumberOfMultipleClicks(); }
  446. Time MouseInputSource::getLastMouseDownTime() const noexcept { return pimpl->getLastMouseDownTime(); }
  447. Point<int> MouseInputSource::getLastMouseDownPosition() const noexcept { return pimpl->getLastMouseDownPosition(); }
  448. bool MouseInputSource::hasMouseMovedSignificantlySincePressed() const noexcept { return pimpl->hasMouseMovedSignificantlySincePressed(); }
  449. bool MouseInputSource::canDoUnboundedMovement() const noexcept { return isMouse(); }
  450. void MouseInputSource::enableUnboundedMouseMovement (bool isEnabled, bool keepCursorVisibleUntilOffscreen) const
  451. { pimpl->enableUnboundedMouseMovement (isEnabled, keepCursorVisibleUntilOffscreen); }
  452. bool MouseInputSource::isUnboundedMouseMovementEnabled() const { return pimpl->isUnboundedMouseModeOn; }
  453. bool MouseInputSource::hasMouseCursor() const noexcept { return isMouse(); }
  454. void MouseInputSource::showMouseCursor (const MouseCursor& cursor) { pimpl->showMouseCursor (cursor, false); }
  455. void MouseInputSource::hideCursor() { pimpl->hideCursor(); }
  456. void MouseInputSource::revealCursor() { pimpl->revealCursor (false); }
  457. void MouseInputSource::forceMouseCursorUpdate() { pimpl->revealCursor (true); }
  458. void MouseInputSource::setScreenPosition (Point<int> p) { pimpl->setScreenPosition (p); }
  459. void MouseInputSource::handleEvent (ComponentPeer& peer, Point<int> positionWithinPeer,
  460. const int64 time, const ModifierKeys mods)
  461. {
  462. pimpl->handleEvent (peer, positionWithinPeer, Time (time), mods.withOnlyMouseButtons());
  463. }
  464. void MouseInputSource::handleWheel (ComponentPeer& peer, Point<int> positionWithinPeer,
  465. const int64 time, const MouseWheelDetails& wheel)
  466. {
  467. pimpl->handleWheel (peer, positionWithinPeer, Time (time), wheel);
  468. }
  469. void MouseInputSource::handleMagnifyGesture (ComponentPeer& peer, Point<int> positionWithinPeer,
  470. const int64 time, const float scaleFactor)
  471. {
  472. pimpl->handleMagnifyGesture (peer, positionWithinPeer, Time (time), scaleFactor);
  473. }
  474. //==============================================================================
  475. struct MouseInputSource::SourceList : public Timer
  476. {
  477. SourceList()
  478. {
  479. addSource();
  480. }
  481. bool addSource();
  482. void addSource (int index, bool isMouse)
  483. {
  484. MouseInputSourceInternal* s = new MouseInputSourceInternal (index, isMouse);
  485. sources.add (s);
  486. sourceArray.add (MouseInputSource (s));
  487. }
  488. MouseInputSource* getMouseSource (int index) const noexcept
  489. {
  490. return isPositiveAndBelow (index, sourceArray.size()) ? &sourceArray.getReference (index)
  491. : nullptr;
  492. }
  493. MouseInputSource* getOrCreateMouseInputSource (int touchIndex)
  494. {
  495. jassert (touchIndex >= 0 && touchIndex < 100); // sanity-check on number of fingers
  496. for (;;)
  497. {
  498. if (MouseInputSource* mouse = getMouseSource (touchIndex))
  499. return mouse;
  500. if (! addSource())
  501. {
  502. jassertfalse; // not enough mouse sources!
  503. return nullptr;
  504. }
  505. }
  506. }
  507. int getNumDraggingMouseSources() const noexcept
  508. {
  509. int num = 0;
  510. for (int i = 0; i < sources.size(); ++i)
  511. if (sources.getUnchecked(i)->isDragging())
  512. ++num;
  513. return num;
  514. }
  515. MouseInputSource* getDraggingMouseSource (int index) const noexcept
  516. {
  517. int num = 0;
  518. for (int i = 0; i < sources.size(); ++i)
  519. {
  520. MouseInputSource* const mi = &(sourceArray.getReference(i));
  521. if (mi->isDragging())
  522. {
  523. if (index == num)
  524. return mi;
  525. ++num;
  526. }
  527. }
  528. return nullptr;
  529. }
  530. void beginDragAutoRepeat (const int interval)
  531. {
  532. if (interval > 0)
  533. {
  534. if (getTimerInterval() != interval)
  535. startTimer (interval);
  536. }
  537. else
  538. {
  539. stopTimer();
  540. }
  541. }
  542. void timerCallback() override
  543. {
  544. int numMiceDown = 0;
  545. for (int i = 0; i < sources.size(); ++i)
  546. {
  547. MouseInputSourceInternal* const mi = sources.getUnchecked(i);
  548. if (mi->isDragging())
  549. {
  550. mi->triggerFakeMove();
  551. ++numMiceDown;
  552. }
  553. }
  554. if (numMiceDown == 0)
  555. stopTimer();
  556. }
  557. OwnedArray<MouseInputSourceInternal> sources;
  558. Array<MouseInputSource> sourceArray;
  559. };