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.

162 lines
5.0KB

  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, e.pressure);
  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> newPoint, ModifierKeys newMods, float pressure)
  59. {
  60. currentPosition = newPoint;
  61. modifierKeys = newMods;
  62. if (lastPoint.getDistanceFrom (newPoint) > 5.0f)
  63. {
  64. if (lastPoint != Point<float>())
  65. {
  66. Path newSegment;
  67. newSegment.startNewSubPath (lastPoint);
  68. newSegment.lineTo (newPoint);
  69. float diameter = 20.0f * (pressure > 0 && pressure < 1.0f ? pressure : 1.0f);
  70. PathStrokeType (diameter, PathStrokeType::curved, PathStrokeType::rounded).createStrokedPath (newSegment, newSegment);
  71. path.addPath (newSegment);
  72. }
  73. lastPoint = newPoint;
  74. }
  75. }
  76. MouseInputSource source;
  77. Path path;
  78. Colour colour;
  79. Point<float> lastPoint, currentPosition;
  80. ModifierKeys modifierKeys;
  81. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Trail)
  82. };
  83. OwnedArray<Trail> trails;
  84. void drawTrail (Trail& trail, Graphics& g)
  85. {
  86. g.setColour (trail.colour);
  87. g.fillPath (trail.path);
  88. const float radius = 40.0f;
  89. g.setColour (Colours::black);
  90. g.drawEllipse (trail.currentPosition.x - radius,
  91. trail.currentPosition.y - radius,
  92. radius * 2.0f, radius * 2.0f, 2.0f);
  93. g.setFont (14.0f);
  94. String desc ("Mouse #");
  95. desc << trail.source.getIndex();
  96. float pressure = trail.source.getCurrentPressure();
  97. if (pressure > 0.0f && pressure < 1.0f)
  98. desc << " (pressure: " << (int) (pressure * 100.0f) << "%)";
  99. if (trail.modifierKeys.isCommandDown()) desc << " (CMD)";
  100. if (trail.modifierKeys.isShiftDown()) desc << " (SHIFT)";
  101. if (trail.modifierKeys.isCtrlDown()) desc << " (CTRL)";
  102. if (trail.modifierKeys.isAltDown()) desc << " (ALT)";
  103. g.drawText (desc,
  104. Rectangle<int> ((int) trail.currentPosition.x - 200,
  105. (int) trail.currentPosition.y - 60,
  106. 400, 20),
  107. Justification::centredTop, false);
  108. }
  109. Trail* getTrail (const MouseInputSource& source)
  110. {
  111. for (int i = 0; i < trails.size(); ++i)
  112. {
  113. Trail* t = trails.getUnchecked(i);
  114. if (t->source == source)
  115. return t;
  116. }
  117. return nullptr;
  118. }
  119. };
  120. // This static object will register this demo type in a global list of demos..
  121. static JuceDemoType<MultiTouchDemo> demo ("10 Components: Multi-touch");