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

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. //==============================================================================
  20. class MultiTouchDemo : public Component
  21. {
  22. public:
  23. MultiTouchDemo()
  24. {
  25. setOpaque (true);
  26. }
  27. void paint (Graphics& g) override
  28. {
  29. g.fillAll (Colour::greyLevel (0.4f));
  30. g.setColour (Colours::lightgrey);
  31. g.setFont (14.0f);
  32. g.drawFittedText ("Drag here with as many fingers as you have!",
  33. getLocalBounds().reduced (30), Justification::centred, 4);
  34. for (int i = 0; i < trails.size(); ++i)
  35. drawTrail (*trails.getUnchecked(i), g);
  36. }
  37. void mouseDrag (const MouseEvent& e) override
  38. {
  39. Trail* t = getTrail (e.source);
  40. if (t == nullptr)
  41. {
  42. t = new Trail (e.source);
  43. t->path.startNewSubPath (e.position);
  44. trails.add (t);
  45. }
  46. t->pushPoint (e.position, e.mods);
  47. repaint();
  48. }
  49. void mouseUp (const MouseEvent& e) override
  50. {
  51. trails.removeObject (getTrail (e.source));
  52. repaint();
  53. }
  54. struct Trail
  55. {
  56. Trail (const MouseInputSource& ms)
  57. : source (ms), colour (getRandomBrightColour().withAlpha (0.6f))
  58. {}
  59. void pushPoint (Point<float> p, ModifierKeys newMods)
  60. {
  61. currentPosition = p;
  62. modifierKeys = newMods;
  63. if (lastPoint.getDistanceFrom(p) > 5.0f)
  64. {
  65. if (lastPoint != Point<float>())
  66. {
  67. path.quadraticTo (lastPoint, p);
  68. lastPoint = Point<float>();
  69. }
  70. else
  71. {
  72. lastPoint = p;
  73. }
  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.strokePath (trail.path, PathStrokeType (20.0f, PathStrokeType::curved, PathStrokeType::rounded));
  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. if (trail.modifierKeys.isCommandDown()) desc << " (CMD)";
  97. if (trail.modifierKeys.isShiftDown()) desc << " (SHIFT)";
  98. if (trail.modifierKeys.isCtrlDown()) desc << " (CTRL)";
  99. if (trail.modifierKeys.isAltDown()) desc << " (ALT)";
  100. g.drawText (desc,
  101. Rectangle<int> ((int) trail.currentPosition.x - 200,
  102. (int) trail.currentPosition.y - 60,
  103. 400, 20),
  104. Justification::centredTop, false);
  105. }
  106. Trail* getTrail (const MouseInputSource& source)
  107. {
  108. for (int i = 0; i < trails.size(); ++i)
  109. {
  110. Trail* t = trails.getUnchecked(i);
  111. if (t->source == source)
  112. return t;
  113. }
  114. return nullptr;
  115. }
  116. };
  117. // This static object will register this demo type in a global list of demos..
  118. static JuceDemoType<MultiTouchDemo> demo ("10 Components: Multi-touch");