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.

148 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. /**
  24. Utility class to hold a list of TouchSurface::Touch objects with different
  25. indices and blockUIDs, where each touch has a mapping to some kind of
  26. user-supplied data value.
  27. The Type template is a user-defined type of object that will be stored for
  28. each touch element. The type must be default-constructable and copyable.
  29. */
  30. template <typename Type>
  31. class TouchList
  32. {
  33. public:
  34. /** Creates an empty touch list. */
  35. TouchList() {}
  36. /** Destructor. */
  37. ~TouchList() {}
  38. /** Returns the number of entries in the touch list. */
  39. int size() const noexcept { return touches.size(); }
  40. /** Returns the user data object that corresponds to the given touch.
  41. This will also update the stored state of the TouchEntry::touch value
  42. for this touch index.
  43. */
  44. Type& getValue (const TouchSurface::Touch& touch)
  45. {
  46. auto* t = find (touch);
  47. if (t == nullptr)
  48. {
  49. touches.add ({ touch, {} });
  50. return touches.getReference (touches.size() - 1).value;
  51. }
  52. else
  53. {
  54. t->touch = touch;
  55. return t->value;
  56. }
  57. }
  58. /** Returns true if a touch is already in the list. */
  59. bool contains (const TouchSurface::Touch& touch) const noexcept
  60. {
  61. return find (touch) != nullptr;
  62. }
  63. /** Updates the entry for the given touch, copying in the new state.
  64. If no entry with the same index and blockUID exists then a new entry is
  65. created. If given a touch which is a touch-end, this will *remove* any
  66. corresponding entries from the list.
  67. */
  68. void updateTouch (const TouchSurface::Touch& touch)
  69. {
  70. if (touch.isTouchEnd)
  71. {
  72. for (int i = touches.size(); --i >= 0;)
  73. if (matches (touches.getReference(i).touch, touch))
  74. touches.remove (i);
  75. jassert (! contains (touch));
  76. }
  77. else
  78. {
  79. auto t = find (touch);
  80. if (t == nullptr)
  81. touches.add ({ touch, {} });
  82. else
  83. t->touch = touch;
  84. }
  85. }
  86. /** Holds the current state of a touch, along with the user-data associated with it. */
  87. struct TouchEntry
  88. {
  89. TouchSurface::Touch touch;
  90. Type value;
  91. };
  92. /** If a touch is in the list, returns a pointer to the TouchEntry.
  93. Otherwise, returns nullptr.
  94. */
  95. TouchEntry* find (const TouchSurface::Touch& touch) const noexcept
  96. {
  97. for (auto& t : touches)
  98. if (matches (t.touch, touch))
  99. return &t;
  100. return nullptr;
  101. }
  102. /** Allows iterator access to the list of touch entries. */
  103. TouchEntry* begin() const noexcept { return touches.begin(); }
  104. /** Allows iterator access to the list of touch entries. */
  105. TouchEntry* end() const noexcept { return touches.end(); }
  106. /** Retrieve a reference to particular item in the list of touch entires. */
  107. TouchEntry& operator[] (const int index) { return touches.getReference (index); }
  108. /** Resets all contents, doest not generate any call-backs. */
  109. void clear() noexcept { touches.clear(); }
  110. private:
  111. //==========================================================================
  112. static bool matches (const TouchSurface::Touch& t1,
  113. const TouchSurface::Touch& t2) noexcept
  114. {
  115. return t1.index == t2.index && t1.blockUID == t2.blockUID;
  116. }
  117. juce::Array<TouchEntry> touches;
  118. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TouchList)
  119. };