summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-05-18 17:03:24 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-05-18 17:03:24 +0200
commit0a63636e8e5ef6ebcae056e3cf2194a7792d228f (patch)
tree85fbdf7435df571620ad6866040bace04025f34e
parentc39bdc63b1911dcc3b7c827af38e4a026d1ab22e (diff)
downloadgdx-boardgame-0a63636e8e5ef6ebcae056e3cf2194a7792d228f.zip
gdx-boardgame-0a63636e8e5ef6ebcae056e3cf2194a7792d228f.tar.gz
ui : support Container.Pack.BEGIN/END
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Container.java23
-rw-r--r--test/src/ch/asynk/gdx/boardgame/test/UiScreen.java30
2 files changed, 39 insertions, 14 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Container.java b/core/src/ch/asynk/gdx/boardgame/ui/Container.java
index 7275711..c9cff4e 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Container.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Container.java
@@ -10,9 +10,11 @@ public class Container extends Element
{
public static int DEFAULT_CHILD_COUNT = 2;
+ public enum Pack { BEGIN, END };
public enum Direction { FREE, HORIZONTAL, VERTICAL };
protected float spacing;
+ protected Pack pack;
protected Direction direction;
protected IterableSet<Element> children;
private Rectangle subArea;
@@ -32,6 +34,12 @@ public class Container extends Element
taint();
}
+ public void setPacking(Pack pack)
+ {
+ this.pack = pack;
+ taint();
+ }
+
public void setDirection(Direction direction)
{
this.direction = direction;
@@ -73,18 +81,18 @@ public class Container extends Element
};
float f = (area.width - c) / m;
subArea.set(area);
- subArea.x += subArea.width;
- subArea.width = 0;
+ boolean end = (pack == Pack.END);
+ if (end) subArea.x += subArea.width;
for (Element e : children) {
float available = e.rect.width;
if (Sizing.contains(e.sizing, Sizing.EXPAND_X))
available += f;
- subArea.x -= available;
subArea.width = available;
+ if (end) subArea.x -= available;
e.computeGeometry(subArea, true);
+ if (!end) subArea.x += available;
}
} else if (direction == Direction.VERTICAL) {
- // packs at the begining
int n = children.size();
int m = 0;
int c = 0;
@@ -95,15 +103,16 @@ public class Container extends Element
};
float f = (area.height - c) / m;
subArea.set(area);
- subArea.y += subArea.height;
- subArea.height = 0;
+ boolean begin = (pack == Pack.BEGIN);
+ if (begin) subArea.y += subArea.height;
for (Element e : children) {
float available = e.rect.height;
if (Sizing.contains(e.sizing, Sizing.EXPAND_Y))
available += f;
- subArea.y -= available;
subArea.height = available;
+ if (begin) subArea.y -= available;
e.computeGeometry(subArea, true);
+ if (!begin) subArea.y += available;
}
} else {
children.forEach( c -> c.computeGeometry(area, resized) );
diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
index e088dde..16150bb 100644
--- a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
+++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
@@ -32,17 +32,21 @@ public class UiScreen extends AbstractScreen
public enum State
{
- POSITIONS, SCROLL, CONTAINER_V, CONTAINER_H, DONE;
+ POSITIONS, SCROLL, CONTAINER_BV, CONTAINER_BH, CONTAINER_EV, CONTAINER_EH, DONE;
public State next()
{
switch(this) {
case POSITIONS:
return SCROLL;
case SCROLL:
- return CONTAINER_V;
- case CONTAINER_V:
- return CONTAINER_H;
- case CONTAINER_H:
+ return CONTAINER_BV;
+ case CONTAINER_BV:
+ return CONTAINER_BH;
+ case CONTAINER_BH:
+ return CONTAINER_EV;
+ case CONTAINER_EV:
+ return CONTAINER_EH;
+ case CONTAINER_EH:
return DONE;
default:
return POSITIONS;
@@ -120,6 +124,7 @@ public class UiScreen extends AbstractScreen
private Container buildContainer()
{
Container c = new Container();
+ c.setPacking(Container.Pack.BEGIN);
c.setDirection(Container.Direction.VERTICAL);
c.add(this.buttons1[0]);
c.add(this.buttons1[1]);
@@ -137,11 +142,22 @@ public class UiScreen extends AbstractScreen
setButtons(false);
root.add(list);
break;
- case CONTAINER_V:
+ case CONTAINER_BV:
root.remove(list);
root.add(container);
+ this.container.setPacking(Container.Pack.BEGIN);
+ this.container.setDirection(Container.Direction.VERTICAL);
break;
- case CONTAINER_H:
+ case CONTAINER_BH:
+ this.container.setPacking(Container.Pack.BEGIN);
+ this.container.setDirection(Container.Direction.HORIZONTAL);
+ break;
+ case CONTAINER_EV:
+ this.container.setPacking(Container.Pack.END);
+ this.container.setDirection(Container.Direction.VERTICAL);
+ break;
+ case CONTAINER_EH:
+ this.container.setPacking(Container.Pack.END);
this.container.setDirection(Container.Direction.HORIZONTAL);
break;
case DONE: