diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2019-12-28 13:39:58 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2019-12-28 13:39:58 +0100 |
commit | 9cc4698459e0946e4db040cca57e1e3c46dc8f30 (patch) | |
tree | 451b13407d8567ee37b4cab77bc806fb41a976b2 /core/src/ch/asynk/gdx/boardgame | |
parent | 49b7c2aa90c3af33f455e7aa6c7935818e0df37a (diff) | |
download | gdx-boardgame-9cc4698459e0946e4db040cca57e1e3c46dc8f30.zip gdx-boardgame-9cc4698459e0946e4db040cca57e1e3c46dc8f30.tar.gz |
ui : code reordering, Elemnt is abstract again
Diffstat (limited to 'core/src/ch/asynk/gdx/boardgame')
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Element.java | 86 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Root.java | 35 |
2 files changed, 65 insertions, 56 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Element.java b/core/src/ch/asynk/gdx/boardgame/ui/Element.java index 0a44eef..41adf29 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java @@ -10,7 +10,7 @@ import ch.asynk.gdx.boardgame.Positionable; import ch.asynk.gdx.boardgame.Touchable; import ch.asynk.gdx.boardgame.utils.IterableSet; -public class Element implements Drawable, Paddable, Positionable, Touchable +public abstract class Element implements Drawable, Paddable, Positionable, Touchable { public static boolean DEBUG_GEOMETRY = false; public static int DEFAULT_CHILD_COUNT = 2; @@ -95,49 +95,11 @@ public class Element implements Drawable, Paddable, Positionable, Touchable public void remove(Element e) { - if (children.remove(e)) { - e.setParent(null); - } - } - - public void taintChildren() - { - if (children != null) - children.forEach( c -> c.taint() ); - } - - @Override public void draw(Batch batch) - { - if (tainted) computeGeometry(); - children.forEach( c -> c.draw(batch) ); - } - - @Override public void drawDebug(ShapeRenderer shapeRenderer) - { - shapeRenderer.rect(getX(), getY(), getWidth(), getHeight()); - if (children != null) - children.forEach( c -> c.drawDebug(shapeRenderer) ); - } - - @Override public Element touch(float x, float y) - { - if (!blocked && visible && rect.contains(x, y)) { - if (children != null) { - for (Element e : children) { - final Element t = e.touch(x, y); - if (t != null) - return t; - } + if (children != null) { + if (children.remove(e)) { + e.setParent(null); } - return this; } - return null; - } - - @Override public boolean drag(float x, float y, int dx, int dy) - { - if (blocked || !visible) return false; - return rect.contains(x, y); } public void taint() @@ -146,6 +108,12 @@ public class Element implements Drawable, Paddable, Positionable, Touchable if (parent != null && taintParent) parent.taint(); } + public void taintChildren() + { + if (children != null) + children.forEach( c -> c.taint() ); + } + @Override public void setPosition(float x, float y, float w, float h) { this.x = x; @@ -242,4 +210,38 @@ public class Element implements Drawable, Paddable, Positionable, Touchable if (DEBUG_GEOMETRY) System.err.println("Geometry]" + print(-1)); this.tainted = false; } + + public void drawChildren(Batch batch) + { + if (children != null) + children.forEach( c -> c.draw(batch) ); + } + + @Override public void drawDebug(ShapeRenderer shapeRenderer) + { + shapeRenderer.rect(getX(), getY(), getWidth(), getHeight()); + if (children != null) + children.forEach( c -> c.drawDebug(shapeRenderer) ); + } + + @Override public Element touch(float x, float y) + { + if (!blocked && visible && rect.contains(x, y)) { + if (children != null) { + for (Element e : children) { + final Element t = e.touch(x, y); + if (t != null) + return t; + } + } + return this; + } + return null; + } + + @Override public boolean drag(float x, float y, int dx, int dy) + { + if (blocked || !visible) return false; + return rect.contains(x, y); + } } diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Root.java b/core/src/ch/asynk/gdx/boardgame/ui/Root.java index 4690776..96ea218 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Root.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Root.java @@ -1,5 +1,6 @@ package ch.asynk.gdx.boardgame.ui; +import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.math.Rectangle; import ch.asynk.gdx.boardgame.utils.IterableSet; @@ -14,6 +15,23 @@ public class Root extends Element this.children = new IterableSet<Element>(c); } + public void resize(Rectangle r) + { + resize(r.x, r.y, r.width, r.height); + } + + public void resize(float width, float height) + { + resize(getX(), getY(), width, height); + } + + public void resize(float x, float y, float width, float height) + { + setPosition(x, y, width, height); + taint(); + taintChildren(); + } + public Element touched() { return touched; @@ -35,20 +53,9 @@ public class Root extends Element return false; } - public void resize(Rectangle r) - { - resize(r.x, r.y, r.width, r.height); - } - - public void resize(float width, float height) + @Override public void draw(Batch batch) { - resize(getX(), getY(), width, height); - } - - public void resize(float x, float y, float width, float height) - { - setPosition(x, y, width, height); - taint(); - taintChildren(); + if (tainted) computeGeometry(); + children.forEach( c -> c.draw(batch) ); } } |