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.

115 lines
3.2KB

  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. namespace juce
  20. {
  21. template <typename IDType>
  22. class MultiTouchMapper
  23. {
  24. public:
  25. MultiTouchMapper() {}
  26. int getIndexOfTouch (ComponentPeer* peer, IDType touchID)
  27. {
  28. jassert (touchID != 0); // need to rethink this if IDs can be 0!
  29. TouchInfo info {touchID, peer};
  30. int touchIndex = currentTouches.indexOf (info);
  31. if (touchIndex < 0)
  32. {
  33. auto emptyTouchIndex = currentTouches.indexOf ({});
  34. touchIndex = (emptyTouchIndex >= 0 ? emptyTouchIndex : currentTouches.size());
  35. currentTouches.set (touchIndex, info);
  36. }
  37. return touchIndex;
  38. }
  39. void clear()
  40. {
  41. currentTouches.clear();
  42. }
  43. void clearTouch (int index)
  44. {
  45. currentTouches.set (index, {});
  46. }
  47. bool areAnyTouchesActive() const noexcept
  48. {
  49. for (auto& t : currentTouches)
  50. if (t.touchId != 0)
  51. return true;
  52. return false;
  53. }
  54. void deleteAllTouchesForPeer (ComponentPeer* peer)
  55. {
  56. for (auto& t : currentTouches)
  57. if (t.owner == peer)
  58. t.touchId = 0;
  59. }
  60. private:
  61. //==============================================================================
  62. struct TouchInfo
  63. {
  64. TouchInfo() noexcept : touchId (0), owner (nullptr) {}
  65. TouchInfo (IDType idToUse, ComponentPeer* peer) noexcept : touchId (idToUse), owner (peer) {}
  66. TouchInfo (const TouchInfo&) = default;
  67. TouchInfo& operator= (const TouchInfo&) = default;
  68. // VS2013 can't default move constructors
  69. TouchInfo (TouchInfo&& other) noexcept : touchId (other.touchId), owner (other.owner) {}
  70. // VS2013 can't default move assignments
  71. TouchInfo& operator= (TouchInfo&& other) noexcept
  72. {
  73. touchId = other.touchId;
  74. owner = other.owner;
  75. return *this;
  76. }
  77. IDType touchId;
  78. ComponentPeer* owner;
  79. bool operator== (const TouchInfo& o) const noexcept { return (touchId == o.touchId); }
  80. };
  81. //==============================================================================
  82. Array<TouchInfo> currentTouches;
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MultiTouchMapper)
  84. };
  85. } // namespace juce