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.

181 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: MultiTouchDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Showcases multi-touch features.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: MultiTouchDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. class MultiTouchDemo : public Component
  36. {
  37. public:
  38. MultiTouchDemo()
  39. {
  40. setOpaque (true);
  41. setSize (500, 500);
  42. }
  43. void paint (Graphics& g) override
  44. {
  45. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  46. Colour::greyLevel (0.4f)));
  47. g.setColour (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  48. Colours::lightgrey));
  49. g.setFont (14.0f);
  50. g.drawFittedText ("Drag here with as many fingers as you have!",
  51. getLocalBounds().reduced (30), Justification::centred, 4);
  52. for (auto* trail : trails)
  53. drawTrail (*trail, g);
  54. }
  55. void mouseDrag (const MouseEvent& e) override
  56. {
  57. auto* t = getTrail (e.source);
  58. if (t == nullptr)
  59. {
  60. t = new Trail (e.source);
  61. t->path.startNewSubPath (e.position);
  62. trails.add (t);
  63. }
  64. t->pushPoint (e.position, e.mods, e.pressure);
  65. repaint();
  66. }
  67. void mouseUp (const MouseEvent& e) override
  68. {
  69. trails.removeObject (getTrail (e.source));
  70. repaint();
  71. }
  72. struct Trail
  73. {
  74. Trail (const MouseInputSource& ms)
  75. : source (ms)
  76. {}
  77. void pushPoint (Point<float> newPoint, ModifierKeys newMods, float pressure)
  78. {
  79. currentPosition = newPoint;
  80. modifierKeys = newMods;
  81. if (lastPoint.getDistanceFrom (newPoint) > 5.0f)
  82. {
  83. if (lastPoint != Point<float>())
  84. {
  85. Path newSegment;
  86. newSegment.startNewSubPath (lastPoint);
  87. newSegment.lineTo (newPoint);
  88. auto diameter = 20.0f * (pressure > 0 && pressure < 1.0f ? pressure : 1.0f);
  89. PathStrokeType (diameter, PathStrokeType::curved, PathStrokeType::rounded).createStrokedPath (newSegment, newSegment);
  90. path.addPath (newSegment);
  91. }
  92. lastPoint = newPoint;
  93. }
  94. }
  95. MouseInputSource source;
  96. Path path;
  97. Colour colour { getRandomBrightColour().withAlpha (0.6f) };
  98. Point<float> lastPoint, currentPosition;
  99. ModifierKeys modifierKeys;
  100. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Trail)
  101. };
  102. OwnedArray<Trail> trails;
  103. void drawTrail (Trail& trail, Graphics& g)
  104. {
  105. g.setColour (trail.colour);
  106. g.fillPath (trail.path);
  107. auto radius = 40.0f;
  108. g.setColour (Colours::black);
  109. g.drawEllipse (trail.currentPosition.x - radius,
  110. trail.currentPosition.y - radius,
  111. radius * 2.0f, radius * 2.0f, 2.0f);
  112. g.setFont (14.0f);
  113. String desc ("Mouse #");
  114. desc << trail.source.getIndex();
  115. auto pressure = trail.source.getCurrentPressure();
  116. if (pressure > 0.0f && pressure < 1.0f)
  117. desc << " (pressure: " << (int) (pressure * 100.0f) << "%)";
  118. if (trail.modifierKeys.isCommandDown()) desc << " (CMD)";
  119. if (trail.modifierKeys.isShiftDown()) desc << " (SHIFT)";
  120. if (trail.modifierKeys.isCtrlDown()) desc << " (CTRL)";
  121. if (trail.modifierKeys.isAltDown()) desc << " (ALT)";
  122. g.drawText (desc,
  123. Rectangle<int> ((int) trail.currentPosition.x - 200,
  124. (int) trail.currentPosition.y - 60,
  125. 400, 20),
  126. Justification::centredTop, false);
  127. }
  128. Trail* getTrail (const MouseInputSource& source)
  129. {
  130. for (auto* trail : trails)
  131. {
  132. if (trail->source == source)
  133. return trail;
  134. }
  135. return nullptr;
  136. }
  137. };