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.

155 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::detail
  19. {
  20. class MouseInputSourceList : public Timer
  21. {
  22. public:
  23. MouseInputSourceList()
  24. {
  25. #if JUCE_ANDROID || JUCE_IOS
  26. auto mainMouseInputType = MouseInputSource::InputSourceType::touch;
  27. #else
  28. auto mainMouseInputType = MouseInputSource::InputSourceType::mouse;
  29. #endif
  30. addSource (0, mainMouseInputType);
  31. }
  32. MouseInputSource* addSource (int index, MouseInputSource::InputSourceType type)
  33. {
  34. auto* s = new MouseInputSourceImpl (index, type);
  35. sources.add (s);
  36. sourceArray.add (MouseInputSource (s));
  37. return &sourceArray.getReference (sourceArray.size() - 1);
  38. }
  39. MouseInputSource* getMouseSource (int index) noexcept
  40. {
  41. return isPositiveAndBelow (index, sourceArray.size()) ? &sourceArray.getReference (index)
  42. : nullptr;
  43. }
  44. MouseInputSource* getOrCreateMouseInputSource (MouseInputSource::InputSourceType type, int touchIndex = 0)
  45. {
  46. if (type == MouseInputSource::InputSourceType::mouse
  47. || type == MouseInputSource::InputSourceType::pen)
  48. {
  49. for (auto& m : sourceArray)
  50. if (type == m.getType())
  51. return &m;
  52. addSource (0, type);
  53. }
  54. else if (type == MouseInputSource::InputSourceType::touch)
  55. {
  56. jassert (0 <= touchIndex && touchIndex < 100); // sanity-check on number of fingers
  57. for (auto& m : sourceArray)
  58. if (type == m.getType() && touchIndex == m.getIndex())
  59. return &m;
  60. if (canUseTouch())
  61. return addSource (touchIndex, type);
  62. }
  63. return nullptr;
  64. }
  65. int getNumDraggingMouseSources() const noexcept
  66. {
  67. int num = 0;
  68. for (auto* s : sources)
  69. if (s->isDragging())
  70. ++num;
  71. return num;
  72. }
  73. MouseInputSource* getDraggingMouseSource (int index) noexcept
  74. {
  75. int num = 0;
  76. for (auto& s : sourceArray)
  77. {
  78. if (s.isDragging())
  79. {
  80. if (index == num)
  81. return &s;
  82. ++num;
  83. }
  84. }
  85. return nullptr;
  86. }
  87. void beginDragAutoRepeat (int interval)
  88. {
  89. if (interval > 0)
  90. {
  91. if (getTimerInterval() != interval)
  92. startTimer (interval);
  93. }
  94. else
  95. {
  96. stopTimer();
  97. }
  98. }
  99. void timerCallback() override
  100. {
  101. bool anyDragging = false;
  102. for (auto* s : sources)
  103. {
  104. // NB: when doing auto-repeat, we need to force an update of the current position and button state,
  105. // because on some OSes the queue can get overloaded with messages so that mouse-events don't get through..
  106. if (s->isDragging() && ComponentPeer::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  107. {
  108. s->lastPointerState.position = s->getRawScreenPosition();
  109. s->triggerFakeMove();
  110. anyDragging = true;
  111. }
  112. }
  113. if (! anyDragging)
  114. stopTimer();
  115. }
  116. OwnedArray<MouseInputSourceImpl> sources;
  117. Array<MouseInputSource> sourceArray;
  118. private:
  119. bool addSource();
  120. bool canUseTouch() const;
  121. };
  122. } // namespace juce::detail