Esempio n. 1
0
        override protected void DoLayout()
        {
//			if (this.model == null) {
//				return new Bounds();
//			}
            InitArray();
            Rect bound = new Rect();

            CalculateBound(bound);
            int row = rowCount;
            int col = columnCount;

            UITablePrefabs prefabs = GetPrefabs();
            float          pixely  = 0;

            for (int r = 0; r < row; r++)
            {
                float pixelx    = 0;
                bool  activeRow = false; // inactive row is removed from layout computation
                for (int c = 0; c < col; c++)
                {
                    UITableCell cell = GetCell(r, c);
                    if (cell != null && cell.gameObject.activeInHierarchy)
                    {
                        Transform t = cell.transform;
                        if (!t.IsChildOf(transform))
                        {
                            t.SetParent(transform, false);
                        }
                        Vector3 point     = bounds[r, c].min;
                        Vector3 size      = bounds[r, c].size;
                        float   halignPad = 0;
                        float   valignPad = 0;
                        Vector3 pos       = t.localPosition;
                        if (haligns[c] == HAlign.None)
                        {
                            if (r >= rowHeader && c >= columnHeader)
                            {
                                UITableCell prefab = prefabs.GetPrefab(r, c);
                                if (prefab != null)
                                {
                                    if (prefab == defaultPrefab)
                                    {
                                        pos.x = pixelx + prefab.transform.localPosition.x;
                                    }
                                    else
                                    {
                                        pos.x = prefab.transform.localPosition.x;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (haligns[c] == HAlign.Center)
                            {
                                halignPad = (maxWidths[c] - size.x) / 2f;
                            }
                            else if (haligns[c] == HAlign.Right)
                            {
                                halignPad = maxWidths[c] - size.x;
                            }
                            pos.x = pixelx - point.x + halignPad;
                        }
                        if (valigns[r] == VAlign.None)
                        {
                            if (r >= rowHeader && c >= columnHeader)
                            {
                                UITableCell prefab = prefabs.GetPrefab(r, c);
                                if (prefab != null)
                                {
                                    if (prefab == defaultPrefab)
                                    {
                                        pos.y = pixely + prefab.transform.localPosition.y;
                                    }
                                    else
                                    {
                                        pos.y = prefab.transform.localPosition.y;
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (valigns[r] == VAlign.Center)
                            {
                                valignPad = (maxHeights[r] - size.y) / 2f;
                            }
                            else if (valigns[r] == VAlign.Bottom)
                            {
                                valignPad = maxHeights[r] - size.y;
                            }
                            pos.y = pixely - (point.y + size.y) - valignPad;
                        }

                        t.SetLocalPosition(pos, 0.01f);
                        NGUIUtil.ApplyPivot(t);

                        // update Collider Bound
                        if (resizeCollider)
                        {
                            BoxCollider box = t.GetComponentInChildren <BoxCollider>();
                            if (box != null)
                            {
                                Vector3 center = box.center;
                                center.x = pixelx + maxWidths[c] * 0.5f;
                                center.y = pixely - maxHeights[r] * 0.5f;
                                Vector3 boxSize = box.size;
                                boxSize.x = maxWidths[c];
                                boxSize.y = maxHeights[r];
                                if (expandColliderToPadding)
                                {
                                    if (c < col - 1)
                                    {
                                        boxSize.x += padding.x;
                                        center.x  += padding.x * 0.5f;
                                    }
                                    if (r < row - 1)
                                    {
                                        boxSize.y += padding.y;
                                        center.y  -= padding.y * 0.5f;
                                    }
                                }
                                center     = transform.TransformSpace(center, t);
                                center.z   = box.center.z;
                                box.center = center;
                                box.size   = boxSize;
                            }
                            else
                            {
                                BoxCollider2D box2d = t.GetComponentInChildren <BoxCollider2D>();
                                if (box2d != null)
                                {
                                    Vector2 center = box2d.offset;
                                    center.x = pixelx + maxWidths[c] * 0.5f;
                                    center.y = pixely - maxHeights[r] * 0.5f;
                                    Vector2 boxSize = box2d.size;
                                    boxSize.x = maxWidths[c];
                                    boxSize.y = maxHeights[r];
                                    if (expandColliderToPadding)
                                    {
                                        if (c < col - 1)
                                        {
                                            boxSize.x += padding.x;
                                            center.x  += padding.x * 0.5f;
                                        }
                                        if (r < row - 1)
                                        {
                                            boxSize.y += padding.y;
                                            center.y  -= padding.y * 0.5f;
                                        }
                                    }
                                    center       = transform.TransformSpace(center, t);
                                    box2d.offset = center;
                                    box2d.size   = boxSize;
                                }
                            }
                        }
                        //					if (bound.width != 0) {
                        //						bound.x = Math.Min(bound.x, pos.x);
                        //					}
                        //					if (bound.height != 0) {
                        //						bound.y = Math.Max(bound.y, pos.y);
                        //					}
                    }
                    Vector3 extent = bounds[r, c].extents;
                    bounds[r, c].center = new Vector2(pixelx + extent.x, pixely - extent.y);

                    cellPos[r, c] = new Vector2(pixelx, pixely);
                    activeRow    |= cell == null || cell.gameObject.activeInHierarchy;
                    pixelx       += maxWidths[c] + padding.x;
                }
                if (activeRow)
                {
                    pixely += -maxHeights[r] - padding.y;
                }
            }

            if (NGUIUtil.IsValid(bound))
            {
//				UIDraggablePanel drag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);
//				if (drag != null) drag.UpdateScrollbars(true);
            }
            else
            {
                bound = new Rect();
            }
            if (propagateReposition)
            {
                NGUIUtil.Reposition(transform);
            }
        }
Esempio n. 2
0
        override protected void DoLayout()
        {
            Bounds topBound    = GetBounds(top);
            Bounds bottomBound = GetBounds(bottom);
            Bounds leftBound   = GetBounds(left);
            Bounds rightBound  = GetBounds(right);

            Vector3 topPoint    = topBound.min;
            Vector3 topSize     = topBound.size;
            Vector3 bottomPoint = bottomBound.min;
            Vector3 bottomSize  = bottomBound.size;
            Vector3 leftPoint   = leftBound.min;
            Vector3 leftSize    = leftBound.size;
            Vector3 rightPoint  = rightBound.min;
            Vector3 rightSize   = rightBound.size;

            // Align Top
            if (top != null)
            {
                Vector3 topPos = top.localPosition;
                topPos.x = -topPoint.x;
                topPos.y = -topPoint.y;
                top.SetLocalPosition(topPos, 0.01f);
                NGUIUtil.ApplyPivot(top);
            }

            // Align Bottom
            if (bottom != null)
            {
                Vector3 botPos = bottom.localPosition;
                botPos.x = -bottomPoint.x;
                botPos.y = -height - bottomSize.y - bottomPoint.y;
                bottom.SetLocalPosition(botPos, 0.01f);
                bottom.localPosition = botPos;
                NGUIUtil.ApplyPivot(bottom);
            }

            // Align Left
            if (left != null)
            {
                Vector3 leftPos = left.localPosition;
                leftPos.x = -leftPoint.x;
                leftPos.y = -leftPoint.y + topSize.y;
                left.SetLocalPosition(leftPos, 0.01f);
                NGUIUtil.ApplyPivot(left);
            }

            // Align Right
            if (right != null)
            {
                Vector3 rightPos = right.localPosition;
                rightPos.x = width - rightPoint.x - rightSize.x;
                rightPos.y = -rightPoint.y + topSize.y;
                right.SetLocalPosition(rightPos, 0.0001f);
                NGUIUtil.ApplyPivot(right);
            }

            if (center != null)
            {
                Vector3 pos = center.transform.localPosition;
                pos.x = 0;
                pos.y = 0;
                center.SetLocalPosition(pos, 0.0001f);

                float x = (leftSize.x - rightSize.x) / 2;
                float y = (topSize.y - bottomSize.y) / 2;
                float w = width - (leftSize.x + rightSize.x);
                float h = height + topSize.y + bottomSize.y;
                NGUIUtil.SetBoundingBox(center, new Rect(x, y, w, h));
                NGUIUtil.ApplyPivot(center);
            }

//			return new Bounds(new Vector3(width*0.5f, height*0.5f, 0), new Vector3(0, width, -height));
        }