diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/src/ch/asynk/gdx/tabletop/ui/Assembly.java | 4 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/tabletop/ui/Button.java | 31 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/tabletop/ui/Element.java | 35 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/tabletop/ui/Label.java | 13 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/tabletop/ui/Patch.java | 1 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/tabletop/ui/Root.java | 2 |
6 files changed, 45 insertions, 41 deletions
diff --git a/core/src/ch/asynk/gdx/tabletop/ui/Assembly.java b/core/src/ch/asynk/gdx/tabletop/ui/Assembly.java index 505bc2c..9facd26 100644 --- a/core/src/ch/asynk/gdx/tabletop/ui/Assembly.java +++ b/core/src/ch/asynk/gdx/tabletop/ui/Assembly.java @@ -45,9 +45,9 @@ public abstract class Assembly extends Element return false; } - @Override public void update() + @Override public void taint() { - children.forEach( c -> c.update() ); + children.forEach( c -> c.taint() ); } @Override public void draw(Batch batch) diff --git a/core/src/ch/asynk/gdx/tabletop/ui/Button.java b/core/src/ch/asynk/gdx/tabletop/ui/Button.java index cc9c6da..06688c9 100644 --- a/core/src/ch/asynk/gdx/tabletop/ui/Button.java +++ b/core/src/ch/asynk/gdx/tabletop/ui/Button.java @@ -8,11 +8,16 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; public class Button extends Patch { private Label label; - private float spacing; + private float spacing; // for label alignment; public Button(BitmapFont font, NinePatch patch) { - this(font, patch, 0, 0); + this(font, patch, 0); + } + + public Button(BitmapFont font, NinePatch patch, float padding) + { + this(font, patch, padding, 0); } public Button(BitmapFont font, NinePatch patch, float padding, float spacing) @@ -27,12 +32,8 @@ public class Button extends Patch public void write(String text) { - write(text, getX(), getY()); - } - - public void write(String text, float x, float y) - { - label.write(text, x, y); + label.write(text); + this.tainted = true; // might impact Button's geometry } public void setLabelAlignment(Alignment alignment) @@ -40,19 +41,19 @@ public class Button extends Patch label.setAlignment(alignment); } - @Override public void update() + @Override public void computeGeometry() { - label.preUpdate(); // compute width and height - rect.width = label.getWidth() + 2 * (padding + spacing); - rect.height = label.getHeight() + 2 * (padding + spacing); - super.update(); - label.doUpdate(); // compute x and y - label.postUpdate(); // compute fx and fy + float dd = 2 * (padding + spacing); + rect.width = label.getWidth() + dd; + rect.height = label.getHeight() + dd; + super.computeGeometry(); + label.computeGeometry(); } @Override public void draw(Batch batch) { if (!visible) return; + if (tainted) computeGeometry(); super.draw(batch); label.draw(batch); } diff --git a/core/src/ch/asynk/gdx/tabletop/ui/Element.java b/core/src/ch/asynk/gdx/tabletop/ui/Element.java index 17511a9..5b3c0fe 100644 --- a/core/src/ch/asynk/gdx/tabletop/ui/Element.java +++ b/core/src/ch/asynk/gdx/tabletop/ui/Element.java @@ -13,18 +13,20 @@ public abstract class Element implements Drawable, Touchable protected float padding; protected Element parent; protected Alignment alignment; - protected Rectangle rect; // drawing coordinates + protected Rectangle rect; // outer drawing coordinates protected float x, y; // given position + protected boolean tainted; // geometry must be computed protected Element() { this.blocked = false; this.visible = true; this.padding = 0; - this.rect = new Rectangle(0, 0, 0, 0); this.parent = null; this.alignment = alignment.RELATIVE; + this.rect = new Rectangle(0, 0, 0, 0); this.x = this.y = 0; + this.tainted = true; } @Override public final float getX() { return rect.x; } @@ -48,28 +50,37 @@ public abstract class Element implements Drawable, Touchable return rect.contains(x, y); } + public void taint() + { + this.tainted = true; + } + @Override public void setPosition(float x, float y, float w, float h) { this.x = x; this.y = y; - rect.width = w; - rect.height = h; - // rect.set(rec.x, y, w, h); + this.rect.width = w; + this.rect.height = h; + this.tainted = true; + // rect.(x,y) will be set in computeGeometry } public void setParent(Element parent) { this.parent = parent; + this.tainted = true; } public void setPadding(float padding) { this.padding = padding; + this.tainted = true; } public void setAlignment(Alignment alignment) { this.alignment = alignment; + this.tainted = true; } public final void translate(float dx, float dy) @@ -87,26 +98,18 @@ public abstract class Element implements Drawable, Touchable setPosition(x, y, rect.width, rect.height); } - protected void preUpdate() { } - protected void postUpdate() { } - protected void doUpdate() + protected void computeGeometry() { if (alignment == Alignment.ABSOLUTE || parent == null) { rect.x = x; rect.y = y; } else if (alignment == Alignment.RELATIVE) { - rect.x = x + parent.getInnerX(); + rect.x = x + parent.getInnerX(); rect.y = y + parent.getInnerX(); } else { rect.x = x + alignment.getX(parent, rect.width); rect.y = y + alignment.getY(parent, rect.height); } - } - - public void update() - { - preUpdate(); - doUpdate(); - postUpdate(); + this.tainted = false; } } diff --git a/core/src/ch/asynk/gdx/tabletop/ui/Label.java b/core/src/ch/asynk/gdx/tabletop/ui/Label.java index d2b5fc6..55eab83 100644 --- a/core/src/ch/asynk/gdx/tabletop/ui/Label.java +++ b/core/src/ch/asynk/gdx/tabletop/ui/Label.java @@ -39,17 +39,15 @@ public class Label extends Element public void write(String text, float x, float y) { this.text = text; - setPosition(x, y); - } - - @Override protected void preUpdate() - { this.layout.setText(font, (text == null) ? "" : text); - super.setPosition(x, y, (layout.width + (2 * padding)), (layout.height + (2 * padding))); + this.rect.width = (layout.width + (2 * padding)); + this.rect.height = (layout.height + (2 * padding)); + this.tainted = true; } - @Override protected void postUpdate() + @Override protected void computeGeometry() { + super.computeGeometry(); fx = getInnerX(); fy = getInnerY() + layout.height; } @@ -57,6 +55,7 @@ public class Label extends Element @Override public void draw(Batch batch) { if (!visible) return; + if (tainted) computeGeometry(); font.draw(batch, layout, fx, fy); } } diff --git a/core/src/ch/asynk/gdx/tabletop/ui/Patch.java b/core/src/ch/asynk/gdx/tabletop/ui/Patch.java index c012f70..a829ce0 100644 --- a/core/src/ch/asynk/gdx/tabletop/ui/Patch.java +++ b/core/src/ch/asynk/gdx/tabletop/ui/Patch.java @@ -17,6 +17,7 @@ public class Patch extends Element @Override public void draw(Batch batch) { if (!visible) return; + if (tainted) computeGeometry(); patch.draw(batch, getX(), getY(), getWidth(), getHeight()); } } diff --git a/core/src/ch/asynk/gdx/tabletop/ui/Root.java b/core/src/ch/asynk/gdx/tabletop/ui/Root.java index f42a8df..99d63cf 100644 --- a/core/src/ch/asynk/gdx/tabletop/ui/Root.java +++ b/core/src/ch/asynk/gdx/tabletop/ui/Root.java @@ -11,6 +11,6 @@ public class Root extends Assembly public void resize(float width, float height) { setPosition(0, 0, width, height); - update(); + taint(); } } |