Exemplo n.º 1
0
 public void AddElement(FlexElement element)
 {
     if (element == null)
     {
         return;
     }
     element.transform.SetParent(transform);
 }
Exemplo n.º 2
0
    /* AddElement, adds an element to the grid
     *
     * @param element, the gameObject to add (must have a FlexElement component)
     */
    public void AddElement(GameObject element)
    {
        if (element == null)
        {
            return;
        }

        FlexElement flex = element.GetComponent <FlexElement>();

        if (flex != null)
        {
            element.transform.SetParent(transform);
        }
    }
Exemplo n.º 3
0
    public override float HeightFromWidth(float width)
    {
        FlexElement element = Element;

        if (FixedHeight > 0)
        {
            float height = FixedHeight;
            if (element != null)
            {
                element.Width = width;
                if (element.Height > height)
                {
                    element.Height = height;
                }
                element.Pos = new Vector2(0, 0);
            }
            return(height);
        }
        else if (FixedRatio > 0)
        {
            float height = width * FixedRatio;
            if (element != null)
            {
                element.Width = width;
                if (element.Height > height)
                {
                    element.Height = height;
                }
            }
            return(height);
        }
        else if (element != null)
        {
            element.Width = width;
            element.Pos   = new Vector2(0, 0);
            return(element.Height);
        }
        return(-1);
    }
Exemplo n.º 4
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);
    }
Exemplo n.º 5
0
    // Positions elements acording to xOffset
    private void PositionElements()
    {
        List <FlexElement> elements = ActiveElements;
        int n = elements.Count;

        if (n < 1 || minHeight == 0)
        {
            return;
        }

        int selected = Ci(Selected, n);

        if (selected != _selected)
        {
            xOffset = elements[selected].Pos.x;
        }
        _selected = selected;

        float x = xOffset;

        FlexElement cur = elements[selected];

        // Set width, height and pos acording to the x offset of selected element
        cur.Height = minHeight;
        float scale = Mathf.Abs(x / cur.Width);

        scale      = scale > 1 ? 1 : scale;
        cur.Width -= (2 * spacing_px) * scale;
        cur.Pos    = new Vector2(x, 0);

        // Left and right x positions
        float lx = x - cur.Width / 2 - spacing_px;
        float rx = x + cur.Width / 2 + spacing_px;

        // Left and right circular indexs of selected element
        int li = Li(selected, n);
        int ri = Ri(selected, n);

        // For the remaining elements to the left
        // and the right of the selected element
        int count = n - 1;

        while (count > 0)
        {
            FlexElement left  = elements[li];
            FlexElement right = elements[ri];

            if (n != 2 || selected != 0)
            {
                // Set height, width and pos of the current left element
                // acording to the x offset of the selected element
                left.Height = minHeight;
                scale       = Mathf.Abs((lx - left.Width / 2) / left.Width);
                scale       = scale > 1 ? 1 : scale;
                left.Width -= (2 * spacing_px) * scale;
                if (float.IsNaN(lx - left.Width))
                {
                    Debug.Log($"lx: {lx}, left width: {left.Width}");
                }
                else
                {
                    left.Pos = new Vector2(lx - left.Width / 2, 0);
                    left.gameObject.SetActive(true);
                }

                // move left x position
                lx -= left.Width + spacing_px;

                // no right element
                count--;
                if (count <= 0)
                {
                    break;
                }
            }

            // Set height, width and pos of the current right element
            // acording to the x offset of the selected element
            right.Height = minHeight;
            scale        = Mathf.Abs((rx + right.Width / 2) / right.Width);
            scale        = scale > 1 ? 1 : scale;
            right.Width -= (2 * spacing_px) * scale;
            right.Pos    = new Vector2(rx + right.Width / 2, 0);
            right.gameObject.SetActive(true);

            rx += right.Width + spacing_px;

            //Move left index left and right index right
            ri = Ri(ri, n);
            li = Li(li, n);

            count--;
        }

        //Update icons
        if (Dots != null)
        {
            if (n < 3)
            {
                Dots.Active = false;
            }
            else
            {
                if (!Dots.Active)
                {
                    Dots.Active = true;
                }
                Dots.Count    = n;
                Dots.Selected = selected;
            }
        }
    }