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.

183 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: MultiTouchDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. class MultiTouchDemo final : public Component
  37. {
  38. public:
  39. MultiTouchDemo()
  40. {
  41. setOpaque (true);
  42. setSize (500, 500);
  43. }
  44. void paint (Graphics& g) override
  45. {
  46. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  47. Colour::greyLevel (0.4f)));
  48. g.setColour (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::defaultText,
  49. Colours::lightgrey));
  50. g.setFont (14.0f);
  51. g.drawFittedText ("Drag here with as many fingers as you have!",
  52. getLocalBounds().reduced (30), Justification::centred, 4);
  53. for (auto* trail : trails)
  54. drawTrail (*trail, g);
  55. }
  56. void mouseDrag (const MouseEvent& e) override
  57. {
  58. auto* t = getTrail (e.source);
  59. if (t == nullptr)
  60. {
  61. t = new Trail (e.source);
  62. t->path.startNewSubPath (e.position);
  63. trails.add (t);
  64. }
  65. t->pushPoint (e.position, e.mods, e.pressure);
  66. repaint();
  67. }
  68. void mouseUp (const MouseEvent& e) override
  69. {
  70. trails.removeObject (getTrail (e.source));
  71. repaint();
  72. }
  73. struct Trail
  74. {
  75. Trail (const MouseInputSource& ms)
  76. : source (ms)
  77. {}
  78. void pushPoint (Point<float> newPoint, ModifierKeys newMods, float pressure)
  79. {
  80. currentPosition = newPoint;
  81. modifierKeys = newMods;
  82. if (lastPoint.getDistanceFrom (newPoint) > 5.0f)
  83. {
  84. if (lastPoint != Point<float>())
  85. {
  86. Path newSegment;
  87. newSegment.startNewSubPath (lastPoint);
  88. newSegment.lineTo (newPoint);
  89. auto diameter = 20.0f * (pressure > 0 && pressure < 1.0f ? pressure : 1.0f);
  90. PathStrokeType (diameter, PathStrokeType::curved, PathStrokeType::rounded).createStrokedPath (newSegment, newSegment);
  91. path.addPath (newSegment);
  92. }
  93. lastPoint = newPoint;
  94. }
  95. }
  96. MouseInputSource source;
  97. Path path;
  98. Colour colour { getRandomBrightColour().withAlpha (0.6f) };
  99. Point<float> lastPoint, currentPosition;
  100. ModifierKeys modifierKeys;
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Trail)
  102. };
  103. OwnedArray<Trail> trails;
  104. void drawTrail (Trail& trail, Graphics& g)
  105. {
  106. g.setColour (trail.colour);
  107. g.fillPath (trail.path);
  108. auto radius = 40.0f;
  109. g.setColour (Colours::black);
  110. g.drawEllipse (trail.currentPosition.x - radius,
  111. trail.currentPosition.y - radius,
  112. radius * 2.0f, radius * 2.0f, 2.0f);
  113. g.setFont (14.0f);
  114. String desc ("Mouse #");
  115. desc << trail.source.getIndex();
  116. auto pressure = trail.source.getCurrentPressure();
  117. if (pressure > 0.0f && pressure < 1.0f)
  118. desc << " (pressure: " << (int) (pressure * 100.0f) << "%)";
  119. if (trail.modifierKeys.isCommandDown()) desc << " (CMD)";
  120. if (trail.modifierKeys.isShiftDown()) desc << " (SHIFT)";
  121. if (trail.modifierKeys.isCtrlDown()) desc << " (CTRL)";
  122. if (trail.modifierKeys.isAltDown()) desc << " (ALT)";
  123. g.drawText (desc,
  124. Rectangle<int> ((int) trail.currentPosition.x - 200,
  125. (int) trail.currentPosition.y - 60,
  126. 400, 20),
  127. Justification::centredTop, false);
  128. }
  129. Trail* getTrail (const MouseInputSource& source)
  130. {
  131. for (auto* trail : trails)
  132. {
  133. if (trail->source == source)
  134. return trail;
  135. }
  136. return nullptr;
  137. }
  138. };