Permalink
Browse files

Vertical/HorizontalGroup got pad, fill and an API similar to Table/Co…

…ntainer.

#1336
  • Loading branch information...
NathanSweet committed Feb 16, 2014
1 parent 82f6f63 commit 91a30df025d416ea6b213f1b3e61a343ced44cde
View
@@ -25,7 +25,8 @@
- scene2d.ui, renamed Actor methods translate, rotate, scale, size to moveBy, rotateBy, scaleBy, sizeBy. May have conflicts with Actions static import, eg you'll need to use "Actions.moveBy"
- scene2d.ui, Table background is now drawn usign the table's transform
- scene2d.ui, added Container which is similar to a Table with one cell, but more lightweight
-- Adds texture filters and mip map generation to BitMapFontLoader and FreeTypeFontGenerator
+- Added texture filters and mip map generation to BitMapFontLoader and FreeTypeFontGenerator
+- scene2d.ui, VerticalGroup and HorizontalGroup got pad, fill and an API similar to Table/Container
[0.9.9]
- added setCursorImage method to Input interface to support custom mouse cursors on the desktop
@@ -10,7 +10,7 @@
import com.badlogic.gdx.scenes.scene2d.utils.Layout;
/** A group with a single child that sizes and positions the child using constraints. This provides layout similar to a
- * {@link Table} with a single cell.
+ * {@link Table} with a single cell but is more lightweight.
* @author Nathan Sweet */
public class Container extends WidgetGroup {
private Actor widget;
@@ -380,41 +380,41 @@ public Container fill (boolean fill) {
return this;
}
- /** Sets the alignment of the widget within the cell. Set to {@link Align#center}, {@link Align#top}, {@link Align#bottom},
+ /** Sets the alignment of the widget within the container. Set to {@link Align#center}, {@link Align#top}, {@link Align#bottom},
* {@link Align#left}, {@link Align#right}, or any combination of those. */
public Container align (int align) {
this.align = align;
return this;
}
- /** Sets the alignment of the widget within the cell to {@link Align#center}. This clears any other alignment. */
+ /** Sets the alignment of the widget within the container to {@link Align#center}. This clears any other alignment. */
public Container center () {
align = Align.center;
return this;
}
- /** Sets {@link Align#top} and clears {@link Align#bottom} for the alignment of the widget within the cell. */
+ /** Sets {@link Align#top} and clears {@link Align#bottom} for the alignment of the widget within the container. */
public Container top () {
align |= Align.top;
align &= ~Align.bottom;
return this;
}
- /** Sets {@link Align#left} and clears {@link Align#right} for the alignment of the widget within the cell. */
+ /** Sets {@link Align#left} and clears {@link Align#right} for the alignment of the widget within the container. */
public Container left () {
align |= Align.left;
align &= ~Align.right;
return this;
}
- /** Sets {@link Align#bottom} and clears {@link Align#top} for the alignment of the widget within the cell. */
+ /** Sets {@link Align#bottom} and clears {@link Align#top} for the alignment of the widget within the container. */
public Container bottom () {
align |= Align.bottom;
align &= ~Align.top;
return this;
}
- /** Sets {@link Align#right} and clears {@link Align#left} for the alignment of the widget within the cell. */
+ /** Sets {@link Align#right} and clears {@link Align#left} for the alignment of the widget within the container. */
public Container right () {
align |= Align.right;
align &= ~Align.left;
@@ -497,17 +497,14 @@ public float getPadRight () {
return padRight;
}
- /** @return May be null if this value is not set. */
public float getFillX () {
return fillX;
}
- /** @return May be null. */
public float getFillY () {
return fillY;
}
- /** @return May be null. */
public int getAlign () {
return align;
}
@@ -35,25 +35,16 @@
public class HorizontalGroup extends WidgetGroup {
private float prefWidth, prefHeight;
private boolean sizeInvalid = true;
- private int alignment;
+ private int align;
private boolean reverse, round = true;
private float spacing;
+ private float padTop, padLeft, padBottom, padRight;
+ private float fill;
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;
@@ -63,7 +54,7 @@ private void computeSize () {
sizeInvalid = false;
SnapshotArray<Actor> children = getChildren();
int n = children.size;
- prefWidth = spacing * (n - 1);
+ prefWidth = padLeft + padRight + spacing * (n - 1);
prefHeight = 0;
for (int i = 0; i < n; i++) {
Actor child = children.get(i);
@@ -76,37 +67,51 @@ private void computeSize () {
prefHeight = Math.max(prefHeight, child.getHeight());
}
}
+ prefHeight += padTop + padBottom;
if (round) {
prefWidth = Math.round(prefWidth);
prefHeight = Math.round(prefHeight);
}
}
public void layout () {
- float spacing = this.spacing;
- float groupHeight = getHeight() > 0 ? getHeight() : getMinHeight();
- float x = reverse ? (getWidth() > 0 ? getWidth() : getMinWidth()) : 0;
+ float spacing = this.spacing, padBottom = this.padBottom;
+ int align = this.align;
+ boolean reverse = this.reverse, round = this.round;
+
+ float groupHeight = getHeight() - padTop - padBottom;
+ float x = !reverse ? padLeft : getWidth() - padRight + spacing;
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;
+ if (fill > 0)
+ height = groupHeight * fill;
+ else
+ height = Math.min(layout.getPrefHeight(), groupHeight);
+ height = Math.max(height, layout.getMinHeight());
+ float maxHeight = layout.getMaxHeight();
+ if (maxHeight > 0 && height > maxHeight) height = maxHeight;
width = layout.getPrefWidth();
- height = layout.getPrefHeight();
} else {
width = child.getWidth();
height = child.getHeight();
+ if (fill > 0) height *= fill;
}
- float y;
- if ((alignment & Align.bottom) != 0)
- y = 0;
- else if ((alignment & Align.top) != 0)
- y = groupHeight - height;
- else
- y = (groupHeight - height) / 2;
+
+ float y = padBottom;
+ if ((align & Align.top) != 0)
+ y += groupHeight - height;
+ else if ((align & Align.bottom) == 0) // center
+ y += (groupHeight - height) / 2;
+
if (reverse) x -= (width + spacing);
- child.setBounds(x, y, width, height);
+ if (round)
+ child.setBounds(Math.round(x), Math.round(y), Math.round(width), Math.round(height));
+ else
+ child.setBounds(x, y, width, height);
if (!reverse) x += (width + spacing);
}
}
@@ -121,13 +126,133 @@ public float getPrefHeight () {
return prefHeight;
}
+ /** If true (the default), positions and sizes are rounded to integers. */
+ public void setRound (boolean round) {
+ this.round = round;
+ }
+
+ /** The children will be ordered from right to left rather than the default left to right. */
+ public HorizontalGroup reverse () {
+ reverse(true);
+ return this;
+ }
+
+ /** If true, the children will be ordered from right to left rather than the default left to right. */
+ public HorizontalGroup reverse (boolean reverse) {
+ this.reverse = reverse;
+ return this;
+ }
+
+ public boolean getReverse () {
+ return reverse;
+ }
+
/** Sets the space between children. */
- public void setSpacing (float spacing) {
+ public HorizontalGroup space (float spacing) {
this.spacing = spacing;
+ return this;
}
- /** If true (the default), positions and sizes are rounded to integers. */
- public void setRound (boolean round) {
- this.round = round;
+ public float getSpace () {
+ return spacing;
+ }
+
+ /** Sets the padTop, padLeft, padBottom, and padRight to the specified value. */
+ public HorizontalGroup pad (float pad) {
+ padTop = pad;
+ padLeft = pad;
+ padBottom = pad;
+ padRight = pad;
+ return this;
+ }
+
+ public HorizontalGroup pad (float top, float left, float bottom, float right) {
+ padTop = top;
+ padLeft = left;
+ padBottom = bottom;
+ padRight = right;
+ return this;
+ }
+
+ public HorizontalGroup padTop (float padTop) {
+ this.padTop = padTop;
+ return this;
+ }
+
+ public HorizontalGroup padLeft (float padLeft) {
+ this.padLeft = padLeft;
+ return this;
+ }
+
+ public HorizontalGroup padBottom (float padBottom) {
+ this.padBottom = padBottom;
+ return this;
+ }
+
+ public HorizontalGroup padRight (float padRight) {
+ this.padRight = padRight;
+ return this;
+ }
+
+ public float getPadTop () {
+ return padTop;
+ }
+
+ public float getPadLeft () {
+ return padLeft;
+ }
+
+ public float getPadBottom () {
+ return padBottom;
+ }
+
+ public float getPadRight () {
+ return padRight;
+ }
+
+ /** Sets the alignment of widgets within the horizontal group. Set to {@link Align#center}, {@link Align#top},
+ * {@link Align#bottom}, {@link Align#left}, {@link Align#right}, or any combination of those. */
+ public HorizontalGroup align (int align) {
+ this.align = align;
+ return this;
+ }
+
+ /** Sets the alignment of widgets within the horizontal group to {@link Align#center}. This clears any other alignment. */
+ public HorizontalGroup center () {
+ align = Align.center;
+ return this;
+ }
+
+ /** Sets {@link Align#top} and clears {@link Align#bottom} for the alignment of widgets within the horizontal group. */
+ public HorizontalGroup top () {
+ align |= Align.top;
+ align &= ~Align.bottom;
+ return this;
+ }
+
+ /** Sets {@link Align#bottom} and clears {@link Align#top} for the alignment of widgets within the horizontal group. */
+ public HorizontalGroup bottom () {
+ align |= Align.bottom;
+ align &= ~Align.top;
+ return this;
+ }
+
+ public int getAlign () {
+ return align;
+ }
+
+ public HorizontalGroup fill () {
+ fill = 1f;
+ return this;
+ }
+
+ /** @param fill 0 will use pref width. */
+ public HorizontalGroup fill (float fill) {
+ this.fill = fill;
+ return this;
+ }
+
+ public float getFill () {
+ return fill;
}
-}
+}
Oops, something went wrong.

0 comments on commit 91a30df

Please sign in to comment.