summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2020-05-13 07:11:00 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2020-05-13 07:11:00 +0200
commitea93755e4b9d56181bc4e7910044bdc321e748c5 (patch)
tree100fdd14717c699904e359000247a5cb0c53fd98 /core/src/ch/asynk/gdx
parent98b46c29d1b11cd8492911dd242de60aedc44964 (diff)
downloadgdx-boardgame-ea93755e4b9d56181bc4e7910044bdc321e748c5.zip
gdx-boardgame-ea93755e4b9d56181bc4e7910044bdc321e748c5.tar.gz
add ui/Container
Diffstat (limited to 'core/src/ch/asynk/gdx')
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Container.java75
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Element.java47
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Root.java10
3 files changed, 77 insertions, 55 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Container.java b/core/src/ch/asynk/gdx/boardgame/ui/Container.java
new file mode 100644
index 0000000..f12a15d
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Container.java
@@ -0,0 +1,75 @@
+package ch.asynk.gdx.boardgame.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+
+import ch.asynk.gdx.boardgame.utils.IterableSet;
+
+public class Container extends Element
+{
+ public static int DEFAULT_CHILD_COUNT = 2;
+
+ protected IterableSet<Element> children;
+
+ protected Container()
+ {
+ super();
+ this.children = null;
+ }
+
+ public void add(Element e)
+ {
+ if (children == null)
+ children = new IterableSet<Element>(DEFAULT_CHILD_COUNT);
+ if (children.add(e)) {
+ e.setParent(this);
+ }
+ }
+
+ public void remove(Element e)
+ {
+ if (children != null) {
+ if (children.remove(e)) {
+ e.setParent(null);
+ }
+ }
+ }
+
+ @Override public void taint()
+ {
+ super.taint();
+ if (children != null)
+ children.forEach( c -> c.taint() );
+ }
+
+ @Override public void draw(Batch batch)
+ {
+ if (!visible) return;
+ if (tainted) computeGeometry();
+ 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 (super.touch(x, y) != null) {
+ if (children != null) {
+ for (Element e : children) {
+ final Element t = e.touch(x, y);
+ if (t != null)
+ return t;
+ }
+ }
+ return this;
+ }
+ return null;
+ }
+}
+
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Element.java b/core/src/ch/asynk/gdx/boardgame/ui/Element.java
index 41adf29..5df6587 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java
@@ -1,6 +1,5 @@
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;
@@ -8,26 +7,22 @@ import ch.asynk.gdx.boardgame.Drawable;
import ch.asynk.gdx.boardgame.Paddable;
import ch.asynk.gdx.boardgame.Positionable;
import ch.asynk.gdx.boardgame.Touchable;
-import ch.asynk.gdx.boardgame.utils.IterableSet;
public abstract class Element implements Drawable, Paddable, Positionable, Touchable
{
public static boolean DEBUG_GEOMETRY = false;
- public static int DEFAULT_CHILD_COUNT = 2;
public boolean blocked;
public boolean visible;
protected float padding;
protected Element parent;
- protected Sizing sizing; // set dimensions according to parent and children
+ protected Sizing sizing; // sizing policy
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
public boolean taintParent; // propagate tainted state up the tree
- protected IterableSet<Element> children;
-
protected Element()
{
this(false);
@@ -45,7 +40,6 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
this.x = this.y = 0;
this.tainted = true;
this.taintParent = taintParent;
- this.children = null;
}
@Override public final float getX() { return rect.x; }
@@ -84,36 +78,12 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
return r;
}
- public void add(Element e)
- {
- if (children == null)
- children = new IterableSet<Element>(DEFAULT_CHILD_COUNT);
- if (children.add(e)) {
- e.setParent(this);
- }
- }
-
- public void remove(Element e)
- {
- if (children != null) {
- if (children.remove(e)) {
- e.setParent(null);
- }
- }
- }
-
public void taint()
{
this.tainted = true;
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;
@@ -211,29 +181,14 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
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;
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Root.java b/core/src/ch/asynk/gdx/boardgame/ui/Root.java
index c84287b..c3ad0c3 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Root.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Root.java
@@ -5,7 +5,7 @@ import com.badlogic.gdx.math.Rectangle;
import ch.asynk.gdx.boardgame.utils.IterableSet;
-public class Root extends Element
+public class Root extends Container
{
private Element touched;
@@ -29,7 +29,6 @@ public class Root extends Element
{
setPosition(x, y, width, height);
taint();
- taintChildren();
}
public Element touched()
@@ -52,11 +51,4 @@ public class Root extends Element
}
return false;
}
-
- @Override public void draw(Batch batch)
- {
- if (!visible) return;
- if (tainted) computeGeometry();
- children.forEach( c -> c.draw(batch) );
- }
}