From 9e4dc0af7d92fa47f2193f9555c02733fabfea32 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Mon, 14 Oct 2013 20:40:23 +0200 Subject: [PATCH] Added HorizontalGroup, spacing. --- .../gdx/scenes/scene2d/ui/HorizontalGroup.java | 121 +++++++++++++++++++++ .../gdx/scenes/scene2d/ui/VerticalGroup.java | 21 +++- .../gdx/tests/lwjgl/LwjglDebugStarter.java | 32 +----- .../src/com/badlogic/gdx/tests/Scene2dTest.java | 14 ++- 4 files changed, 153 insertions(+), 35 deletions(-) create mode 100644 gdx/src/com/badlogic/gdx/scenes/scene2d/ui/HorizontalGroup.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 index 000000000..675ae581a --- /dev/null +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/HorizontalGroup.java @@ -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. + *

+ * 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 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 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; + } +} diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/VerticalGroup.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/VerticalGroup.java index 02dcc55d6..070071329 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/VerticalGroup.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/VerticalGroup.java @@ -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 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; + } +} diff --git a/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglDebugStarter.java b/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglDebugStarter.java index 7a33dd889..42baade71 100644 --- a/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglDebugStarter.java +++ b/tests/gdx-tests-lwjgl/src/com/badlogic/gdx/tests/lwjgl/LwjglDebugStarter.java @@ -16,33 +16,9 @@ package com.badlogic.gdx.tests.lwjgl; -import com.badlogic.gdx.Gdx; import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; -import com.badlogic.gdx.graphics.Camera; -import com.badlogic.gdx.graphics.GL10; -import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.scenes.scene2d.Stage; -import com.badlogic.gdx.tests.Bresenham2Test; -import com.badlogic.gdx.tests.DelaunayTriangulatorTest; -import com.badlogic.gdx.tests.EarClippingTriangulatorTest; -import com.badlogic.gdx.tests.MeshShaderTest; -import com.badlogic.gdx.tests.MipMapTest; -import com.badlogic.gdx.tests.PixelPerfectTest; -import com.badlogic.gdx.tests.TextureAtlasTest; -import com.badlogic.gdx.tests.TimerTest; -import com.badlogic.gdx.tests.g3d.Basic3DSceneTest; -import com.badlogic.gdx.tests.g3d.Basic3DTest; -import com.badlogic.gdx.tests.g3d.FogTest; -import com.badlogic.gdx.tests.g3d.MaterialTest; -import com.badlogic.gdx.tests.g3d.ModelLoaderTest; -import com.badlogic.gdx.tests.g3d.ModelTest; -import com.badlogic.gdx.tests.g3d.ShaderCollectionTest; -import com.badlogic.gdx.tests.g3d.voxel.VoxelTest; -import com.badlogic.gdx.tests.net.NetAPITest; +import com.badlogic.gdx.tests.Scene2dTest; import com.badlogic.gdx.tests.utils.GdxTest; public class LwjglDebugStarter { @@ -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"); // new SharedLibraryLoader("../../gdx/libs/gdx-natives.jar").load("gdx"); - GdxTest test = new PixelPerfectTest(); + GdxTest test = new Scene2dTest(); LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); config.useGL20 = test.needsGL20(); - config.width = 320; - config.height = 241; +// config.width = 320; +// config.height = 241; new LwjglApplication(test, config); } } diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/Scene2dTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/Scene2dTest.java index dd02234eb..68a0dcb2b 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/Scene2dTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/Scene2dTest.java @@ -27,6 +27,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.actions.FloatAction; +import com.badlogic.gdx.scenes.scene2d.ui.HorizontalGroup; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; @@ -74,14 +75,25 @@ public class Scene2dTest extends GdxTest { Skin skin = new Skin(Gdx.files.internal("data/uiskin.json")); VerticalGroup g = new VerticalGroup(); - g.setPosition(100, 100); + g.setPosition(10, 100); g.setReverse(true); + g.setSpacing(5); stage.addActor(g); for (int i = 0; i < 10; i++) { g.addActor(new TextButton("button " + i, skin)); } g.pack(); + HorizontalGroup h = new HorizontalGroup(); + h.setPosition(100, 100); + h.setReverse(true); + h.setSpacing(5); + stage.addActor(h); + for (int i = 0; i < 7; i++) { + h.addActor(new TextButton("button " + i, skin)); + } + h.pack(); + final TextButton button = new TextButton("Fancy Background", skin); // button.addListener(new ClickListener() { -- 2.11.0