示例#1
0
        } // doMeasure

        public void MeasureCell(Datum d, Element e, Column c)
        {
            if (c.columnType == Column.ColumnType.TEXT || d.isHeader || d.isFooter)
            {
                if (e == null)
                {
                    this.MeasureCellDone(c, d, e);
                    return;
                }
                if (string.IsNullOrEmpty(e.value))
                {
                    e.measuredWidth  = 0;
                    e.measuredHeight = 0;
                    this.MeasureCellDone(c, d, e);
                    return;
                }

                TextGenerationSettings settings;
                float   useW;
                float   mw;
                float   mh;
                float   pixelsPerUnit;
                Vector2 size;
                this.text.fontSize = c.CalcFont(d);
                this.text.text     = e.value;

                if (c.measuredMaxWidth.HasValue)
                {
                    this.text.horizontalOverflow = HorizontalWrapMode.Wrap;
                    size = new Vector2(c.measuredMaxWidth.Value, 0);
                }
                else
                {
                    this.text.horizontalOverflow = HorizontalWrapMode.Overflow;
                    size = Vector2.zero;
                }

                settings      = this.text.GetGenerationSettings(size);
                pixelsPerUnit = this.text.pixelsPerUnit;

                mw = this.text.cachedTextGeneratorForLayout.GetPreferredWidth
                         (e.value, settings) / pixelsPerUnit;
                mh = this.text.cachedTextGeneratorForLayout.GetPreferredHeight
                         (e.value, settings) / pixelsPerUnit;

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                useW = c.maxWidth;
                if (c.measuredMaxWidth.HasValue &&
                    (c.measuredMaxWidth.Value < useW || useW <= 0))
                {
                    useW = c.measuredMaxWidth.Value;
                }

                if (useW > 0 && mw > useW)
                {
                    //Debug.Log("Doing Max Width on " + c.idx + " Row: " + d.uid + " Width: " + useW);

                    this.text.horizontalOverflow = HorizontalWrapMode.Wrap;
                    size = new Vector2(useW, 0);

                    settings      = this.text.GetGenerationSettings(size);
                    pixelsPerUnit = this.text.pixelsPerUnit;

                    mw = this.text.cachedTextGeneratorForLayout.GetPreferredWidth
                             (e.value, settings) / pixelsPerUnit;
                    mh = this.text.cachedTextGeneratorForLayout.GetPreferredHeight
                             (e.value, settings) / pixelsPerUnit;
                }

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                mw = Mathf.Max(c.minWidth, mw);

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                if (c.measuredMinWidth.HasValue && c.measuredMinWidth.Value > mw)
                {
                    mw = c.measuredMinWidth.Value;
                }

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                e.measuredWidth  = mw;
                e.measuredHeight = mh;
                this.MeasureCellDone(c, d, e);
            } // if TEXT
            else
            {
                e.measuredWidth  = c.imageWidth;
                e.measuredHeight = c.imageHeight;
                this.MeasureCellDone(c, d, e);
            }
        }
        }     // makeRows

        private void InstantiateCells(Row row, bool isHeader, bool isFooter)
        {
            GameObject    go;
            RectTransform rt;
            RectTransform crt;

    #if TMP_PRESENT
            TextMeshProUGUI text;
    #else
            Text text;
    #endif
            Cell  cell;
            Image img = null;

            for (int j = 0; j < this.table.columns.Count; j++)
            {
                Column column = this.table.columns[j];

                if (row.cells.Count <= j)
                {
                    go           = new GameObject();
                    go.name      = "Column" + j;
                    rt           = go.AddComponent <RectTransform>();
                    rt.anchorMin = new Vector2(0, 1);
                    rt.anchorMax = new Vector2(0, 1);
                    rt.pivot     = new Vector2(0, 1f);
                    img          = go.AddComponent <Image>();
                    img.sprite   = this.table.fillerSprite;
                    img.color    = Color.red;
                    if (isHeader)
                    {
                        cell = go.AddComponent <HeaderCell>();
                    }
                    else
                    {
                        cell = go.AddComponent <Cell>();
                    }
                    cell.background = img;
                    go  = new GameObject();
                    crt = go.AddComponent <RectTransform>();
                    crt.transform.SetParent(rt, false);
                }
                else
                {
                    cell = row.cells[j];
                    // We can't change an image cell to a text cell, so
                    //  nuke here and rebuild if that's what's happening
                    if (((column.columnType == Column.ColumnType.TEXT ||
                          isHeader || isFooter) && cell.image != null) ||
                        ((column.columnType == Column.ColumnType.IMAGE &&
                          !isHeader && !isFooter) && cell.text != null))
                    {
                        Component.Destroy(cell.gameObject);
                        go           = new GameObject();
                        go.name      = "Column" + j;
                        rt           = go.AddComponent <RectTransform>();
                        rt.anchorMin = new Vector2(0, 1);
                        rt.anchorMax = new Vector2(0, 1);
                        rt.pivot     = new Vector2(0, 1f);
                        img          = go.AddComponent <Image>();
                        img.sprite   = this.table.fillerSprite;
                        img.color    = Color.red;
                        if (isHeader)
                        {
                            cell = go.AddComponent <HeaderCell>();
                        }
                        else
                        {
                            cell = go.AddComponent <Cell>();
                        }
                        cell.background = img;
                        go  = new GameObject();
                        crt = go.AddComponent <RectTransform>();
                        crt.transform.SetParent(rt, false);
                    }
                    else
                    {
                        cell.gameObject.SetActive(true);
                        go  = cell.crt.gameObject;
                        rt  = cell.rt;
                        crt = cell.crt;
                    }
                }

                if (column.columnType == Column.ColumnType.TEXT || isHeader || isFooter)
                {
                    if (column.horAlignment == Column.HorAlignment.LEFT)
                    {
                        crt.anchorMin = new Vector2(0, 0.5f);
                        crt.anchorMax = new Vector2(0, 0.5f);
                    }
                    else if (column.horAlignment == Column.HorAlignment.CENTER)
                    {
                        crt.anchorMin = new Vector2(0.5f, 0.5f);
                        crt.anchorMax = new Vector2(0.5f, 0.5f);
                        crt.pivot     = new Vector2(0, 1);
                    }
                    else
                    {
                        crt.anchorMin = new Vector2(1, 0.5f);
                        crt.anchorMax = new Vector2(1, 0.5f);
                        crt.pivot     = new Vector2(0, 1);
                    }

                    crt.pivot     = new Vector2(0, 1);
                    crt.sizeDelta = Vector2.zero;
                    go.name       = j + "Text";
                    if (cell.text == null)
                    {
          #if TMP_PRESENT
                        text = go.AddComponent <TextMeshProUGUI>();
          #else
                        text = go.AddComponent <Text>();
          #endif
                    }
                    else
                    {
                        text = cell.text;
                    }

        #if TMP_PRESENT
                    text.overflowMode       = TextOverflowModes.Overflow;
                    text.enableWordWrapping = true;
        #else
                    text.horizontalOverflow = HorizontalWrapMode.Wrap;
                    text.verticalOverflow   = VerticalWrapMode.Overflow;
        #endif

                    text.font      = this.table.font;
                    text.fontStyle = this.table.fontStyle;
                    if (isHeader)
                    {
                        text.color = this.table.headerTextColor;
                    }
                    else if (isFooter)
                    {
                        text.color = this.table.footerTextColor;
                    }
                    else
                    {
                        text.color = this.table.rowTextColor;
                    }
                    if (column.horAlignment == Column.HorAlignment.LEFT)
                    {
          #if TMP_PRESENT
                        text.alignment = TextAlignmentOptions.MidlineLeft;
          #else
                        text.alignment = TextAnchor.MiddleLeft;
          #endif
                    }
                    else if (column.horAlignment == Column.HorAlignment.CENTER)
                    {
          #if TMP_PRESENT
                        text.alignment = TextAlignmentOptions.Midline;
          #else
                        text.alignment = TextAnchor.MiddleCenter;
          #endif
                    }
                    else
                    {
          #if TMP_PRESENT
                        text.alignment = TextAlignmentOptions.MidlineRight;
          #else
                        text.alignment = TextAnchor.MiddleRight;
          #endif
                    }
                    cell.Initialize(this.table, row, column, j, rt, crt, text);
                    text.fontSize = column.CalcFont(isHeader, isFooter);
                }
                else
                {
                    crt.anchorMin = new Vector2(0.5f, 0.5f);
                    crt.anchorMax = new Vector2(0.5f, 0.5f);
                    crt.pivot     = new Vector2(0.5f, 0.5f);
                    crt.sizeDelta = new Vector2(column.imageWidth, column.imageHeight);
                    go.name       = j + "Image";
                    if (cell.image == null)
                    {
                        img        = go.AddComponent <Image>();
                        img.sprite = this.table.fillerSprite;
                    }
                    else
                    {
                        img = cell.image;
                    }
                    cell.Initialize(this.table, row, column, j, rt, crt, img);
                }

                cell.rt.SetParent(row.cgrt, false);

                if (isHeader && (cell as HeaderCell).icon == null)
                {
                    go           = new GameObject();
                    go.name      = "Icon";
                    rt           = go.AddComponent <RectTransform>();
                    img          = go.AddComponent <Image>();
                    img.sprite   = this.table.fillerSprite;
                    img.color    = this.table.rowLineColor;
                    rt.anchorMin = new Vector2(1f, 0.5f);
                    rt.anchorMax = new Vector2(1f, 0.5f);
                    rt.pivot     = new Vector2(0f, 0.5f);
                    rt.SetParent(cell.crt, false);
                    rt.sizeDelta = new Vector2
                                       (this.table.headerIconWidth, this.table.headerIconHeight);
                    (cell as HeaderCell).icon = img;
                }
            } // for (int j = 0; j < this.table.columns.Count; j++)

            for (int j = 0; j < row.cells.Count; j++)
            {
                if (j <= this.table.columns.Count - 1)
                {
                    row.cells[j].gameObject.SetActive(true);
                }
                else
                {
                    row.cells[j].gameObject.SetActive(false);
                }
            }
        } // instantiateCells
        }         // doMeasure

        public void MeasureCell(Datum d, Element e, Column c)
        {
            if (c.columnType == Column.ColumnType.TEXT || d.isHeader || d.isFooter)
            {
                if (e == null)
                {
                    this.MeasureCellDone(c, d, e);
                    return;
                }
                if (string.IsNullOrEmpty(e.value))
                {
                    e.measuredWidth  = 0;
                    e.measuredHeight = 0;
                    this.MeasureCellDone(c, d, e);
                    return;
                }

      #if !TMP_PRESENT
                TextGenerationSettings settings;
                float pixelsPerUnit;
      #endif

                float   useW;
                float   mw;
                float   mh;
                Vector2 size;
                this.text.fontSize = c.CalcFont(d);

                this.text.text = e.value;

                if (c.measuredMaxWidth.HasValue)
                {
        #if TMP_PRESENT
                    this.text.enableWordWrapping = true;
        #else
                    this.text.horizontalOverflow = HorizontalWrapMode.Wrap;
        #endif
                    size = new Vector2(c.measuredMaxWidth.Value, 0);
                }
                else
                {
        #if TMP_PRESENT
                    this.text.enableWordWrapping = false;
                    size = new Vector2(this.table.root.rect.width, 0);
        #else
                    this.text.horizontalOverflow = HorizontalWrapMode.Overflow;
                    size = Vector2.zero;
        #endif
                }

      #if TMP_PRESENT
                this.text.rectTransform.offsetMin = new Vector2(size.x * -0.5f, 0);
                this.text.rectTransform.offsetMax = new Vector2(size.x * 0.5f, 0);
                this.text.ForceMeshUpdate();
                mw = this.text.preferredWidth;
                mh = this.text.preferredHeight;
      #else
                settings      = this.text.GetGenerationSettings(size);
                pixelsPerUnit = this.text.pixelsPerUnit;
                mw            = this.text.cachedTextGeneratorForLayout.GetPreferredWidth
                                    (e.value, settings) / pixelsPerUnit;
                mh = this.text.cachedTextGeneratorForLayout.GetPreferredHeight
                         (e.value, settings) / pixelsPerUnit;
      #endif

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                useW = c.maxWidth;
                if (c.measuredMaxWidth.HasValue &&
                    (c.measuredMaxWidth.Value < useW || useW <= 0))
                {
                    useW = c.measuredMaxWidth.Value;
                }

                if (useW > 0 && mw > useW)
                {
                    //Debug.Log("Doing Max Width on " + c.idx + " Row: " + d.uid + " Width: " + useW);
        #if TMP_PRESENT
                    this.text.enableWordWrapping = true;
        #else
                    this.text.horizontalOverflow = HorizontalWrapMode.Wrap;
        #endif

                    size = new Vector2(useW, 0);

        #if TMP_PRESENT
                    this.text.rectTransform.offsetMin = new Vector2(size.x * -0.5f, 0);
                    this.text.rectTransform.offsetMax = new Vector2(size.x * 0.5f, 0);
                    this.text.ForceMeshUpdate();
                    mw = this.text.preferredWidth;
                    mh = this.text.preferredHeight;
        #else
                    settings      = this.text.GetGenerationSettings(size);
                    pixelsPerUnit = this.text.pixelsPerUnit;
                    mw            = this.text.cachedTextGeneratorForLayout.GetPreferredWidth
                                        (e.value, settings) / pixelsPerUnit;
                    mh = this.text.cachedTextGeneratorForLayout.GetPreferredHeight
                             (e.value, settings) / pixelsPerUnit;
        #endif
                }

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                mw = Mathf.Max(c.minWidth, mw);

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                if (c.measuredMinWidth.HasValue && c.measuredMinWidth.Value > mw)
                {
                    mw = c.measuredMinWidth.Value;
                }

                //if (c.idx == 0) Debug.Log("Col0 Width: " + mw + " Row: " + d.uid + " Value: " + e.value);

                e.measuredWidth  = mw;
                e.measuredHeight = mh;
                this.MeasureCellDone(c, d, e);
            } // if TEXT
            else
            {
                e.measuredWidth  = c.imageWidth;
                e.measuredHeight = c.imageHeight;
                this.MeasureCellDone(c, d, e);
            }
        }