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.

318 lines
13KB

  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: FlexBoxDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Responsive layouts using FlexBox.
  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: FlexBoxDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. struct DemoFlexPanel : public juce::Component
  36. {
  37. DemoFlexPanel (juce::Colour col, FlexItem& item)
  38. : flexItem (item), colour (col)
  39. {
  40. int x = 70;
  41. int y = 3;
  42. setupTextEditor (flexOrderEditor, { x, y, 20, 18 }, "0", [this] { flexItem.order = (int) flexOrderEditor.getText().getFloatValue(); });
  43. addLabel ("order", flexOrderEditor);
  44. y += 20;
  45. setupTextEditor (flexGrowEditor, { x, y, 20, 18 }, "0", [this] { flexItem.flexGrow = flexGrowEditor.getText().getFloatValue(); });
  46. addLabel ("flex-grow", flexGrowEditor);
  47. y += 20;
  48. setupTextEditor (flexShrinkEditor, { x, y, 20, 18 }, "1", [this] { flexItem.flexShrink = flexShrinkEditor.getText().getFloatValue(); });
  49. addLabel ("flex-shrink", flexShrinkEditor);
  50. y += 20;
  51. setupTextEditor (flexBasisEditor, { x, y, 33, 18 }, "100", [this] { flexItem.flexBasis = flexBasisEditor.getText().getFloatValue(); });
  52. addLabel ("flex-basis", flexBasisEditor);
  53. y += 20;
  54. alignSelfCombo.addItem ("auto", 1);
  55. alignSelfCombo.addItem ("flex-start", 2);
  56. alignSelfCombo.addItem ("flex-end", 3);
  57. alignSelfCombo.addItem ("center", 4);
  58. alignSelfCombo.addItem ("stretch", 5);
  59. alignSelfCombo.setBounds (x, y, 90, 18);
  60. alignSelfCombo.onChange = [this] { updateAssignSelf(); };
  61. alignSelfCombo.setSelectedId (5);
  62. alignSelfCombo.setColour (ComboBox::outlineColourId, Colours::transparentBlack);
  63. addAndMakeVisible (alignSelfCombo);
  64. addLabel ("align-self", alignSelfCombo);
  65. }
  66. void setupTextEditor (TextEditor& te, Rectangle<int> b, StringRef initialText, std::function<void()> updateFn)
  67. {
  68. te.setBounds (b);
  69. te.setText (initialText);
  70. te.onTextChange = [this, updateFn]
  71. {
  72. updateFn();
  73. refreshLayout();
  74. };
  75. addAndMakeVisible (te);
  76. }
  77. void addLabel (const String& name, Component& target)
  78. {
  79. auto label = new Label (name, name);
  80. label->attachToComponent (&target, true);
  81. labels.add (label);
  82. addAndMakeVisible (label);
  83. }
  84. void updateAssignSelf()
  85. {
  86. switch (alignSelfCombo.getSelectedId())
  87. {
  88. case 1: flexItem.alignSelf = FlexItem::AlignSelf::autoAlign; break;
  89. case 2: flexItem.alignSelf = FlexItem::AlignSelf::flexStart; break;
  90. case 3: flexItem.alignSelf = FlexItem::AlignSelf::flexEnd; break;
  91. case 4: flexItem.alignSelf = FlexItem::AlignSelf::center; break;
  92. case 5: flexItem.alignSelf = FlexItem::AlignSelf::stretch; break;
  93. }
  94. refreshLayout();
  95. }
  96. void refreshLayout()
  97. {
  98. if (auto parent = getParentComponent())
  99. parent->resized();
  100. }
  101. void paint (Graphics& g) override
  102. {
  103. auto r = getLocalBounds();
  104. g.setColour (colour);
  105. g.fillRect (r);
  106. g.setColour (Colours::black);
  107. g.drawFittedText ("w: " + String (r.getWidth()) + newLine + "h: " + String (r.getHeight()),
  108. r.reduced (4), Justification::bottomRight, 2);
  109. }
  110. void lookAndFeelChanged() override
  111. {
  112. flexOrderEditor .applyFontToAllText (flexOrderEditor .getFont());
  113. flexGrowEditor .applyFontToAllText (flexGrowEditor .getFont());
  114. flexShrinkEditor.applyFontToAllText (flexShrinkEditor.getFont());
  115. flexBasisEditor .applyFontToAllText (flexBasisEditor .getFont());
  116. }
  117. FlexItem& flexItem;
  118. TextEditor flexOrderEditor, flexGrowEditor, flexShrinkEditor, flexBasisEditor;
  119. ComboBox alignSelfCombo;
  120. juce::Colour colour;
  121. OwnedArray<Label> labels;
  122. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DemoFlexPanel)
  123. };
  124. //==============================================================================
  125. struct FlexBoxDemo : public juce::Component
  126. {
  127. FlexBoxDemo()
  128. {
  129. setupPropertiesPanel();
  130. setupFlexBoxItems();
  131. setSize (1000, 500);
  132. }
  133. void resized() override
  134. {
  135. flexBox.performLayout (getFlexBoxBounds());
  136. }
  137. Rectangle<float> getFlexBoxBounds() const
  138. {
  139. return getLocalBounds().withTrimmedLeft (300)
  140. .reduced (10)
  141. .toFloat();
  142. }
  143. void paint (Graphics& g) override
  144. {
  145. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground,
  146. Colours::lightgrey));
  147. g.setColour (Colours::white);
  148. g.fillRect (getFlexBoxBounds());
  149. }
  150. void setupPropertiesPanel()
  151. {
  152. auto directionGroup = addControl (new GroupComponent ("direction", "flex-direction"));
  153. directionGroup->setBounds (10, 30, 140, 110);
  154. int i = 0;
  155. int groupID = 1234;
  156. int leftMargin = 15;
  157. int topMargin = 45;
  158. createToggleButton ("row", groupID, leftMargin, topMargin + i++ * 22, true, [this] { flexBox.flexDirection = FlexBox::Direction::row; }).setToggleState (true, dontSendNotification);
  159. createToggleButton ("row-reverse", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.flexDirection = FlexBox::Direction::rowReverse; });
  160. createToggleButton ("column", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.flexDirection = FlexBox::Direction::column; });
  161. createToggleButton ("column-reverse", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.flexDirection = FlexBox::Direction::columnReverse; });
  162. auto wrapGroup = addControl (new GroupComponent ("wrap", "flex-wrap"));
  163. wrapGroup->setBounds (160, 30, 140, 110);
  164. i = 0;
  165. ++groupID;
  166. leftMargin = 165;
  167. createToggleButton ("nowrap", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.flexWrap = FlexBox::Wrap::noWrap; });
  168. createToggleButton ("wrap", groupID, leftMargin, topMargin + i++ * 22, true, [this] { flexBox.flexWrap = FlexBox::Wrap::wrap; });
  169. createToggleButton ("wrap-reverse", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.flexWrap = FlexBox::Wrap::wrapReverse; });
  170. auto justifyGroup = addControl (new GroupComponent ("justify", "justify-content"));
  171. justifyGroup->setBounds (10, 150, 140, 140);
  172. i = 0;
  173. ++groupID;
  174. leftMargin = 15;
  175. topMargin = 165;
  176. createToggleButton ("flex-start", groupID, leftMargin, topMargin + i++ * 22, true, [this] { flexBox.justifyContent = FlexBox::JustifyContent::flexStart; });
  177. createToggleButton ("flex-end", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.justifyContent = FlexBox::JustifyContent::flexEnd; });
  178. createToggleButton ("center", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.justifyContent = FlexBox::JustifyContent::center; });
  179. createToggleButton ("space-between", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.justifyContent = FlexBox::JustifyContent::spaceBetween; });
  180. createToggleButton ("space-around", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.justifyContent = FlexBox::JustifyContent::spaceAround; });
  181. auto alignGroup = addControl (new GroupComponent ("align", "align-items"));
  182. alignGroup->setBounds (160, 150, 140, 140);
  183. i = 0;
  184. ++groupID;
  185. leftMargin = 165;
  186. topMargin = 165;
  187. createToggleButton ("stretch", groupID, leftMargin, topMargin + i++ * 22, true, [this] { flexBox.alignItems = FlexBox::AlignItems::stretch; });
  188. createToggleButton ("flex-start", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignItems = FlexBox::AlignItems::flexStart; });
  189. createToggleButton ("flex-end", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignItems = FlexBox::AlignItems::flexEnd; });
  190. createToggleButton ("center", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignItems = FlexBox::AlignItems::center; });
  191. auto alignContentGroup = addControl (new GroupComponent ("content", "align-content"));
  192. alignContentGroup->setBounds (10, 300, 140, 160);
  193. i = 0;
  194. ++groupID;
  195. leftMargin = 15;
  196. topMargin = 315;
  197. createToggleButton ("stretch", groupID, leftMargin, topMargin + i++ * 22, true, [this] { flexBox.alignContent = FlexBox::AlignContent::stretch; });
  198. createToggleButton ("flex-start", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignContent = FlexBox::AlignContent::flexStart; });
  199. createToggleButton ("flex-end", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignContent = FlexBox::AlignContent::flexEnd; });
  200. createToggleButton ("center", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignContent = FlexBox::AlignContent::center; });
  201. createToggleButton ("space-between", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignContent = FlexBox::AlignContent::spaceBetween; });
  202. createToggleButton ("space-around", groupID, leftMargin, topMargin + i++ * 22, false, [this] { flexBox.alignContent = FlexBox::AlignContent::spaceAround; });
  203. }
  204. void setupFlexBoxItems()
  205. {
  206. addItem (Colours::orange);
  207. addItem (Colours::aqua);
  208. addItem (Colours::lightcoral);
  209. addItem (Colours::aquamarine);
  210. addItem (Colours::forestgreen);
  211. }
  212. void addItem (Colour colour)
  213. {
  214. flexBox.items.add (FlexItem (100, 150)
  215. .withMargin (10)
  216. .withWidth (200));
  217. auto& flexItem = flexBox.items.getReference (flexBox.items.size() - 1);
  218. auto panel = panels.add (new DemoFlexPanel (colour, flexItem));
  219. flexItem.associatedComponent = panel;
  220. addAndMakeVisible (panel);
  221. }
  222. ToggleButton& createToggleButton (StringRef text, int groupID, int x, int y, bool toggleOn, std::function<void()> fn)
  223. {
  224. auto* tb = buttons.add (new ToggleButton());
  225. tb->setButtonText (text);
  226. tb->setRadioGroupId (groupID);
  227. tb->setToggleState (toggleOn, dontSendNotification);
  228. tb->onClick = [this, fn]
  229. {
  230. fn();
  231. resized();
  232. };
  233. tb->setBounds (x, y, 130, 22);
  234. addAndMakeVisible (tb);
  235. return *tb;
  236. }
  237. template <typename ComponentType>
  238. ComponentType* addControl (ComponentType* newControlComp)
  239. {
  240. controls.add (newControlComp);
  241. addAndMakeVisible (newControlComp);
  242. return newControlComp;
  243. }
  244. FlexBox flexBox;
  245. OwnedArray<DemoFlexPanel> panels;
  246. OwnedArray<Component> controls;
  247. OwnedArray<ToggleButton> buttons;
  248. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FlexBoxDemo)
  249. };