From 97caa84105cceb98ec1253426e200858b4bcb9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 26 Dec 2019 21:41:39 +0100 Subject: ui : add Sizing, final computeGeometry() calls computeDimensions() and computePosition() --- core/src/ch/asynk/gdx/boardgame/ui/Button.java | 34 +++++++++++----- core/src/ch/asynk/gdx/boardgame/ui/Element.java | 45 ++++++++++++++++++--- core/src/ch/asynk/gdx/boardgame/ui/Label.java | 21 +++++----- core/src/ch/asynk/gdx/boardgame/ui/List.java | 15 +++++-- core/src/ch/asynk/gdx/boardgame/ui/Menu.java | 14 ++++--- core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java | 9 ++++- core/src/ch/asynk/gdx/boardgame/ui/Sizing.java | 47 ++++++++++++++++++++++ test/src/ch/asynk/gdx/boardgame/test/UiScreen.java | 23 ++++++----- 8 files changed, 161 insertions(+), 47 deletions(-) create mode 100644 core/src/ch/asynk/gdx/boardgame/ui/Sizing.java diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Button.java b/core/src/ch/asynk/gdx/boardgame/ui/Button.java index c19eab1..9ba4dad 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Button.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Button.java @@ -8,7 +8,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; public class Button extends Patch { protected Label label; - protected float spacing; // for label alignment; + protected float spacing; // label is aligned within spacing frame; public Button(BitmapFont font, NinePatch patch) { @@ -25,14 +25,15 @@ public class Button extends Patch super(patch); this.padding = padding; this.spacing = spacing; - label = new Label(font); - label.setParent(this, Alignment.MIDDLE_CENTER); + this.label = new Label(font); + this.label.setParent(this, Alignment.MIDDLE_CENTER); + this.sizing = Sizing.EXPAND_BOTH; } public void write(String text) { label.write(text); - taint(); // might impact Button's geometry + taint(); } public void setLabelAlignment(Alignment alignment) @@ -40,14 +41,25 @@ public class Button extends Patch label.setAlignment(alignment); } - @Override public void computeGeometry() + @Override public void computeDimensions() { - float dd = 2 * (padding + spacing); - label.computeGeometry(); // update dimensions - rect.width = label.getWidth() + dd; - rect.height = label.getHeight() + dd; - super.computeGeometry(); - label.computeGeometry(); // update position + if(sizing.fill()) { + super.computeDimensions(); + } else { + float dd = 2 * (padding + spacing); + label.computeDimensions(); + if (sizing.expandWidth()) + rect.width = label.getWidth() + dd; + if (sizing.expandHeight()) + rect.height = label.getHeight() + dd; + if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1)); + } + } + + @Override public void computePosition() + { + super.computePosition(); + label.computePosition(); } @Override public void draw(Batch batch) diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Element.java b/core/src/ch/asynk/gdx/boardgame/ui/Element.java index dec0cc7..9885b59 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java @@ -10,11 +10,14 @@ import ch.asynk.gdx.boardgame.Touchable; public abstract class Element implements Drawable, Paddable, Positionable, Touchable { + public static boolean DEBUG_GEOMETRY = false; + public boolean blocked; public boolean visible; protected float padding; protected Element parent; - protected Alignment alignment; + protected Sizing sizing; // set dimensions according to parent and children + protected Alignment alignment; // where to position itself protected Rectangle rect; // outer drawing coordinates protected float x, y; // given position protected boolean tainted; // geometry must be computed @@ -31,6 +34,7 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch this.visible = true; this.padding = 0; this.parent = null; + this.sizing = Sizing.NONE; this.alignment = alignment.RELATIVE; this.rect = new Rectangle(0, 0, 0, 0); this.x = this.y = 0; @@ -62,9 +66,11 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch suffix += " "; String r = suffix; - r += getClass().getName() + " : " + (int)x + " " + (int)y + + r += " : " + (int)x + " " + (int)y + " [" + (int)rect.x + " " + (int)rect.y + " " + (int)rect.width + " " + (int)rect.height + "] +" + - (int)padding + " " + alignment; + (int)padding + " " + alignment + " " + sizing + " "+ getClass().getName(); + if (level < 0) + return r; if (parent != null) r += "\n" + parent.print(level + 1); else @@ -102,7 +108,6 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch this.rect.width = w; this.rect.height = h; taint(); - // rect.(x,y) will be set in computeGeometry } public void setParent(Element parent) @@ -123,6 +128,12 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch taint(); } + public void setSizing(Sizing sizing) + { + this.sizing = sizing; + taint(); + } + public void setAlignment(Alignment alignment) { this.alignment = alignment; @@ -149,7 +160,21 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch setPosition(x, y, rect.width, rect.height); } - public void computeGeometry() + public void computeDimensions() + { + if (parent != null && sizing.fill()) { + if (sizing.fillWidth()) { + rect.width = parent.getInnerWidth(); + } + if (sizing.fillHeight()) { + rect.width = parent.getInnerHeight(); + } + } + // it is up to the subclass to implement the expand logic + if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1)); + } + + public void computePosition() { if (alignment == Alignment.ABSOLUTE || parent == null) { rect.x = x; @@ -161,7 +186,15 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch rect.x = x + alignment.getX(parent, rect.width); rect.y = y + alignment.getY(parent, rect.height); } + if (DEBUG_GEOMETRY) System.err.println(" pos " + print(-1)); + } + + public final void computeGeometry() + { + if (DEBUG_GEOMETRY) System.err.println("[Geometry " + print(-1)); + computeDimensions(); + computePosition(); + if (DEBUG_GEOMETRY) System.err.println("Geometry]" + print(-1)); this.tainted = false; - // System.err.println(String.format("%s : %s", this, rect)); } } diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Label.java b/core/src/ch/asynk/gdx/boardgame/ui/Label.java index 4d89ffa..038a4e8 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Label.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Label.java @@ -8,8 +8,6 @@ public class Label extends Element { private BitmapFont font; private GlyphLayout layout; - private float fx; - private float fy; private String text; public Label(BitmapFont font) @@ -29,6 +27,7 @@ public class Label extends Element this.padding = padding; this.alignment = alignment; this.layout = new GlyphLayout(); + this.sizing = Sizing.EXPAND_BOTH; } public String getText() @@ -54,19 +53,23 @@ public class Label extends Element taint(); } - @Override public void computeGeometry() + @Override public void computeDimensions() { - this.rect.width = (layout.width + (2 * padding)); - this.rect.height = (layout.height + (2 * padding)); - super.computeGeometry(); - fx = getInnerX(); - fy = getInnerY() + layout.height; + if(sizing.fill()) { + super.computeDimensions(); + } else { + if (sizing.expandWidth()) + this.rect.width = (layout.width + (2 * padding)); + if (sizing.expandHeight()) + this.rect.height = (layout.height + (2 * padding)); + if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1)); + } } @Override public void draw(Batch batch) { if (!visible) return; if (tainted) computeGeometry(); - font.draw(batch, layout, fx, fy); + font.draw(batch, layout, getInnerX(), getInnerY() + layout.height); } } diff --git a/core/src/ch/asynk/gdx/boardgame/ui/List.java b/core/src/ch/asynk/gdx/boardgame/ui/List.java index e9f874a..4f47e60 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/List.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/List.java @@ -31,6 +31,7 @@ public class List extends Element this.idx = null; this.selected = new Bg(selected); this.selected.visible = false; + this.sizing = Sizing.EXPAND_BOTH; } public void unselect() { idx = null; } @@ -59,7 +60,7 @@ public class List extends Element taint(); } - @Override public void computeGeometry() + @Override public void computeDimensions() { float w = 0f; for (Item e: items) { @@ -67,10 +68,16 @@ public class List extends Element if (layout.width > w) w = layout.width; } itemHeight = (layout.height + spacing); + if (sizing.fill()) { + super.computeDimensions(); + } else if (sizing.expand()) { - rect.width = w + (2 * padding); - rect.height = (itemHeight * items.size()) + (2 * padding) - spacing; - super.computeGeometry(); + if (sizing.expandWidth()) + rect.width = w + (2 * padding); + if (sizing.expandHeight()) + rect.height = (itemHeight * items.size()) + (2 * padding) - spacing; + if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1)); + } } @Override public void draw(Batch batch) diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java index bc4e609..cf5317c 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java @@ -65,14 +65,13 @@ public class Menu extends Patch taint(); } - @Override public void computeGeometry() + @Override public void computeDimensions() { - // compute width and height - title.computeGeometry(); + title.computeDimensions(); float h = title.getHeight(); float w = title.getWidth(); for (Label label : entries) { - label.computeGeometry(); + label.computeDimensions(); h += label.getHeight(); float t = label.getWidth() + entriesOffset; if (t > w) @@ -80,9 +79,12 @@ public class Menu extends Patch } rect.width = w + (2 * padding); rect.height = h + (titleSpacing + (entriesSpacing * (entries.length - 1)) + (2 * padding)); + if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1)); + } - // compute position - super.computeGeometry(); + @Override public void computePosition() + { + super.computePosition(); float y = getInnerHeight() - title.getHeight(); title.setPosition(0, y); diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java index 842adc0..fea1522 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java @@ -37,9 +37,14 @@ public class Scrollable extends Element return (child.getHeight() + (2 * padding)); } - @Override public void computeGeometry() + @Override public void computeDimensions() { - child.computeGeometry(); + child.computeDimensions(); + } + + @Override public void computePosition() + { + child.computePosition(); } @Override public boolean touch(float x, float y) diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Sizing.java b/core/src/ch/asynk/gdx/boardgame/ui/Sizing.java new file mode 100644 index 0000000..a8d089c --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/ui/Sizing.java @@ -0,0 +1,47 @@ +package ch.asynk.gdx.boardgame.ui; + +public enum Sizing +{ + NONE(0), + // 0x02 + FILL_WIDTH(2), + FILL_HEIGHT(3), + FILL_BOTH(6), + // 0x08 + EXPAND_WIDTH(8), + EXPAND_HEIGHT(9), + EXPAND_BOTH(24); + + private int s; + Sizing(int s) { this.s = s; } + + public boolean fill() + { + return (this.s & 0x2) == 0x2; + } + + public boolean fillWidth() + { + return (this == FILL_WIDTH || this == FILL_BOTH); + } + + public boolean fillHeight() + { + return (this == FILL_HEIGHT || this == FILL_BOTH); + } + + public boolean expand() + { + return (this.s & 0x8) == 0x8; + } + + public boolean expandWidth() + { + return (this == EXPAND_WIDTH || this == EXPAND_BOTH); + } + + public boolean expandHeight() + { + return (this == EXPAND_HEIGHT || this == EXPAND_BOTH); + } +} diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java index e8aea09..e0fcaf4 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.graphics.OrthographicCamera; import ch.asynk.gdx.boardgame.ui.Alignment; import ch.asynk.gdx.boardgame.ui.Button; +import ch.asynk.gdx.boardgame.ui.Element; import ch.asynk.gdx.boardgame.ui.Label; import ch.asynk.gdx.boardgame.ui.List; import ch.asynk.gdx.boardgame.ui.Patch; @@ -46,6 +47,8 @@ public class UiScreen extends AbstractScreen { super(app, "UiScreen"); + Element.DEBUG_GEOMETRY = true; + final NinePatch patch = app.assets.getNinePatch(app.assets.PATCH, 23, 23, 23 ,23); final BitmapFont font = app.assets.getFont(app.assets.FONT_25); @@ -97,6 +100,7 @@ public class UiScreen extends AbstractScreen app.switchToMenu(); break; } + System.err.println("switched to : " + state); this.state = state; } @@ -150,9 +154,9 @@ class MyButton extends Button super(font, patch, padding, spacing); } - @Override public void computeGeometry() + @Override public void computePosition() { - super.computeGeometry(); + super.computePosition(); label.write(String.format("%04d;%04d", (int)getX(), (int)getY())); } } @@ -194,17 +198,18 @@ class MyList extends Patch this.scrollable.hScroll = true; } - @Override public void computeGeometry() + @Override public void computeDimensions() { - // update dimensions - title.computeGeometry(); - scrollable.computeGeometry(); + title.computeDimensions(); + scrollable.computeDimensions(); rect.height = 300; rect.width = scrollable.getBestWidth() + (2 * padding) - 100; + } - // update position - super.computeGeometry(); - title.computeGeometry(); + @Override public void computePosition() + { + super.computePosition(); + title.computePosition(); scrollable.setPosition(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight() - title.getHeight() - 15); } -- cgit v1.1-2-g2b99