|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2017 - ROLI Ltd.
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 5 End-User License
- Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
- 27th April 2017).
-
- End User License Agreement: www.juce.com/juce-5-licence
- Privacy Policy: www.juce.com/juce-5-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- struct Grid::SizeCalculation
- {
- static float getTotalAbsoluteSize (const juce::Array<Grid::TrackInfo>& tracks, Px gapSize) noexcept
- {
- float totalCellSize = 0.0f;
-
- for (const auto& trackInfo : tracks)
- if (! trackInfo.isFraction || trackInfo.hasKeyword)
- totalCellSize += trackInfo.size;
-
- float totalGap = tracks.size() > 1 ? static_cast<float> ((tracks.size() - 1) * gapSize.pixels)
- : 0.0f;
-
- return totalCellSize + totalGap;
- }
-
- static float getRelativeUnitSize (float size, float totalAbsolute, const juce::Array<Grid::TrackInfo>& tracks) noexcept
- {
- const float totalRelative = juce::jlimit (0.0f, size, size - totalAbsolute);
- float factorsSum = 0.0f;
-
- for (const auto& trackInfo : tracks)
- if (trackInfo.isFraction)
- factorsSum += trackInfo.size;
-
- jassert (factorsSum != 0.0f);
- return totalRelative / factorsSum;
- }
-
- //==============================================================================
- static float getTotalAbsoluteHeight (const juce::Array<Grid::TrackInfo>& rowTracks, Px rowGap)
- {
- return getTotalAbsoluteSize (rowTracks, rowGap);
- }
-
- static float getTotalAbsoluteWidth (const juce::Array<Grid::TrackInfo>& columnTracks, Px columnGap)
- {
- return getTotalAbsoluteSize (columnTracks, columnGap);
- }
-
- static float getRelativeWidthUnit (float gridWidth, Px columnGap, const juce::Array<Grid::TrackInfo>& columnTracks)
- {
- return getRelativeUnitSize (gridWidth, getTotalAbsoluteWidth (columnTracks, columnGap), columnTracks);
- }
-
- static float getRelativeHeightUnit (float gridHeight, Px rowGap, const juce::Array<Grid::TrackInfo>& rowTracks)
- {
- return getRelativeUnitSize (gridHeight, getTotalAbsoluteHeight (rowTracks, rowGap), rowTracks);
- }
-
- //==============================================================================
- static bool hasAnyFractions (const juce::Array<Grid::TrackInfo>& tracks)
- {
- for (auto& t : tracks)
- if (t.isFraction)
- return true;
-
- return false;
- }
-
- void computeSizes (float gridWidth, float gridHeight,
- Px columnGapToUse, Px rowGapToUse,
- const juce::Array<Grid::TrackInfo>& columnTracks,
- const juce::Array<Grid::TrackInfo>& rowTracks)
- {
- if (hasAnyFractions (columnTracks))
- relativeWidthUnit = getRelativeWidthUnit (gridWidth, columnGapToUse, columnTracks);
- else
- remainingWidth = gridWidth - getTotalAbsoluteSize (columnTracks, columnGapToUse);
-
- if (hasAnyFractions (rowTracks))
- relativeHeightUnit = getRelativeHeightUnit (gridHeight, rowGapToUse, rowTracks);
- else
- remainingHeight = gridHeight - getTotalAbsoluteSize (rowTracks, rowGapToUse);
- }
-
- float relativeWidthUnit = 0.0f;
- float relativeHeightUnit = 0.0f;
- float remainingWidth = 0.0f;
- float remainingHeight = 0.0f;
- };
-
- //==============================================================================
- struct Grid::PlacementHelpers
- {
- enum { invalid = -999999 };
- static constexpr auto emptyAreaCharacter = ".";
-
- //==============================================================================
- struct LineRange { int start, end; };
- struct LineArea { LineRange column, row; };
- struct LineInfo { juce::StringArray lineNames; };
-
- struct NamedArea
- {
- juce::String name;
- LineArea lines;
- };
-
- //==============================================================================
- static juce::Array<LineInfo> getArrayOfLinesFromTracks (const juce::Array<Grid::TrackInfo>& tracks)
- {
- // fill line info array
- juce::Array<LineInfo> lines;
-
- for (int i = 1; i <= tracks.size(); ++i)
- {
- const auto& currentTrack = tracks.getReference (i - 1);
-
- if (i == 1) // start line
- {
- LineInfo li;
- li.lineNames.add (currentTrack.startLineName);
- lines.add (li);
- }
-
- if (i > 1 && i <= tracks.size()) // two lines in between tracks
- {
- const auto& prevTrack = tracks.getReference (i - 2);
-
- LineInfo li;
- li.lineNames.add (prevTrack.endLineName);
- li.lineNames.add (currentTrack.startLineName);
-
- lines.add (li);
- }
-
- if (i == tracks.size()) // end line
- {
- LineInfo li;
- li.lineNames.add (currentTrack.endLineName);
- lines.add (li);
- }
- }
-
- jassert (lines.size() == tracks.size() + 1);
-
- return lines;
- }
-
- //==============================================================================
- static int deduceAbsoluteLineNumberFromLineName (GridItem::Property prop,
- const juce::Array<Grid::TrackInfo>& tracks)
- {
- jassert (prop.hasAbsolute());
-
- const auto lines = getArrayOfLinesFromTracks (tracks);
- int count = 0;
-
- for (int i = 0; i < lines.size(); i++)
- {
- for (const auto& name : lines.getReference (i).lineNames)
- {
- if (prop.name == name)
- {
- ++count;
- break;
- }
- }
-
- if (count == prop.number)
- return i + 1;
- }
-
- jassertfalse;
- return count;
- }
-
- static int deduceAbsoluteLineNumber (GridItem::Property prop,
- const juce::Array<Grid::TrackInfo>& tracks)
- {
- jassert (prop.hasAbsolute());
-
- if (prop.hasName())
- return deduceAbsoluteLineNumberFromLineName (prop, tracks);
-
- return prop.number > 0 ? prop.number : tracks.size() + 2 + prop.number;
- }
-
- static int deduceAbsoluteLineNumberFromNamedSpan (int startLineNumber,
- GridItem::Property propertyWithSpan,
- const juce::Array<Grid::TrackInfo>& tracks)
- {
- jassert (propertyWithSpan.hasSpan());
-
- const auto lines = getArrayOfLinesFromTracks (tracks);
- int count = 0;
-
- for (int i = startLineNumber; i < lines.size(); i++)
- {
- for (const auto& name : lines.getReference (i).lineNames)
- {
- if (propertyWithSpan.name == name)
- {
- ++count;
- break;
- }
- }
-
- if (count == propertyWithSpan.number)
- return i + 1;
- }
-
- jassertfalse;
- return count;
- }
-
- static int deduceAbsoluteLineNumberBasedOnSpan (int startLineNumber,
- GridItem::Property propertyWithSpan,
- const juce::Array<Grid::TrackInfo>& tracks)
- {
- jassert (propertyWithSpan.hasSpan());
-
- if (propertyWithSpan.hasName())
- return deduceAbsoluteLineNumberFromNamedSpan (startLineNumber, propertyWithSpan, tracks);
-
- return startLineNumber + propertyWithSpan.number;
- }
-
- //==============================================================================
- static LineRange deduceLineRange (GridItem::StartAndEndProperty prop, const juce::Array<Grid::TrackInfo>& tracks)
- {
- LineRange s;
-
- jassert (! (prop.start.hasAuto() && prop.end.hasAuto()));
-
- if (prop.start.hasAbsolute() && prop.end.hasAuto())
- {
- prop.end = GridItem::Span (1);
- }
- else if (prop.start.hasAuto() && prop.end.hasAbsolute())
- {
- prop.start = GridItem::Span (1);
- }
-
- if (prop.start.hasAbsolute() && prop.end.hasAbsolute())
- {
- s.start = deduceAbsoluteLineNumber (prop.start, tracks);
- s.end = deduceAbsoluteLineNumber (prop.end, tracks);
- }
- else if (prop.start.hasAbsolute() && prop.end.hasSpan())
- {
- s.start = deduceAbsoluteLineNumber (prop.start, tracks);
- s.end = deduceAbsoluteLineNumberBasedOnSpan (s.start, prop.end, tracks);
- }
- else if (prop.start.hasSpan() && prop.end.hasAbsolute())
- {
- s.start = deduceAbsoluteLineNumber (prop.end, tracks);
- s.end = deduceAbsoluteLineNumberBasedOnSpan (s.start, prop.start, tracks);
- }
- else
- {
- // Can't have an item with spans on both start and end.
- jassertfalse;
- s.start = s.end = {};
- }
-
- // swap if start overtakes end
- if (s.start > s.end)
- std::swap (s.start, s.end);
- else if (s.start == s.end)
- s.end = s.start + 1;
-
- return s;
- }
-
- static LineArea deduceLineArea (const GridItem& item,
- const Grid& grid,
- const std::map<juce::String, LineArea>& namedAreas)
- {
- if (item.area.isNotEmpty() && ! grid.templateAreas.isEmpty())
- return namedAreas.at (item.area);
-
- return { deduceLineRange (item.column, grid.templateColumns),
- deduceLineRange (item.row, grid.templateRows) };
- }
-
- //==============================================================================
- static juce::Array<juce::StringArray> parseAreasProperty (const juce::StringArray& areasStrings)
- {
- juce::Array<juce::StringArray> strings;
-
- for (const auto& areaString : areasStrings)
- strings.add (juce::StringArray::fromTokens (areaString, false));
-
- if (strings.size() > 0)
- for (auto s : strings)
- jassert (s.size() == strings[0].size()); // all rows must have the same number of columns
-
- return strings;
- }
-
- static NamedArea findArea (juce::Array<juce::StringArray>& stringsArrays)
- {
- NamedArea area;
-
- for (auto& stringArray : stringsArrays)
- {
- for (auto& string : stringArray)
- {
- // find anchor
- if (area.name.isEmpty())
- {
- if (string != emptyAreaCharacter)
- {
- area.name = string;
- area.lines.row.start = stringsArrays.indexOf (stringArray) + 1; // non-zero indexed;
- area.lines.column.start = stringArray.indexOf (string) + 1; // non-zero indexed;
-
- area.lines.row.end = stringsArrays.indexOf (stringArray) + 2;
- area.lines.column.end = stringArray.indexOf (string) + 2;
-
- // mark as visited
- string = emptyAreaCharacter;
- }
- }
- else
- {
- if (string == emptyAreaCharacter)
- {
- break;
- }
- else if (string == area.name)
- {
- area.lines.row.end = stringsArrays.indexOf (stringArray) + 2;
- area.lines.column.end = stringArray.indexOf (string) + 2;
-
- // mark as visited
- string = emptyAreaCharacter;
- }
- }
- }
- }
-
- return area;
- }
-
- //==============================================================================
- static std::map<juce::String, LineArea> deduceNamedAreas (const juce::StringArray& areasStrings)
- {
- auto stringsArrays = parseAreasProperty (areasStrings);
-
- std::map<juce::String, LineArea> areas;
-
- for (auto area = findArea (stringsArrays); area.name.isNotEmpty(); area = findArea (stringsArrays))
- {
- if (areas.count (area.name) == 0)
- areas[area.name] = area.lines;
- else
- // Make sure your template-areas property only has one area with the same name and is well-formed
- jassertfalse;
- }
-
- return areas;
- }
-
- //==============================================================================
- static float getCoord (int trackNumber, float relativeUnit, Px gap, const juce::Array<Grid::TrackInfo>& tracks)
- {
- float c = 0;
-
- for (const auto* it = tracks.begin(); it != tracks.begin() + trackNumber - 1; ++it)
- c += (it->isFraction ? it->size * relativeUnit : it->size) + static_cast<float> (gap.pixels);
-
- return c;
- }
-
- static juce::Rectangle<float> getCellBounds (int columnNumber, int rowNumber,
- const juce::Array<Grid::TrackInfo>& columnTracks,
- const juce::Array<Grid::TrackInfo>& rowTracks,
- Grid::SizeCalculation calculation,
- Px columnGap, Px rowGap)
- {
- jassert (columnNumber >= 1 && columnNumber <= columnTracks.size());
- jassert (rowNumber >= 1 && rowNumber <= rowTracks.size());
-
- const auto x = getCoord (columnNumber, calculation.relativeWidthUnit, columnGap, columnTracks);
- const auto y = getCoord (rowNumber, calculation.relativeHeightUnit, rowGap, rowTracks);
-
- const auto& columnTrackInfo = columnTracks.getReference (columnNumber - 1);
- const float width = columnTrackInfo.isFraction ? columnTrackInfo.size * calculation.relativeWidthUnit
- : columnTrackInfo.size;
-
- const auto& rowTrackInfo = rowTracks.getReference (rowNumber - 1);
- const float height = rowTrackInfo.isFraction ? rowTrackInfo.size * calculation.relativeHeightUnit
- : rowTrackInfo.size;
-
- return { x, y, width, height };
- }
-
- static juce::Rectangle<float> alignCell (juce::Rectangle<float> area,
- int columnNumber, int rowNumber,
- int numberOfColumns, int numberOfRows,
- Grid::SizeCalculation calculation,
- Grid::AlignContent alignContent,
- Grid::JustifyContent justifyContent)
- {
- if (alignContent == Grid::AlignContent::end)
- area.setY (area.getY() + calculation.remainingHeight);
-
- if (justifyContent == Grid::JustifyContent::end)
- area.setX (area.getX() + calculation.remainingWidth);
-
- if (alignContent == Grid::AlignContent::center)
- area.setY (area.getY() + calculation.remainingHeight / 2);
-
- if (justifyContent == Grid::JustifyContent::center)
- area.setX (area.getX() + calculation.remainingWidth / 2);
-
- if (alignContent == Grid::AlignContent::spaceBetween)
- {
- const auto shift = ((rowNumber - 1) * (calculation.remainingHeight / float(numberOfRows - 1)));
- area.setY (area.getY() + shift);
- }
-
- if (justifyContent == Grid::JustifyContent::spaceBetween)
- {
- const auto shift = ((columnNumber - 1) * (calculation.remainingWidth / float(numberOfColumns - 1)));
- area.setX (area.getX() + shift);
- }
-
- if (alignContent == Grid::AlignContent::spaceEvenly)
- {
- const auto shift = (rowNumber * (calculation.remainingHeight / float(numberOfRows + 1)));
- area.setY (area.getY() + shift);
- }
-
- if (justifyContent == Grid::JustifyContent::spaceEvenly)
- {
- const auto shift = (columnNumber * (calculation.remainingWidth / float(numberOfColumns + 1)));
- area.setX (area.getX() + shift);
- }
-
- if (alignContent == Grid::AlignContent::spaceAround)
- {
- const auto inbetweenShift = calculation.remainingHeight / float(numberOfRows);
- const auto sidesShift = inbetweenShift / 2;
- auto shift = (rowNumber - 1) * inbetweenShift + sidesShift;
-
- area.setY (area.getY() + shift);
- }
-
- if (justifyContent == Grid::JustifyContent::spaceAround)
- {
- const auto inbetweenShift = calculation.remainingWidth / float(numberOfColumns);
- const auto sidesShift = inbetweenShift / 2;
- auto shift = (columnNumber - 1) * inbetweenShift + sidesShift;
-
- area.setX (area.getX() + shift);
- }
-
- return area;
- }
-
- static juce::Rectangle<float> getAreaBounds (int columnLineNumberStart, int columnLineNumberEnd,
- int rowLineNumberStart, int rowLineNumberEnd,
- const juce::Array<Grid::TrackInfo>& columnTracks,
- const juce::Array<Grid::TrackInfo>& rowTracks,
- Grid::SizeCalculation calculation,
- Grid::AlignContent alignContent,
- Grid::JustifyContent justifyContent,
- Px columnGap, Px rowGap)
- {
- auto startCell = getCellBounds (columnLineNumberStart, rowLineNumberStart,
- columnTracks, rowTracks,
- calculation,
- columnGap, rowGap);
-
- auto endCell = getCellBounds (columnLineNumberEnd - 1, rowLineNumberEnd - 1,
- columnTracks, rowTracks,
- calculation,
- columnGap, rowGap);
-
- startCell = alignCell (startCell,
- columnLineNumberStart, rowLineNumberStart,
- columnTracks.size(), rowTracks.size(),
- calculation,
- alignContent,
- justifyContent);
-
- endCell = alignCell (endCell,
- columnLineNumberEnd - 1, rowLineNumberEnd - 1,
- columnTracks.size(), rowTracks.size(),
- calculation,
- alignContent,
- justifyContent);
-
- return startCell.getUnion (endCell);
- }
- };
-
- //==============================================================================
- struct Grid::AutoPlacement
- {
- using ItemPlacementArray = juce::Array<std::pair<GridItem*, Grid::PlacementHelpers::LineArea>>;
-
- //==============================================================================
- struct OccupancyPlane
- {
- struct Cell { int column, row; };
-
- OccupancyPlane (int highestColumnToUse, int highestRowToUse, bool isColoumnFirst)
- : highestCrossDimension (isColoumnFirst ? highestRowToUse : highestColumnToUse),
- columnFirst (isColoumnFirst)
- {}
-
- Grid::PlacementHelpers::LineArea setCell (Cell cell, int columnSpan, int rowSpan)
- {
- for (int i = 0; i < columnSpan; i++)
- for (int j = 0; j < rowSpan; j++)
- setCell (cell.column + i, cell.row + j);
-
- return { { cell.column, cell.column + columnSpan }, { cell.row, cell.row + rowSpan } };
- }
-
- Grid::PlacementHelpers::LineArea setCell (Cell start, Cell end)
- {
- return setCell (start, std::abs (end.column - start.column),
- std::abs (end.row - start.row));
- }
-
- Cell nextAvailable (Cell referenceCell, int columnSpan, int rowSpan)
- {
- while (isOccupied (referenceCell, columnSpan, rowSpan) || isOutOfBounds (referenceCell, columnSpan, rowSpan))
- referenceCell = advance (referenceCell);
-
- return referenceCell;
- }
-
- Cell nextAvailableOnRow (Cell referenceCell, int columnSpan, int rowSpan, int rowNumber)
- {
- if (columnFirst && (rowNumber + rowSpan) > highestCrossDimension)
- highestCrossDimension = rowNumber + rowSpan;
-
- while (isOccupied (referenceCell, columnSpan, rowSpan)
- || (referenceCell.row != rowNumber))
- referenceCell = advance (referenceCell);
-
- return referenceCell;
- }
-
- Cell nextAvailableOnColumn (Cell referenceCell, int columnSpan, int rowSpan, int columnNumber)
- {
- if (! columnFirst && (columnNumber + columnSpan) > highestCrossDimension)
- highestCrossDimension = columnNumber + columnSpan;
-
- while (isOccupied (referenceCell, columnSpan, rowSpan)
- || (referenceCell.column != columnNumber))
- referenceCell = advance (referenceCell);
-
- return referenceCell;
- }
-
- private:
- struct SortableCell
- {
- int column, row;
- bool columnFirst;
-
- bool operator< (const SortableCell& other) const
- {
- if (columnFirst)
- {
- if (row == other.row)
- return column < other.column;
-
- return row < other.row;
- }
-
- if (row == other.row)
- return column < other.column;
-
- return row < other.row;
- }
- };
-
- void setCell (int column, int row)
- {
- occupiedCells.insert ({ column, row, columnFirst });
- }
-
- bool isOccupied (Cell cell) const
- {
- return occupiedCells.count ({ cell.column, cell.row, columnFirst }) > 0;
- }
-
- bool isOccupied (Cell cell, int columnSpan, int rowSpan) const
- {
- for (int i = 0; i < columnSpan; i++)
- for (int j = 0; j < rowSpan; j++)
- if (isOccupied ({ cell.column + i, cell.row + j }))
- return true;
-
- return false;
- }
-
- bool isOutOfBounds (Cell cell, int columnSpan, int rowSpan) const
- {
- const auto crossSpan = columnFirst ? rowSpan : columnSpan;
-
- return (getCrossDimension (cell) + crossSpan) > getHighestCrossDimension();
- }
-
- int getHighestCrossDimension() const
- {
- Cell cell { 1, 1 };
-
- if (occupiedCells.size() > 0)
- cell = { occupiedCells.crbegin()->column, occupiedCells.crbegin()->row };
-
- return std::max (getCrossDimension (cell), highestCrossDimension);
- }
-
- Cell advance (Cell cell) const
- {
- if ((getCrossDimension (cell) + 1) >= getHighestCrossDimension())
- return fromDimensions (getMainDimension (cell) + 1, 1);
-
- return fromDimensions (getMainDimension (cell), getCrossDimension (cell) + 1);
- }
-
- int getMainDimension (Cell cell) const { return columnFirst ? cell.column : cell.row; }
- int getCrossDimension (Cell cell) const { return columnFirst ? cell.row : cell.column; }
-
- Cell fromDimensions (int mainDimension, int crossDimension) const
- {
- if (columnFirst)
- return { mainDimension, crossDimension };
-
- return { crossDimension, mainDimension };
- }
-
- int highestCrossDimension;
- bool columnFirst;
- std::set<SortableCell> occupiedCells;
- };
-
- //==============================================================================
- static bool isFixed (GridItem::StartAndEndProperty prop)
- {
- return prop.start.hasName() || prop.start.hasAbsolute() || prop.end.hasName() || prop.end.hasAbsolute();
- }
-
- static bool hasFullyFixedPlacement (const GridItem& item)
- {
- if (item.area.isNotEmpty())
- return true;
-
- if (isFixed (item.column) && isFixed (item.row))
- return true;
-
- return false;
- }
-
- static bool hasPartialFixedPlacement (const GridItem& item)
- {
- if (item.area.isNotEmpty())
- return false;
-
- if (isFixed (item.column) ^ isFixed (item.row))
- return true;
-
- return false;
- }
-
- static bool hasAutoPlacement (const GridItem& item)
- {
- return ! hasFullyFixedPlacement (item) && ! hasPartialFixedPlacement (item);
- }
-
- //==============================================================================
- static bool hasDenseAutoFlow (Grid::AutoFlow autoFlow)
- {
- return autoFlow == Grid::AutoFlow::columnDense
- || autoFlow == Grid::AutoFlow::rowDense;
- }
-
- static bool isColumnAutoFlow (Grid::AutoFlow autoFlow)
- {
- return autoFlow == Grid::AutoFlow::column
- || autoFlow == Grid::AutoFlow::columnDense;
- }
-
- //==============================================================================
- static int getSpanFromAuto (GridItem::StartAndEndProperty prop)
- {
- if (prop.end.hasSpan())
- return prop.end.number;
-
- if (prop.start.hasSpan())
- return prop.start.number;
-
- return 1;
- }
-
- //==============================================================================
- ItemPlacementArray deduceAllItems (Grid& grid) const
- {
- const auto namedAreas = Grid::PlacementHelpers::deduceNamedAreas (grid.templateAreas);
-
- OccupancyPlane plane (juce::jmax (grid.templateColumns.size() + 1, 2),
- juce::jmax (grid.templateRows.size() + 1, 2),
- isColumnAutoFlow (grid.autoFlow));
-
- ItemPlacementArray itemPlacementArray;
- juce::Array<GridItem*> sortedItems;
-
- for (auto& item : grid.items)
- sortedItems.add (&item);
-
- sortedItems.sort (*this, true);
-
- // place fixed items first
- for (auto* item : sortedItems)
- {
- if (hasFullyFixedPlacement (*item))
- {
- const auto a = Grid::PlacementHelpers::deduceLineArea (*item, grid, namedAreas);
- plane.setCell ({ a.column.start, a.row.start }, { a.column.end, a.row.end });
- itemPlacementArray.add ({ item, a });
- }
- }
-
- OccupancyPlane::Cell lastInsertionCell = { 1, 1 };
-
- for (auto* item : sortedItems)
- {
- if (hasPartialFixedPlacement (*item))
- {
- if (isFixed (item->column))
- {
- const auto p = Grid::PlacementHelpers::deduceLineRange (item->column, grid.templateColumns);
- const auto columnSpan = std::abs (p.start - p.end);
- const auto rowSpan = getSpanFromAuto (item->row);
-
- const auto insertionCell = hasDenseAutoFlow (grid.autoFlow) ? OccupancyPlane::Cell { p.start, 1 }
- : lastInsertionCell;
- const auto nextAvailableCell = plane.nextAvailableOnColumn (insertionCell, columnSpan, rowSpan, p.start);
- const auto lineArea = plane.setCell (nextAvailableCell, columnSpan, rowSpan);
- lastInsertionCell = nextAvailableCell;
-
- itemPlacementArray.add ({ item, lineArea });
- }
- else if (isFixed (item->row))
- {
- const auto p = Grid::PlacementHelpers::deduceLineRange (item->row, grid.templateRows);
- const auto columnSpan = getSpanFromAuto (item->column);
- const auto rowSpan = std::abs (p.start - p.end);
-
- const auto insertionCell = hasDenseAutoFlow (grid.autoFlow) ? OccupancyPlane::Cell { 1, p.start }
- : lastInsertionCell;
-
- const auto nextAvailableCell = plane.nextAvailableOnRow (insertionCell, columnSpan, rowSpan, p.start);
- const auto lineArea = plane.setCell (nextAvailableCell, columnSpan, rowSpan);
-
- lastInsertionCell = nextAvailableCell;
-
- itemPlacementArray.add ({ item, lineArea });
- }
- }
- }
-
- lastInsertionCell = { 1, 1 };
-
- for (auto* item : sortedItems)
- {
- if (hasAutoPlacement (*item))
- {
- const auto columnSpan = getSpanFromAuto (item->column);
- const auto rowSpan = getSpanFromAuto (item->row);
-
- const auto nextAvailableCell = plane.nextAvailable (lastInsertionCell, columnSpan, rowSpan);
- const auto lineArea = plane.setCell (nextAvailableCell, columnSpan, rowSpan);
-
- if (! hasDenseAutoFlow (grid.autoFlow))
- lastInsertionCell = nextAvailableCell;
-
- itemPlacementArray.add ({ item, lineArea });
- }
- }
-
- return itemPlacementArray;
- }
-
- //==============================================================================
- static std::pair<int, int> getHighestEndLinesNumbers (const ItemPlacementArray& items)
- {
- int columnEndLine = 1;
- int rowEndLine = 1;
-
- for (auto& item : items)
- {
- const auto p = item.second;
- columnEndLine = std::max (p.column.end, columnEndLine);
- rowEndLine = std::max (p.row.end, rowEndLine);
- }
-
- return { columnEndLine, rowEndLine };
- }
-
- static std::pair<juce::Array<TrackInfo>, juce::Array<TrackInfo>> createImplicitTracks (const Grid& grid,
- const ItemPlacementArray& items)
- {
- const auto columnAndRowLineEnds = getHighestEndLinesNumbers (items);
-
- juce::Array<TrackInfo> implicitColumnTracks, implicitRowTracks;
-
- for (int i = grid.templateColumns.size() + 1; i < columnAndRowLineEnds.first; i++)
- implicitColumnTracks.add (grid.autoColumns);
-
- for (int i = grid.templateRows.size() + 1; i < columnAndRowLineEnds.second; i++)
- implicitRowTracks.add (grid.autoRows);
-
- return { implicitColumnTracks, implicitRowTracks };
- }
-
- //==============================================================================
- static int compareElements (const GridItem* i1, const GridItem* i2) noexcept
- {
- return i1->order < i2->order ? -1 : (i2->order < i1->order ? 1 : 0);
- }
-
- //==============================================================================
- static void applySizeForAutoTracks (juce::Array<Grid::TrackInfo>& columns,
- juce::Array<Grid::TrackInfo>& rows,
- const ItemPlacementArray& itemPlacementArray)
- {
- auto isSpan = [](Grid::PlacementHelpers::LineRange r) -> bool { return std::abs (r.end - r.start) > 1; };
-
- auto getHighestItemOnRow = [isSpan](int rowNumber, const ItemPlacementArray& itemPlacementArrayToUse) -> float
- {
- float highestRowSize = 0.0f;
-
- for (const auto& i : itemPlacementArrayToUse)
- if (! isSpan (i.second.row) && i.second.row.start == rowNumber)
- highestRowSize = std::max (highestRowSize, i.first->height + i.first->margin.top + i.first->margin.bottom);
-
- return highestRowSize;
- };
-
- auto getHighestItemOnColumn = [isSpan](int rowNumber, const ItemPlacementArray& itemPlacementArrayToUse) -> float
- {
- float highestColumnSize = 0.0f;
- for (const auto& i : itemPlacementArrayToUse)
- if (! isSpan (i.second.column) && i.second.column.start == rowNumber)
- highestColumnSize = std::max (highestColumnSize, i.first->width + i.first->margin.left + i.first->margin.right);
-
- return highestColumnSize;
- };
-
- for (int i = 0; i < rows.size(); i++)
- if (rows.getReference (i).hasKeyword)
- rows.getReference (i).size = getHighestItemOnRow (i + 1, itemPlacementArray);
-
- for (int i = 0; i < columns.size(); i++)
- if (columns.getReference (i).hasKeyword)
- columns.getReference (i).size = getHighestItemOnColumn (i + 1, itemPlacementArray);
- }
- };
-
- //==============================================================================
- struct Grid::BoxAlignment
- {
- static juce::Rectangle<float> alignItem (const GridItem& item,
- const Grid& grid,
- juce::Rectangle<float> area)
- {
- // if item align is auto, inherit value from grid
- Grid::AlignItems alignType = Grid::AlignItems::start;
- Grid::JustifyItems justifyType = Grid::JustifyItems::start;
-
- if (item.alignSelf == GridItem::AlignSelf::autoValue)
- alignType = grid.alignItems;
- else
- alignType = static_cast<Grid::AlignItems> (item.alignSelf);
-
- if (item.justifySelf == GridItem::JustifySelf::autoValue)
- justifyType = grid.justifyItems;
- else
- justifyType = static_cast<Grid::JustifyItems> (item.justifySelf);
-
- // subtract margin from area
- area = juce::BorderSize<float> (item.margin.top, item.margin.left, item.margin.bottom, item.margin.right)
- .subtractedFrom (area);
-
- // align and justify
- auto r = area;
-
- if (item.width != GridItem::notAssigned)
- r.setWidth (item.width);
-
- if (item.height != GridItem::notAssigned)
- r.setHeight (item.height);
-
- if (alignType == Grid::AlignItems::start && justifyType == Grid::JustifyItems::start)
- return r;
-
- if (alignType == Grid::AlignItems::end)
- r.setY (r.getY() + (area.getHeight() - r.getHeight()));
-
- if (justifyType == Grid::JustifyItems::end)
- r.setX (r.getX() + (area.getWidth() - r.getWidth()));
-
- if (alignType == Grid::AlignItems::center)
- r.setCentre (r.getCentreX(), area.getCentreY());
-
- if (justifyType == Grid::JustifyItems::center)
- r.setCentre (area.getCentreX(), r.getCentreY());
-
- return r;
- }
- };
-
- //==============================================================================
- Grid::TrackInfo::TrackInfo() noexcept : hasKeyword (true) {}
-
- Grid::TrackInfo::TrackInfo (Px sizeInPixels) noexcept : size (static_cast<float> (sizeInPixels.pixels)), isFraction (false) {}
-
- Grid::TrackInfo::TrackInfo (Fr fractionOfFreeSpace) noexcept : size ((float)fractionOfFreeSpace.fraction), isFraction (true) {}
-
- Grid::TrackInfo::TrackInfo (Px sizeInPixels, const juce::String& endLineNameToUse) noexcept : Grid::TrackInfo (sizeInPixels)
- {
- endLineName = endLineNameToUse;
- }
-
- Grid::TrackInfo::TrackInfo (Fr fractionOfFreeSpace, const juce::String& endLineNameToUse) noexcept : Grid::TrackInfo (fractionOfFreeSpace)
- {
- endLineName = endLineNameToUse;
- }
-
- Grid::TrackInfo::TrackInfo (const juce::String& startLineNameToUse, Px sizeInPixels) noexcept : Grid::TrackInfo (sizeInPixels)
- {
- startLineName = startLineNameToUse;
- }
-
- Grid::TrackInfo::TrackInfo (const juce::String& startLineNameToUse, Fr fractionOfFreeSpace) noexcept : Grid::TrackInfo (fractionOfFreeSpace)
- {
- startLineName = startLineNameToUse;
- }
-
- Grid::TrackInfo::TrackInfo (const juce::String& startLineNameToUse, Px sizeInPixels, const juce::String& endLineNameToUse) noexcept
- : Grid::TrackInfo (startLineNameToUse, sizeInPixels)
- {
- endLineName = endLineNameToUse;
- }
-
- Grid::TrackInfo::TrackInfo (const juce::String& startLineNameToUse, Fr fractionOfFreeSpace, const juce::String& endLineNameToUse) noexcept
- : Grid::TrackInfo (startLineNameToUse, fractionOfFreeSpace)
- {
- endLineName = endLineNameToUse;
- }
-
- //==============================================================================
- Grid::Grid() noexcept {}
- Grid::~Grid() noexcept {}
-
- //==============================================================================
- void Grid::performLayout (juce::Rectangle<int> targetArea)
- {
- const auto itemsAndAreas = Grid::AutoPlacement().deduceAllItems (*this);
-
- const auto implicitTracks = Grid::AutoPlacement::createImplicitTracks (*this, itemsAndAreas);
- auto columnTracks = templateColumns;
- auto rowTracks = templateRows;
- columnTracks.addArray (implicitTracks.first);
- rowTracks.addArray (implicitTracks.second);
-
- Grid::AutoPlacement::applySizeForAutoTracks (columnTracks, rowTracks, itemsAndAreas);
-
- Grid::SizeCalculation calculation;
- calculation.computeSizes (targetArea.toFloat().getWidth(),
- targetArea.toFloat().getHeight(),
- columnGap,
- rowGap,
- columnTracks,
- rowTracks);
-
- for (auto& itemAndArea : itemsAndAreas)
- {
- const auto a = itemAndArea.second;
- const auto areaBounds = Grid::PlacementHelpers::getAreaBounds (a.column.start, a.column.end,
- a.row.start, a.row.end,
- columnTracks,
- rowTracks,
- calculation,
- alignContent,
- justifyContent,
- columnGap,
- rowGap);
-
- auto* item = itemAndArea.first;
- item->currentBounds = Grid::BoxAlignment::alignItem (*item, *this, areaBounds)
- + targetArea.toFloat().getPosition();
-
- if (auto* c = item->associatedComponent)
- c->setBounds (item->currentBounds.toNearestInt());
- }
- }
-
- } // namespace juce
|