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.

505 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. struct BlankCanvas : public AnimatedContent
  19. {
  20. String getName() const override { return "Blank Canvas"; }
  21. void reset() override {}
  22. void handleTouch (Point<float>) override {}
  23. void generateCanvas (Graphics&, SharedCanvasDescription&, Rectangle<float>) override {}
  24. };
  25. //==============================================================================
  26. struct GridLines : public AnimatedContent
  27. {
  28. String getName() const override { return "Grid Lines"; }
  29. void reset() override {}
  30. void handleTouch (Point<float>) override {}
  31. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float>) override
  32. {
  33. auto limits = canvas.getLimits();
  34. float lineThickness = 0.1f;
  35. g.setColour (Colours::blue);
  36. g.drawRect (canvas.getLimits(), lineThickness);
  37. for (float y = limits.getY(); y < limits.getBottom(); y += 2.0f)
  38. g.drawLine (limits.getX(), y, limits.getRight(), y, lineThickness);
  39. for (float x = limits.getX(); x < limits.getRight(); x += 2.0f)
  40. g.drawLine (x, limits.getY(), x, limits.getBottom(), lineThickness);
  41. g.setColour (Colours::darkred);
  42. g.drawLine (limits.getX(), limits.getCentreY(), limits.getRight(), limits.getCentreY(), lineThickness);
  43. g.drawLine (limits.getCentreX(), limits.getY(), limits.getCentreX(), limits.getBottom(), lineThickness);
  44. g.setColour (Colours::lightgrey);
  45. g.drawLine (limits.getX(), limits.getY(), limits.getRight(), limits.getBottom(), lineThickness);
  46. g.drawLine (limits.getX(), limits.getBottom(), limits.getRight(), limits.getY(), lineThickness);
  47. }
  48. };
  49. //==============================================================================
  50. struct BackgroundLogo : public AnimatedContent
  51. {
  52. BackgroundLogo()
  53. {
  54. static const char logoData[] = R"blahblah(
  55. <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
  56. viewBox="0 0 239.2 239.2" enable-background="new 0 0 239.2 239.2" xml:space="preserve">
  57. <path fill="#6CC04A" d="M118.8,201.3c-44.6,0-81-36.3-81-81s36.3-81,81-81s81,36.3,81,81S163.4,201.3,118.8,201.3z M118.8,44.8c-41.7,0-75.6,33.9-75.6,75.6s33.9,75.6,75.6,75.6s75.6-33.9,75.6-75.6S160.4,44.8,118.8,44.8z"/>
  58. <path fill="#3B5CAD" d="M182.6,117.6c1.4,0,2.7-0.5,3.7-1.5c1.1-1.1,1.6-2.5,1.4-4c-1.5-12.7-6.5-24.7-14.4-34.8c-1-1.2-2.3-1.9-3.8-1.9c-1.3,0-2.6,0.5-3.6,1.5l-39,39c-0.6,0.6-0.2,1.6,0.7,1.6L182.6,117.6z"/>
  59. <path fill="#E73E51" d="M169.5,165.2L169.5,165.2c1.5,0,2.8-0.7,3.8-1.9c7.9-10.1,12.9-22.1,14.4-34.8c0.2-1.5-0.3-2.9-1.4-4c-1-1-2.3-1.5-3.7-1.5l-55,0c-0.9,0-1.3,1-0.7,1.6l39,39C166.9,164.7,168.2,165.2,169.5,165.2z"/>
  60. <path fill="#E67E3C" d="M122.9,188L122.9,188c1,1,2.5,1.5,4,1.3c12.7-1.5,24.8-6.5,34.8-14.4c1.2-0.9,1.8-2.3,1.9-3.8c0-1.4-0.6-2.7-1.6-3.7l-38.9-38.9c-0.6-0.6-1.6-0.2-1.6,0.7l0,55.2C121.4,185.8,122,187,122.9,188z"/>
  61. <path fill="#F0E049" d="M68,75.4c-1.5,0-2.8,0.7-3.8,1.9c-7.9,10.1-12.9,22.1-14.4,34.8c-0.2,1.5,0.3,2.9,1.4,4c1,1,2.3,1.5,3.7,1.5l55,0c0.9,0,1.3-1,0.7-1.6l-39-39C70.6,76,69.3,75.4,68,75.4z"/>
  62. <path fill="#D5D755" d="M114.6,52.7c-1-1-2.5-1.5-4-1.3c-12.7,1.5-24.8,6.5-34.8,14.4c-1.2,0.9-1.8,2.3-1.9,3.8c0,1.4,0.6,2.7,1.6,3.7l38.9,38.9c0.6,0.6,1.6,0.2,1.6-0.7l0-55.2C116.1,54.9,115.5,53.6,114.6,52.7z"/>
  63. <path fill="#9CB6D3" d="M163.7,69.6c0-1.5-0.7-2.8-1.9-3.8c-10.1-7.9-22.1-12.9-34.8-14.4c-1.5-0.2-2.9,0.3-4,1.4c-1,1-1.5,2.3-1.5,3.7l0,55c0,0.9,1,1.3,1.6,0.7l39-39C163.1,72.1,163.7,70.9,163.7,69.6z"/>
  64. <path fill="#F5BD47" d="M109.9,123l-55,0c-1.4,0-2.7,0.5-3.7,1.5c-1.1,1.1-1.6,2.5-1.4,4c1.5,12.7,6.5,24.7,14.4,34.8c1,1.2,2.3,1.9,3.8,1.9c1.3,0,2.6-0.5,3.5-1.5c0,0,0,0,0,0l39-39C111.2,124,110.8,123,109.9,123z"/>
  65. <path fill="#F19F53" d="M114.4,128.5l-38.9,38.9c-1,1-1.6,2.3-1.6,3.7c0,1.5,0.7,2.9,1.9,3.8c10,7.9,22.1,12.9,34.8,14.4c1.6,0.2,3-0.3,4-1.3c0.9-0.9,1.4-2.2,1.4-3.6c0,0,0,0,0,0l0-55.2C116.1,128.3,115,127.9,114.4,128.5z"/>
  66. </svg>
  67. )blahblah";
  68. logo = Drawable::createFromSVG (*parseXML (logoData));
  69. }
  70. String getName() const override { return "Background Image"; }
  71. void reset() override {}
  72. void handleTouch (Point<float>) override {}
  73. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float>) override
  74. {
  75. logo->drawWithin (g, canvas.getLimits().reduced (3.0f), RectanglePlacement (RectanglePlacement::centred), 0.6f);
  76. }
  77. std::unique_ptr<Drawable> logo;
  78. };
  79. //==============================================================================
  80. struct FlockDemo : public BackgroundLogo
  81. {
  82. String getName() const override { return "Flock"; }
  83. void setNumBirds (int numBirds)
  84. {
  85. BackgroundLogo::reset();
  86. birds.clear();
  87. for (int i = numBirds; --i >= 0;)
  88. birds.add ({});
  89. centreOfGravity = {};
  90. lastGravityMove = {};
  91. fakeMouseTouchLengthToRun = 0;
  92. fakeMouseTouchPosition = {};
  93. fakeMouseTouchVelocity = {};
  94. }
  95. void reset() override
  96. {
  97. BackgroundLogo::reset();
  98. setNumBirds (100);
  99. }
  100. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float> activeArea) override
  101. {
  102. BackgroundLogo::generateCanvas (g, canvas, activeArea);
  103. if (Time::getCurrentTime() > lastGravityMove + RelativeTime::seconds (0.5))
  104. {
  105. if (fakeMouseTouchLengthToRun > 0)
  106. {
  107. --fakeMouseTouchLengthToRun;
  108. fakeMouseTouchPosition += fakeMouseTouchVelocity;
  109. centreOfGravity = fakeMouseTouchPosition;
  110. }
  111. else
  112. {
  113. centreOfGravity = {};
  114. if (rng.nextInt (300) == 2 && canvas.clients.size() > 0)
  115. {
  116. fakeMouseTouchLengthToRun = 50;
  117. fakeMouseTouchPosition = canvas.clients.getReference (rng.nextInt (canvas.clients.size())).centre;
  118. fakeMouseTouchVelocity = { rng.nextFloat() * 0.3f - 0.15f,
  119. rng.nextFloat() * 0.3f - 0.15f };
  120. }
  121. }
  122. }
  123. g.setColour (Colours::white.withAlpha (0.2f));
  124. if (! centreOfGravity.isOrigin())
  125. g.fillEllipse (centreOfGravity.getX() - 1.0f, centreOfGravity.getY() - 1.0f, 2.0f, 2.0f);
  126. for (int i = 0; i < birds.size(); ++i)
  127. for (int j = i + 1; j < birds.size(); ++j)
  128. attractBirds (birds.getReference(i), birds.getReference(j));
  129. for (auto& b : birds)
  130. {
  131. if (! centreOfGravity.isOrigin())
  132. b.move (centreOfGravity, 0.4f);
  133. b.update();
  134. b.draw (g);
  135. b.bounceOffEdges (canvas.getLimits().expanded (1.0f));
  136. }
  137. for (int i = rings.size(); --i >= 0;)
  138. {
  139. if (rings.getReference(i).update())
  140. rings.getReference(i).draw (g);
  141. else
  142. rings.remove (i);
  143. }
  144. }
  145. bool isRingNear (Point<float> p) const
  146. {
  147. for (auto& r : rings)
  148. if (r.centre.getDistanceFrom (p) < 1.0f)
  149. return true;
  150. return false;
  151. }
  152. void handleTouch (Point<float> position) override
  153. {
  154. lastGravityMove = Time::getCurrentTime();
  155. centreOfGravity = position;
  156. fakeMouseTouchLengthToRun = 0;
  157. if (! isRingNear (position))
  158. rings.add ({ position, 1.0f, 0.5f });
  159. }
  160. //==============================================================================
  161. struct Bird
  162. {
  163. Bird()
  164. {
  165. Random randGen;
  166. pos.x = randGen.nextFloat() * 10.0f - 5.0f;
  167. pos.y = randGen.nextFloat() * 10.0f - 5.0f;
  168. velocity.x = randGen.nextFloat() * 0.001f;
  169. velocity.y = randGen.nextFloat() * 0.001f;
  170. colour = Colour::fromHSV (randGen.nextFloat(), 0.2f, 0.9f, randGen.nextFloat() * 0.4f + 0.2f);
  171. shape.addTriangle (0.0f, 0.0f, -0.3f, 1.0f, 0.3f, 1.0f);
  172. shape = shape.createPathWithRoundedCorners (0.2f);
  173. shape.applyTransform (AffineTransform::scale (randGen.nextFloat() + 1.0f));
  174. }
  175. Point<float> pos, velocity, acc;
  176. Colour colour;
  177. Path shape;
  178. void move (Point<float> target, float strength)
  179. {
  180. auto r = target - pos;
  181. float rSquared = jmax (0.1f, (r.x * r.x) + (r.y * r.y));
  182. if (rSquared > 1.0f)
  183. velocity += (r * strength / rSquared);
  184. acc = {};
  185. }
  186. void accelerate (Point<float> acceleration)
  187. {
  188. acc += acceleration;
  189. }
  190. void bounceOffEdges (Rectangle<float> limits)
  191. {
  192. if (pos.x < limits.getX()) { velocity.x = std::abs (velocity.x); acc = {}; }
  193. if (pos.x > limits.getRight()) { velocity.x = -std::abs (velocity.x); acc = {}; }
  194. if (pos.y < limits.getY()) { velocity.y = std::abs (velocity.y); acc = {}; }
  195. if (pos.y > limits.getBottom()) { velocity.y = -std::abs (velocity.y); acc = {}; }
  196. }
  197. void update()
  198. {
  199. velocity += acc;
  200. float length = velocity.getDistanceFromOrigin();
  201. const float maxSpeed = 0.5f;
  202. if (length > maxSpeed)
  203. velocity = getVectorWithLength (velocity, maxSpeed);
  204. pos += velocity;
  205. }
  206. void draw (Graphics& g)
  207. {
  208. g.setColour (colour);
  209. g.fillPath (shape, AffineTransform::rotation (Point<float>().getAngleToPoint (velocity)).translated (pos));
  210. }
  211. };
  212. static Point<float> getVectorWithLength (Point<float> v, float newLength)
  213. {
  214. return v * (newLength / v.getDistanceFromOrigin());
  215. }
  216. static void attractBirds (Bird& b1, Bird& b2)
  217. {
  218. auto delta = b1.pos - b2.pos;
  219. const float zoneRadius = 10.0f;
  220. const float low = 0.4f;
  221. const float high = 0.65f;
  222. const float strength = 0.01f;
  223. const float distanceSquared = (delta.x * delta.x) * (delta.y * delta.y);
  224. if (distanceSquared < zoneRadius * zoneRadius && distanceSquared > 0.01f)
  225. {
  226. float proportion = distanceSquared / (zoneRadius * zoneRadius);
  227. if (proportion < low)
  228. {
  229. const float F = (low / proportion - 1.0f) * strength * 0.003f;
  230. delta = getVectorWithLength (delta, F);
  231. b1.accelerate (delta);
  232. b2.accelerate (-delta);
  233. }
  234. else if (proportion < high)
  235. {
  236. const float regionSize = high - low;
  237. const float adjustedProportion = (proportion - low) / regionSize;
  238. const float F = (0.5f - std::cos (adjustedProportion * MathConstants<float>::twoPi) * 0.5f + 0.5f) * strength;
  239. b1.accelerate (getVectorWithLength (b2.velocity, F));
  240. b2.accelerate (getVectorWithLength (b1.velocity, F));
  241. }
  242. else
  243. {
  244. const float regionSize = 1.0f - high;
  245. const float adjustedProportion = (proportion - high) / regionSize;
  246. const float F = (0.5f - std::cos (adjustedProportion * MathConstants<float>::twoPi) * 0.5f + 0.5f) * strength;
  247. delta = getVectorWithLength (delta, F);
  248. b1.accelerate (-delta);
  249. b2.accelerate (delta);
  250. }
  251. }
  252. }
  253. Random rng;
  254. Array<Bird> birds;
  255. Point<float> centreOfGravity;
  256. Time lastGravityMove;
  257. int fakeMouseTouchLengthToRun = 0;
  258. Point<float> fakeMouseTouchPosition, fakeMouseTouchVelocity;
  259. //==============================================================================
  260. struct Ring
  261. {
  262. Point<float> centre;
  263. float diameter, opacity;
  264. bool update()
  265. {
  266. diameter += 0.7f;
  267. opacity -= 0.01f;
  268. return opacity > 0;
  269. }
  270. void draw (Graphics& g)
  271. {
  272. const float thickness = 0.2f;
  273. auto r = Rectangle<float> (diameter, diameter).withCentre (centre);
  274. Path p;
  275. p.addEllipse (r);
  276. p.addEllipse (r.reduced (thickness));
  277. p.setUsingNonZeroWinding (false);
  278. g.setColour (Colours::white.withAlpha (opacity));
  279. g.fillPath (p);
  280. }
  281. };
  282. Array<Ring> rings;
  283. };
  284. //==============================================================================
  285. struct FlockWithText : public FlockDemo
  286. {
  287. FlockWithText()
  288. {
  289. messages.add ("JUCE is our cross-platform C++ framework\n\n"
  290. "In this demo, the same C++ app is running natively on NUMDEVICES devices,\n"
  291. "which are sharing their graphic state via the network");
  292. messages.add ("No other libraries were needed to create this demo.\n"
  293. "JUCE provides thousands of classes for cross-platform GUI,\n"
  294. "audio, networking, data-structures and many other common tasks");
  295. messages.add ("As well as a code library, JUCE provides tools for managing\n"
  296. "cross-platform projects that are built with Xcode,\n"
  297. "Visual Studio, Android Studio, GCC and other compilers");
  298. messages.add ("JUCE can be used to build desktop or mobile apps, and also\n"
  299. "audio plug-ins in the VST2, VST3, AudioUnit and AAX formats");
  300. }
  301. String getName() const override { return "Flock with text"; }
  302. void reset() override
  303. {
  304. FlockDemo::reset();
  305. currentMessage = 0;
  306. currentMessageStart = {};
  307. clientIndex = 0;
  308. }
  309. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float> activeArea) override
  310. {
  311. FlockDemo::generateCanvas (g, canvas, activeArea);
  312. const float textSize = 0.5f; // inches
  313. const float textBlockWidth = 20.0f; // inches
  314. tick();
  315. Graphics::ScopedSaveState ss (g);
  316. const float scale = 20.0f; // scaled to allow the fonts to use more reasonable sizes
  317. g.addTransform (AffineTransform::scale (1.0f / scale));
  318. String text = String (messages[currentMessage]).replace ("NUMDEVICES", String (canvas.clients.size()));
  319. AttributedString as;
  320. as.append (text, Font (textSize * scale), Colour (0x80ffffff).withMultipliedAlpha (alpha));
  321. as.setJustification (Justification::centred);
  322. auto middle = canvas.clients[clientIndex % canvas.clients.size()].centre * scale;
  323. as.draw (g, Rectangle<float> (textBlockWidth * scale, textBlockWidth * scale).withCentre (middle));
  324. }
  325. void tick()
  326. {
  327. const double displayTimeSeconds = 5.0;
  328. const double fadeTimeSeconds = 1.0;
  329. Time now = Time::getCurrentTime();
  330. const double secondsSinceStart = (now - currentMessageStart).inSeconds();
  331. if (secondsSinceStart > displayTimeSeconds)
  332. {
  333. currentMessageStart = now;
  334. currentMessage = (currentMessage + 1) % messages.size();
  335. ++clientIndex;
  336. alpha = 0;
  337. }
  338. else if (secondsSinceStart > displayTimeSeconds - fadeTimeSeconds)
  339. {
  340. alpha = (float) jlimit (0.0, 1.0, (displayTimeSeconds - secondsSinceStart) / fadeTimeSeconds);
  341. }
  342. else if (secondsSinceStart < fadeTimeSeconds)
  343. {
  344. alpha = (float) jlimit (0.0, 1.0, secondsSinceStart / fadeTimeSeconds);
  345. }
  346. }
  347. StringArray messages;
  348. int currentMessage = 0, clientIndex = 0;
  349. float alpha = 0;
  350. Point<float> centre;
  351. Time currentMessageStart;
  352. };
  353. //==============================================================================
  354. struct SmallFlock : public FlockDemo
  355. {
  356. String getName() const override { return "Small Flock"; }
  357. void reset() override
  358. {
  359. setNumBirds (20);
  360. }
  361. };
  362. //==============================================================================
  363. struct BigFlock : public FlockDemo
  364. {
  365. String getName() const override { return "Big Flock"; }
  366. void reset() override
  367. {
  368. setNumBirds (200);
  369. }
  370. };
  371. //==============================================================================
  372. template <int numHorizontalLogos>
  373. struct MultiLogo : public BackgroundLogo
  374. {
  375. String getName() const override { return "Multi-Logo " + String ((int) numHorizontalLogos); }
  376. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float>) override
  377. {
  378. float indent = 0.5f;
  379. float logoSize = canvas.getLimits().getWidth() / numHorizontalLogos;
  380. auto limits = canvas.getLimits();
  381. for (float x = limits.getX(); x < limits.getRight(); x += logoSize)
  382. {
  383. for (float y = limits.getY(); y < limits.getBottom(); y += logoSize)
  384. {
  385. logo->drawWithin (g, Rectangle<float> (x, y, logoSize, logoSize).reduced (indent),
  386. RectanglePlacement (RectanglePlacement::centred), 0.5f);
  387. }
  388. }
  389. }
  390. };
  391. //==============================================================================
  392. void createAllDemos (OwnedArray<AnimatedContent>& demos)
  393. {
  394. demos.add (new FlockDemo());
  395. demos.add (new FlockWithText());
  396. demos.add (new SmallFlock());
  397. demos.add (new BigFlock());
  398. demos.add (new BackgroundLogo());
  399. demos.add (new MultiLogo<5>());
  400. demos.add (new MultiLogo<10>());
  401. demos.add (new GridLines());
  402. demos.add (new BlankCanvas());
  403. }