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.

343 lines
9.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceDemoHeader.h"
  18. // (These classes and random functions are used inside the 3rd-party Box2D demo code)
  19. inline float32 RandomFloat() { return Random::getSystemRandom().nextFloat() * 2.0f - 1.0f; }
  20. inline float32 RandomFloat (float32 lo, float32 hi) { return Random::getSystemRandom().nextFloat() * (hi - lo) + lo; }
  21. struct Settings
  22. {
  23. Settings()
  24. : viewCenter (0.0f, 20.0f),
  25. hz (60.0f),
  26. velocityIterations (8),
  27. positionIterations (3),
  28. drawShapes (1),
  29. drawJoints (1),
  30. drawAABBs (0),
  31. drawPairs (0),
  32. drawContactPoints (0),
  33. drawContactNormals (0),
  34. drawContactForces (0),
  35. drawFrictionForces (0),
  36. drawCOMs (0),
  37. drawStats (0),
  38. drawProfile (0),
  39. enableWarmStarting (1),
  40. enableContinuous (1),
  41. enableSubStepping (0),
  42. pause (0),
  43. singleStep (0)
  44. {}
  45. b2Vec2 viewCenter;
  46. float32 hz;
  47. int velocityIterations;
  48. int positionIterations;
  49. int drawShapes;
  50. int drawJoints;
  51. int drawAABBs;
  52. int drawPairs;
  53. int drawContactPoints;
  54. int drawContactNormals;
  55. int drawContactForces;
  56. int drawFrictionForces;
  57. int drawCOMs;
  58. int drawStats;
  59. int drawProfile;
  60. int enableWarmStarting;
  61. int enableContinuous;
  62. int enableSubStepping;
  63. int pause;
  64. int singleStep;
  65. };
  66. struct Test
  67. {
  68. Test() : m_world (new b2World (b2Vec2 (0.0f, -10.0f))) {}
  69. virtual ~Test() {}
  70. virtual void Keyboard (unsigned char /*key*/) {}
  71. virtual void KeyboardUp (unsigned char /*key*/) {}
  72. ScopedPointer<b2World> m_world;
  73. };
  74. #include "Box2DTests/AddPair.h"
  75. #include "Box2DTests/ApplyForce.h"
  76. #include "Box2DTests/Dominos.h"
  77. #include "Box2DTests/Chain.h"
  78. //==============================================================================
  79. /** This list box just displays a StringArray and broadcasts a change message when the
  80. selected row changes.
  81. */
  82. class Box2DTestList : public ListBoxModel,
  83. public ChangeBroadcaster
  84. {
  85. public:
  86. Box2DTestList (const StringArray& testList) : tests (testList)
  87. {
  88. }
  89. int getNumRows() override { return tests.size(); }
  90. void paintListBoxItem (int row, Graphics& g, int w, int h, bool rowIsSelected) override
  91. {
  92. if (rowIsSelected)
  93. g.fillAll (LookAndFeel::getDefaultLookAndFeel().findColour (TextEditor::highlightColourId));
  94. const Font f (h * 0.7f);
  95. g.setColour (Colours::black);
  96. g.setFont (f);
  97. g.drawText (tests[row], Rectangle<int> (0, 0, w, h).reduced (2),
  98. Justification::centredLeft, true);
  99. }
  100. void selectedRowsChanged (int /*lastRowSelected*/) override
  101. {
  102. sendChangeMessage();
  103. }
  104. private:
  105. StringArray tests;
  106. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Box2DTestList)
  107. };
  108. //==============================================================================
  109. struct Box2DRenderComponent : public Component
  110. {
  111. Box2DRenderComponent()
  112. {
  113. setOpaque (true);
  114. }
  115. void paint (Graphics& g) override
  116. {
  117. g.fillAll (Colours::white);
  118. if (currentTest != nullptr)
  119. {
  120. Box2DRenderer renderer;
  121. renderer.render (g, *currentTest->m_world,
  122. -16.0f, 30.0f, 16.0f, -1.0f,
  123. getLocalBounds().toFloat().reduced (8.0f));
  124. }
  125. }
  126. ScopedPointer<Test> currentTest;
  127. };
  128. //==============================================================================
  129. class Box2DDemo : public Component,
  130. private Timer,
  131. private ChangeListener
  132. {
  133. public:
  134. enum Demos
  135. {
  136. addPair = 0,
  137. applyForce,
  138. dominoes,
  139. chain,
  140. numTests
  141. };
  142. Box2DDemo()
  143. : testsList (getTestsList()),
  144. testsListModel (testsList)
  145. {
  146. setOpaque (true);
  147. setWantsKeyboardFocus (true);
  148. testsListModel.addChangeListener (this);
  149. addAndMakeVisible (renderComponent);
  150. addAndMakeVisible (testsListBox);
  151. testsListBox.setModel (&testsListModel);
  152. testsListBox.selectRow (dominoes);
  153. testsListBox.setColour (ListBox::backgroundColourId, Colours::lightgrey);
  154. addAndMakeVisible (instructions);
  155. instructions.setMultiLine (true);
  156. instructions.setReadOnly (true);
  157. instructions.setColour (TextEditor::backgroundColourId, Colours::lightgrey);
  158. startTimerHz (60);
  159. }
  160. ~Box2DDemo()
  161. {
  162. testsListModel.removeChangeListener (this);
  163. }
  164. void paint (Graphics& g) override
  165. {
  166. fillStandardDemoBackground (g);
  167. }
  168. void resized() override
  169. {
  170. Rectangle<int> r (getLocalBounds().reduced (4));
  171. Rectangle<int> area (r.removeFromBottom (150));
  172. testsListBox.setBounds (area.removeFromLeft (150));
  173. area.removeFromLeft (4);
  174. instructions.setBounds (area);
  175. r.removeFromBottom (6);
  176. renderComponent.setBounds (r);
  177. }
  178. bool keyPressed (const KeyPress& key) override
  179. {
  180. if (renderComponent.currentTest != nullptr)
  181. {
  182. // We override this to avoid the system beeping for an unused keypress
  183. switch (key.getTextCharacter())
  184. {
  185. case 'a':
  186. case 'w':
  187. case 'd':
  188. return true;
  189. default:
  190. break;
  191. }
  192. }
  193. return false;
  194. }
  195. private:
  196. StringArray testsList;
  197. Box2DTestList testsListModel;
  198. Box2DRenderComponent renderComponent;
  199. ListBox testsListBox;
  200. TextEditor instructions;
  201. static Test* createTest (int index)
  202. {
  203. switch (index)
  204. {
  205. case addPair: return new AddPair();
  206. case applyForce: return new ApplyForce();
  207. case dominoes: return new Dominos();
  208. case chain: return new Chain();
  209. default: break;
  210. }
  211. return nullptr;
  212. }
  213. static String getInstructions (int index)
  214. {
  215. switch (index)
  216. {
  217. case applyForce:
  218. {
  219. String s;
  220. s << "Keys:" << newLine
  221. << newLine
  222. << "Left: \'a\'" << newLine
  223. << "Right: \'d\'" << newLine
  224. << "Forward: \'w\'";
  225. return s;
  226. }
  227. default:
  228. break;
  229. }
  230. return String();
  231. }
  232. void checkKeys()
  233. {
  234. if (renderComponent.currentTest == nullptr)
  235. return;
  236. checkKeyCode ('a');
  237. checkKeyCode ('w');
  238. checkKeyCode ('d');
  239. }
  240. void checkKeyCode (const int keyCode)
  241. {
  242. if (KeyPress::isKeyCurrentlyDown (keyCode))
  243. renderComponent.currentTest->Keyboard ((unsigned char) keyCode);
  244. }
  245. void timerCallback() override
  246. {
  247. if (renderComponent.currentTest == nullptr)
  248. return;
  249. grabKeyboardFocus();
  250. checkKeys();
  251. renderComponent.currentTest->m_world->Step (1.0f / 60.0f, 6, 2);
  252. repaint();
  253. }
  254. void changeListenerCallback (ChangeBroadcaster* source) override
  255. {
  256. if (source == &testsListModel)
  257. {
  258. const int index = testsListBox.getSelectedRow();
  259. renderComponent.currentTest = createTest (index);
  260. instructions.setText (getInstructions (index));
  261. repaint();
  262. }
  263. }
  264. static StringArray getTestsList()
  265. {
  266. const char* tests[] =
  267. {
  268. "Add Pair Stress Test",
  269. "Apply Force",
  270. "Dominoes",
  271. "Chain"
  272. };
  273. jassert (numElementsInArray (tests) == numTests);
  274. return StringArray (tests, numElementsInArray (tests));
  275. }
  276. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Box2DDemo)
  277. };
  278. // This static object will register this demo type in a global list of demos..
  279. static JuceDemoType<Box2DDemo> demo ("29 Graphics: Box 2D");