1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE examples.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    The code included in this file is provided under the terms of the ISC license
8    http://www.isc.org/downloads/software-support-policy/isc-license. Permission
9    To use, copy, modify, and/or distribute this software for any purpose with or
10    without fee is hereby granted provided that the above copyright notice and
11    this permission notice appear in all copies.
12 
13    THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
14    WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
15    PURPOSE, ARE DISCLAIMED.
16 
17   ==============================================================================
18 */
19 
20 /*******************************************************************************
21  The block below describes the properties of this PIP. A PIP is a short snippet
22  of code that can be read by the Projucer and used to generate a JUCE project.
23 
24  BEGIN_JUCE_PIP_METADATA
25 
26  name:             GridDemo
27  version:          1.0.0
28  vendor:           JUCE
29  website:          http://juce.com
30  description:      Responsive layouts using Grid.
31 
32  dependencies:     juce_core, juce_data_structures, juce_events, juce_graphics,
33                    juce_gui_basics
34  exporters:        xcode_mac, vs2019, linux_make, androidstudio, xcode_iphone
35 
36  moduleFlags:      JUCE_STRICT_REFCOUNTEDPOINTER=1
37 
38  type:             Component
39  mainClass:        GridDemo
40 
41  useLocalCopy:     1
42 
43  END_JUCE_PIP_METADATA
44 
45 *******************************************************************************/
46 
47 #pragma once
48 
49 #include "../Assets/DemoUtilities.h"
50 
51 //==============================================================================
52 struct GridDemo   : public Component
53 {
GridDemoGridDemo54     GridDemo()
55     {
56         addGridItemPanel (Colours::aquamarine, "0");
57         addGridItemPanel (Colours::red,        "1");
58         addGridItemPanel (Colours::blue,       "2");
59         addGridItemPanel (Colours::green,      "3");
60         addGridItemPanel (Colours::orange,     "4");
61         addGridItemPanel (Colours::white,      "5");
62         addGridItemPanel (Colours::aquamarine, "6");
63         addGridItemPanel (Colours::red,        "7");
64         addGridItemPanel (Colours::blue,       "8");
65         addGridItemPanel (Colours::green,      "9");
66         addGridItemPanel (Colours::orange,     "10");
67         addGridItemPanel (Colours::white,      "11");
68 
69         setSize (750, 750);
70     }
71 
addGridItemPanelGridDemo72     void addGridItemPanel (Colour colour, const char* text)
73     {
74         addAndMakeVisible (items.add (new GridItemPanel (colour, text)));
75     }
76 
paintGridDemo77     void paint (Graphics& g) override
78     {
79         g.fillAll (Colours::black);
80     }
81 
resizedGridDemo82     void resized() override
83     {
84         Grid grid;
85 
86         grid.rowGap    = 20_px;
87         grid.columnGap = 20_px;
88 
89         using Track = Grid::TrackInfo;
90 
91         grid.templateRows = { Track (1_fr), Track (1_fr), Track (1_fr) };
92 
93         grid.templateColumns = { Track (1_fr),
94                                  Track (1_fr),
95                                  Track (1_fr) };
96 
97 
98         grid.autoColumns = Track (1_fr);
99         grid.autoRows    = Track (1_fr);
100 
101         grid.autoFlow = Grid::AutoFlow::column;
102 
103         grid.items.addArray ({ GridItem (items[0]).withArea (2, 2, 4, 4),
104                                GridItem (items[1]),
105                                GridItem (items[2]).withArea ({}, 3),
106                                GridItem (items[3]),
107                                GridItem (items[4]).withArea (GridItem::Span (2), {}),
108                                GridItem (items[5]),
109                                GridItem (items[6]),
110                                GridItem (items[7]),
111                                GridItem (items[8]),
112                                GridItem (items[9]),
113                                GridItem (items[10]),
114                                GridItem (items[11])
115                             });
116 
117         grid.performLayout (getLocalBounds());
118     }
119 
120     //==============================================================================
121     struct GridItemPanel  : public Component
122     {
GridItemPanelGridDemo::GridItemPanel123         GridItemPanel (Colour colourToUse, const String& textToUse)
124             : colour (colourToUse),
125               text (textToUse)
126         {}
127 
paintGridDemo::GridItemPanel128         void paint (Graphics& g) override
129         {
130             g.fillAll (colour.withAlpha (0.5f));
131 
132             g.setColour (Colours::black);
133             g.drawText (text, getLocalBounds().withSizeKeepingCentre (100, 100), Justification::centred, false);
134         }
135 
136         Colour colour;
137         String text;
138     };
139 
140     OwnedArray<GridItemPanel> items;
141 };
142