diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2020-05-18 15:27:17 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2020-05-18 15:27:17 +0200 |
commit | 320e18f47913ecf807cd73a4868e50a675a49ddf (patch) | |
tree | 4c6f8b462404a06e742dfae9dafbe408697c137f /core/src/ch/asynk/gdx/boardgame/ui | |
parent | 5098b45162596e4a23e1e0dd2866c0fdb2ef5680 (diff) | |
download | gdx-boardgame-320e18f47913ecf807cd73a4868e50a675a49ddf.zip gdx-boardgame-320e18f47913ecf807cd73a4868e50a675a49ddf.tar.gz |
UI/Container : knows basic layering
Diffstat (limited to 'core/src/ch/asynk/gdx/boardgame/ui')
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Button.java | 5 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Container.java | 70 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Element.java | 11 |
3 files changed, 83 insertions, 3 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Button.java b/core/src/ch/asynk/gdx/boardgame/ui/Button.java index 7fdc799..dc7e624 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Button.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Button.java @@ -41,13 +41,16 @@ public class Button extends Patch label.setAlignment(alignment); } - @Override public void computeGeometry(Rectangle area, boolean resized) + @Override public void computeDimensions() { label.computeDimensions(); float dd = 2 * (padding + spacing); rect.width = label.getWidth() + dd; rect.height = label.getHeight() + dd; + } + @Override public void computeGeometry(Rectangle area, boolean resized) + { super.computeGeometry(area, resized); label.computeGeometry(innerRect, resized); } diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Container.java b/core/src/ch/asynk/gdx/boardgame/ui/Container.java index 46a013a..7275711 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Container.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Container.java @@ -10,12 +10,32 @@ public class Container extends Element { public static int DEFAULT_CHILD_COUNT = 2; + public enum Direction { FREE, HORIZONTAL, VERTICAL }; + + protected float spacing; + protected Direction direction; protected IterableSet<Element> children; + private Rectangle subArea; - protected Container() + public Container() { super(); + this.spacing = 0f; + this.direction = Direction.FREE; this.children = null; + this.subArea = new Rectangle(); + } + + public void setSpacing(float spacing) + { + this.spacing = spacing; + taint(); + } + + public void setDirection(Direction direction) + { + this.direction = direction; + taint(); } public void add(Element e) @@ -41,7 +61,53 @@ public class Container extends Element @Override public void computeGeometry(Rectangle area, boolean resized) { if ((dirty || damaged || resized) && children != null) { - children.forEach( c -> c.computeGeometry(area, resized) ); + if (direction == Direction.HORIZONTAL) { + // packs at the end + int n = children.size(); + int m = 0; + int c = 0; + for (Element e : children) { + e.computeDimensions(); + c += e.rect.width; + if (Sizing.contains(e.sizing, Sizing.EXPAND_X)) m += 1; + }; + float f = (area.width - c) / m; + subArea.set(area); + subArea.x += subArea.width; + subArea.width = 0; + 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; + e.computeGeometry(subArea, true); + } + } else if (direction == Direction.VERTICAL) { + // packs at the begining + int n = children.size(); + int m = 0; + int c = 0; + for (Element e : children) { + e.computeDimensions(); + c += e.rect.height; + if (Sizing.contains(e.sizing, Sizing.EXPAND_Y)) m += 1; + }; + float f = (area.height - c) / m; + subArea.set(area); + subArea.y += subArea.height; + subArea.height = 0; + 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; + e.computeGeometry(subArea, true); + } + } else { + children.forEach( c -> c.computeGeometry(area, resized) ); + } damaged = false; } if (dirty || resized) { diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Element.java b/core/src/ch/asynk/gdx/boardgame/ui/Element.java index 9bc7551..433826c 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java @@ -179,6 +179,16 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch protected void computeDimensions() { } + protected void computeDimensions(Rectangle area) + { + if (Sizing.contains(sizing, Sizing.FILL_X)) { + this.rect.width = area.width; + } + if (Sizing.contains(sizing, Sizing.FILL_Y)) { + this.rect.height = area.height; + } + } + private void computePosition(Rectangle area) { if (alignment == Alignment.ABSOLUTE) { @@ -199,6 +209,7 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch { if (dirty || resized) { computeDimensions(); + computeDimensions(area); computePosition(area); clear(); } else if (damaged) { |