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.

140 lines
4.5KB

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