/*
 * Copyright 2014 MovingBlocks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.terasology.rendering.nui.layouts;

import com.google.common.collect.Lists;
import org.terasology.math.geom.Rect2i;
import org.terasology.math.geom.Vector2i;
import org.terasology.rendering.nui.Canvas;
import org.terasology.rendering.nui.CoreLayout;
import org.terasology.rendering.nui.LayoutHint;
import org.terasology.rendering.nui.UIWidget;

import java.util.Iterator;
import java.util.List;

/**
 */
public class FlowLayout extends CoreLayout<LayoutHint> {

    private List<UIWidget> contents = Lists.newArrayList();

    @Override
    public void addWidget(UIWidget element, LayoutHint hint) {
        contents.add(element);
    }

    @Override
    public void removeWidget(UIWidget element) {
        contents.remove(element);
    }

    @Override
    public void removeAllWidgets() {
        contents.clear();
    }

    @Override
    public void onDraw(Canvas canvas) {
        int filledWidth = 0;
        int filledHeight = 0;
        int heightOffset = 0;
        for (UIWidget widget : contents) {
            Vector2i size = canvas.calculatePreferredSize(widget);
            if (filledWidth != 0 && filledWidth + size.x  > canvas.size().x) {
                heightOffset += filledHeight;
                filledWidth = 0;
                filledHeight = 0;
            }
            canvas.drawWidget(widget, Rect2i.createFromMinAndSize(filledWidth, heightOffset, size.x, size.y));
            filledWidth += size.x;
            filledHeight = Math.max(filledHeight, size.y);
        }
    }

    @Override
    public Vector2i getPreferredContentSize(Canvas canvas, Vector2i sizeHint) {
        Vector2i result = new Vector2i();
        int filledWidth = 0;
        int filledHeight = 0;
        for (UIWidget widget : contents) {
            Vector2i size = canvas.calculatePreferredSize(widget);
            if (filledWidth != 0 && filledWidth + size.x  > sizeHint.x) {
                result.x = Math.max(result.x, filledWidth);
                result.y += filledHeight;
                filledWidth = size.x;
                filledHeight = size.y;
            } else {
                filledWidth += size.x;
                filledHeight = Math.max(filledHeight, size.y);
            }
        }
        result.x = Math.max(result.x, filledWidth);
        result.y += filledHeight;

        return result;
    }

    @Override
    public Vector2i getMaxContentSize(Canvas canvas) {
        return new Vector2i(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    @Override
    public Iterator<UIWidget> iterator() {
        return contents.iterator();
    }
}

@@ -847,16 +847,66 @@
 Layout;%0A
+import org.terasology.rendering.nui.LayoutConfig;%0A
 import o
@@ -1047,16 +1047,19 @@
 t;%0A%0A/**%0A
+ *%0A
  */%0Apubl
@@ -1173,16 +1173,148 @@
 ist();%0A%0A
+    /**%0A     * The vertical spacing between adjacent widgets, in pixels%0A     */%0A    @LayoutConfig%0A    private int verticalSpacing;%0A%0A
     @Ove
@@ -1918,17 +1918,16 @@
  size.x 
- 
 %3E canvas
@@ -1975,32 +1975,50 @@
  += filledHeight
+ + verticalSpacing
 ;%0A              
@@ -2668,17 +2668,16 @@
  size.x 
- 
 %3E sizeHi
@@ -2784,16 +2784,34 @@
 edHeight
+ + verticalSpacing
 ;%0A      
@@ -3386,11 +3386,557 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Retrieves the vertical spacing between adjacent widgets in this %7B@code FlowLayout%7D.%0A     *%0A     * @return The spacing, in pixels%0A     */%0A    public int getVerticalSpacing() %7B%0A        return verticalSpacing;%0A    %7D%0A%0A    /**%0A     * Sets the vertical spacing between adjacent widgets in this %7B@code FlowLayout%7D.%0A     *%0A     * @param spacing The spacing, in pixels%0A     * @return This %7B@code FlowLayout%7D%0A     */%0A    public FlowLayout setVerticalSpacing(int spacing) %7B%0A        this.verticalSpacing = spacing;%0A        return this;%0A    %7D
 %0A%7D%0A

add option for vertical spacing (#4089)