OSDN Git Service

Added HorizontalGroup, spacing.
authorNathanSweet <nathan.sweet@gmail.com>
Mon, 14 Oct 2013 18:40:23 +0000 (20:40 +0200)
committerNathanSweet <nathan.sweet@gmail.com>
Mon, 14 Oct 2013 18:40:23 +0000 (20:40 +0200)
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/HorizontalGroup.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/VerticalGroup.java
tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglDebugStarter.java
tests/gdx-tests/src/com/badlogic/gdx/tests/Scene2dTest.java

diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/HorizontalGroup.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/HorizontalGroup.java
new file mode 100644 (file)
index 0000000..675ae58
--- /dev/null
@@ -0,0 +1,121 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
+package com.badlogic.gdx.scenes.scene2d.ui;
+
+import com.badlogic.gdx.scenes.scene2d.Actor;
+import com.badlogic.gdx.scenes.scene2d.Touchable;
+import com.badlogic.gdx.scenes.scene2d.utils.Align;
+import com.badlogic.gdx.scenes.scene2d.utils.Layout;
+import com.badlogic.gdx.utils.SnapshotArray;
+
+/** A group that lays out its children side by side in a single column. This can be easier than using {@link Table} when actors
+ * need to be inserted in the middle of the group.
+ * <p>
+ * The preferred width is the sum of the children's preferred widths. The preferred height is the largest preferred height of any
+ * child. The min size is the preferred size and the max size is 0.
+ * @author Nathan Sweet */
+public class HorizontalGroup extends WidgetGroup {
+       private float prefWidth, prefHeight;
+       private boolean sizeInvalid = true;
+       private int alignment;
+       private boolean reverse;
+       private float spacing;
+
+       public HorizontalGroup () {
+               setTouchable(Touchable.childrenOnly);
+       }
+
+       /** Sets the vertical alignment of the children. Default is center.
+        * @see Align */
+       public void setAlignment (int alignment) {
+               this.alignment = alignment;
+       }
+
+       /** If true, the children will be ordered from right to left rather than the default left to right. */
+       public void setReverse (boolean reverse) {
+               this.reverse = reverse;
+       }
+
+       public void invalidate () {
+               super.invalidate();
+               sizeInvalid = true;
+       }
+
+       private void computeSize () {
+               sizeInvalid = false;
+               SnapshotArray<Actor> children = getChildren();
+               int n = children.size;
+               prefWidth = spacing * (n - 1);
+               prefHeight = 0;
+               for (int i = 0; i < n; i++) {
+                       Actor child = children.get(i);
+                       if (child instanceof Layout) {
+                               Layout layout = (Layout)child;
+                               prefWidth += layout.getPrefWidth();
+                               prefHeight = Math.max(prefHeight, layout.getPrefHeight());
+                       } else {
+                               prefWidth += child.getWidth();
+                               prefHeight = Math.max(prefHeight, child.getHeight());
+                       }
+               }
+       }
+
+       public void layout () {
+               float spacing = this.spacing;
+               float groupHeight = getHeight();
+               float x = reverse ? getWidth() : 0;
+               float dir = reverse ? -1 : 1;
+               SnapshotArray<Actor> children = getChildren();
+               for (int i = 0, n = children.size; i < n; i++) {
+                       Actor child = children.get(i);
+                       float width, height;
+                       if (child instanceof Layout) {
+                               Layout layout = (Layout)child;
+                               width = layout.getPrefWidth();
+                               height = layout.getPrefHeight();
+                       } else {
+                               width = child.getWidth();
+                               height = child.getHeight();
+                       }
+                       float y;
+                       if ((alignment & Align.left) != 0)
+                               y = 0;
+                       else if ((alignment & Align.right) != 0)
+                               y = groupHeight - height;
+                       else
+                               y = (groupHeight - height) / 2;
+                       if (reverse) x += (width + spacing) * dir;
+                       child.setBounds(x, y, width, height);
+                       if (!reverse) x += (width + spacing) * dir;
+               }
+       }
+
+       public float getPrefWidth () {
+               if (sizeInvalid) computeSize();
+               return prefWidth;
+       }
+
+       public float getPrefHeight () {
+               if (sizeInvalid) computeSize();
+               return prefHeight;
+       }
+
+       /** Sets the space between children. */
+       public void setSpacing (float spacing) {
+               this.spacing = spacing;
+       }
+}
index 02dcc55..0700713 100644 (file)
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  ******************************************************************************/
+
 package com.badlogic.gdx.scenes.scene2d.ui;
 
 import com.badlogic.gdx.scenes.scene2d.Actor;
@@ -32,6 +33,7 @@ public class VerticalGroup extends WidgetGroup {
        private boolean sizeInvalid = true;
        private int alignment;
        private boolean reverse;
+       private float spacing;
 
        public VerticalGroup () {
                setTouchable(Touchable.childrenOnly);
@@ -55,10 +57,11 @@ public class VerticalGroup extends WidgetGroup {
 
        private void computeSize () {
                sizeInvalid = false;
-               prefWidth = 0;
-               prefHeight = 0;
                SnapshotArray<Actor> children = getChildren();
-               for (int i = 0, n = children.size; i < n; i++) {
+               int n = children.size;
+               prefWidth = 0;
+               prefHeight = spacing * (n - 1);
+               for (int i = 0; i < n; i++) {
                        Actor child = children.get(i);
                        if (child instanceof Layout) {
                                Layout layout = (Layout)child;
@@ -72,6 +75,7 @@ public class VerticalGroup extends WidgetGroup {
        }
 
        public void layout () {
+               float spacing = this.spacing;
                float groupWidth = getWidth();
                float y = reverse ? 0 : getHeight();
                float dir = reverse ? 1 : -1;
@@ -94,9 +98,9 @@ public class VerticalGroup extends WidgetGroup {
                                x = groupWidth - width;
                        else
                                x = (groupWidth - width) / 2;
-                       if (!reverse) y += height * dir;
+                       if (!reverse) y += (height + spacing) * dir;
                        child.setBounds(x, y, width, height);
-                       if (reverse) y += height * dir;
+                       if (reverse) y += (height + spacing) * dir;
                }
        }
 
@@ -109,4 +113,9 @@ public class VerticalGroup extends WidgetGroup {
                if (sizeInvalid) computeSize();
                return prefHeight;
        }
-}
\ No newline at end of file
+
+       /** Sets the space between children. */
+       public void setSpacing (float spacing) {
+               this.spacing = spacing;
+       }
+}
index 7a33dd8..42baade 100644 (file)
 \r
 package com.badlogic.gdx.tests.lwjgl;\r
 \r
-import com.badlogic.gdx.Gdx;\r
 import com.badlogic.gdx.backends.lwjgl.LwjglApplication;\r
 import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;\r
-import com.badlogic.gdx.graphics.Camera;\r
-import com.badlogic.gdx.graphics.GL10;\r
-import com.badlogic.gdx.graphics.OrthographicCamera;\r
-import com.badlogic.gdx.graphics.glutils.ShapeRenderer;\r
-import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;\r
-import com.badlogic.gdx.math.Vector2;\r
-import com.badlogic.gdx.scenes.scene2d.Stage;\r
-import com.badlogic.gdx.tests.Bresenham2Test;\r
-import com.badlogic.gdx.tests.DelaunayTriangulatorTest;\r
-import com.badlogic.gdx.tests.EarClippingTriangulatorTest;\r
-import com.badlogic.gdx.tests.MeshShaderTest;\r
-import com.badlogic.gdx.tests.MipMapTest;\r
-import com.badlogic.gdx.tests.PixelPerfectTest;\r
-import com.badlogic.gdx.tests.TextureAtlasTest;\r
-import com.badlogic.gdx.tests.TimerTest;\r
-import com.badlogic.gdx.tests.g3d.Basic3DSceneTest;\r
-import com.badlogic.gdx.tests.g3d.Basic3DTest;\r
-import com.badlogic.gdx.tests.g3d.FogTest;\r
-import com.badlogic.gdx.tests.g3d.MaterialTest;\r
-import com.badlogic.gdx.tests.g3d.ModelLoaderTest;\r
-import com.badlogic.gdx.tests.g3d.ModelTest;\r
-import com.badlogic.gdx.tests.g3d.ShaderCollectionTest;\r
-import com.badlogic.gdx.tests.g3d.voxel.VoxelTest;\r
-import com.badlogic.gdx.tests.net.NetAPITest;\r
+import com.badlogic.gdx.tests.Scene2dTest;\r
 import com.badlogic.gdx.tests.utils.GdxTest;\r
 \r
 public class LwjglDebugStarter {\r
@@ -54,11 +30,11 @@ public class LwjglDebugStarter {
 //             new SharedLibraryLoader("../../extensions/gdx-controllers/gdx-controllers-desktop/libs/gdx-controllers-desktop-natives.jar").load("gdx-controllers-desktop");\r
 //             new SharedLibraryLoader("../../gdx/libs/gdx-natives.jar").load("gdx");\r
 \r
-               GdxTest test = new PixelPerfectTest();\r
+               GdxTest test = new Scene2dTest();\r
                LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();\r
                config.useGL20 = test.needsGL20();\r
-               config.width = 320;\r
-               config.height = 241;\r
+//             config.width = 320;\r
+//             config.height = 241;\r
                new LwjglApplication(test, config);\r
        }\r
 }\r
index dd02234..68a0dcb 100644 (file)
@@ -27,6 +27,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent;
 import com.badlogic.gdx.scenes.scene2d.InputListener;\r
 import com.badlogic.gdx.scenes.scene2d.Stage;\r
 import com.badlogic.gdx.scenes.scene2d.actions.FloatAction;\r
+import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup;\r
 import com.badlogic.gdx.scenes.scene2d.ui.Label;\r
 import com.badlogic.gdx.scenes.scene2d.ui.Skin;\r
 import com.badlogic.gdx.scenes.scene2d.ui.Table;\r
@@ -74,14 +75,25 @@ public class Scene2dTest extends GdxTest {
                Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));\r
 \r
                VerticalGroup g = new VerticalGroup();\r
-               g.setPosition(100, 100);\r
+               g.setPosition(10, 100);\r
                g.setReverse(true);\r
+               g.setSpacing(5);\r
                stage.addActor(g);\r
                for (int i = 0; i < 10; i++) {\r
                        g.addActor(new TextButton("button " + i, skin));\r
                }\r
                g.pack();\r
 \r
+               HorizontalGroup h = new HorizontalGroup();\r
+               h.setPosition(100, 100);\r
+               h.setReverse(true);\r
+               h.setSpacing(5);\r
+               stage.addActor(h);\r
+               for (int i = 0; i < 7; i++) {\r
+                       h.addActor(new TextButton("button " + i, skin));\r
+               }\r
+               h.pack();\r
+\r
                final TextButton button = new TextButton("Fancy Background", skin);\r
 \r
 // button.addListener(new ClickListener() {\r