diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2020-05-15 12:11:31 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2020-05-15 12:11:31 +0200 |
commit | b6074b00c0cdb14becdecd896c1e4db7af2b2df1 (patch) | |
tree | e47c4f6f1a7df9f4ee6fb9f5c1ad88a7592a2780 /core | |
parent | 639f146d92e0e2503f4360e40f79b885db6d1ba5 (diff) | |
download | gdx-boardgame-b6074b00c0cdb14becdecd896c1e4db7af2b2df1.zip gdx-boardgame-b6074b00c0cdb14becdecd896c1e4db7af2b2df1.tar.gz |
UI : computeDimensons()+computePosition() -> computeGeometry(rect)
Diffstat (limited to 'core')
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Alignment.java | 18 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Button.java | 12 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Container.java | 13 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Element.java | 50 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Label.java | 4 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/List.java | 18 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Menu.java | 16 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Root.java | 8 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java | 13 |
9 files changed, 85 insertions, 67 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Alignment.java b/core/src/ch/asynk/gdx/boardgame/ui/Alignment.java index 67da089..3c12087 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Alignment.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Alignment.java @@ -1,5 +1,7 @@ package ch.asynk.gdx.boardgame.ui; +import com.badlogic.gdx.math.Rectangle; + public enum Alignment { ABSOLUTE, // Root @@ -122,9 +124,9 @@ public enum Alignment return false; } - public float getX(Element element, float width) + public float getX(Rectangle area, float width) { - float x = element.getInnerX(); + float x = area.x; switch(this) { case TOP_LEFT: case MIDDLE_LEFT: @@ -133,30 +135,30 @@ public enum Alignment case TOP_CENTER: case MIDDLE_CENTER: case BOTTOM_CENTER: - x += ((element.getInnerWidth() - width) / 2); + x += ((area.width - width) / 2); break; case TOP_RIGHT: case MIDDLE_RIGHT: case BOTTOM_RIGHT: - x += (element.getInnerWidth() - width); + x += (area.width - width); break; } return x; } - public float getY(Element element, float height) + public float getY(Rectangle area, float height) { - float y = element.getInnerY(); + float y = area.y; switch(this) { case TOP_LEFT: case TOP_CENTER: case TOP_RIGHT: - y += (element.getInnerHeight() - height); + y += (area.height - height); break; case MIDDLE_LEFT: case MIDDLE_CENTER: case MIDDLE_RIGHT: - y += ((element.getInnerHeight() - height) / 2); + y += ((area.height - height) / 2); break; case BOTTOM_LEFT: case BOTTOM_CENTER: diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Button.java b/core/src/ch/asynk/gdx/boardgame/ui/Button.java index 46232ba..488fcfa 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Button.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Button.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Rectangle; public class Button extends Patch { @@ -40,19 +41,16 @@ public class Button extends Patch label.setAlignment(alignment); } - @Override public void computeDimensions() + @Override public void computeGeometry(Rectangle area) { float dd = 2 * (padding + spacing); - label.computeDimensions(); + label.computeGeometry(area); rect.width = label.getWidth() + dd; rect.height = label.getHeight() + dd; if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1)); - } - @Override public void computePosition() - { - super.computePosition(); - label.computePosition(); + super.computeGeometry(area); + label.computeGeometry(innerRect); } @Override public void drawReal(Batch batch) diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Container.java b/core/src/ch/asynk/gdx/boardgame/ui/Container.java index ab6b781..0a82a67 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Container.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Container.java @@ -2,6 +2,7 @@ package ch.asynk.gdx.boardgame.ui; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Rectangle; import ch.asynk.gdx.boardgame.utils.IterableSet; @@ -44,10 +45,20 @@ public class Container extends Element children.forEach( c -> c.drip() ); } + @Override public void computeGeometry(Rectangle area) + { + if (children != null) { + for(Element e : children) { + e.computeGeometry(area); + e.clear(); + } + } + } + @Override public void drawReal(Batch batch) { if (children != null) - children.forEach( c -> c.draw(batch) ); + children.forEach( c -> c.draw(batch, innerRect) ); } @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 b7604f6..54d2953 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java @@ -20,6 +20,7 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch protected Sizing sizing; // sizing policy protected Alignment alignment; // where to position itself protected Rectangle rect; // outer drawing coordinates + protected Rectangle innerRect; // inner drawing coordinates protected float x, y; // given position protected boolean dirty; // geometry must be computed @@ -32,6 +33,7 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch this.sizing = Sizing.NONE; this.alignment = alignment.RELATIVE; this.rect = new Rectangle(0, 0, 0, 0); + this.innerRect = new Rectangle(0, 0, 0, 0); this.x = this.y = 0; taint(); } @@ -111,11 +113,11 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch taint(); } - public final void setPositionClear(float x, float y) + public final void setPositionClear(float x, float y, Rectangle area) { this.x = x; this.y = y; - computePosition(); + computePosition(area); clear(); } @@ -154,7 +156,7 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch setPosition((cx - (rect.width / 2f)), (cy - (rect.height / 2f))); } - @Override public final void translate(float dx, float dy) + @Override public void translate(float dx, float dy) { setPosition(x + dx, y + dy); } @@ -169,44 +171,44 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch setPosition(x, y, rect.width, rect.height); } - public void computeDimensions() + private void computePosition(Rectangle area) { - if (parent != null && sizing.fill()) { - if (sizing.fillWidth()) { - rect.width = parent.getInnerWidth(); - } - if (sizing.fillHeight()) { - rect.height = parent.getInnerHeight(); - } + if (area == null) { + System.err.println("******** ERROR : computeGeometry(null) check for draw(batch) calls : " + print(-1)); + return; } - // 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) { + if (alignment == Alignment.ABSOLUTE) { rect.x = x; rect.y = y; } else if (alignment == Alignment.RELATIVE) { - rect.x = x + parent.getInnerX(); - rect.y = y + parent.getInnerY(); + rect.x = x + area.x; + rect.y = y + area.y; } else { - rect.x = x + alignment.getX(parent, rect.width); - rect.y = y + alignment.getY(parent, rect.height); + rect.x = x + alignment.getX(area, rect.width); + rect.y = y + alignment.getY(area, rect.height); } + innerRect.set(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight()); if (DEBUG_GEOMETRY) System.err.println(" pos " + print(-1)); } + public void computeGeometry(Rectangle area) + { + computePosition(area); + } + 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)); } - computeDimensions(); - computePosition(); + computeGeometry(area); clear(); if (DEBUG_GEOMETRY) System.err.println("Geometry]"); } diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Label.java b/core/src/ch/asynk/gdx/boardgame/ui/Label.java index 4ff4789..8acb5e5 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Label.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Label.java @@ -3,6 +3,7 @@ package ch.asynk.gdx.boardgame.ui; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.GlyphLayout; +import com.badlogic.gdx.math.Rectangle; public class Label extends Element { @@ -52,11 +53,12 @@ public class Label extends Element taint(); } - @Override public void computeDimensions() + @Override public void computeGeometry(Rectangle area) { 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 2d84a94..b316241 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/List.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/List.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.math.Rectangle; import ch.asynk.gdx.boardgame.utils.Collection; @@ -64,7 +65,13 @@ public class List extends Element taint(); } - @Override public void computeDimensions() + @Override public void translate(float x, float y) + { + super.translate(x, y); + selected.mark(); + } + + @Override public void computeGeometry(Rectangle area) { float w = 0f; for (Item e: items) { @@ -75,13 +82,8 @@ public class List extends Element rect.width = w + (2 * padding); rect.height = (itemHeight * items.size()) + (2 * padding) - spacing; if (DEBUG_GEOMETRY) System.err.println(" dim " + print(-1)); - } - @Override public void computePosition() - { - super.computePosition(); - selected.computePosition(); - if (DEBUG_GEOMETRY) System.err.println(" pos " + print(-1)); + super.computeGeometry(area); } @Override public void drawReal(Batch batch) @@ -92,6 +94,6 @@ public class List extends Element font.draw(batch, e.s(), x, y); y -= itemHeight; } - selected.draw(batch); + selected.draw(batch, innerRect); } } diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java index 2c0c429..14c8f2f 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Rectangle; public class Menu extends Patch { @@ -65,13 +66,13 @@ public class Menu extends Patch taint(); } - @Override public void computeDimensions() + @Override public void computeGeometry(Rectangle area) { - title.computeDimensions(); + title.computeGeometry(area); float h = title.getHeight(); float w = title.getWidth(); for (Label label : entries) { - label.computeDimensions(); + label.computeGeometry(area); h += label.getHeight(); float t = label.getWidth() + entriesOffset; if (t > w) @@ -80,17 +81,14 @@ 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)); - } - @Override public void computePosition() - { - super.computePosition(); + super.computeGeometry(area); float y = getInnerHeight() - title.getHeight(); - title.setPositionClear(0, y); + title.setPositionClear(0, y, innerRect); y -= titleSpacing; for (Label l : entries) { - l.setPositionClear(x + entriesOffset, y - l.getHeight()); + l.setPositionClear(x + entriesOffset, y - l.getHeight(), innerRect); y -= (l.getHeight() + entriesSpacing); } if (DEBUG_GEOMETRY) System.err.println(" pos " + print(-1)); diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Root.java b/core/src/ch/asynk/gdx/boardgame/ui/Root.java index f11a617..2099cbd 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Root.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Root.java @@ -31,6 +31,14 @@ public class Root extends Container drip(); } + @Override public void computeGeometry(Rectangle area) + { + rect.x = x; + rect.y = y; + innerRect.set(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight()); + super.computeGeometry(innerRect); + } + public Element touched() { return touched; diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java index b5f55f4..e07df0f 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java @@ -37,14 +37,9 @@ public class Scrollable extends Element return (child.getHeight() + (2 * padding)); } - @Override public void computeDimensions() + @Override public void computeGeometry(Rectangle area) { - child.computeDimensions(); - } - - @Override public void computePosition() - { - child.computePosition(); + child.computeGeometry(area); } @Override public Element touch(float x, float y) @@ -93,7 +88,7 @@ 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)); + child.setPositionClear(clip.x, clip.y - (child.rect.height - clip.height), innerRect); scissorIdx = Scissors.compute(scissorIdx, clip); } @@ -103,7 +98,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); + child.draw(batch, innerRect); batch.flush(); Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST); } |