summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2019-12-26 21:41:39 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2019-12-26 21:41:39 +0100
commit97caa84105cceb98ec1253426e200858b4bcb9da (patch)
treedcb5590bec09f216cfbb44806f5eaab561eca9c0 /core
parent059f93fd23c46dd58a9a6e92491c108eec05e26c (diff)
downloadgdx-boardgame-97caa84105cceb98ec1253426e200858b4bcb9da.zip
gdx-boardgame-97caa84105cceb98ec1253426e200858b4bcb9da.tar.gz
ui : add Sizing, final computeGeometry() calls computeDimensions() and computePosition()
Diffstat (limited to 'core')
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Button.java34
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Element.java45
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Label.java21
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/List.java15
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Menu.java14
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java9
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Sizing.java47
7 files changed, 147 insertions, 38 deletions
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);
+ }
+}