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.

321 lines
13KB

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