summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-05-17 06:23:33 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-05-17 06:23:33 +0200
commit8647dafb89759dc3b7e4d80781405878e196bca0 (patch)
treef560e8b6e3a8459f5de6af0ebe860d714f9b218e
parentb6074b00c0cdb14becdecd896c1e4db7af2b2df1 (diff)
downloadgdx-boardgame-8647dafb89759dc3b7e4d80781405878e196bca0.zip
gdx-boardgame-8647dafb89759dc3b7e4d80781405878e196bca0.tar.gz
UI : computeGeometry is called from Root before drawing if dirty or resized
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Button.java11
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Container.java20
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Element.java47
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Label.java4
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/List.java16
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Menu.java14
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Root.java16
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java20
-rw-r--r--test/src/ch/asynk/gdx/boardgame/test/UiScreen.java21
9 files changed, 82 insertions, 87 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Button.java b/core/src/ch/asynk/gdx/boardgame/ui/Button.java
index 488fcfa..7fdc799 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Button.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Button.java
@@ -41,22 +41,21 @@ public class Button extends Patch
label.setAlignment(alignment);
}
- @Override public void computeGeometry(Rectangle area)
+ @Override public void computeGeometry(Rectangle area, boolean resized)
{
+ label.computeDimensions();
float dd = 2 * (padding + spacing);
- label.computeGeometry(area);
rect.width = label.getWidth() + dd;
rect.height = label.getHeight() + dd;
- if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1));
- super.computeGeometry(area);
- label.computeGeometry(innerRect);
+ super.computeGeometry(area, resized);
+ label.computeGeometry(innerRect, resized);
}
@Override public void drawReal(Batch batch)
{
super.drawReal(batch);
- label.drawReal(batch);
+ label.draw(batch);
}
@Override public void drawDebug(ShapeRenderer shapeRenderer)
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Container.java b/core/src/ch/asynk/gdx/boardgame/ui/Container.java
index 0a82a67..30b5f20 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Container.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Container.java
@@ -38,27 +38,19 @@ public class Container extends Element
}
}
- @Override public void drip()
+ @Override public void computeGeometry(Rectangle area, boolean resized)
{
- super.drip();
- if (children != null)
- children.forEach( c -> c.drip() );
- }
-
- @Override public void computeGeometry(Rectangle area)
- {
- if (children != null) {
- for(Element e : children) {
- e.computeGeometry(area);
- e.clear();
- }
+ if (dirty || resized) {
+ if (children != null)
+ children.forEach( c -> c.computeGeometry(area, resized) );
+ super.computeGeometry(area, resized);
}
}
@Override public void drawReal(Batch batch)
{
if (children != null)
- children.forEach( c -> c.draw(batch, innerRect) );
+ children.forEach( c -> c.draw(batch) );
}
@Override public void drawDebug(ShapeRenderer shapeRenderer)
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Element.java b/core/src/ch/asynk/gdx/boardgame/ui/Element.java
index 54d2953..89e28a7 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java
@@ -35,7 +35,7 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
this.rect = new Rectangle(0, 0, 0, 0);
this.innerRect = new Rectangle(0, 0, 0, 0);
this.x = this.y = 0;
- taint();
+ this.dirty = true;
}
@Override public final float getX() { return rect.x; }
@@ -85,23 +85,15 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
this.dirty = false;
}
- public void mark()
- {
- if (DEBUG_GEOMETRY) print(" mark");
- this.dirty = true;
- }
-
- public void drip()
- {
- if (DEBUG_GEOMETRY) print(" drip");
- this.dirty = true;
- }
-
public void taint()
{
+ if (this.dirty) {
+ if (DEBUG_GEOMETRY) print(" already dirty");
+ return;
+ }
if (DEBUG_GEOMETRY) print(" taint");
- mark();
- if (parent != null) parent.mark();
+ this.dirty = true;
+ if (parent != null) parent.taint();
}
@Override public void setPosition(float x, float y, float w, float h)
@@ -171,12 +163,10 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
setPosition(x, y, rect.width, rect.height);
}
+ protected void computeDimensions() { }
+
private void computePosition(Rectangle area)
{
- if (area == null) {
- System.err.println("******** ERROR : computeGeometry(null) check for draw(batch) calls : " + print(-1));
- return;
- }
if (alignment == Alignment.ABSOLUTE) {
rect.x = x;
rect.y = y;
@@ -191,27 +181,20 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
if (DEBUG_GEOMETRY) System.err.println(" pos " + print(-1));
}
- public void computeGeometry(Rectangle area)
+ public void computeGeometry(Rectangle area, boolean resized)
{
- computePosition(area);
+ if (dirty || resized) {
+ computeDimensions();
+ computePosition(area);
+ clear();
+ }
}
public abstract void drawReal(Batch batch);
@Override public void draw(Batch batch)
{
- draw(batch, null);
- }
-
- public void draw(Batch batch, Rectangle area)
- {
if (!visible) return;
- if (dirty) {
- if (DEBUG_GEOMETRY) { print("[Geometry"); System.err.println(" --> " + print(-1)); }
- computeGeometry(area);
- clear();
- if (DEBUG_GEOMETRY) System.err.println("Geometry]");
- }
drawReal(batch);
}
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Label.java b/core/src/ch/asynk/gdx/boardgame/ui/Label.java
index 8acb5e5..6129547 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Label.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Label.java
@@ -53,12 +53,10 @@ public class Label extends Element
taint();
}
- @Override public void computeGeometry(Rectangle area)
+ @Override public void computeDimensions()
{
this.rect.width = (layout.width + (2 * padding));
this.rect.height = (layout.height + (2 * padding));
- if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1));
- super.computeGeometry(area);
}
@Override public void drawReal(Batch batch)
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/List.java b/core/src/ch/asynk/gdx/boardgame/ui/List.java
index b316241..4b0d388 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/List.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/List.java
@@ -47,7 +47,7 @@ public class List extends Element
if (i >= 0 && i < items.size()) {
if (idx == null || idx != i) {
idx = i;
- selected.setPosition(0, getInnerHeight() - ((idx + 1) * itemHeight) + spacing / 2f, getWidth(), itemHeight);
+ selected.setPosition(0, getHeight() - padding + spacing / 2 - ((idx + 1) * itemHeight), getWidth(), itemHeight);
selected.visible = true;
}
return touched;
@@ -68,10 +68,10 @@ public class List extends Element
@Override public void translate(float x, float y)
{
super.translate(x, y);
- selected.mark();
+ selected.taint();
}
- @Override public void computeGeometry(Rectangle area)
+ public void computeDimensions()
{
float w = 0f;
for (Item e: items) {
@@ -81,9 +81,13 @@ public class List extends Element
itemHeight = (layout.height + spacing);
rect.width = w + (2 * padding);
rect.height = (itemHeight * items.size()) + (2 * padding) - spacing;
- if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1));
+ }
- super.computeGeometry(area);
+ @Override public void computeGeometry(Rectangle area, boolean resized)
+ {
+ computeDimensions();
+ super.computeGeometry(area, resized);
+ selected.computeGeometry(rect, resized);
}
@Override public void drawReal(Batch batch)
@@ -94,6 +98,6 @@ public class List extends Element
font.draw(batch, e.s(), x, y);
y -= itemHeight;
}
- selected.draw(batch, innerRect);
+ selected.draw(batch);
}
}
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java
index 14c8f2f..b754083 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java
@@ -66,13 +66,13 @@ public class Menu extends Patch
taint();
}
- @Override public void computeGeometry(Rectangle area)
+ @Override public void computeGeometry(Rectangle area, boolean resized)
{
- title.computeGeometry(area);
+ title.computeGeometry(area, resized);
float h = title.getHeight();
float w = title.getWidth();
for (Label label : entries) {
- label.computeGeometry(area);
+ label.computeGeometry(area, resized);
h += label.getHeight();
float t = label.getWidth() + entriesOffset;
if (t > w)
@@ -80,9 +80,8 @@ 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));
- super.computeGeometry(area);
+ super.computeGeometry(area, resized);
float y = getInnerHeight() - title.getHeight();
title.setPositionClear(0, y, innerRect);
@@ -91,7 +90,6 @@ public class Menu extends Patch
l.setPositionClear(x + entriesOffset, y - l.getHeight(), innerRect);
y -= (l.getHeight() + entriesSpacing);
}
- if (DEBUG_GEOMETRY) System.err.println(" pos " + print(-1));
}
public int touched()
@@ -117,9 +115,9 @@ public class Menu extends Patch
@Override public void drawReal(Batch batch)
{
super.drawReal(batch);
- title.drawReal(batch);
+ title.draw(batch);
for (Label label : entries) {
- label.drawReal(batch);
+ label.draw(batch);
}
}
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Root.java b/core/src/ch/asynk/gdx/boardgame/ui/Root.java
index 2099cbd..750f574 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Root.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Root.java
@@ -8,6 +8,7 @@ import ch.asynk.gdx.boardgame.utils.IterableSet;
public class Root extends Container
{
private Element touched;
+ private boolean resized;
public Root(int c)
{
@@ -28,15 +29,15 @@ public class Root extends Container
public void resize(float x, float y, float width, float height)
{
setPosition(x, y, width, height);
- drip();
+ resized = true;
}
- @Override public void computeGeometry(Rectangle area)
+ @Override public void computeGeometry(Rectangle area, boolean resized)
{
rect.x = x;
rect.y = y;
innerRect.set(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight());
- super.computeGeometry(innerRect);
+ super.computeGeometry(innerRect, resized);
}
public Element touched()
@@ -44,6 +45,15 @@ public class Root extends Container
return touched;
}
+ @Override public void draw(Batch batch)
+ {
+ if (dirty || resized) {
+ computeGeometry(null, resized);
+ resized = false;
+ }
+ super.draw(batch);
+ }
+
@Override public Element touch(float x, float y)
{
touched = super.touch(x, y);
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java
index e07df0f..c95c7f1 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java
@@ -20,9 +20,10 @@ public class Scrollable extends Element
super();
this.vScroll = true;
this.hScroll = false;
- this.child = child;
this.scissorIdx = -1;
this.clip = new Rectangle();
+ this.child = child;
+ child.setParent(this);
}
public Element getChild() { return child; }
@@ -37,11 +38,20 @@ public class Scrollable extends Element
return (child.getHeight() + (2 * padding));
}
- @Override public void computeGeometry(Rectangle area)
+ public void computeDimensions()
+ {
+ child.computeDimensions();
+ }
+
+ @Override public void computeGeometry(Rectangle area, boolean resized)
{
- child.computeGeometry(area);
+ if (resized || scissorIdx == -1)
+ setPosition(area.x, area.y, area.width, area.height);
+ child.computeGeometry(clip, resized);
+ clear();
}
+
@Override public Element touch(float x, float y)
{
if (clip.contains(x, y)) {
@@ -88,8 +98,8 @@ public class Scrollable extends Element
{
rect.set(x, y, w, h);
clip.set((x + padding), (y + padding), (w - 2 * padding), (h - 2 * padding));
- child.setPositionClear(clip.x, clip.y - (child.rect.height - clip.height), innerRect);
scissorIdx = Scissors.compute(scissorIdx, clip);
+ child.setPositionClear(0, - (child.rect.height - clip.height), clip);
}
@Override public void drawReal(Batch batch)
@@ -98,7 +108,7 @@ public class Scrollable extends Element
Rectangle scissor = Scissors.get(scissorIdx, clip);
HdpiUtils.glScissor((int)scissor.x, (int)scissor.y, (int)scissor.width, (int)scissor.height);
Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST);
- child.draw(batch, innerRect);
+ child.draw(batch);
batch.flush();
Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
}
diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
index 8a732b3..73431a2 100644
--- a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
+++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
@@ -153,19 +153,20 @@ class MyButton extends Button
super(font, patch, padding, spacing);
}
- @Override public void computeGeometry(Rectangle space)
+ @Override public void computeGeometry(Rectangle space, boolean resized)
{
- super.computeGeometry(space);
- System.err.println("call to label.write(…)");
+ super.computeGeometry(space, resized);
label.write(String.format("%04d;%04d", (int)getX(), (int)getY()));
+ label.clear();
+ clear();
}
}
class MyList extends Patch
{
private Label title;
- private Scrollable scrollable;
private List list;
+ private Scrollable scrollable;
class Item implements List.Item
{
@@ -198,16 +199,16 @@ class MyList extends Patch
this.scrollable.hScroll = true;
}
- @Override public void computeGeometry(Rectangle space)
+ @Override public void computeGeometry(Rectangle space, boolean resized)
{
- scrollable.computeGeometry(space);
+ scrollable.computeDimensions();
rect.height = 300;
rect.width = scrollable.getBestWidth() + (2 * padding) - 100;
- super.computeGeometry(space);
- title.computeGeometry(innerRect);
- scrollable.computeGeometry(space);
- scrollable.setPosition(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight() - title.getHeight() - 15);
+ super.computeGeometry(space, resized);
+ title.computeGeometry(innerRect, resized);
+ Rectangle a = new Rectangle(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight() - title.getHeight() - 0);
+ scrollable.computeGeometry(a, resized);
}
@Override public Element touch(float x, float y)