Skip to content

Commit

Permalink
Layout demo
Browse files Browse the repository at this point in the history
  • Loading branch information
FigBug committed Oct 13, 2024
1 parent fcb7bdd commit b7b1e59
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
128 changes: 128 additions & 0 deletions examples/Demo/Source/MainComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1855,9 +1855,137 @@ struct EquationParserDemo : public juce::Component
juce::TextEditor result;
};

//==============================================================================
juce::String layoutTxt = R"~~~(
{
"id": "root",
"children": [
{
"id": "a",
"x": 0,
"y": 0,
"w": "parW()",
"h": "parH() / 2 - 10",
"children": [
{
"id": "e",
"x": 5,
"y": 5,
"r": "parW() / 2 - 2",
"b": "parH() - 5"
},
{
"id": "f",
"x": "prevR() + 4",
"y": 5,
"r": "parW() - 5",
"b": "parH() - 5"
}
]
},
{
"id": "b",
"x": 0,
"y": "prevB() + 20",
"w": "parW()",
"b": "parH()",
"children": [
{
"id": "g",
"x": 5,
"y": 5,
"r": "parW() / 2 - 2",
"b": "parH() - 5"
},
{
"id": "h",
"x": "prevR() + 4",
"y": 5,
"r": "parW() - 5",
"b": "parH() - 5"
}
]
}
]
}
)~~~";

struct LayoutDemo : public juce::Component
{
LayoutDemo()
{
setName ("Layout");

auto a = new ColouredComponent ("a", juce::Colours::green);
auto b = new ColouredComponent ("b", juce::Colours::green);

a->addAndMakeVisible (new ColouredComponent ("e", juce::Colours::yellow));
a->addAndMakeVisible (new ColouredComponent ("f", juce::Colours::yellow));
b->addAndMakeVisible (new ColouredComponent ("g", juce::Colours::yellow));
b->addAndMakeVisible (new ColouredComponent ("h", juce::Colours::yellow));

layoutRoot.addAndMakeVisible (a);
layoutRoot.addAndMakeVisible (b);

layoutJson.setMultiLine (true);
layoutJson.setReturnKeyStartsNewLine (true);
layoutJson.setText (layoutTxt, juce::dontSendNotification);
layoutJson.onTextChange = [this]
{
auto t = layoutJson.getText();
auto o = juce::JSON::parse (t);

if (o.isObject())
resized();
};
addAndMakeVisible (layoutJson);
addAndMakeVisible (layoutRoot);
}

void resized() override
{
auto rc = getLocalBounds().reduced (8);

layoutJson.setBounds (rc.removeFromLeft (rc.getWidth() / 2 - 4));
rc.removeFromLeft (8);
layoutRoot.setBounds (rc);

layout.parseLayout (layoutJson.getText());
}

class ColouredComponent : public juce::Component
{
public:
ColouredComponent (const juce::String& n, const juce::Colour& c)
: juce::Component (n), colour (c)
{
}

~ColouredComponent() override
{
deleteAllChildren();
}

void paint (juce::Graphics& g) override
{
g.fillAll (colour.withMultipliedAlpha (0.8f));
g.setColour (juce::Colours::white);
g.drawText (getName(), getLocalBounds(), juce::Justification::centred);
}

juce::Colour colour;
};

juce::TextEditor layoutJson;
ColouredComponent layoutRoot { "root", juce::Colours::blue };

gin::Layout layout { layoutRoot };
};

//==============================================================================
MainContentComponent::MainContentComponent()
{
demoComponents.add (new LayoutDemo());
demoComponents.add (new EquationParserDemo());
demoComponents.add (new BLLTDemo());
demoComponents.add (new WavetableDemo());
Expand Down
2 changes: 1 addition & 1 deletion modules/gin_gui/utilities/gin_layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class Layout
#endif

void setLayout (const juce::String& filename, const juce::File& source = {});
bool parseLayout (const juce::String& content);

private:
void setupParser();

bool parseLayout (const juce::String& content);
int parse (const juce::var& equation, int equationIndex);

void doComponent (const juce::String& currentPath, const juce::var& components);
Expand Down

0 comments on commit b7b1e59

Please sign in to comment.