juce_MultiTouchMapper.h 3.3 KB

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