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.

164 lines
5.2KB

  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 (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  29. Colour::greyLevel (0.4f)));
  30. g.setColour (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  31. Colours::lightgrey));
  32. g.setFont (14.0f);
  33. g.drawFittedText ("Drag here with as many fingers as you have!",
  34. getLocalBounds().reduced (30), Justification::centred, 4);
  35. for (int i = 0; i < trails.size(); ++i)
  36. drawTrail (*trails.getUnchecked(i), g);
  37. }
  38. void mouseDrag (const MouseEvent& e) override
  39. {
  40. Trail* t = getTrail (e.source);
  41. if (t == nullptr)
  42. {
  43. t = new Trail (e.source);
  44. t->path.startNewSubPath (e.position);
  45. trails.add (t);
  46. }
  47. t->pushPoint (e.position, e.mods, e.pressure);
  48. repaint();
  49. }
  50. void mouseUp (const MouseEvent& e) override
  51. {
  52. trails.removeObject (getTrail (e.source));
  53. repaint();
  54. }
  55. struct Trail
  56. {
  57. Trail (const MouseInputSource& ms)
  58. : source (ms), colour (getRandomBrightColour().withAlpha (0.6f))
  59. {}
  60. void pushPoint (Point<float> newPoint, ModifierKeys newMods, float pressure)
  61. {
  62. currentPosition = newPoint;
  63. modifierKeys = newMods;
  64. if (lastPoint.getDistanceFrom (newPoint) > 5.0f)
  65. {
  66. if (lastPoint != Point<float>())
  67. {
  68. Path newSegment;
  69. newSegment.startNewSubPath (lastPoint);
  70. newSegment.lineTo (newPoint);
  71. float diameter = 20.0f * (pressure > 0 && pressure < 1.0f ? pressure : 1.0f);
  72. PathStrokeType (diameter, PathStrokeType::curved, PathStrokeType::rounded).createStrokedPath (newSegment, newSegment);
  73. path.addPath (newSegment);
  74. }
  75. lastPoint = newPoint;
  76. }
  77. }
  78. MouseInputSource source;
  79. Path path;
  80. Colour colour;
  81. Point<float> lastPoint, currentPosition;
  82. ModifierKeys modifierKeys;
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Trail)
  84. };
  85. OwnedArray<Trail> trails;
  86. void drawTrail (Trail& trail, Graphics& g)
  87. {
  88. g.setColour (trail.colour);
  89. g.fillPath (trail.path);
  90. const float radius = 40.0f;
  91. g.setColour (Colours::black);
  92. g.drawEllipse (trail.currentPosition.x - radius,
  93. trail.currentPosition.y - radius,
  94. radius * 2.0f, radius * 2.0f, 2.0f);
  95. g.setFont (14.0f);
  96. String desc ("Mouse #");
  97. desc << trail.source.getIndex();
  98. float pressure = trail.source.getCurrentPressure();
  99. if (pressure > 0.0f && pressure < 1.0f)
  100. desc << " (pressure: " << (int) (pressure * 100.0f) << "%)";
  101. if (trail.modifierKeys.isCommandDown()) desc << " (CMD)";
  102. if (trail.modifierKeys.isShiftDown()) desc << " (SHIFT)";
  103. if (trail.modifierKeys.isCtrlDown()) desc << " (CTRL)";
  104. if (trail.modifierKeys.isAltDown()) desc << " (ALT)";
  105. g.drawText (desc,
  106. Rectangle<int> ((int) trail.currentPosition.x - 200,
  107. (int) trail.currentPosition.y - 60,
  108. 400, 20),
  109. Justification::centredTop, false);
  110. }
  111. Trail* getTrail (const MouseInputSource& source)
  112. {
  113. for (int i = 0; i < trails.size(); ++i)
  114. {
  115. Trail* t = trails.getUnchecked(i);
  116. if (t->source == source)
  117. return t;
  118. }
  119. return nullptr;
  120. }
  121. };
  122. // This static object will register this demo type in a global list of demos..
  123. static JuceDemoType<MultiTouchDemo> demo ("10 Components: Multi-touch");