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.

151 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. */
  26. template <typename Type>
  27. class TouchList
  28. {
  29. public:
  30. /** Creates an empty touch list. */
  31. TouchList() {}
  32. /** Destructor. */
  33. ~TouchList() {}
  34. /** Returns the number of entries in the touch list. */
  35. int size() const noexcept { return touches.size(); }
  36. /** Returns the user data object that corresponds to the given touch.
  37. This will also update the stored state of the TouchEntry::touch value
  38. for this touch index.
  39. */
  40. Type& getValue (const TouchSurface::Touch& touch)
  41. {
  42. auto* t = find (touch);
  43. if (t == nullptr)
  44. {
  45. touches.add ({ touch, {} });
  46. return touches.getReference (touches.size() - 1).value;
  47. }
  48. else
  49. {
  50. t->touch = touch;
  51. return t->value;
  52. }
  53. }
  54. /** Returns true if a touch is already in the list. */
  55. bool contains (const TouchSurface::Touch& touch) const noexcept
  56. {
  57. return find (touch) != nullptr;
  58. }
  59. /** Updates the entry for the given touch, copying in the new state.
  60. If no entry with the same index and blockUID exists then a new entry is
  61. created. If given a touch which is a touch-end, this will *remove* any
  62. corresponding entries from the list.
  63. */
  64. void updateTouch (const TouchSurface::Touch& touch)
  65. {
  66. if (touch.isTouchEnd)
  67. {
  68. for (int i = touches.size(); --i >= 0;)
  69. if (matches (touches.getReference(i).touch, touch))
  70. touches.remove (i);
  71. jassert (! contains (touch));
  72. }
  73. else
  74. {
  75. auto t = find (touch);
  76. if (t == nullptr)
  77. touches.add ({ touch, {} });
  78. else
  79. t->touch = touch;
  80. }
  81. }
  82. /** Holds the current state of a touch, along with the user-data associated with it. */
  83. struct TouchEntry
  84. {
  85. TouchSurface::Touch touch;
  86. Type value;
  87. };
  88. /** If a touch is in the list, returns a pointer to the TouchEntry.
  89. Otherwise, returns nullptr.
  90. */
  91. const TouchEntry* find (const TouchSurface::Touch& touch) const noexcept
  92. {
  93. for (auto& t : touches)
  94. if (matches (t.touch, touch))
  95. return &t;
  96. return nullptr;
  97. }
  98. TouchEntry* find (const TouchSurface::Touch& touch) noexcept
  99. {
  100. return const_cast<TouchEntry*> (static_cast<const TouchList&> (*this).find (touch));
  101. }
  102. /** Allows iterator access to the list of touch entries. */
  103. TouchEntry* begin() noexcept { return touches.begin(); }
  104. const TouchEntry* begin() const noexcept { return touches.begin(); }
  105. /** Allows iterator access to the list of touch entries. */
  106. TouchEntry* end() noexcept { return touches.end(); }
  107. const TouchEntry* end() const noexcept { return touches.end(); }
  108. /** Retrieve a reference to particular item in the list of touch entires. */
  109. TouchEntry& operator[] (const int index) { return touches.getReference (index); }
  110. /** Resets all contents, doest not generate any call-backs. */
  111. void clear() noexcept { touches.clear(); }
  112. private:
  113. //==========================================================================
  114. static bool matches (const TouchSurface::Touch& t1,
  115. const TouchSurface::Touch& t2) noexcept
  116. {
  117. return t1.index == t2.index && t1.blockUID == t2.blockUID;
  118. }
  119. juce::Array<TouchEntry> touches;
  120. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TouchList)
  121. };
  122. } // namespace juce