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.

506 lines
19KB

  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. struct BlankCanvas : public AnimatedContent
  20. {
  21. String getName() const override { return "Blank Canvas"; }
  22. void reset() override {}
  23. void handleTouch (Point<float>) override {}
  24. void generateCanvas (Graphics&, SharedCanvasDescription&, Rectangle<float>) override {}
  25. };
  26. //==============================================================================
  27. struct GridLines : public AnimatedContent
  28. {
  29. String getName() const override { return "Grid Lines"; }
  30. void reset() override {}
  31. void handleTouch (Point<float>) override {}
  32. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float>) override
  33. {
  34. auto limits = canvas.getLimits();
  35. float lineThickness = 0.1f;
  36. g.setColour (Colours::blue);
  37. g.drawRect (canvas.getLimits(), lineThickness);
  38. for (float y = limits.getY(); y < limits.getBottom(); y += 2.0f)
  39. g.drawLine (limits.getX(), y, limits.getRight(), y, lineThickness);
  40. for (float x = limits.getX(); x < limits.getRight(); x += 2.0f)
  41. g.drawLine (x, limits.getY(), x, limits.getBottom(), lineThickness);
  42. g.setColour (Colours::darkred);
  43. g.drawLine (limits.getX(), limits.getCentreY(), limits.getRight(), limits.getCentreY(), lineThickness);
  44. g.drawLine (limits.getCentreX(), limits.getY(), limits.getCentreX(), limits.getBottom(), lineThickness);
  45. g.setColour (Colours::lightgrey);
  46. g.drawLine (limits.getX(), limits.getY(), limits.getRight(), limits.getBottom(), lineThickness);
  47. g.drawLine (limits.getX(), limits.getBottom(), limits.getRight(), limits.getY(), lineThickness);
  48. }
  49. };
  50. //==============================================================================
  51. struct BackgroundLogo : public AnimatedContent
  52. {
  53. BackgroundLogo()
  54. {
  55. static const char logoData[] = R"blahblah(
  56. <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"
  57. viewBox="0 0 239.2 239.2" enable-background="new 0 0 239.2 239.2" xml:space="preserve">
  58. <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"/>
  59. <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"/>
  60. <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"/>
  61. <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"/>
  62. <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"/>
  63. <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"/>
  64. <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"/>
  65. <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"/>
  66. <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"/>
  67. </svg>
  68. )blahblah";
  69. logo.reset (Drawable::createFromSVG (*parseXML (logoData)));
  70. }
  71. String getName() const override { return "Background Image"; }
  72. void reset() override {}
  73. void handleTouch (Point<float>) override {}
  74. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float> activeArea) override
  75. {
  76. logo->drawWithin (g, canvas.getLimits().reduced (3.0f), RectanglePlacement (RectanglePlacement::centred), 0.6f);
  77. }
  78. std::unique_ptr<Drawable> logo;
  79. };
  80. //==============================================================================
  81. struct FlockDemo : public BackgroundLogo
  82. {
  83. String getName() const override { return "Flock"; }
  84. void setNumBirds (int numBirds)
  85. {
  86. BackgroundLogo::reset();
  87. birds.clear();
  88. for (int i = numBirds; --i >= 0;)
  89. birds.add ({});
  90. centreOfGravity = {};
  91. lastGravityMove = {};
  92. fakeMouseTouchLengthToRun = 0;
  93. fakeMouseTouchPosition = {};
  94. fakeMouseTouchVelocity = {};
  95. }
  96. void reset() override
  97. {
  98. BackgroundLogo::reset();
  99. setNumBirds (100);
  100. }
  101. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float> activeArea) override
  102. {
  103. BackgroundLogo::generateCanvas (g, canvas, activeArea);
  104. if (Time::getCurrentTime() > lastGravityMove + RelativeTime::seconds (0.5))
  105. {
  106. if (fakeMouseTouchLengthToRun > 0)
  107. {
  108. --fakeMouseTouchLengthToRun;
  109. fakeMouseTouchPosition += fakeMouseTouchVelocity;
  110. centreOfGravity = fakeMouseTouchPosition;
  111. }
  112. else
  113. {
  114. centreOfGravity = {};
  115. if (rng.nextInt (300) == 2 && canvas.clients.size() > 0)
  116. {
  117. fakeMouseTouchLengthToRun = 50;
  118. fakeMouseTouchPosition = canvas.clients.getReference (rng.nextInt (canvas.clients.size())).centre;
  119. fakeMouseTouchVelocity = { rng.nextFloat() * 0.3f - 0.15f,
  120. rng.nextFloat() * 0.3f - 0.15f };
  121. }
  122. }
  123. }
  124. g.setColour (Colours::white.withAlpha (0.2f));
  125. if (! centreOfGravity.isOrigin())
  126. g.fillEllipse (centreOfGravity.getX() - 1.0f, centreOfGravity.getY() - 1.0f, 2.0f, 2.0f);
  127. for (int i = 0; i < birds.size(); ++i)
  128. for (int j = i + 1; j < birds.size(); ++j)
  129. attractBirds (birds.getReference(i), birds.getReference(j));
  130. for (auto& b : birds)
  131. {
  132. if (! centreOfGravity.isOrigin())
  133. b.move (centreOfGravity, 0.4f);
  134. b.update();
  135. b.draw (g);
  136. b.bounceOffEdges (canvas.getLimits().expanded (1.0f));
  137. }
  138. for (int i = rings.size(); --i >= 0;)
  139. {
  140. if (rings.getReference(i).update())
  141. rings.getReference(i).draw (g);
  142. else
  143. rings.remove (i);
  144. }
  145. }
  146. bool isRingNear (Point<float> p) const
  147. {
  148. for (auto& r : rings)
  149. if (r.centre.getDistanceFrom (p) < 1.0f)
  150. return true;
  151. return false;
  152. }
  153. void handleTouch (Point<float> position) override
  154. {
  155. lastGravityMove = Time::getCurrentTime();
  156. centreOfGravity = position;
  157. fakeMouseTouchLengthToRun = 0;
  158. if (! isRingNear (position))
  159. rings.add ({ position, 1.0f, 0.5f });
  160. }
  161. //==============================================================================
  162. struct Bird
  163. {
  164. Bird()
  165. {
  166. Random randGen;
  167. pos.x = randGen.nextFloat() * 10.0f - 5.0f;
  168. pos.y = randGen.nextFloat() * 10.0f - 5.0f;
  169. velocity.x = randGen.nextFloat() * 0.001f;
  170. velocity.y = randGen.nextFloat() * 0.001f;
  171. colour = Colour::fromHSV (randGen.nextFloat(), 0.2f, 0.9f, randGen.nextFloat() * 0.4f + 0.2f);
  172. shape.addTriangle (0.0f, 0.0f, -0.3f, 1.0f, 0.3f, 1.0f);
  173. shape = shape.createPathWithRoundedCorners (0.2f);
  174. shape.applyTransform (AffineTransform::scale (randGen.nextFloat() + 1.0f));
  175. }
  176. Point<float> pos, velocity, acc;
  177. Colour colour;
  178. Path shape;
  179. void move (Point<float> target, float strength)
  180. {
  181. auto r = target - pos;
  182. float rSquared = jmax (0.1f, (r.x * r.x) + (r.y * r.y));
  183. if (rSquared > 1.0f)
  184. velocity += (r * strength / rSquared);
  185. acc = {};
  186. }
  187. void accelerate (Point<float> acceleration)
  188. {
  189. acc += acceleration;
  190. }
  191. void bounceOffEdges (Rectangle<float> limits)
  192. {
  193. if (pos.x < limits.getX()) { velocity.x = std::abs (velocity.x); acc = {}; }
  194. if (pos.x > limits.getRight()) { velocity.x = -std::abs (velocity.x); acc = {}; }
  195. if (pos.y < limits.getY()) { velocity.y = std::abs (velocity.y); acc = {}; }
  196. if (pos.y > limits.getBottom()) { velocity.y = -std::abs (velocity.y); acc = {}; }
  197. }
  198. void update()
  199. {
  200. velocity += acc;
  201. float length = velocity.getDistanceFromOrigin();
  202. const float maxSpeed = 0.5f;
  203. if (length > maxSpeed)
  204. velocity = getVectorWithLength (velocity, maxSpeed);
  205. pos += velocity;
  206. }
  207. void draw (Graphics& g)
  208. {
  209. g.setColour (colour);
  210. g.fillPath (shape, AffineTransform::rotation (Point<float>().getAngleToPoint (velocity)).translated (pos));
  211. }
  212. };
  213. static Point<float> getVectorWithLength (Point<float> v, float newLength)
  214. {
  215. return v * (newLength / v.getDistanceFromOrigin());
  216. }
  217. static void attractBirds (Bird& b1, Bird& b2)
  218. {
  219. auto delta = b1.pos - b2.pos;
  220. const float zoneRadius = 10.0f;
  221. const float low = 0.4f;
  222. const float high = 0.65f;
  223. const float strength = 0.01f;
  224. const float distanceSquared = (delta.x * delta.x) * (delta.y * delta.y);
  225. if (distanceSquared < zoneRadius * zoneRadius && distanceSquared > 0.01f)
  226. {
  227. float proportion = distanceSquared / (zoneRadius * zoneRadius);
  228. if (proportion < low)
  229. {
  230. const float F = (low / proportion - 1.0f) * strength * 0.003f;
  231. delta = getVectorWithLength (delta, F);
  232. b1.accelerate (delta);
  233. b2.accelerate (-delta);
  234. }
  235. else if (proportion < high)
  236. {
  237. const float regionSize = high - low;
  238. const float adjustedProportion = (proportion - low) / regionSize;
  239. const float F = (0.5f - std::cos (adjustedProportion * MathConstants<float>::twoPi) * 0.5f + 0.5f) * strength;
  240. b1.accelerate (getVectorWithLength (b2.velocity, F));
  241. b2.accelerate (getVectorWithLength (b1.velocity, F));
  242. }
  243. else
  244. {
  245. const float regionSize = 1.0f - high;
  246. const float adjustedProportion = (proportion - high) / regionSize;
  247. const float F = (0.5f - std::cos (adjustedProportion * MathConstants<float>::twoPi) * 0.5f + 0.5f) * strength;
  248. delta = getVectorWithLength (delta, F);
  249. b1.accelerate (-delta);
  250. b2.accelerate (delta);
  251. }
  252. }
  253. }
  254. Random rng;
  255. Array<Bird> birds;
  256. Point<float> centreOfGravity;
  257. Time lastGravityMove;
  258. int fakeMouseTouchLengthToRun = 0;
  259. Point<float> fakeMouseTouchPosition, fakeMouseTouchVelocity;
  260. //==============================================================================
  261. struct Ring
  262. {
  263. Point<float> centre;
  264. float diameter, opacity;
  265. bool update()
  266. {
  267. diameter += 0.7f;
  268. opacity -= 0.01f;
  269. return opacity > 0;
  270. }
  271. void draw (Graphics& g)
  272. {
  273. const float thickness = 0.2f;
  274. auto r = Rectangle<float> (diameter, diameter).withCentre (centre);
  275. Path p;
  276. p.addEllipse (r);
  277. p.addEllipse (r.reduced (thickness));
  278. p.setUsingNonZeroWinding (false);
  279. g.setColour (Colours::white.withAlpha (opacity));
  280. g.fillPath (p);
  281. }
  282. };
  283. Array<Ring> rings;
  284. };
  285. //==============================================================================
  286. struct FlockWithText : public FlockDemo
  287. {
  288. FlockWithText()
  289. {
  290. messages.add ("JUCE is our cross-platform C++ framework\n\n"
  291. "In this demo, the same C++ app is running natively on NUMDEVICES devices,\n"
  292. "which are sharing their graphic state via the network");
  293. messages.add ("No other libraries were needed to create this demo.\n"
  294. "JUCE provides thousands of classes for cross-platform GUI,\n"
  295. "audio, networking, data-structures and many other common tasks");
  296. messages.add ("As well as a code library, JUCE provides tools for managing\n"
  297. "cross-platform projects that are built with Xcode,\n"
  298. "Visual Studio, Android Studio, GCC and other compilers");
  299. messages.add ("JUCE can be used to build desktop or mobile apps, and also\n"
  300. "audio plug-ins in the VST2, VST3, AudioUnit, AAX and RTAS formats");
  301. }
  302. String getName() const override { return "Flock with text"; }
  303. void reset() override
  304. {
  305. FlockDemo::reset();
  306. currentMessage = 0;
  307. currentMessageStart = {};
  308. clientIndex = 0;
  309. }
  310. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float> activeArea) override
  311. {
  312. FlockDemo::generateCanvas (g, canvas, activeArea);
  313. const float textSize = 0.5f; // inches
  314. const float textBlockWidth = 20.0f; // inches
  315. tick();
  316. Graphics::ScopedSaveState ss (g);
  317. const float scale = 20.0f; // scaled to allow the fonts to use more reasonable sizes
  318. g.addTransform (AffineTransform::scale (1.0f / scale));
  319. String text = String (messages[currentMessage]).replace ("NUMDEVICES", String (canvas.clients.size()));
  320. AttributedString as;
  321. as.append (text, Font (textSize * scale), Colour (0x80ffffff).withMultipliedAlpha (alpha));
  322. as.setJustification (Justification::centred);
  323. auto middle = canvas.clients[clientIndex % canvas.clients.size()].centre * scale;
  324. as.draw (g, Rectangle<float> (textBlockWidth * scale, textBlockWidth * scale).withCentre (middle));
  325. }
  326. void tick()
  327. {
  328. const double displayTimeSeconds = 5.0;
  329. const double fadeTimeSeconds = 1.0;
  330. Time now = Time::getCurrentTime();
  331. const double secondsSinceStart = (now - currentMessageStart).inSeconds();
  332. if (secondsSinceStart > displayTimeSeconds)
  333. {
  334. currentMessageStart = now;
  335. currentMessage = (currentMessage + 1) % messages.size();
  336. ++clientIndex;
  337. alpha = 0;
  338. }
  339. else if (secondsSinceStart > displayTimeSeconds - fadeTimeSeconds)
  340. {
  341. alpha = (float) jlimit (0.0, 1.0, (displayTimeSeconds - secondsSinceStart) / fadeTimeSeconds);
  342. }
  343. else if (secondsSinceStart < fadeTimeSeconds)
  344. {
  345. alpha = (float) jlimit (0.0, 1.0, secondsSinceStart / fadeTimeSeconds);
  346. }
  347. }
  348. StringArray messages;
  349. int currentMessage = 0, clientIndex = 0;
  350. float alpha = 0;
  351. Point<float> centre;
  352. Time currentMessageStart;
  353. };
  354. //==============================================================================
  355. struct SmallFlock : public FlockDemo
  356. {
  357. String getName() const override { return "Small Flock"; }
  358. void reset() override
  359. {
  360. setNumBirds (20);
  361. }
  362. };
  363. //==============================================================================
  364. struct BigFlock : public FlockDemo
  365. {
  366. String getName() const override { return "Big Flock"; }
  367. void reset() override
  368. {
  369. setNumBirds (200);
  370. }
  371. };
  372. //==============================================================================
  373. template <int numHorizontalLogos>
  374. struct MultiLogo : public BackgroundLogo
  375. {
  376. String getName() const override { return "Multi-Logo " + String ((int) numHorizontalLogos); }
  377. void generateCanvas (Graphics& g, SharedCanvasDescription& canvas, Rectangle<float> activeArea) override
  378. {
  379. float indent = 0.5f;
  380. float logoSize = canvas.getLimits().getWidth() / numHorizontalLogos;
  381. auto limits = canvas.getLimits();
  382. for (float x = limits.getX(); x < limits.getRight(); x += logoSize)
  383. {
  384. for (float y = limits.getY(); y < limits.getBottom(); y += logoSize)
  385. {
  386. logo->drawWithin (g, Rectangle<float> (x, y, logoSize, logoSize).reduced (indent),
  387. RectanglePlacement (RectanglePlacement::centred), 0.5f);
  388. }
  389. }
  390. }
  391. };
  392. //==============================================================================
  393. void createAllDemos (OwnedArray<AnimatedContent>& demos)
  394. {
  395. demos.add (new FlockDemo());
  396. demos.add (new FlockWithText());
  397. demos.add (new SmallFlock());
  398. demos.add (new BigFlock());
  399. demos.add (new BackgroundLogo());
  400. demos.add (new MultiLogo<5>());
  401. demos.add (new MultiLogo<10>());
  402. demos.add (new GridLines());
  403. demos.add (new BlankCanvas());
  404. }