OSDN Git Service

scene2d, added Actor clipBegin/clipEnd to simplify using ScissorStack with screen...
authorNathanSweet <nathan.sweet@gmail.com>
Tue, 20 Nov 2012 02:56:56 +0000 (18:56 -0800)
committerNathanSweet <nathan.sweet@gmail.com>
Tue, 20 Nov 2012 02:56:56 +0000 (18:56 -0800)
gdx/src/com/badlogic/gdx/scenes/scene2d/Actor.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Table.java
gdx/src/com/badlogic/gdx/scenes/scene2d/utils/ScissorStack.java

index d0873ea..e2e21ff 100644 (file)
@@ -19,8 +19,10 @@ package com.badlogic.gdx.scenes.scene2d;
 import com.badlogic.gdx.graphics.Color;\r
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
 import com.badlogic.gdx.math.MathUtils;\r
+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.utils.ScissorStack;\r
 import com.badlogic.gdx.utils.Array;\r
 import com.badlogic.gdx.utils.DelayedRemovalArray;\r
 import com.badlogic.gdx.utils.GdxRuntimeException;\r
@@ -511,6 +513,33 @@ public class Actor {
                return parent.getChildren().indexOf(this, true);\r
        }\r
 \r
+       /** Calls {@link #clipBegin(float, float, float, float)} to clip this actor's bounds. */\r
+       public boolean clipBegin () {\r
+               return clipBegin(getX(), getY(), getWidth(), getHeight());\r
+       }\r
+\r
+       /** Clips the specified screen aligned rectangle, specified relative to the transform matrix of the stage's SpriteBatch. The\r
+        * transform matrix and the stage's camera must not have rotational components. Calling this method must be followed by a call\r
+        * to {@link #clipEnd()} if true is returned.\r
+        * @return false if the clipping area is zero and no drawing should occur.\r
+        * @see ScissorStack */\r
+       public boolean clipBegin (float x, float y, float width, float height) {\r
+               Rectangle tableBounds = Rectangle.tmp;\r
+               tableBounds.x = x;\r
+               tableBounds.y = y;\r
+               tableBounds.width = width;\r
+               tableBounds.height = height;\r
+               Stage stage = getStage();\r
+               Rectangle scissorBounds = Pools.obtain(Rectangle.class);\r
+               ScissorStack.calculateScissors(stage.getCamera(), stage.getSpriteBatch().getTransformMatrix(), tableBounds, scissorBounds);\r
+               return ScissorStack.pushScissors(scissorBounds);\r
+       }\r
+\r
+       /** Ends clipping begun by {@link #clipBegin(float, float, float, float)}. */\r
+       public void clipEnd () {\r
+               Pools.free(ScissorStack.popScissors());\r
+       }\r
+\r
        /** Transforms the specified point in screen coordinates to the actor's local coordinate system. */\r
        public Vector2 screenToLocalCoordinates (Vector2 screenCoords) {\r
                Stage stage = getStage();\r
index 0d47642..60a4fdd 100644 (file)
@@ -32,10 +32,10 @@ import com.esotericsoftware.tablelayout.Cell;
 import com.esotericsoftware.tablelayout.Toolkit;\r
 import com.esotericsoftware.tablelayout.Value;\r
 \r
+import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.graphics.Color;\r
+import com.badlogic.gdx.graphics.GL10;\r
 import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
-import com.badlogic.gdx.math.Matrix4;\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.Stage;\r
@@ -43,7 +43,6 @@ import com.badlogic.gdx.scenes.scene2d.Touchable;
 import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;\r
 import com.badlogic.gdx.scenes.scene2d.utils.Align;\r
 import com.badlogic.gdx.scenes.scene2d.utils.Drawable;\r
-import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;\r
 import com.badlogic.gdx.utils.Array;\r
 \r
 import java.util.List;\r
@@ -61,7 +60,6 @@ public class Table extends WidgetGroup {
        private final TableLayout layout;\r
        private Drawable background;\r
        private boolean clip;\r
-       private Rectangle scissorBounds;\r
        private Skin skin;\r
 \r
        public Table () {\r
@@ -83,9 +81,12 @@ public class Table extends WidgetGroup {
                if (isTransform()) {\r
                        applyTransform(batch, computeTransform());\r
                        if (clip) {\r
-                               if (ScissorStack.pushScissors(calculateScissors(batch.getTransformMatrix()))) {\r
+                               boolean draw = background == null ? clipBegin(0, 0, getWidth(), getHeight()) : clipBegin(layout.getPadLeft(),\r
+                                       layout.getPadBottom(), getWidth() - layout.getPadLeft() - layout.getPadRight(),\r
+                                       getHeight() - layout.getPadBottom() - layout.getPadTop());\r
+                               if (draw) {\r
                                        drawChildren(batch, parentAlpha);\r
-                                       ScissorStack.popScissors();\r
+                                       clipEnd();\r
                                }\r
                        } else\r
                                drawChildren(batch, parentAlpha);\r
@@ -104,24 +105,6 @@ public class Table extends WidgetGroup {
                }\r
        }\r
 \r
-       private Rectangle calculateScissors (Matrix4 transform) {\r
-               Rectangle tableBounds = Rectangle.tmp;\r
-               tableBounds.width = getWidth();\r
-               tableBounds.height = getHeight();\r
-               if (background == null) {\r
-                       tableBounds.x = 0;\r
-                       tableBounds.y = 0;\r
-               } else {\r
-                       tableBounds.x = layout.getPadLeft();\r
-                       tableBounds.y = layout.getPadBottom();\r
-                       tableBounds.width -= tableBounds.x + layout.getPadRight();\r
-                       tableBounds.height -= tableBounds.y + layout.getPadTop();\r
-               }\r
-               if (scissorBounds == null) scissorBounds = new Rectangle();\r
-               ScissorStack.calculateScissors(getStage().getCamera(), transform, tableBounds, scissorBounds);\r
-               return scissorBounds;\r
-       }\r
-\r
        public void invalidate () {\r
                layout.invalidate();\r
                super.invalidate();\r
index 11f0213..578f45c 100644 (file)
@@ -70,14 +70,15 @@ public class ScissorStack {
 \r
        /** Pops the current scissor rectangle from the stack and sets the new scissor area to the new top of stack rectangle. In case\r
         * no more rectangles are on the stack, {@link GL10#GL_SCISSOR_TEST} is disabled. */\r
-       public static void popScissors () {\r
-               scissors.pop();\r
+       public static Rectangle popScissors () {\r
+               Rectangle old = scissors.pop();\r
                if (scissors.size == 0)\r
                        Gdx.gl.glDisable(GL10.GL_SCISSOR_TEST);\r
                else {\r
                        Rectangle scissor = scissors.peek();\r
                        Gdx.gl.glScissor((int)scissor.x, (int)scissor.y, (int)scissor.width, (int)scissor.height);\r
                }\r
+               return old;\r
        }\r
 \r
        private static void fix (Rectangle rect) {\r