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.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. class MultiTouchDemo : public Component
  20. {
  21. public:
  22. MultiTouchDemo()
  23. {
  24. setOpaque (true);
  25. }
  26. void paint (Graphics& g) override
  27. {
  28. g.fillAll (Colour::greyLevel (0.4f));
  29. g.setColour (Colours::lightgrey);
  30. g.setFont (14.0f);
  31. g.drawFittedText ("Drag here with as many fingers as you have!",
  32. getLocalBounds().reduced (30), Justification::centred, 4);
  33. for (int i = 0; i < trails.size(); ++i)
  34. drawTrail (*trails.getUnchecked(i), g);
  35. }
  36. void mouseDrag (const MouseEvent& e) override
  37. {
  38. Trail* t = getTrail (e.source);
  39. if (t == nullptr)
  40. {
  41. t = new Trail (e.source);
  42. t->path.startNewSubPath (e.position);
  43. trails.add (t);
  44. }
  45. t->pushPoint (e.position, e.mods);
  46. repaint();
  47. }
  48. void mouseUp (const MouseEvent& e) override
  49. {
  50. trails.removeObject (getTrail (e.source));
  51. repaint();
  52. }
  53. struct Trail
  54. {
  55. Trail (const MouseInputSource& ms)
  56. : source (ms), colour (getRandomBrightColour().withAlpha (0.6f))
  57. {}
  58. void pushPoint (Point<float> p, ModifierKeys newMods)
  59. {
  60. currentPosition = p;
  61. modifierKeys = newMods;
  62. if (lastPoint.getDistanceFrom(p) > 5.0f)
  63. {
  64. if (lastPoint != Point<float>())
  65. {
  66. path.quadraticTo (lastPoint, p);
  67. lastPoint = Point<float>();
  68. }
  69. else
  70. {
  71. lastPoint = p;
  72. }
  73. }
  74. }
  75. MouseInputSource source;
  76. Path path;
  77. Colour colour;
  78. Point<float> lastPoint, currentPosition;
  79. ModifierKeys modifierKeys;
  80. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Trail)
  81. };
  82. OwnedArray<Trail> trails;
  83. void drawTrail (Trail& trail, Graphics& g)
  84. {
  85. g.setColour (trail.colour);
  86. g.strokePath (trail.path, PathStrokeType (20.0f, PathStrokeType::curved, PathStrokeType::rounded));
  87. const float radius = 40.0f;
  88. g.setColour (Colours::black);
  89. g.drawEllipse (trail.currentPosition.x - radius,
  90. trail.currentPosition.y - radius,
  91. radius * 2.0f, radius * 2.0f, 2.0f);
  92. g.setFont (14.0f);
  93. String desc ("Mouse #");
  94. desc << trail.source.getIndex();
  95. if (trail.modifierKeys.isCommandDown()) desc << " (CMD)";
  96. if (trail.modifierKeys.isShiftDown()) desc << " (SHIFT)";
  97. if (trail.modifierKeys.isCtrlDown()) desc << " (CTRL)";
  98. if (trail.modifierKeys.isAltDown()) desc << " (ALT)";
  99. g.drawText (desc,
  100. Rectangle<int> ((int) trail.currentPosition.x - 200,
  101. (int) trail.currentPosition.y - 60,
  102. 400, 20),
  103. Justification::centredTop, false);
  104. }
  105. Trail* getTrail (const MouseInputSource& source)
  106. {
  107. for (int i = 0; i < trails.size(); ++i)
  108. {
  109. Trail* t = trails.getUnchecked(i);
  110. if (t->source == source)
  111. return t;
  112. }
  113. return nullptr;
  114. }
  115. };
  116. // This static object will register this demo type in a global list of demos..
  117. static JuceDemoType<MultiTouchDemo> demo ("10 Components: Multi-touch");