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.

153 lines
4.9KB

  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. /**
  20. Utility class to hold a list of TouchSurface::Touch objects with different
  21. indices and blockUIDs, where each touch has a mapping to some kind of
  22. user-supplied data value.
  23. The Type template is a user-defined type of object that will be stored for
  24. each touch element. The type must be default-constructable and copyable.
  25. @tags{Blocks}
  26. */
  27. template <typename Type>
  28. class TouchList
  29. {
  30. public:
  31. /** Creates an empty touch list. */
  32. TouchList() = default;
  33. /** Destructor. */
  34. ~TouchList() = default;
  35. /** Returns the number of entries in the touch list. */
  36. int size() const noexcept { return touches.size(); }
  37. /** Returns the user data object that corresponds to the given touch.
  38. This will also update the stored state of the TouchEntry::touch value
  39. for this touch index.
  40. */
  41. Type& getValue (const TouchSurface::Touch& touch)
  42. {
  43. auto* t = find (touch);
  44. if (t == nullptr)
  45. {
  46. touches.add ({ touch, {} });
  47. return touches.getReference (touches.size() - 1).value;
  48. }
  49. else
  50. {
  51. t->touch = touch;
  52. return t->value;
  53. }
  54. }
  55. /** Returns true if a touch is already in the list. */
  56. bool contains (const TouchSurface::Touch& touch) const noexcept
  57. {
  58. return find (touch) != nullptr;
  59. }
  60. /** Updates the entry for the given touch, copying in the new state.
  61. If no entry with the same index and blockUID exists then a new entry is
  62. created. If given a touch which is a touch-end, this will *remove* any
  63. corresponding entries from the list.
  64. */
  65. void updateTouch (const TouchSurface::Touch& touch)
  66. {
  67. if (touch.isTouchEnd)
  68. {
  69. for (int i = touches.size(); --i >= 0;)
  70. if (matches (touches.getReference(i).touch, touch))
  71. touches.remove (i);
  72. jassert (! contains (touch));
  73. }
  74. else
  75. {
  76. auto t = find (touch);
  77. if (t == nullptr)
  78. touches.add ({ touch, {} });
  79. else
  80. t->touch = touch;
  81. }
  82. }
  83. /** Holds the current state of a touch, along with the user-data associated with it. */
  84. struct TouchEntry
  85. {
  86. TouchSurface::Touch touch;
  87. Type value;
  88. };
  89. /** If a touch is in the list, returns a pointer to the TouchEntry.
  90. Otherwise, returns nullptr.
  91. */
  92. const TouchEntry* find (const TouchSurface::Touch& touch) const noexcept
  93. {
  94. for (auto& t : touches)
  95. if (matches (t.touch, touch))
  96. return &t;
  97. return nullptr;
  98. }
  99. TouchEntry* find (const TouchSurface::Touch& touch) noexcept
  100. {
  101. return const_cast<TouchEntry*> (static_cast<const TouchList&> (*this).find (touch));
  102. }
  103. /** Allows iterator access to the list of touch entries. */
  104. TouchEntry* begin() noexcept { return touches.begin(); }
  105. const TouchEntry* begin() const noexcept { return touches.begin(); }
  106. /** Allows iterator access to the list of touch entries. */
  107. TouchEntry* end() noexcept { return touches.end(); }
  108. const TouchEntry* end() const noexcept { return touches.end(); }
  109. /** Retrieve a reference to particular item in the list of touch entries. */
  110. TouchEntry& operator[] (const int index) { return touches.getReference (index); }
  111. /** Resets all contents, doest not generate any call-backs. */
  112. void clear() noexcept { touches.clear(); }
  113. private:
  114. //==============================================================================
  115. static bool matches (const TouchSurface::Touch& t1,
  116. const TouchSurface::Touch& t2) noexcept
  117. {
  118. return t1.index == t2.index && t1.blockUID == t2.blockUID;
  119. }
  120. Array<TouchEntry> touches;
  121. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TouchList)
  122. };
  123. } // namespace juce