Exemplo n.º 1
0
    /* UpdateHeightFromWidth, given a width postions all elements in the grid
     * and returns the height of the grid.
     *
     * @param width, a given width to constrain the grid
     *
     * @return height, the resulting height of the grid
     */
    public override float HeightFromWidth(float width)
    {
        if (width <= 0 || Columns <= 0 && Columns > 20)
        {
            return(0);
        }
        List <FlexElement> elements = ActiveElements;
        int n = elements.Count;

        //Center grid anchor and pivots whilst positioning elements
        DefaultAnchors();
        DefaultPivot();

        //Convert spacing from vw to pixels
        Vector2 space = Screen.width * GridSpacing / 100;

        float fullHeight = 0;
        int   cols       = Columns;
        int   i          = 0;

        while (i < n)
        {
            //Fill row of elements
            int   colCount  = 0;
            float x         = -width / 2;
            float rowHeight = 0;
            while (colCount < cols && i < n)
            {
                FlexElement flex = elements[i];
                flex.gameObject.SetActive(true);
                //Center anchors and pivot before positioning
                flex.DefaultAnchors();
                flex.DefaultPivot();

                //Calculate width for grid element and set it
                flex.Width = (width - (cols - 1) * space.x) / cols;

                //If element is the heigher than the row update the row height
                if (flex.Height > rowHeight)
                {
                    rowHeight = flex.Height;
                }

                //Set position of grid element
                float y = -1 * (fullHeight + flex.Height / 2);
                flex.Pos = new Vector2(x + flex.Width / 2, y);

                //Set anchor to top middle of grid and reset pivot
                flex.SetAnchors(new Vector2(0.5f, 1), new Vector2(0.5f, 1));
                flex.ResetPivot();


                x += flex.Width + space.x;
                i++;
                colCount++;
            }

            fullHeight += (i == n ? 0 : space.y) + rowHeight;
        }

        //Restore anchors and pivot of grid
        ResetAnchors();
        ResetPivot();
        return(fullHeight);
    }
    /* UpdateElements, repositions elements with respect to the scroll position.
     */
    public void UpdateElements()
    {
        float width  = ViewWidth;
        float height = 0;

        if (StaticHeaderElement != null)
        {
            //Header anchors and pivot to center
            StaticHeaderElement.DefaultAnchors();
            StaticHeaderElement.DefaultPivot();

            //Resize and position header
            StaticHeaderElement.Width = width;

            StaticHeaderElement.Pos = new Vector2(0, -1 * (StaticHeaderElement.Height / 2));

            StaticHeaderElement.SetAnchors(new Vector2(0.5f, 1), new Vector2(0.5f, 1));
            StaticHeaderElement.ResetPivot();

            height += StaticHeaderElement.Height;
        }

        // If HeaderElement is given
        if (HeaderElement != null)
        {
            //Header anchors and pivot to center
            HeaderElement.DefaultAnchors();
            HeaderElement.DefaultPivot();

            //Resize and position header
            HeaderElement.Width = width;
            //Header will not bounce at top
            if (sPos > 0)
            {
                HeaderElement.Pos = new Vector2(0, -1 * (height + HeaderElement.Height / 2));
            }
            else
            {
                HeaderElement.Pos = new Vector2(0, -1 * (sPos + height + HeaderElement.Height / 2));
            }

            //Set header anchor to middle top and restore pivot
            HeaderElement.SetAnchors(new Vector2(0.5f, 1), new Vector2(0.5f, 1));
            HeaderElement.ResetPivot();

            height += HeaderElement.Height;
        }



        //If BodyElement is given
        if (BodyElement != null)
        {
            //Header anchors and pivot to center
            BodyElement.DefaultAnchors();
            BodyElement.DefaultPivot();

            //Resize and position header
            BodyElement.Width = width;
            BodyElement.Pos   = new Vector2(0, -1 * (sPos + height + BodyElement.Height / 2));

            //Set body anchor to middle top and restore pivot
            BodyElement.SetAnchors(new Vector2(0.5f, 1), new Vector2(0.5f, 1));
            BodyElement.ResetPivot();

            height += BodyElement.Height;
        }

        //Update content height
        ContentHeight = height > ViewHeight ? height : ViewHeight;
    }