Created
December 18, 2019 12:24
-
-
Save tomfclarke/2ff7999cd554aa06a05dd2dcbc20510d to your computer and use it in GitHub Desktop.
JUCE PIP: FlexBox Wrap Rows By Half
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
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: FlexBoxWrapHalf | |
dependencies: juce_core, juce_data_structures, juce_events, juce_graphics, juce_gui_basics | |
exporters: vs2017, xcode_mac | |
moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1 | |
type: Component | |
mainClass: FlexBoxWrapHalf | |
END_JUCE_PIP_METADATA | |
*******************************************************************************/ | |
#pragma once | |
#include <array> | |
//============================================================================== | |
class FlexBoxWrapHalf : public Component | |
{ | |
public: | |
//============================================================================== | |
FlexBoxWrapHalf() | |
{ | |
for (auto& box : boxes) | |
{ | |
addAndMakeVisible (box); | |
box.setName (String (getNumChildComponents())); | |
} | |
setSize (820, 820); | |
} | |
~FlexBoxWrapHalf() | |
{ | |
} | |
//============================================================================== | |
void paint (Graphics& g) override | |
{ | |
g.fillAll (Colours::slategrey); | |
} | |
void resized() override | |
{ | |
constexpr auto size = 100; | |
FlexBox two0, two1, two2, two3; | |
two0.flexWrap = FlexBox::Wrap::wrap; | |
two1.flexWrap = FlexBox::Wrap::wrap; | |
two2.flexWrap = FlexBox::Wrap::wrap; | |
two3.flexWrap = FlexBox::Wrap::wrap; | |
two0.justifyContent = FlexBox::JustifyContent::spaceAround; | |
two1.justifyContent = FlexBox::JustifyContent::spaceAround; | |
two2.justifyContent = FlexBox::JustifyContent::spaceAround; | |
two3.justifyContent = FlexBox::JustifyContent::spaceAround; | |
two0.items.add (FlexItem (boxes[0]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
two0.items.add (FlexItem (boxes[1]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
two1.items.add (FlexItem (boxes[2]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
two1.items.add (FlexItem (boxes[3]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
two2.items.add (FlexItem (boxes[4]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
two2.items.add (FlexItem (boxes[5]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
two3.items.add (FlexItem (boxes[6]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
two3.items.add (FlexItem (boxes[7]).withMaxWidth (size).withMaxHeight (size).withMinWidth (size).withMinHeight (size)); | |
FlexBox four0, four1; | |
four0.flexWrap = FlexBox::Wrap::wrap; | |
four1.flexWrap = FlexBox::Wrap::wrap; | |
const auto fourmin = static_cast<float>(std::min (getWidth(), size * 2)); | |
four0.items.add (FlexItem (two0).withMinWidth (fourmin).withFlex (1.f)); | |
four0.items.add (FlexItem (two1).withMinWidth (fourmin).withFlex (1.f)); | |
four1.items.add (FlexItem (two2).withMinWidth (fourmin).withFlex (1.f)); | |
four1.items.add (FlexItem (two3).withMinWidth (fourmin).withFlex (1.f)); | |
FlexBox eight; | |
eight.flexWrap = FlexBox::Wrap::wrap; | |
const auto eightmin = static_cast<float>(std::min (getWidth(), size * 4)); | |
eight.items.add (FlexItem (four0).withMinWidth (eightmin).withFlex (1.f)); | |
eight.items.add (FlexItem (four1).withMinWidth (eightmin).withFlex (1.f)); | |
eight.performLayout (getLocalBounds()); | |
} | |
void parentHierarchyChanged() | |
{ | |
if (auto* parent = dynamic_cast<ResizableWindow*>(getParentComponent())) | |
{ | |
parent->setResizeLimits (100, 100, 900, 900); | |
} | |
} | |
private: | |
struct Box : public Component | |
{ | |
Box() | |
: colour (Colour (Random().nextInt()).withBrightness (4.f).withAlpha (1.f)) | |
{ | |
} | |
void paint (Graphics& g) override | |
{ | |
g.fillAll (colour); | |
g.setColour (Colours::black); | |
g.setFont (Font (20.f)); | |
g.drawFittedText (getName(), getLocalBounds(), Justification::centred, 1); | |
} | |
Colour colour; | |
}; | |
std::array<Box, 8> boxes; | |
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FlexBoxWrapHalf) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment