--- /dev/null
+/*******************************************************************************
+ * 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;
+ }
+}
* 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;
private boolean sizeInvalid = true;
private int alignment;
private boolean reverse;
+ private float spacing;
public VerticalGroup () {
setTouchable(Touchable.childrenOnly);
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;
}
public void layout () {
+ float spacing = this.spacing;
float groupWidth = getWidth();
float y = reverse ? 0 : getHeight();
float dir = reverse ? 1 : -1;
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;
}
}
if (sizeInvalid) computeSize();
return prefHeight;
}
-}
\ No newline at end of file
+
+ /** Sets the space between children. */
+ public void setSpacing (float spacing) {
+ this.spacing = spacing;
+ }
+}
\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
// 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
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
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