OSDN Git Service

[added] simple LinearLayout. Works so far
authorbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Fri, 22 Oct 2010 00:51:59 +0000 (00:51 +0000)
committerbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Fri, 22 Oct 2010 00:51:59 +0000 (00:51 +0000)
[todo] fix Group.toChildCoordinates(). Rotation code of origin is borked. wtf was i thinking...

gdx/src/com/badlogic/gdx/scenes/scene2d/Actor.java
gdx/src/com/badlogic/gdx/scenes/scene2d/Group.java
gdx/src/com/badlogic/gdx/scenes/scene2d/actors/BoundGroup.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/scenes/scene2d/actors/Button.java
gdx/src/com/badlogic/gdx/scenes/scene2d/actors/Image.java
gdx/src/com/badlogic/gdx/scenes/scene2d/actors/Label.java
gdx/src/com/badlogic/gdx/scenes/scene2d/actors/LinearGroup.java [new file with mode: 0644]

index d85f487..2c763b5 100644 (file)
@@ -48,8 +48,8 @@ public abstract class Actor
        public float y;\r
        public float width;\r
        public float height;\r
-       public float refX;\r
-       public float refY;\r
+       public float originX;\r
+       public float originY;\r
        public float scaleX = 1;\r
        public float scaleY = 1;\r
        public float rotation;\r
@@ -118,7 +118,7 @@ public abstract class Actor
        \r
        public String toString( )\r
        {\r
-               return name + ": [x=" + x + ", y=" + y + ", refX=" + refX + ", refY=" + refY + ", width=" + width + ", height=" + height + "]";\r
+               return name + ": [x=" + x + ", y=" + y + ", refX=" + originX + ", refY=" + originY + ", width=" + width + ", height=" + height + "]";\r
        }\r
        \r
 }\r
index 2733da4..ba3b406 100644 (file)
@@ -71,14 +71,14 @@ public class Group extends Actor
        private void updateTransform( )\r
        {\r
                transform.idt();\r
-               if( refX != 0 || refY != 0 )\r
-                       transform.setToTranslation( refX, refY );\r
+               if( originX != 0 || originY != 0 )\r
+                       transform.setToTranslation( originX, originY );\r
                if( scaleX != 1 || scaleY != 1 )\r
                        transform.mul( scenetransform.setToScaling( scaleX, scaleY ) );\r
                if( rotation != 0 )\r
                        transform.mul( scenetransform.setToRotation( rotation ) );\r
-               if( refX != 0 || refY != 0 )\r
-                       transform.mul( scenetransform.setToTranslation( -refX, -refY ) );\r
+               if( originX != 0 || originY != 0 )\r
+                       transform.mul( scenetransform.setToTranslation( -originX, -originY ) );\r
                if( x != 0 || y != 0 )\r
                {\r
                        transform.getValues()[6] += x;\r
@@ -115,7 +115,7 @@ public class Group extends Actor
                tmp4.set( scenetransform );             \r
                \r
                if( debug && debugTexture != null )\r
-                       batch.draw( debugTexture, x, y, refX, refY, 200, 200, scaleX, scaleY, rotation, 0, 0, debugTexture.getWidth(), debugTexture.getHeight(), Color.WHITE, false, false );\r
+                       batch.draw( debugTexture, x, y, originX, originY, width==0?200:width, height==0?200:height, scaleX, scaleY, rotation, 0, 0, debugTexture.getWidth(), debugTexture.getHeight(), Color.WHITE, false, false );\r
                \r
                batch.end();            \r
                oldBatchTransform.set(batch.getTransformMatrix());\r
@@ -148,15 +148,15 @@ public class Group extends Actor
                        }\r
                        else\r
                        {\r
-                               if( child.refX == 0 && child.refY == 0 )\r
+                               if( child.originX == 0 && child.originY == 0 )\r
                                {\r
                                        out.x = (x - child.x) / child.scaleX;\r
                                        out.y = (y - child.y) / child.scaleY;\r
                                }\r
                                else\r
                                {\r
-                                       out.x = x / child.scaleX - (child.x - child.refX);\r
-                                       out.x = x / child.scaleX - (child.x - child.refX);\r
+                                       out.x = x / child.scaleX - (child.x - child.originX);\r
+                                       out.x = x / child.scaleX - (child.x - child.originX);\r
                                }\r
                        }\r
                }\r
@@ -167,7 +167,7 @@ public class Group extends Actor
                        \r
                        if( child.scaleX == 1 && child.scaleY == 1 )\r
                        {\r
-                               if( child.refX == 0 && child.refY == 0 )\r
+                               if( child.originX == 0 && child.originY == 0 )\r
                                {       \r
                                        float tox = x - child.x;\r
                                        float toy = y - child.y;\r
@@ -177,11 +177,11 @@ public class Group extends Actor
                                }\r
                                else\r
                                {\r
-                                       float refX = -sin * child.refX + cos * child.refY;\r
-                                       float refY =  cos * child.refX + sin * child.refY;\r
+                                       float refX = -sin * child.originX + cos * child.originY;\r
+                                       float refY =  cos * child.originX + sin * child.originY;\r
                                        \r
-                                       float px = child.x + child.refX - refX;\r
-                                       float py = child.y + child.refY - refY;\r
+                                       float px = child.x + child.originX - refX;\r
+                                       float py = child.y + child.originY - refY;\r
                                        \r
                                        float tox = x - px;\r
                                        float toy = y - py;\r
@@ -192,7 +192,7 @@ public class Group extends Actor
                        }\r
                        else\r
                        {\r
-                               if( child.refX == 0 && child.refY == 0 )\r
+                               if( child.originX == 0 && child.originY == 0 )\r
                                {       \r
                                        float tox = x - child.x;\r
                                        float toy = y - child.y;\r
@@ -205,14 +205,14 @@ public class Group extends Actor
                                }\r
                                else\r
                                {\r
-                                       float srefX = child.refX * child.scaleY;\r
-                                       float srefY = child.refY * child.scaleX;\r
+                                       float srefX = child.originX * child.scaleY;\r
+                                       float srefY = child.originY * child.scaleX;\r
                                        \r
                                        float refX = -sin * srefX + cos * srefY;\r
                                        float refY =  cos * srefX + sin * srefY;\r
                                        \r
-                                       float px = child.x + child.refX - refX;\r
-                                       float py = child.y + child.refY - refY;\r
+                                       float px = child.x + child.originX - refX;\r
+                                       float py = child.y + child.originY - refY;\r
                                        \r
                                        float tox = x - px;\r
                                        float toy = y - py;\r
@@ -233,6 +233,9 @@ public class Group extends Actor
                if( !touchable )\r
                        return false;\r
                \r
+               if( debug )\r
+                       Gdx.app.log( "Group", name + ": " + x + ", " + y );\r
+               \r
                if( focusedActor != null )\r
                {\r
                        point.x = x; point.y = y;\r
diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/BoundGroup.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/BoundGroup.java
new file mode 100644 (file)
index 0000000..eb86d24
--- /dev/null
@@ -0,0 +1,69 @@
+package com.badlogic.gdx.scenes.scene2d.actors;\r
+\r
+import com.badlogic.gdx.scenes.scene2d.Actor;\r
+import com.badlogic.gdx.scenes.scene2d.Group;\r
+\r
+/**\r
+ * This is a group that respects its width and height. Useful for implementing\r
+ * layouts.\r
+ * @author mzechner\r
+ *\r
+ */\r
+public class BoundGroup extends Group \r
+{\r
+       public BoundGroup(String name, float width, float height ) \r
+       {\r
+               super(name);    \r
+               this.width = width;\r
+               this.height = height;\r
+               this.originX = width / 2;\r
+               this.originY = height / 2;\r
+       }\r
+       \r
+       protected boolean touchDown( float x, float y, int pointer )\r
+       {\r
+               if( focusedActor != null )\r
+               {\r
+                       return super.touchDown(x, y, pointer);\r
+               }\r
+               else\r
+               {\r
+                       if( !(x > 0 && y > 0 && x < width && y < height) )\r
+                               return false;                   \r
+                       return super.touchDown( x, y, pointer );\r
+               }               \r
+       }\r
+       \r
+       protected boolean touchUp( float x, float y, int pointer )\r
+       {\r
+               if( focusedActor != null )\r
+               {\r
+                       return super.touchUp(x, y, pointer);\r
+               }\r
+               else\r
+               {\r
+                       if( !(x > 0 && y > 0 && x < width && y < height) )\r
+                               return false;\r
+                       return super.touchUp(x, y, pointer);\r
+               }               \r
+       }\r
+       \r
+       protected boolean touchDragged( float x, float y, int pointer )\r
+       {\r
+               if( focusedActor != null )\r
+               {\r
+                       return super.touchDragged(x, y, pointer);\r
+               }\r
+               else\r
+               {\r
+                       if( !(x > 0 && y > 0 && x < width && y < height) )\r
+                               return false;\r
+                       return super.touchDragged(x, y, pointer);\r
+               }       \r
+       }\r
+       \r
+       public Actor hit( float x, float y )\r
+       {\r
+               return x > 0 && y > 0 && x < width && y < height? this: null;\r
+       }\r
+}\r
index 17def50..b9b34dc 100644 (file)
@@ -27,8 +27,8 @@ public class Button extends Actor
        public Button( String name, Texture texture )\r
        {\r
                super( name );\r
-               this.refX = texture.getWidth() / 2.0f;\r
-               this.refY = texture.getHeight() / 2.0f;\r
+               this.originX = texture.getWidth() / 2.0f;\r
+               this.originY = texture.getHeight() / 2.0f;\r
                this.width = texture.getWidth();\r
                this.height = texture.getHeight();\r
                this.pressedRegion = new TextureRegion( texture, 0, 0, texture.getWidth(), texture.getHeight() );\r
@@ -43,8 +43,8 @@ public class Button extends Actor
        public Button( String name, TextureRegion unpressedRegion, TextureRegion pressedRegion )\r
        {\r
                super( name );\r
-               this.refX = unpressedRegion.width / 2.0f;\r
-               this.refY = unpressedRegion.height / 2.0f;\r
+               this.originX = unpressedRegion.width / 2.0f;\r
+               this.originY = unpressedRegion.height / 2.0f;\r
                this.width = unpressedRegion.width;\r
                this.height = unpressedRegion.height;\r
                this.unpressedRegion = new TextureRegion( unpressedRegion.texture, unpressedRegion.x, unpressedRegion.y, unpressedRegion.width, unpressedRegion.height );\r
@@ -62,7 +62,7 @@ public class Button extends Actor
                        if( scaleX == 0 && scaleY == 0 && rotation == 0 )\r
                                batch.draw( region.texture, x, y, width, height, region.x, region.y, region.width, region.height, color, false, false );\r
                        else\r
-                               batch.draw( region.texture, x, y, refX, refY, width, height, scaleX, scaleY, rotation, region.x, region.y, region.width, region.height, color, false, false );\r
+                               batch.draw( region.texture, x, y, originX, originY, width, height, scaleX, scaleY, rotation, region.x, region.y, region.width, region.height, color, false, false );\r
                }\r
        }\r
 \r
index 8dd5783..629f442 100644 (file)
@@ -33,8 +33,8 @@ public class Image extends Actor
        public Image( String name, Texture texture )\r
        {\r
                super( name );\r
-               this.refX = texture.getWidth() / 2.0f;\r
-               this.refY = texture.getHeight() / 2.0f;\r
+               this.originX = texture.getWidth() / 2.0f;\r
+               this.originY = texture.getHeight() / 2.0f;\r
                this.width = texture.getWidth();\r
                this.height = texture.getHeight();\r
                this.region = new TextureRegion( texture, 0, 0, texture.getWidth(), texture.getHeight() );\r
@@ -43,8 +43,8 @@ public class Image extends Actor
        public Image( String name, TextureRegion region )\r
        {\r
                super( name );\r
-               this.refX = region.width / 2.0f;\r
-               this.refY = region.height / 2.0f;\r
+               this.originX = region.width / 2.0f;\r
+               this.originY = region.height / 2.0f;\r
                this.width = region.width;\r
                this.height = region.height;\r
                this.region = new TextureRegion( region.texture, region.x, region.y, region.width, region.height );\r
@@ -58,7 +58,7 @@ public class Image extends Actor
                        if( scaleX == 0 && scaleY == 0 && rotation == 0 )\r
                                batch.draw( region.texture, x, y, width, height, region.x, region.y, region.width, region.height, color, false, false );\r
                        else\r
-                               batch.draw( region.texture, x, y, refX, refY, width, height, scaleX, scaleY, rotation, region.x, region.y, region.width, region.height, color, false, false );\r
+                               batch.draw( region.texture, x, y, originX, originY, width, height, scaleX, scaleY, rotation, region.x, region.y, region.width, region.height, color, false, false );\r
                }\r
        }\r
 \r
index 7c67ced..e378239 100644 (file)
@@ -16,6 +16,8 @@ public class Label extends Actor
                super(name);\r
                this.font = font;\r
                this.text = text;\r
+               this.width = font.getStringWidth( text );\r
+               this.height = font.getLineHeight();\r
        }\r
        \r
        @Override\r
diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/LinearGroup.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/actors/LinearGroup.java
new file mode 100644 (file)
index 0000000..00445bb
--- /dev/null
@@ -0,0 +1,53 @@
+package com.badlogic.gdx.scenes.scene2d.actors;\r
+\r
+import com.badlogic.gdx.GdxRuntimeException;\r
+import com.badlogic.gdx.scenes.scene2d.Actor;\r
+import com.badlogic.gdx.scenes.scene2d.Group;\r
+\r
+public class LinearGroup extends BoundGroup\r
+{\r
+       public static enum LinearGroupLayout\r
+       {\r
+               Vertical,\r
+               Horizontal\r
+       }\r
+       \r
+       private final LinearGroupLayout layout;\r
+       \r
+       public LinearGroup(String name, int width, int height, LinearGroupLayout layout ) \r
+       {\r
+               super(name, width, height);     \r
+               this.layout = layout;\r
+       }\r
+       \r
+       public void addActor( Actor actor )\r
+       {\r
+               if( actor instanceof Group && !(actor instanceof BoundGroup) )\r
+                       throw new GdxRuntimeException( "Can only add Actors and BoundGroup subclasses" );\r
+               \r
+               super.addActor( actor );\r
+               layout( );\r
+       }\r
+       \r
+       public void removeActor( Actor actor )\r
+       {\r
+               super.removeActor( actor );\r
+               layout();\r
+       }\r
+       \r
+       protected void layout( )\r
+       {\r
+               int len = getActors().size();\r
+               float x = 0;\r
+               float y = 0;\r
+               for( int i = 0; i < len; i++ )\r
+               {\r
+                       Actor actor = getActors().get(i);\r
+                       actor.x = x; actor.y = y;\r
+                       if( layout == LinearGroupLayout.Horizontal )\r
+                               x += actor.width;\r
+                       else\r
+                               y += actor.height;\r
+               }\r
+       }\r
+}\r