Search in sources :

Example 26 with Point

use of de.johni0702.minecraft.gui.utils.lwjgl.Point in project jGui by ReplayMod.

the class GridLayout method layOut.

@Override
public Map<GuiElement, Pair<ReadablePoint, ReadableDimension>> layOut(GuiContainer<?> container, ReadableDimension size) {
    Preconditions.checkState(columns != 0, "Columns may not be 0.");
    int elements = container.getElements().size();
    int rows = (elements - 1 + columns) / columns;
    if (rows < 1) {
        return Collections.emptyMap();
    }
    int cellWidth = (size.getWidth() + spacingX) / columns - spacingX;
    int cellHeight = (size.getHeight() + spacingY) / rows - spacingY;
    Pair<int[], int[]> maxCellSize = null;
    if (!cellsEqualSize) {
        maxCellSize = calcNeededCellSize(container);
    }
    Map<GuiElement, Pair<ReadablePoint, ReadableDimension>> map = new LinkedHashMap<>();
    Iterator<Map.Entry<GuiElement, LayoutData>> iter = container.getElements().entrySet().iterator();
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            if (!iter.hasNext()) {
                return map;
            }
            int x = j * (cellWidth + spacingX);
            int y = i * (cellHeight + spacingY);
            if (maxCellSize != null) {
                cellWidth = maxCellSize.getLeft()[j];
                cellHeight = maxCellSize.getRight()[i];
                x = 0;
                for (int x1 = 0; x1 < j; x1++) {
                    x += maxCellSize.getLeft()[x1];
                    x += spacingX;
                }
                y = 0;
                for (int y1 = 0; y1 < i; y1++) {
                    y += maxCellSize.getRight()[y1];
                    y += spacingY;
                }
            }
            Map.Entry<GuiElement, LayoutData> entry = iter.next();
            GuiElement element = entry.getKey();
            Data data = entry.getValue() instanceof Data ? (Data) entry.getValue() : DEFAULT_DATA;
            Dimension elementSize = new Dimension(element.getMinSize());
            ReadableDimension elementMaxSize = element.getMaxSize();
            elementSize.setWidth(Math.min(cellWidth, elementMaxSize.getWidth()));
            elementSize.setHeight(Math.min(cellHeight, elementMaxSize.getHeight()));
            int remainingWidth = cellWidth - elementSize.getWidth();
            int remainingHeight = cellHeight - elementSize.getHeight();
            x += (int) (data.alignmentX * remainingWidth);
            y += (int) (data.alignmentY * remainingHeight);
            map.put(element, Pair.<ReadablePoint, ReadableDimension>of(new Point(x, y), elementSize));
        }
    }
    return map;
}
Also used : Dimension(de.johni0702.minecraft.gui.utils.lwjgl.Dimension) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) Point(de.johni0702.minecraft.gui.utils.lwjgl.Point) ReadablePoint(de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint) LinkedHashMap(java.util.LinkedHashMap) ReadableDimension(de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension) GuiElement(de.johni0702.minecraft.gui.element.GuiElement) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

Point (de.johni0702.minecraft.gui.utils.lwjgl.Point)25 ReadablePoint (de.johni0702.minecraft.gui.utils.lwjgl.ReadablePoint)25 ReadableDimension (de.johni0702.minecraft.gui.utils.lwjgl.ReadableDimension)11 Dimension (de.johni0702.minecraft.gui.utils.lwjgl.Dimension)8 GuiElement (de.johni0702.minecraft.gui.element.GuiElement)6 OffsetGuiRenderer (de.johni0702.minecraft.gui.OffsetGuiRenderer)5 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 TextRenderer (net.minecraft.client.font.TextRenderer)4 Pair (org.apache.commons.lang3.tuple.Pair)4 CrashException (net.minecraft.util.crash.CrashException)2 CrashReport (net.minecraft.util.crash.CrashReport)2 CrashReportSection (net.minecraft.util.crash.CrashReportSection)2 RenderInfo (de.johni0702.minecraft.gui.RenderInfo)1 WritablePoint (de.johni0702.minecraft.gui.utils.lwjgl.WritablePoint)1 Window (net.minecraft.client.util.Window)1