示例#1
0
    /// <summary>
    /// Sets up the client GameObject along with it's layer and caches the transform
    /// </summary>
    public UIObject(GameObject prefab = null)
    {
        // Setup our GO
        if (prefab)
        {
            _client = Object.Instantiate(prefab) as GameObject;
        }
        else
        {
            _client = new GameObject(this.GetType().Name);
        }

        // Cache the clientTransform
        clientTransform = _client.transform;

        clientTransform.parent = UI.instance.transform; // Just for orginization in the hierarchy
        _client.layer          = UI.instance.layer;     // Set the proper layer so we only render on the UI camera

        UIElement uie = _client.AddComponent <UIElement>();

        uie.UIObject = this;


        // Create a default anchor info object
        _anchorInfo = UIAnchorInfo.DefaultAnchorInfo();
    }
示例#2
0
    /// <summary>
    /// Sets up the client GameObject along with it's layer and caches the transform
    /// </summary>
    public UIObject()
    {
        // Setup our GO
        _client = new GameObject(this.GetType().Name);
        _client.transform.parent = UI.instance.transform; // Just for orginization in the hierarchy
        _client.layer            = UI.instance.layer;     // Set the proper layer so we only render on the UI camera

        // Cache the clientTransform
        clientTransform = _client.transform;
        // Create a default anchor info object
        _anchorInfo = UIAnchorInfo.DefaultAnchorInfo();
    }
    public UIGridLayout(int columns, int rows, int cellPadding)
        : base(UIAbstractContainer.UILayoutType.AbsoluteLayout)           //don't manage layout for us
    {
        //padding
        _spacing = cellPadding;

        this.columns = columns;
        this.rows    = rows;

        //top left
        m_gridAnchor = UIAnchorInfo.DefaultAnchorInfo();
        m_gridAnchor.ParentUIObject = this;
        m_gridAnchor.UIPrecision    = UIPrecision.Pixel;
    }
    //helper functions
    UIAnchorInfo GetCellAnchorFor(int column, int row)
    {
        float cellWidth  = Screen.width / columns;
        float cellHeight = Screen.height / rows;

        var cellAnchor = UIAnchorInfo.DefaultAnchorInfo();

        cellAnchor.ParentUIObject  = this;
        cellAnchor.ParentUIxAnchor = m_gridAnchor.OriginUIxAnchor;
        cellAnchor.ParentUIyAnchor = m_gridAnchor.OriginUIyAnchor;
        cellAnchor.OffsetX         = UI.scaleFactor * cellWidth * column + _edgeInsets.left;
        cellAnchor.OffsetY         = UI.scaleFactor * cellHeight * row + _edgeInsets.right;

        return(cellAnchor);
    }
示例#5
0
    /// <summary>
    /// Responsible for laying out the child UISprites
    /// </summary>
    protected virtual void layoutChildren()
    {
        if (_suspendUpdates)
        {
            return;
        }

        // Get HD factor
        var hdFactor = 1f / UI.scaleFactor;

        // rules for vertical and horizontal layouts
        if (_layoutType == UIAbstractContainer.UILayoutType.Horizontal || _layoutType == UIAbstractContainer.UILayoutType.Vertical)
        {
            // start with the insets, then add each object + spacing then end with insets
            _contentWidth  = _edgeInsets.left;
            _contentHeight = _edgeInsets.top;

            // create UIAnchorInfo to control positioning
            var anchorInfo = UIAnchorInfo.DefaultAnchorInfo();
            anchorInfo.ParentUIObject = this;

            if (_layoutType == UIAbstractContainer.UILayoutType.Horizontal)
            {
                // Set anchor information
                switch (_verticalAlignMode)
                {
                case UIContainerVerticalAlignMode.Top:
                    anchorInfo.UIyAnchor       = UIyAnchor.Top;
                    anchorInfo.ParentUIyAnchor = UIyAnchor.Top;
                    anchorInfo.OffsetY         = _edgeInsets.top * hdFactor;
                    break;

                case UIContainerVerticalAlignMode.Middle:
                    anchorInfo.UIyAnchor       = UIyAnchor.Center;
                    anchorInfo.ParentUIyAnchor = UIyAnchor.Center;
                    break;

                case UIContainerVerticalAlignMode.Bottom:
                    anchorInfo.UIyAnchor       = UIyAnchor.Bottom;
                    anchorInfo.ParentUIyAnchor = UIyAnchor.Bottom;
                    anchorInfo.OffsetY         = _edgeInsets.bottom * hdFactor;
                    break;
                }

                var i         = 0;
                var lastIndex = _children.Count;
                foreach (var item in _children)
                {
                    // we add spacing for all but the first and last
                    if (i != 0 && i != lastIndex)
                    {
                        _contentWidth += _spacing;
                    }

                    // Set anchor offset
                    anchorInfo.OffsetX = (_contentWidth + _scrollPosition) * hdFactor;

                    // dont overwrite the sprites origin anchor!
                    anchorInfo.OriginUIxAnchor = item.anchorInfo.OriginUIxAnchor;
                    anchorInfo.OriginUIyAnchor = item.anchorInfo.OriginUIyAnchor;

                    item.anchorInfo = anchorInfo;

                    // all items get their width added
                    _contentWidth += item.width;

                    // height will just be the height of the tallest item
                    if (_contentHeight < item.height)
                    {
                        _contentHeight = item.height;
                    }

                    i++;
                }
            }
            else             // vertical alignment
            {
                // Set anchor information
                switch (_alignMode)
                {
                case UIContainerAlignMode.Left:
                    anchorInfo.UIxAnchor       = UIxAnchor.Left;
                    anchorInfo.ParentUIxAnchor = UIxAnchor.Left;
                    anchorInfo.OffsetX         = _edgeInsets.left * hdFactor;
                    break;

                case UIContainerAlignMode.Center:
                    anchorInfo.UIxAnchor       = UIxAnchor.Center;
                    anchorInfo.ParentUIxAnchor = UIxAnchor.Center;
                    break;

                case UIContainerAlignMode.Right:
                    anchorInfo.UIxAnchor       = UIxAnchor.Right;
                    anchorInfo.ParentUIxAnchor = UIxAnchor.Right;
                    anchorInfo.OffsetX         = _edgeInsets.right * hdFactor;
                    break;
                }

                var i         = 0;
                var lastIndex = _children.Count;
                foreach (var item in _children)
                {
                    // we add spacing for all but the first and last
                    if (i != 0 && i != lastIndex)
                    {
                        _contentHeight += _spacing;
                    }

                    // Set anchor offset
                    anchorInfo.OffsetY = (_contentHeight + _scrollPosition) * hdFactor;
                    //anchorInfo.OffsetX = item.anchorInfo.OffsetX;

                    // dont overwrite the sprites origin anchor!
                    anchorInfo.OriginUIxAnchor = item.anchorInfo.OriginUIxAnchor;
                    anchorInfo.OriginUIyAnchor = item.anchorInfo.OriginUIyAnchor;
                    item.anchorInfo            = anchorInfo;

                    // all items get their height added
                    _contentHeight += item.height;

                    // width will just be the width of the widest item
                    if (_contentWidth < item.width)
                    {
                        _contentWidth = item.width;
                    }

                    i++;
                }
            }

            // add the right and bottom edge inset to finish things off
            _contentWidth  += _edgeInsets.right;
            _contentHeight += _edgeInsets.bottom;
        }
        else if (_layoutType == UIAbstractContainer.UILayoutType.AbsoluteLayout)
        {
            foreach (var item in _children)
            {
                if (!item.hidden)
                {
                    item.localPosition = new Vector3(item.position.x, item.position.y, item.position.z);

                    // find the width that contains the item with the largest offset/width
                    if (_contentWidth < item.localPosition.x + item.width)
                    {
                        _contentWidth = item.localPosition.x + item.width;
                    }

                    // find the height that contains the item with the largest offset/height
                    if (_contentHeight < -item.localPosition.y + item.height)
                    {
                        _contentHeight = -item.localPosition.y + item.height;
                    }
                }
            }
        }

        // Refresh child position to proper positions
        foreach (var item in _children)
        {
            item.refreshPosition();
        }
    }
示例#6
0
    protected override void layoutChildren()
    {
        if (_suspendUpdates)
        {
            return;
        }

        // Get HD factor
        var hdFactor = 1f / UI.scaleFactor;

        // rules for vertical and horizontal layouts
        if (layoutType == UIAbstractContainerWrap.UILayoutType.Horizontal || layoutType == UIAbstractContainerWrap.UILayoutType.Vertical)
        {
            // start with the insets, then add each object + spacing then end with insets
            _contentWidth  = _edgeInsets.left;
            _contentHeight = _edgeInsets.top;

            // create UIAnchorInfo to control positioning
            var anchorInfo = UIAnchorInfo.DefaultAnchorInfo();
            anchorInfo.ParentUIObject = this;

            if (layoutType == UIAbstractContainerWrap.UILayoutType.Horizontal)
            {
                // Set anchor information
                switch (verticalAlignMode)
                {
                case UIContainerVerticalAlignMode.Top:
                    anchorInfo.UIyAnchor       = UIyAnchor.Top;
                    anchorInfo.ParentUIyAnchor = UIyAnchor.Top;
                    anchorInfo.OffsetY         = _edgeInsets.top * hdFactor;
                    break;

                case UIContainerVerticalAlignMode.Middle:
                    anchorInfo.UIyAnchor       = UIyAnchor.Center;
                    anchorInfo.ParentUIyAnchor = UIyAnchor.Center;
                    break;

                case UIContainerVerticalAlignMode.Bottom:
                    anchorInfo.UIyAnchor       = UIyAnchor.Bottom;
                    anchorInfo.ParentUIyAnchor = UIyAnchor.Bottom;
                    anchorInfo.OffsetY         = _edgeInsets.bottom * hdFactor;
                    break;
                }

                var          i         = 0;
                var          lastIndex = _children.Count;
                float        tempWidth = 0;
                List <float> rows      = new List <float>(); rows.Add(0); // first row.
                foreach (var item in _children)
                {
                    if (item as UIAbstractContainer2 != null)
                    {
                        // -WIP----------------------------------------------------------------------
                        // we add spacing for all but the first and last
                        if (i != 0 && i != lastIndex)
                        {
                            _contentWidth += _spacing;
                        }

                        // Set anchor offset

                        if (position.x + tempWidth + ((UIAbstractContainer2)item).width > Screen.width)
                        {
                            rows.Add(0);
                            tempWidth          = 0;
                            anchorInfo.OffsetY = (_contentHeight) * hdFactor;
                        }

                        anchorInfo.OffsetX = (tempWidth + _scrollPosition) * hdFactor;
                        tempWidth         += ((UIAbstractContainer2)item).width;

                        if (rows[rows.Count - 1] < ((UIAbstractContainer2)item).height)
                        {
                            rows[rows.Count - 1] = ((UIAbstractContainer2)item).height + _spacing;
                        }

                        if (tempWidth > _contentWidth)
                        {
                            _contentWidth = tempWidth;
                        }

                        // dont overwrite the sprites origin anchor!
                        anchorInfo.OriginUIxAnchor = ((UIAbstractContainer2)item).anchorInfo.OriginUIxAnchor;
                        anchorInfo.OriginUIyAnchor = ((UIAbstractContainer2)item).anchorInfo.OriginUIyAnchor;

                        ((UIAbstractContainer2)item).anchorInfo = anchorInfo;

                        _contentHeight = 0;
                        foreach (float row in rows)
                        {
                            _contentHeight += row;
                        }
                        // --------------------------------------------------------------------------
                    }
                    else if (item as UISprite != null)
                    {
                        // -WIP----------------------------------------------------------------------
                        // we add spacing for all but the first and last
                        if (i != 0 && i != lastIndex)
                        {
                            _contentWidth += _spacing;
                        }

                        // Set anchor offset

                        if (position.x + tempWidth + ((UISprite)item).width > Screen.width)
                        {
                            rows.Add(0);
                            tempWidth          = 0;
                            anchorInfo.OffsetY = (_contentHeight) * hdFactor;
                        }

                        anchorInfo.OffsetX = (tempWidth + _scrollPosition) * hdFactor;
                        tempWidth         += ((UISprite)item).width;

                        if (rows[rows.Count - 1] < ((UISprite)item).height)
                        {
                            rows[rows.Count - 1] = ((UISprite)item).height;
                        }

                        if (tempWidth > _contentWidth)
                        {
                            _contentWidth = tempWidth;
                        }

                        // dont overwrite the sprites origin anchor!
                        anchorInfo.OriginUIxAnchor = ((UISprite)item).anchorInfo.OriginUIxAnchor;
                        anchorInfo.OriginUIyAnchor = ((UISprite)item).anchorInfo.OriginUIyAnchor;

                        ((UISprite)item).anchorInfo = anchorInfo;

                        _contentHeight = 0;
                        foreach (float row in rows)
                        {
                            _contentHeight += row;
                        }
                        // --------------------------------------------------------------------------
                    }
                    else if (item as UITextInstance != null)
                    {
                        // we add spacing for all but the first and last
                        if (i != 0 && i != lastIndex)
                        {
                            _contentHeight += _spacing;
                        }

                        // Set anchor offset
                        //anchorInfo.OffsetY = (_contentHeight + _scrollPosition) * hdFactor;
                        //anchorInfo.OffsetX = item.anchorInfo.OffsetX;

                        // Set anchor offset

                        if (position.x + tempWidth + ((UITextInstance)item).width > Screen.width)
                        {
                            rows.Add(0);
                            tempWidth          = 0;
                            anchorInfo.OffsetY = (_contentHeight) * hdFactor;
                            Debug.Log("newtextrow");
                        }

                        anchorInfo.OffsetX = (tempWidth + _scrollPosition) * hdFactor;
                        tempWidth         += ((UITextInstance)item).width;

                        if (rows[rows.Count - 1] < ((UITextInstance)item).height)
                        {
                            rows[rows.Count - 1] = ((UITextInstance)item).height;
                        }

                        if (tempWidth > _contentWidth)
                        {
                            _contentWidth = tempWidth;
                        }

                        // dont overwrite the sprites origin anchor!
                        anchorInfo.OriginUIxAnchor        = ((UITextInstance)item).anchorInfo.OriginUIxAnchor;
                        anchorInfo.OriginUIyAnchor        = ((UITextInstance)item).anchorInfo.OriginUIyAnchor;
                        ((UITextInstance)item).anchorInfo = anchorInfo;

                        // all items get their height added
                        _contentHeight += ((UITextInstance)item).height;

                        // width will just be the width of the widest item
                        if (_contentWidth < ((UITextInstance)item).width)
                        {
                            _contentWidth = ((UITextInstance)item).width;
                        }
                    }
                    i++;
                }
            }
            else             // vertical alignment
            {
                // Set anchor information
                switch (alignMode)
                {
                case UIContainerAlignMode.Left:
                    anchorInfo.UIxAnchor       = UIxAnchor.Left;
                    anchorInfo.ParentUIxAnchor = UIxAnchor.Left;
                    anchorInfo.OffsetX         = _edgeInsets.left * hdFactor;
                    break;

                case UIContainerAlignMode.Center:
                    anchorInfo.UIxAnchor       = UIxAnchor.Center;
                    anchorInfo.ParentUIxAnchor = UIxAnchor.Center;
                    break;

                case UIContainerAlignMode.Right:
                    anchorInfo.UIxAnchor       = UIxAnchor.Right;
                    anchorInfo.ParentUIxAnchor = UIxAnchor.Right;
                    anchorInfo.OffsetX         = _edgeInsets.right * hdFactor;
                    break;
                }

                var i         = 0;
                var lastIndex = _children.Count;
                foreach (var item in _children)
                {
                    if (item as UIAbstractContainer2 != null)
                    {
                        // we add spacing for all but the first and last
                        if (i != 0 && i != lastIndex)
                        {
                            _contentHeight += _spacing;
                        }

                        // Set anchor offset
                        anchorInfo.OffsetY = (_contentHeight + _scrollPosition) * hdFactor;
                        //anchorInfo.OffsetX = item.anchorInfo.OffsetX;

                        // dont overwrite the sprites origin anchor!
                        anchorInfo.OriginUIxAnchor = ((UIAbstractContainer2)item).anchorInfo.OriginUIxAnchor;
                        anchorInfo.OriginUIyAnchor = ((UIAbstractContainer2)item).anchorInfo.OriginUIyAnchor;
                        ((UIAbstractContainer2)item).anchorInfo = anchorInfo;

                        // all items get their height added
                        _contentHeight += ((UIAbstractContainer2)item).height;

                        // width will just be the width of the widest item
                        if (_contentWidth < ((UIAbstractContainer2)item).width)
                        {
                            _contentWidth = ((UIAbstractContainer2)item).width;
                        }
                    }
                    else if (item as UISprite != null)
                    {
                        // we add spacing for all but the first and last
                        if (i != 0 && i != lastIndex)
                        {
                            _contentHeight += _spacing;
                        }

                        // Set anchor offset
                        anchorInfo.OffsetY = (_contentHeight + _scrollPosition) * hdFactor;
                        //anchorInfo.OffsetX = item.anchorInfo.OffsetX;

                        // dont overwrite the sprites origin anchor!
                        anchorInfo.OriginUIxAnchor  = ((UISprite)item).anchorInfo.OriginUIxAnchor;
                        anchorInfo.OriginUIyAnchor  = ((UISprite)item).anchorInfo.OriginUIyAnchor;
                        ((UISprite)item).anchorInfo = anchorInfo;

                        // all items get their height added
                        _contentHeight += ((UISprite)item).height;

                        // width will just be the width of the widest item
                        if (_contentWidth < ((UISprite)item).width)
                        {
                            _contentWidth = ((UISprite)item).width;
                        }
                    }
                    else if (item as UITextInstance != null)
                    {
                        // we add spacing for all but the first and last
                        if (i != 0 && i != lastIndex)
                        {
                            _contentHeight += _spacing;
                        }

                        // Set anchor offset
                        anchorInfo.OffsetY = (_contentHeight + _scrollPosition) * hdFactor;
                        //anchorInfo.OffsetX = item.anchorInfo.OffsetX;

                        // dont overwrite the sprites origin anchor!
                        anchorInfo.OriginUIxAnchor        = ((UITextInstance)item).anchorInfo.OriginUIxAnchor;
                        anchorInfo.OriginUIyAnchor        = ((UITextInstance)item).anchorInfo.OriginUIyAnchor;
                        ((UITextInstance)item).anchorInfo = anchorInfo;

                        // all items get their height added
                        _contentHeight += ((UITextInstance)item).height;

                        // width will just be the width of the widest item
                        if (_contentWidth < ((UITextInstance)item).width)
                        {
                            _contentWidth = ((UITextInstance)item).width;
                        }
                    }

                    i++;
                }
            }

            // add the right and bottom edge inset to finish things off
            _contentWidth  += _edgeInsets.right;
            _contentHeight += _edgeInsets.bottom;
        }
        else if (layoutType == UIAbstractContainerWrap.UILayoutType.AbsoluteLayout)
        {
            foreach (var item in _children)
            {
                if (item as UIAbstractContainer2 != null)
                {
                    UIAbstractContainer2 temp = (UIAbstractContainer2)item;
                    //temp.localPosition = new Vector3(temp.position.x, temp.position.y, temp.position.z);

                    // find the width that contains the item with the largest offset/width
                    if (_contentWidth < temp.localPosition.x + temp.width)
                    {
                        _contentWidth = temp.localPosition.x + temp.width;
                    }

                    // find the height that contains the item with the largest offset/height
                    if (_contentHeight < -temp.localPosition.y + temp.height)
                    {
                        _contentHeight = -temp.localPosition.y + temp.height;
                    }
                }
                else if (item as UISprite != null)
                {
                    UISprite temp = (UISprite)item;
                    //temp.localPosition = new Vector3(temp.position.x, temp.position.y, temp.position.z);

                    // find the width that contains the item with the largest offset/width
                    if (_contentWidth < temp.localPosition.x + temp.width)
                    {
                        _contentWidth = temp.localPosition.x + temp.width;
                    }

                    // find the height that contains the item with the largest offset/height
                    if (_contentHeight < -temp.localPosition.y + temp.height)
                    {
                        _contentHeight = -temp.localPosition.y + temp.height;
                    }
                }
                else if (item as UITextInstance != null)
                {
                    UITextInstance temp = (UITextInstance)item;
                    //temp.localPosition = new Vector3(temp.position.x, temp.position.y, temp.position.z);

                    // find the width that contains the item with the largest offset/width
                    if (_contentWidth < temp.localPosition.x + temp.width)
                    {
                        _contentWidth = temp.localPosition.x + temp.width;
                    }

                    // find the height that contains the item with the largest offset/height
                    if (_contentHeight < -temp.localPosition.y + temp.height)
                    {
                        _contentHeight = -temp.localPosition.y + temp.height;
                    }
                }
            }
        }

        // Refresh child position to proper positions
        foreach (var item in _children)
        {
            item.refreshPosition();
        }

        matchSizeToContentSize();
    }
    /// <summary>
    /// Responsible for laying out the child Layouts who will in turn
    /// layout their child sprites
    /// </summary>
    protected override void layoutChildren()
    {
        //Debug.Log("In UILayoutContainer's layoutChildren");
        if (_suspendUpdates)
        {
            return;
        }

        //var hdFactor = 1f / UI.scaleFactor;
        var anchorInfo = UIAnchorInfo.DefaultAnchorInfo();

        anchorInfo.ParentUIObject = this;
        var i         = 0;
        var lastIndex = _children.Count;

        _contentHeight = 0;
        foreach (var item in _children)
        {
            if (item as UIAbstractContainer2 != null)
            {
                // we add spacing for all but the first and last
                if (i != 0 && i != lastIndex)
                {
                    _contentHeight += _spacing;
                }
                else
                {
                    _contentHeight += myPadding;
                }

                item.localPosition = new Vector3(_edgeInsets.left, -(_contentHeight + _scrollPosition), item.localPosition.z);

                // all items get their height added
                _contentHeight += ((UIAbstractContainer2)item).contentHeight;

                // width will just be the width of the widest item
                if (_contentWidth < ((UIAbstractContainer2)item).contentWidth)
                {
                    _contentWidth = ((UIAbstractContainer2)item).contentWidth;
                }
            }
            else if (item as UISprite != null)
            {
                // we add spacing for all but the first and last
                if (i != 0 && i != lastIndex)
                {
                    _contentHeight += _spacing;
                }
                else
                {
                    _contentHeight += myPadding;
                }

                item.localPosition = new Vector3(_edgeInsets.left, -(_contentHeight + _scrollPosition), item.localPosition.z);

                // all items get their height added
                _contentHeight += ((UISprite)item).height;

                // width will just be the width of the widest item
                if (_contentWidth < ((UISprite)item).width)
                {
                    _contentWidth = ((UISprite)item).width;
                }

                /*
                 * if (i != 0 && i != lastIndex)
                 *  _contentHeight += _spacing;
                 *
                 * // Set anchor offset
                 * anchorInfo.OffsetY = (_contentHeight + _scrollPosition) * hdFactor;
                 * //anchorInfo.OffsetX = item.anchorInfo.OffsetX;
                 *
                 * // dont overwrite the sprites origin anchor!
                 * anchorInfo.OriginUIxAnchor = ((UISprite)item).anchorInfo.OriginUIxAnchor;
                 * anchorInfo.OriginUIyAnchor = ((UISprite)item).anchorInfo.OriginUIyAnchor;
                 * ((UISprite)item).anchorInfo = anchorInfo;
                 *
                 * // all items get their height added
                 * _contentHeight += ((UISprite)item).height;
                 *
                 * // width will just be the width of the widest item
                 * if (_contentWidth < ((UISprite)item).width)
                 *  _contentWidth = ((UISprite)item).width;*/
            }
            else if (item as UITextInstance != null)
            {
                // we add spacing for all but the first and last
                if (i != 0 && i != lastIndex)
                {
                    _contentHeight += _spacing;
                }
                else
                {
                    _contentHeight += myPadding;
                }

                item.localPosition = new Vector3(_edgeInsets.left, -(_contentHeight + _scrollPosition), item.localPosition.z);

                // all items get their height added
                _contentHeight += ((UITextInstance)item).height;

                // width will just be the width of the widest item
                if (_contentWidth < ((UITextInstance)item).width)
                {
                    _contentWidth = ((UITextInstance)item).width;
                }
            }

            i++;
        }

        // add the right and bottom edge inset to finish things off
        _contentWidth  += _edgeInsets.right;
        _contentHeight += _edgeInsets.bottom;

        //matchSizeToContentSize();
        clipToBounds();
    }