/* ============================================================================== This file is part of the JUCE examples. Copyright (c) 2020 - Raw Material Software Limited The code included in this file is provided under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license. Permission To use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE DISCLAIMED. ============================================================================== */ /******************************************************************************* The block below describes the properties of this PIP. A PIP is a short snippet of code that can be read by the Projucer and used to generate a JUCE project. BEGIN_JUCE_PIP_METADATA name: FlexBoxDemo version: 1.0.0 vendor: JUCE website: http://juce.com description: Responsive layouts using FlexBox. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics, juce_gui_basics exporters: xcode_mac, vs2019, linux_make, androidstudio, xcode_iphone moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1 type: Component mainClass: FlexBoxDemo useLocalCopy: 1 END_JUCE_PIP_METADATA *******************************************************************************/ #pragma once #include "../Assets/DemoUtilities.h" //============================================================================== struct DemoFlexPanel : public Component { DemoFlexPanel (Colour col, FlexItem& item) : flexItem (item), colour (col) { int x = 70; int y = 3; setupTextEditor (flexOrderEditor, { x, y, 20, 18 }, "0", [this] { flexItem.order = (int) flexOrderEditor.getText().getFloatValue(); }); addLabel ("order", flexOrderEditor); y += 20; setupTextEditor (flexGrowEditor, { x, y, 20, 18 }, "0", [this] { flexItem.flexGrow = flexGrowEditor.getText().getFloatValue(); }); addLabel ("flex-grow", flexGrowEditor); y += 20; setupTextEditor (flexShrinkEditor, { x, y, 20, 18 }, "1", [this] { flexItem.flexShrink = flexShrinkEditor.getText().getFloatValue(); }); addLabel ("flex-shrink", flexShrinkEditor); y += 20; setupTextEditor (flexBasisEditor, { x, y, 33, 18 }, "100", [this] { flexItem.flexBasis = flexBasisEditor.getText().getFloatValue(); }); addLabel ("flex-basis", flexBasisEditor); y += 20; alignSelfCombo.addItem ("auto", 1); alignSelfCombo.addItem ("flex-start", 2); alignSelfCombo.addItem ("flex-end", 3); alignSelfCombo.addItem ("center", 4); alignSelfCombo.addItem ("stretch", 5); alignSelfCombo.setBounds (x, y, 90, 18); alignSelfCombo.onChange = [this] { updateAssignSelf(); }; alignSelfCombo.setSelectedId (5); alignSelfCombo.setColour (ComboBox::outlineColourId, Colours::transparentBlack); addAndMakeVisible (alignSelfCombo); addLabel ("align-self", alignSelfCombo); } void setupTextEditor (TextEditor& te, Rectangle b, StringRef initialText, std::function updateFn) { te.setBounds (b); te.setText (initialText); te.onTextChange = [this, updateFn] { updateFn(); refreshLayout(); }; addAndMakeVisible (te); } void addLabel (const String& name, Component& target) { auto label = new Label (name, name); label->attachToComponent (&target, true); labels.add (label); addAndMakeVisible (label); } void updateAssignSelf() { switch (alignSelfCombo.getSelectedId()) { case 1: flexItem.alignSelf = FlexItem::AlignSelf::autoAlign; break; case 2: flexItem.alignSelf = FlexItem::AlignSelf::flexStart; break; case 3: flexItem.alignSelf = FlexItem::AlignSelf::flexEnd; break; case 4: flexItem.alignSelf = FlexItem::AlignSelf::center; break; case 5: flexItem.alignSelf = FlexItem::AlignSelf::stretch; break; default: break; } refreshLayout(); } void refreshLayout() { if (auto parent = getParentComponent()) parent->resized(); } void paint (Graphics& g) override { auto r = getLocalBounds(); g.setColour (colour); g.fillRect (r); g.setColour (Colours::black); g.drawFittedText ("w: " + String (r.getWidth()) + newLine + "h: " + String (r.getHeight()), r.reduced (4), Justification::bottomRight, 2); } void lookAndFeelChanged() override { flexOrderEditor .applyFontToAllText (flexOrderEditor .getFont()); flexGrowEditor .applyFontToAllText (flexGrowEditor .getFont()); flexShrinkEditor.applyFontToAllText (flexShrinkEditor.getFont()); flexBasisEditor .applyFontToAllText (flexBasisEditor .getFont()); } FlexItem& flexItem; TextEditor flexOrderEditor, flexGrowEditor, flexShrinkEditor, flexBasisEditor; ComboBox alignSelfCombo; Colour colour; OwnedArray