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.

166 lines
5.2KB

  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. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceDemoHeader.h"
  20. //==============================================================================
  21. class MultiTouchDemo : public Component
  22. {
  23. public:
  24. MultiTouchDemo()
  25. {
  26. setOpaque (true);
  27. }
  28. void paint (Graphics& g) override
  29. {
  30. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  31. Colour::greyLevel (0.4f)));
  32. g.setColour (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  33. Colours::lightgrey));
  34. g.setFont (14.0f);
  35. g.drawFittedText ("Drag here with as many fingers as you have!",
  36. getLocalBounds().reduced (30), Justification::centred, 4);
  37. for (int i = 0; i < trails.size(); ++i)
  38. drawTrail (*trails.getUnchecked(i), g);
  39. }
  40. void mouseDrag (const MouseEvent& e) override
  41. {
  42. Trail* t = getTrail (e.source);
  43. if (t == nullptr)
  44. {
  45. t = new Trail (e.source);
  46. t->path.startNewSubPath (e.position);
  47. trails.add (t);
  48. }
  49. t->pushPoint (e.position, e.mods, e.pressure);
  50. repaint();
  51. }
  52. void mouseUp (const MouseEvent& e) override
  53. {
  54. trails.removeObject (getTrail (e.source));
  55. repaint();
  56. }
  57. struct Trail
  58. {
  59. Trail (const MouseInputSource& ms)
  60. : source (ms), colour (getRandomBrightColour().withAlpha (0.6f))
  61. {}
  62. void pushPoint (Point<float> newPoint, ModifierKeys newMods, float pressure)
  63. {
  64. currentPosition = newPoint;
  65. modifierKeys = newMods;
  66. if (lastPoint.getDistanceFrom (newPoint) > 5.0f)
  67. {
  68. if (lastPoint != Point<float>())
  69. {
  70. Path newSegment;
  71. newSegment.startNewSubPath (lastPoint);
  72. newSegment.lineTo (newPoint);
  73. float diameter = 20.0f * (pressure > 0 && pressure < 1.0f ? pressure : 1.0f);
  74. PathStrokeType (diameter, PathStrokeType::curved, PathStrokeType::rounded).createStrokedPath (newSegment, newSegment);
  75. path.addPath (newSegment);
  76. }
  77. lastPoint = newPoint;
  78. }
  79. }
  80. MouseInputSource source;
  81. Path path;
  82. Colour colour;
  83. Point<float> lastPoint, currentPosition;
  84. ModifierKeys modifierKeys;
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Trail)
  86. };
  87. OwnedArray<Trail> trails;
  88. void drawTrail (Trail& trail, Graphics& g)
  89. {
  90. g.setColour (trail.colour);
  91. g.fillPath (trail.path);
  92. const float radius = 40.0f;
  93. g.setColour (Colours::black);
  94. g.drawEllipse (trail.currentPosition.x - radius,
  95. trail.currentPosition.y - radius,
  96. radius * 2.0f, radius * 2.0f, 2.0f);
  97. g.setFont (14.0f);
  98. String desc ("Mouse #");
  99. desc << trail.source.getIndex();
  100. float pressure = trail.source.getCurrentPressure();
  101. if (pressure > 0.0f && pressure < 1.0f)
  102. desc << " (pressure: " << (int) (pressure * 100.0f) << "%)";
  103. if (trail.modifierKeys.isCommandDown()) desc << " (CMD)";
  104. if (trail.modifierKeys.isShiftDown()) desc << " (SHIFT)";
  105. if (trail.modifierKeys.isCtrlDown()) desc << " (CTRL)";
  106. if (trail.modifierKeys.isAltDown()) desc << " (ALT)";
  107. g.drawText (desc,
  108. Rectangle<int> ((int) trail.currentPosition.x - 200,
  109. (int) trail.currentPosition.y - 60,
  110. 400, 20),
  111. Justification::centredTop, false);
  112. }
  113. Trail* getTrail (const MouseInputSource& source)
  114. {
  115. for (int i = 0; i < trails.size(); ++i)
  116. {
  117. Trail* t = trails.getUnchecked(i);
  118. if (t->source == source)
  119. return t;
  120. }
  121. return nullptr;
  122. }
  123. };
  124. // This static object will register this demo type in a global list of demos..
  125. static JuceDemoType<MultiTouchDemo> demo ("10 Components: Multi-touch");