OSDN Git Service

Added Actor#sizeChanged. Widget/WidgetGroup call invalidate in it.
authorNathanSweet <nathan.sweet@gmail.com>
Thu, 10 Oct 2013 13:56:45 +0000 (15:56 +0200)
committerNathanSweet <nathan.sweet@gmail.com>
Thu, 10 Oct 2013 13:56:45 +0000 (15:56 +0200)
gdx/src/com/badlogic/gdx/scenes/scene2d/Actor.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ScrollPane.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/SplitPane.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Stack.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TableLayout.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Widget.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/WidgetGroup.java
gdx/src/com/esotericsoftware/tablelayout/Cell.java

index 83d02f4..6122b17 100644 (file)
@@ -22,13 +22,11 @@ import com.badlogic.gdx.math.MathUtils;
 import com.badlogic.gdx.math.Rectangle;\r
 import com.badlogic.gdx.math.Vector2;\r
 import com.badlogic.gdx.scenes.scene2d.InputEvent.Type;\r
-import com.badlogic.gdx.scenes.scene2d.actions.Actions;\r
 import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;\r
 import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;\r
 import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;\r
 import com.badlogic.gdx.utils.Array;\r
 import com.badlogic.gdx.utils.DelayedRemovalArray;\r
-import com.badlogic.gdx.utils.GdxRuntimeException;\r
 import com.badlogic.gdx.utils.Pools;\r
 \r
 /** 2D scene graph node. An actor has a position, rectangular size, origin, scale, rotation, Z index, and color. The position\r
@@ -368,7 +366,9 @@ public class Actor {
        }\r
 \r
        public void setWidth (float width) {\r
+               float oldWidth = this.width;\r
                this.width = width;\r
+               if (width != oldWidth) sizeChanged();\r
        }\r
 \r
        public float getHeight () {\r
@@ -376,7 +376,9 @@ public class Actor {
        }\r
 \r
        public void setHeight (float height) {\r
+               float oldHeight = this.height;\r
                this.height = height;\r
+               if (height != oldHeight) sizeChanged();\r
        }\r
 \r
        /** Returns y plus height. */\r
@@ -389,30 +391,42 @@ public class Actor {
                return x + width;\r
        }\r
 \r
+       /** Called when the actor's size has been changed. */\r
+       protected void sizeChanged () {\r
+       }\r
+\r
        /** Sets the width and height. */\r
        public void setSize (float width, float height) {\r
+               float oldWidth = this.width;\r
+               float oldHeight = this.height;\r
                this.width = width;\r
                this.height = height;\r
+               if (width != oldWidth || height != oldHeight) sizeChanged();\r
        }\r
 \r
        /** Adds the specified size to the current size. */\r
        public void size (float size) {\r
                width += size;\r
                height += size;\r
+               sizeChanged();\r
        }\r
 \r
        /** Adds the specified size to the current size. */\r
        public void size (float width, float height) {\r
                this.width += width;\r
                this.height += height;\r
+               sizeChanged();\r
        }\r
 \r
        /** Set bounds the x, y, width, and height. */\r
        public void setBounds (float x, float y, float width, float height) {\r
+               float oldWidth = this.width;\r
+               float oldHeight = this.height;\r
                this.x = x;\r
                this.y = y;\r
                this.width = width;\r
                this.height = height;\r
+               if (width != oldWidth || height != oldHeight) sizeChanged();\r
        }\r
 \r
        public float getOriginX () {\r
index c030e5f..89ad9a7 100644 (file)
@@ -508,17 +508,8 @@ public class ScrollPane extends WidgetGroup {
                        }\r
                }\r
 \r
-               if (widget.getWidth() != widgetWidth || widget.getHeight() != widgetHeight) {\r
-                       widget.setWidth(widgetWidth);\r
-                       widget.setHeight(widgetHeight);\r
-                       if (widget instanceof Layout) {\r
-                               Layout layout = (Layout)widget;\r
-                               layout.invalidate();\r
-                               layout.validate();\r
-                       }\r
-               } else {\r
-                       if (widget instanceof Layout) ((Layout)widget).validate();\r
-               }\r
+               widget.setSize(widgetWidth, widgetHeight);\r
+               if (widget instanceof Layout) ((Layout)widget).validate();\r
        }\r
 \r
        @Override\r
index 089f1ab..43381cd 100644 (file)
@@ -149,38 +149,16 @@ public class SplitPane extends WidgetGroup {
                        calculateVertBoundsAndPositions();\r
 \r
                Actor firstWidget = this.firstWidget;\r
-               Rectangle firstWidgetBounds = this.firstWidgetBounds;\r
                if (firstWidget != null) {\r
-                       firstWidget.setX(firstWidgetBounds.x);\r
-                       firstWidget.setY(firstWidgetBounds.y);\r
-                       if (firstWidget.getWidth() != firstWidgetBounds.width || firstWidget.getHeight() != firstWidgetBounds.height) {\r
-                               firstWidget.setWidth(firstWidgetBounds.width);\r
-                               firstWidget.setHeight(firstWidgetBounds.height);\r
-                               if (firstWidget instanceof Layout) {\r
-                                       Layout layout = (Layout)firstWidget;\r
-                                       layout.invalidate();\r
-                                       layout.validate();\r
-                               }\r
-                       } else {\r
-                               if (firstWidget instanceof Layout) ((Layout)firstWidget).validate();\r
-                       }\r
+                       Rectangle firstWidgetBounds = this.firstWidgetBounds;\r
+                       firstWidget.setBounds(firstWidgetBounds.x, firstWidgetBounds.y, firstWidgetBounds.width, firstWidgetBounds.height);\r
+                       if (firstWidget instanceof Layout) ((Layout)firstWidget).validate();\r
                }\r
                Actor secondWidget = this.secondWidget;\r
-               Rectangle secondWidgetBounds = this.secondWidgetBounds;\r
                if (secondWidget != null) {\r
-                       secondWidget.setX(secondWidgetBounds.x);\r
-                       secondWidget.setY(secondWidgetBounds.y);\r
-                       if (secondWidget.getWidth() != secondWidgetBounds.width || secondWidget.getHeight() != secondWidgetBounds.height) {\r
-                               secondWidget.setWidth(secondWidgetBounds.width);\r
-                               secondWidget.setHeight(secondWidgetBounds.height);\r
-                               if (secondWidget instanceof Layout) {\r
-                                       Layout layout = (Layout)secondWidget;\r
-                                       layout.invalidate();\r
-                                       layout.validate();\r
-                               }\r
-                       } else {\r
-                               if (secondWidget instanceof Layout) ((Layout)secondWidget).validate();\r
-                       }\r
+                       Rectangle secondWidgetBounds = this.secondWidgetBounds;\r
+                       secondWidget.setBounds(secondWidgetBounds.x, secondWidgetBounds.y, secondWidgetBounds.width, secondWidgetBounds.height);\r
+                       if (secondWidget instanceof Layout) ((Layout)secondWidget).validate();\r
                }\r
        }\r
 \r
index 93993da..770a9e9 100644 (file)
@@ -97,19 +97,8 @@ public class Stack extends WidgetGroup {
                Array<Actor> children = getChildren();\r
                for (int i = 0, n = children.size; i < n; i++) {\r
                        Actor child = children.get(i);\r
-                       child.setX(0);\r
-                       child.setY(0);\r
-                       if (child.getWidth() != width || child.getHeight() != height) {\r
-                               child.setWidth(width);\r
-                               child.setHeight(height);\r
-                               if (child instanceof Layout) {\r
-                                       Layout layout = (Layout)child;\r
-                                       layout.invalidate();\r
-                                       layout.validate();\r
-                               }\r
-                       } else {\r
-                               if (child instanceof Layout) ((Layout)child).validate();\r
-                       }\r
+                       child.setBounds(0, 0, width, height);\r
+                       if (child instanceof Layout) ((Layout)child).validate();\r
                }\r
        }\r
 \r
index a88f54e..63137d4 100644 (file)
@@ -26,7 +26,6 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer;\r
 import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer10;\r
 import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20;\r
-import com.badlogic.gdx.math.Vector2;\r
 import com.badlogic.gdx.scenes.scene2d.Actor;\r
 import com.badlogic.gdx.scenes.scene2d.Group;\r
 import com.badlogic.gdx.scenes.scene2d.ui.TableToolkit.DebugRect;\r
@@ -60,43 +59,19 @@ class TableLayout extends BaseTableLayout<Actor, Table, TableLayout, TableToolki
                                float widgetHeight = Math.round(c.getWidgetHeight());\r
                                float widgetX = Math.round(c.getWidgetX());\r
                                float widgetY = height - Math.round(c.getWidgetY()) - widgetHeight;\r
-                               c.setWidgetX(widgetX);\r
-                               c.setWidgetY(widgetY);\r
-                               c.setWidgetWidth(widgetWidth);\r
-                               c.setWidgetHeight(widgetHeight);\r
+                               c.setWidgetBounds(widgetX, widgetY, widgetWidth, widgetHeight);\r
                                Actor actor = (Actor)c.getWidget();\r
-                               if (actor != null) {\r
-                                       actor.setX(widgetX);\r
-                                       actor.setY(widgetY);\r
-                                       if (actor.getWidth() != widgetWidth || actor.getHeight() != widgetHeight) {\r
-                                               actor.setWidth(widgetWidth);\r
-                                               actor.setHeight(widgetHeight);\r
-                                               if (actor instanceof Layout) ((Layout)actor).invalidate();\r
-                                       }\r
-                               }\r
+                               if (actor != null) actor.setBounds(widgetX, widgetY, widgetWidth, widgetHeight);\r
                        }\r
                } else {\r
                        for (int i = 0, n = cells.size(); i < n; i++) {\r
                                Cell c = cells.get(i);\r
                                if (c.getIgnore()) continue;\r
-                               float widgetWidth = c.getWidgetWidth();\r
                                float widgetHeight = c.getWidgetHeight();\r
-                               float widgetX = c.getWidgetX();\r
                                float widgetY = height - c.getWidgetY() - widgetHeight;\r
-                               c.setWidgetX(widgetX);\r
                                c.setWidgetY(widgetY);\r
-                               c.setWidgetWidth(widgetWidth);\r
-                               c.setWidgetHeight(widgetHeight);\r
                                Actor actor = (Actor)c.getWidget();\r
-                               if (actor != null) {\r
-                                       actor.setX(widgetX);\r
-                                       actor.setY(widgetY);\r
-                                       if (actor.getWidth() != widgetWidth || actor.getHeight() != widgetHeight) {\r
-                                               actor.setWidth(widgetWidth);\r
-                                               actor.setHeight(widgetHeight);\r
-                                               if (actor instanceof Layout) ((Layout)actor).invalidate();\r
-                                       }\r
-                               }\r
+                               if (actor != null) actor.setBounds(c.getWidgetX(), widgetY, c.getWidgetWidth(), widgetHeight);\r
                        }\r
                }\r
                // Validate children separately from sizing actors to ensure actors without a cell are validated.\r
index 678c0d4..968a443 100644 (file)
@@ -80,11 +80,7 @@ public class Widget extends Actor implements Layout {
                                parentWidth = parent.getWidth();\r
                                parentHeight = parent.getHeight();\r
                        }\r
-                       if (getWidth() != parentWidth || getHeight() != parentHeight) {\r
-                               setWidth(parentWidth);\r
-                               setHeight(parentHeight);\r
-                               invalidate();\r
-                       }\r
+                       setSize(parentWidth, parentHeight);\r
                }\r
 \r
                if (!needsLayout) return;\r
@@ -108,14 +104,12 @@ public class Widget extends Actor implements Layout {
                if (parent instanceof Layout) ((Layout)parent).invalidateHierarchy();\r
        }\r
 \r
+       protected void sizeChanged () {\r
+               invalidate();\r
+       }\r
+\r
        public void pack () {\r
-               float newWidth = getPrefWidth();\r
-               float newHeight = getPrefHeight();\r
-               if (newWidth != getWidth() || newHeight != getHeight()) {\r
-                       setWidth(newWidth);\r
-                       setHeight(newHeight);\r
-                       invalidate();\r
-               }\r
+               setSize(getPrefWidth(), getPrefHeight());\r
                validate();\r
        }\r
 \r
index b4390ca..6af76cc 100644 (file)
@@ -125,6 +125,10 @@ public class WidgetGroup extends Group implements Layout {
                invalidateHierarchy();\r
        }\r
 \r
+       protected void sizeChanged () {\r
+               invalidate();\r
+       }\r
+\r
        public void pack () {\r
                float newWidth = getPrefWidth();\r
                float newHeight = getPrefHeight();\r
index 0473d71..4b53892 100644 (file)
@@ -670,6 +670,13 @@ public class Cell<C> {
                return this;\r
        }\r
 \r
+       public void setWidgetBounds (float x, float y, float width, float height) {\r
+               widgetX = x;\r
+               widgetY = y;\r
+               widgetWidth = width;\r
+               widgetHeight = height;\r
+       }\r
+\r
        public float getWidgetX () {\r
                return widgetX;\r
        }\r