Пример #1
0
    public PUTableCell LoadCellForData(object cellData, PUTableCell reusedCell, float currentContentHeight)
    {
        PUTableCell cell = reusedCell;

        if (reusedCell == null)
        {
            string className = cellData.GetType().Name + "TableCell";
            Type   cellType  = Type.GetType(className, true);

            cell = (Activator.CreateInstance(cellType)) as PUTableCell;
            cell.LoadIntoPUGameObject(this, cellData);
        }
        else
        {
            cell.cellData            = cellData;
            cell.puGameObject.parent = this;
            cell.puGameObject.rectTransform.SetParent(this.contentObject.transform, false);

            cell.UpdateContents();

            if (cell.IsHeader() == false)
            {
                //cell.animatedYOffset = cell.puGameObject.rectTransform.anchoredPosition.y - currentContentHeight;
            }
        }

        allCells.Add(cell);

        cell.puGameObject.rectTransform.anchoredPosition = new Vector3(0, currentContentHeight, 0);
        cell.puGameObject.rectTransform.anchorMin        = new Vector2(0, 1);
        cell.puGameObject.rectTransform.anchorMax        = new Vector2(0, 1);
        cell.puGameObject.rectTransform.pivot            = new Vector2(0, 1);

        return(cell);
    }
Пример #2
0
    public PUTableCell LoadCellForData(object cellData, PUTableCell reusedCell, float currentContentHeight)
    {
        PUTableCell cell = reusedCell;
        if (reusedCell == null) {
            string className = cellData.GetType ().Name + "TableCell";
            Type cellType = Type.GetType (className, true);

            cell = (Activator.CreateInstance (cellType)) as PUTableCell;
            cell.LoadIntoPUGameObject (this, cellData);
        } else {
            cell.cellData = cellData;
            cell.puGameObject.parent = this;
            cell.puGameObject.rectTransform.SetParent (this.contentObject.transform, false);

            cell.UpdateContents ();

            if (cell.IsHeader () == false) {
                //cell.animatedYOffset = cell.puGameObject.rectTransform.anchoredPosition.y - currentContentHeight;
            }
        }

        allCells.Add (cell);

        cell.puGameObject.rectTransform.anchoredPosition = new Vector3 (0, currentContentHeight, 0);

        return cell;
    }
Пример #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");
        }
    }
Пример #4
0
    public void ReloadTable(bool reuseCells = true)
    {
        RectTransform contentRectTransform = contentObject.transform as RectTransform;


        if (gameObject.GetComponent <PUTableUpdateScript>() == null)
        {
            PUTableUpdateScript script = (PUTableUpdateScript)gameObject.AddComponent(typeof(PUTableUpdateScript));
            script.table = this;
        }

        // -2) Calculate the old height, so we can subtract it from the new height and adjust the scrolling appropriately
        float oldHeight = contentRectTransform.rect.height;


        // -1) Save previous cells for reuse if we can...
        List <PUTableCell> savedCells = new List <PUTableCell>(allCells);

        allCells.Clear();

        if (allObjects == null || allObjects.Count == 0)
        {
            return;
        }

        // 0) Remove all of the cells from the list transform temporarily
        foreach (PUTableCell cell in savedCells)
        {
            cell.puGameObject.rectTransform.SetParent(null, false);
        }

        // 1) Run through allObjects; instantiate a cell object based on said object class
        float currentContentHeight = 0;

        for (int i = 0; i < allObjects.Count; i++)
        {
            object myCellData = allObjects [i];

            // Can we reuse an existing cell?
            PUTableCell savedCell = null;
            if (reuseCells)
            {
                foreach (PUTableCell otherCell in savedCells)
                {
                    if (otherCell.IsHeader() == false && otherCell.cellData.Equals(myCellData))
                    {
                        savedCell = otherCell;
                        savedCells.Remove(otherCell);
                        break;
                    }
                }
            }

            PUTableCell newCell = LoadCellForData(myCellData, savedCell, currentContentHeight);

            currentContentHeight += newCell.puGameObject.rectTransform.rect.height;
        }

        // This will layout our cells
        prevLayoutSizeHash = Vector2.zero;
        LateUpdate();

        // 3) offset the scroll based upon the change in table height
        float newHeight = contentRectTransform.rect.height;

        if (oldHeight > 1)
        {
            Vector2 scroll = contentRectTransform.anchoredPosition;
            scroll.y += newHeight - oldHeight;
            contentRectTransform.anchoredPosition = scroll;
        }

        // 2) Remove all previous content which have not reused
        foreach (PUTableCell cell in savedCells)
        {
            cell.unload();
        }
    }
Пример #5
0
    public object ObjectForTableCell(PUTableCell cell)
    {
        int idx = allCells.IndexOf(cell);

        return(allObjects [(allObjects.Count - idx) - 1]);
    }
Пример #6
0
 public object ObjectForTableCell(PUTableCell cell)
 {
     int idx = allCells.IndexOf (cell);
     return allObjects [(allObjects.Count-idx)-1];
 }