Exemple #1
0
    private void LayoutAllCells(MaxRectsBinPack.FreeRectChoiceHeuristic heuristic)
    {
        RectTransform contentRectTransform = contentObject.transform as RectTransform;

        float maxHeight = 0;

        if (allCells.Count > 0)
        {
            // If we have headers, we want to layout the cells in header-to-header chuncks of cells
            if (allCells [0].IsHeader() == false)
            {
                SubLayoutCells(ref maxHeight, allCells.GetRange(0, allCells.Count), heuristic);
            }
            else
            {
                int lastHeaderIdx = 0;

                for (int i = 1; i < allCells.Count; i++)
                {
                    if (allCells [i].IsHeader())
                    {
                        SubLayoutCells(ref maxHeight, allCells.GetRange(lastHeaderIdx, (i - lastHeaderIdx)), heuristic);
                        lastHeaderIdx = i;
                    }
                }
                if (lastHeaderIdx != allCells.Count - 1)
                {
                    SubLayoutCells(ref maxHeight, allCells.GetRange(lastHeaderIdx, (allCells.Count - lastHeaderIdx)), heuristic);
                }
            }
        }

        contentRectTransform.sizeDelta = new Vector2(rectTransform.rect.width, maxHeight);
    }
 public void Insert(List <CustomRectangle> rects, List <CustomRectangle> dst, MaxRectsBinPack.FreeRectChoiceHeuristic method)
 {
     dst.Clear();
     while (rects.Count > 0)
     {
         int             num1             = int.MaxValue;
         int             num2             = int.MaxValue;
         int             index1           = -1;
         CustomRectangle customRectangle1 = new CustomRectangle((object)null);
         for (int index2 = 0; index2 < rects.Count; ++index2)
         {
             int             score1           = 0;
             int             score2           = 0;
             CustomRectangle customRectangle2 = this.ScoreRect(rects[index2].Width, rects[index2].Height, method, ref score1, ref score2);
             if (score1 < num1 || score1 == num1 && score2 < num2)
             {
                 num1                      = score1;
                 num2                      = score2;
                 customRectangle1          = customRectangle2;
                 customRectangle1.UserData = rects[index2].UserData;
                 customRectangle1.Rotated  = customRectangle2.Rotated;
                 index1                    = index2;
             }
         }
         if (index1 == -1)
         {
             break;
         }
         this.PlaceRect(customRectangle1);
         this.CaculateEdge(customRectangle1);
         rects.RemoveAt(index1);
     }
 }
Exemple #3
0
    private void SubLayoutCells(ref float maxHeight, List <PUTableCell> cellsToAdd, MaxRectsBinPack.FreeRectChoiceHeuristic heuristic)
    {
        RectTransform contentRectTransform = contentObject.transform as RectTransform;

        float blockHeight = 2048.0f;
        float baseY       = maxHeight;

        if (cellsToAdd [0].IsHeader())
        {
            PUTableCell cell = cellsToAdd [0];
            cellsToAdd.RemoveAt(0);

            cell.puGameObject.rectTransform.anchoredPosition = new Vector2(0, -baseY);

            baseY += cell.puGameObject.rectTransform.sizeDelta.y;
        }

        // The MaxRects packer works by being given a canvas (width/height) to fit all rectangles in
        // For us to use this and allow arbitrary height, we give it a rect the size of the visible
        // scroll area, fill it up, and then repeat until we run out of cells.
        int bail = 500;

        while (cellsToAdd.Count > 0 && bail > 0)
        {
            MaxRectsBinPack packer = new MaxRectsBinPack((int)contentRectTransform.rect.width, (int)blockHeight, false);

            for (int i = cellsToAdd.Count - 1; i >= 0; i--)
            {
                PUTableCell cell = cellsToAdd [i];
                LORect      packedRect;

                if (packer.Insert((int)cell.puGameObject.rectTransform.sizeDelta.x, (int)cell.puGameObject.rectTransform.sizeDelta.y, heuristic, cell.puGameObject.rectTransform, out packedRect))
                {
                    packedRect.y += baseY;
                    cell.puGameObject.rectTransform.anchoredPosition = new Vector2(packedRect.x, -packedRect.y);
                    if ((packedRect.y + packedRect.height) > maxHeight)
                    {
                        maxHeight = (packedRect.y + packedRect.height);
                    }
                    cellsToAdd.RemoveAt(i);
                }
            }

            if (expandToFill)
            {
                packer.ExpandRectsToFill((int)contentRectTransform.rect.width, (maxHeight - baseY));
            }

            baseY = maxHeight;
            bail--;
        }

        if (bail == 0)
        {
            Debug.Log("Warning: PUGridTable layout failed to place all cells");
        }
    }
Exemple #4
0
    public DynamicAtlas(int width, int height, string name, HeuristicMethod method = HeuristicMethod.RectBestShortSideFit)
    {
        Texture      = new Texture2D(width, height, TextureFormat.RGBA32, false);
        Texture.name = name;
        this.method  = method;

        rectsPack = new MaxRectsBinPack(width, height, false);
        names     = new List <string>();
    }
        public CustomRectangle Insert(int width, int height, MaxRectsBinPack.FreeRectChoiceHeuristic method)
        {
            CustomRectangle usedNode = new CustomRectangle((object)null);
            int             num1     = 0;
            int             num2     = 0;

            switch (method)
            {
            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit:
                usedNode = this.FindPositionForNewNodeBestShortSideFit(width, height, ref num1, ref num2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestLongSideFit:
                usedNode = this.FindPositionForNewNodeBestLongSideFit(width, height, ref num2, ref num1);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit:
                usedNode = this.FindPositionForNewNodeBestAreaFit(width, height, ref num1, ref num2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBottomLeftRule:
                usedNode = this.FindPositionForNewNodeBottomLeft(width, height, ref num1, ref num2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectContactPointRule:
                usedNode = this.FindPositionForNewNodeContactPoint(width, height, ref num1);
                break;
            }
            if (usedNode.Height == 0)
            {
                return(usedNode);
            }
            int count = this.freeRectangles.Count;

            for (int index = 0; index < count; ++index)
            {
                if (this.SplitFreeNode(this.freeRectangles[index], ref usedNode))
                {
                    this.freeRectangles.RemoveAt(index);
                    --index;
                    --count;
                }
            }
            this.PruneFreeList();
            this.usedRectangles.Add(usedNode);
            this.CaculateEdge(usedNode);
            return(usedNode);
        }
        private CustomRectangle ScoreRect(int width, int height, MaxRectsBinPack.FreeRectChoiceHeuristic method, ref int score1, ref int score2)
        {
            CustomRectangle customRectangle = new CustomRectangle((object)null);

            score1 = int.MaxValue;
            score2 = int.MaxValue;
            switch (method)
            {
            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestShortSideFit:
                customRectangle = this.FindPositionForNewNodeBestShortSideFit(width, height, ref score1, ref score2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestLongSideFit:
                customRectangle = this.FindPositionForNewNodeBestLongSideFit(width, height, ref score2, ref score1);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit:
                customRectangle = this.FindPositionForNewNodeBestAreaFit(width, height, ref score1, ref score2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectBottomLeftRule:
                customRectangle = this.FindPositionForNewNodeBottomLeft(width, height, ref score1, ref score2);
                break;

            case MaxRectsBinPack.FreeRectChoiceHeuristic.RectContactPointRule:
                customRectangle = this.FindPositionForNewNodeContactPoint(width, height, ref score1);
                score1          = -score1;
                break;
            }
            if (customRectangle.Height == 0)
            {
                score1 = int.MaxValue;
                score2 = int.MaxValue;
            }
            return(customRectangle);
        }