OSDN Git Service

Pooled cells.
authorNathanSweet <nathan.sweet@gmail.com>
Tue, 28 May 2013 14:39:54 +0000 (16:39 +0200)
committerNathanSweet <nathan.sweet@gmail.com>
Tue, 28 May 2013 14:39:54 +0000 (16:39 +0200)
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TableToolkit.java
gdx/src/com/esotericsoftware/tablelayout/BaseTableLayout.java
gdx/src/com/esotericsoftware/tablelayout/Cell.java
gdx/src/com/esotericsoftware/tablelayout/Toolkit.java

index 4f7b07d..38a0be9 100644 (file)
 \r
 package com.badlogic.gdx.scenes.scene2d.ui;\r
 \r
+import com.esotericsoftware.tablelayout.BaseTableLayout.Debug;\r
+import com.esotericsoftware.tablelayout.Cell;\r
+import com.esotericsoftware.tablelayout.Toolkit;\r
+\r
 import com.badlogic.gdx.math.Rectangle;\r
 import com.badlogic.gdx.scenes.scene2d.Actor;\r
 import com.badlogic.gdx.scenes.scene2d.Group;\r
 import com.badlogic.gdx.scenes.scene2d.utils.Layout;\r
 import com.badlogic.gdx.utils.Array;\r
-import com.esotericsoftware.tablelayout.BaseTableLayout.Debug;\r
-import com.esotericsoftware.tablelayout.Toolkit;\r
+import com.badlogic.gdx.utils.Pool;\r
+import com.badlogic.gdx.utils.ReflectionPool;\r
 \r
 /** The libgdx implementation of the table layout functionality.\r
  * @author Nathan Sweet */\r
 class TableToolkit extends Toolkit<Actor, Table, TableLayout> {\r
        static boolean drawDebug;\r
+       static Pool<Cell> cellPool = new Pool() {\r
+               protected Cell newObject () {\r
+                       return new Cell();\r
+               }\r
+       };\r
+\r
+       public Cell obtainCell (TableLayout layout) {\r
+               Cell cell = cellPool.obtain();\r
+               cell.setLayout(layout);\r
+               return cell;\r
+       }\r
+\r
+       public void freeCell (Cell cell) {\r
+               cell.setLayout(null);\r
+               cellPool.free(cell);\r
+       }\r
 \r
        public void addChild (Actor parent, Actor child) {\r
                child.remove();\r
index 7a3886c..27f1632 100644 (file)
@@ -84,7 +84,7 @@ abstract public class BaseTableLayout<C, T extends C, L extends BaseTableLayout,
 \r
        /** Adds a new cell to the table with the specified widget. */\r
        public Cell<C> add (C widget) {\r
-               Cell cell = new Cell(this);\r
+               Cell cell = toolkit.obtainCell((L)this);\r
                cell.widget = widget;\r
 \r
                if (cells.size() > 0) {\r
@@ -127,7 +127,7 @@ abstract public class BaseTableLayout<C, T extends C, L extends BaseTableLayout,
         * for all cells in the new row. */\r
        public Cell row () {\r
                if (cells.size() > 0) endRow();\r
-               rowDefaults = new Cell(this);\r
+               rowDefaults = toolkit.obtainCell((L)this);\r
                return rowDefaults;\r
        }\r
 \r
@@ -149,7 +149,7 @@ abstract public class BaseTableLayout<C, T extends C, L extends BaseTableLayout,
        public Cell columnDefaults (int column) {\r
                Cell cell = columnDefaults.size() > column ? columnDefaults.get(column) : null;\r
                if (cell == null) {\r
-                       cell = new Cell(this);\r
+                       cell = toolkit.obtainCell((L)this);\r
                        if (column >= columnDefaults.size()) {\r
                                for (int i = columnDefaults.size(); i < column; i++)\r
                                        columnDefaults.add(null);\r
@@ -591,7 +591,7 @@ abstract public class BaseTableLayout<C, T extends C, L extends BaseTableLayout,
                        if (prefWidth < minWidth) prefWidth = minWidth;\r
                        if (maxWidth > 0 && prefWidth > maxWidth) prefWidth = maxWidth;\r
 \r
-                       float spannedMinWidth = 0, spannedPrefWidth = 0;\r
+                       float spannedMinWidth = -(c.computedPadLeft + c.computedPadRight), spannedPrefWidth = spannedMinWidth;\r
                        for (int column = c.column, nn = column + c.colspan; column < nn; column++) {\r
                                spannedMinWidth += columnMinWidth[column];\r
                                spannedPrefWidth += columnPrefWidth[column];\r
@@ -681,25 +681,19 @@ abstract public class BaseTableLayout<C, T extends C, L extends BaseTableLayout,
                float hpadding = w(padLeft) + w(padRight);\r
                float vpadding = h(padTop) + h(padBottom);\r
 \r
-               // totalMinWidth/totalMinHeight are needed because tableMinWidth/tableMinHeight could be based on this.width or this.height.\r
-               float totalMinWidth = 0, totalMinHeight = 0;\r
                float totalExpandWidth = 0, totalExpandHeight = 0;\r
-               for (int i = 0; i < columns; i++) {\r
-                       totalMinWidth += columnMinWidth[i];\r
+               for (int i = 0; i < columns; i++)\r
                        totalExpandWidth += expandWidth[i];\r
-               }\r
-               for (int i = 0; i < rows; i++) {\r
-                       totalMinHeight += rowMinHeight[i];\r
+               for (int i = 0; i < rows; i++)\r
                        totalExpandHeight += expandHeight[i];\r
-               }\r
 \r
                // Size columns and rows between min and pref size using (preferred - min) size to weight distribution of extra space.\r
                float[] columnWeightedWidth;\r
-               float totalGrowWidth = tablePrefWidth - totalMinWidth;\r
+               float totalGrowWidth = tablePrefWidth - tableMinWidth;\r
                if (totalGrowWidth == 0)\r
                        columnWeightedWidth = columnMinWidth;\r
                else {\r
-                       float extraWidth = Math.min(totalGrowWidth, Math.max(0, layoutWidth - totalMinWidth));\r
+                       float extraWidth = Math.min(totalGrowWidth, Math.max(0, layoutWidth - tableMinWidth));\r
                        columnWeightedWidth = this.columnWeightedWidth = ensureSize(this.columnWeightedWidth, columns);\r
                        for (int i = 0; i < columns; i++) {\r
                                float growWidth = columnPrefWidth[i] - columnMinWidth[i];\r
@@ -709,12 +703,12 @@ abstract public class BaseTableLayout<C, T extends C, L extends BaseTableLayout,
                }\r
 \r
                float[] rowWeightedHeight;\r
-               float totalGrowHeight = tablePrefHeight - totalMinHeight;\r
+               float totalGrowHeight = tablePrefHeight - tableMinHeight;\r
                if (totalGrowHeight == 0)\r
                        rowWeightedHeight = rowMinHeight;\r
                else {\r
                        rowWeightedHeight = this.rowWeightedHeight = ensureSize(this.rowWeightedHeight, rows);\r
-                       float extraHeight = Math.min(totalGrowHeight, Math.max(0, layoutHeight - totalMinHeight));\r
+                       float extraHeight = Math.min(totalGrowHeight, Math.max(0, layoutHeight - tableMinHeight));\r
                        for (int i = 0; i < rows; i++) {\r
                                float growHeight = rowPrefHeight[i] - rowMinHeight[i];\r
                                float growRatio = growHeight / (float)totalGrowHeight;\r
index b552f41..d356e77 100644 (file)
@@ -50,13 +50,16 @@ public class Cell<C> {
        float widgetX, widgetY;\r
        float widgetWidth, widgetHeight;\r
 \r
-       private final BaseTableLayout layout;\r
+       private BaseTableLayout layout;\r
        boolean endRow;\r
        int column, row;\r
        int cellAboveIndex = -1;\r
        float computedPadTop, computedPadLeft, computedPadBottom, computedPadRight;\r
 \r
-       Cell (BaseTableLayout layout) {\r
+       public Cell () {\r
+       }\r
+\r
+       public void setLayout (BaseTableLayout layout) {\r
                this.layout = layout;\r
        }\r
 \r
@@ -907,7 +910,8 @@ public class Cell<C> {
        }\r
 \r
        static Cell defaults (BaseTableLayout layout) {\r
-               Cell defaults = new Cell(layout);\r
+               Cell defaults = new Cell();\r
+               defaults.layout = layout;\r
                defaults.minWidth = Value.minWidth;\r
                defaults.minHeight = Value.minHeight;\r
                defaults.prefWidth = Value.prefWidth;\r
index 6e8e248..1a8b5dc 100644 (file)
@@ -34,6 +34,10 @@ import com.esotericsoftware.tablelayout.BaseTableLayout.Debug;
 public abstract class Toolkit<C, T extends C, L extends BaseTableLayout> {\r
        static public Toolkit instance;\r
 \r
+       abstract public Cell obtainCell (L layout);\r
+       \r
+       abstract public void freeCell (Cell cell);\r
+\r
        abstract public void addChild (C parent, C child);\r
 \r
        abstract public void removeChild (C parent, C child);\r