summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/ch/asynk')
-rw-r--r--core/src/ch/asynk/gdx/boardgame/Touchable.java4
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Assembly.java75
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Element.java53
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/List.java9
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Menu.java19
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Root.java29
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java11
7 files changed, 101 insertions, 99 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/Touchable.java b/core/src/ch/asynk/gdx/boardgame/Touchable.java
index fab7ab9..a12f711 100644
--- a/core/src/ch/asynk/gdx/boardgame/Touchable.java
+++ b/core/src/ch/asynk/gdx/boardgame/Touchable.java
@@ -1,7 +1,9 @@
package ch.asynk.gdx.boardgame;
+import ch.asynk.gdx.boardgame.ui.Element;
+
public interface Touchable
{
- public boolean touch(float x, float y);
+ public Element touch(float x, float y);
public boolean drag(float x, float y, int dx, int dy);
}
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java b/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java
deleted file mode 100644
index 866c3dd..0000000
--- a/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java
+++ /dev/null
@@ -1,75 +0,0 @@
-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 abstract class Assembly extends Element
-{
- protected IterableSet<Element> children;
- private Element touched;
-
- public Assembly(int c)
- {
- this.children = new IterableSet<Element>(c);
- }
-
- public void add(Element e)
- {
- if (children.add(e)) {
- e.setParent(this);
- }
- }
-
- public void remove(Element e)
- {
- if (children.remove(e)) {
- e.setParent(null);
- }
- }
-
- public void taintChildren()
- {
- children.forEach( c -> c.taint() );
- }
-
- public Element touched()
- {
- return touched;
- }
-
- @Override public boolean touch(float x, float y)
- {
- for (Element e : children) {
- if (e.touch(x, y)) {
- touched = e;
- return true;
- }
-
- }
- touched = null;
- return false;
- }
-
- @Override public boolean drag(float x, float y, int dx, int dy)
- {
- if (touched != null) {
- if (touched.drag(x, y, dx, dy))
- return true;
- touched = null;
- }
- return false;
- }
-
- @Override public void draw(Batch batch)
- {
- if (tainted) computeGeometry();
- children.forEach( c -> c.draw(batch) );
- }
-
- @Override public void drawDebug(ShapeRenderer shapeRenderer)
- {
- children.forEach( c -> c.drawDebug(shapeRenderer) );
- }
-}
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Element.java b/core/src/ch/asynk/gdx/boardgame/ui/Element.java
index bf747b5..0a44eef 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java
@@ -1,5 +1,6 @@
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;
@@ -7,10 +8,12 @@ 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 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;
@@ -23,6 +26,8 @@ public abstract class Element implements Drawable, Paddable, Positionable, Touch
protected boolean tainted; // geometry must be computed
public boolean taintParent; // propagate tainted state up the tree
+ protected IterableSet<Element> children;
+
protected Element()
{
this(false);
@@ -40,6 +45,7 @@ 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; }
@@ -78,15 +84,54 @@ 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.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 boolean touch(float x, float y)
+ @Override public Element touch(float x, float y)
{
- if (blocked || !visible) return false;
- return rect.contains(x, 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)
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/List.java b/core/src/ch/asynk/gdx/boardgame/ui/List.java
index 4f47e60..0c40a7b 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/List.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/List.java
@@ -38,19 +38,20 @@ public class List extends Element
public Integer getIdx() { return idx; }
public Item getSelected() { return ((idx == null) ? null : items.get(idx)); }
- @Override public boolean touch(float x, float y)
+ @Override public Element touch(float x, float y)
{
- if (super.touch(x, y)) {
+ final Element touched = super.touch(x, y);
+ if (touched != null) {
idx = (int) Math.floor((getInnerTop() - y) / itemHeight);
if ((idx >= 0) && (idx < items.size())) {
selected.setPosition(getX(), getInnerTop() - ((idx + 1) * itemHeight) + spacing / 2f, getWidth(), itemHeight);
selected.visible = true;
- return true;
+ return touched;
}
}
idx = null;
selected.visible = false;
- return false;
+ return null;
}
public void setItems(Collection<Item> items)
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java
index cf5317c..e851175 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Menu.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Menu.java
@@ -9,7 +9,7 @@ public class Menu extends Patch
{
private Label title;
private Label[] entries;
- private Integer touchedItem;
+ private int touchedItem;
private int entriesOffset; // horizontal offset
private int titleSpacing; // between title and entries
@@ -18,7 +18,7 @@ public class Menu extends Patch
public Menu(BitmapFont font, NinePatch patch, String title, String[] entries)
{
super(patch);
- this.touchedItem = null;
+ this.touchedItem = -1;
setTitle(font, title);
setEntries(font, entries);
}
@@ -95,23 +95,24 @@ public class Menu extends Patch
}
}
- public Integer touched()
+ public int touched()
{
return touchedItem;
}
- @Override public boolean touch(float x, float y)
+ @Override public Element touch(float x, float y)
{
- touchedItem = null;
- if (super.touch(x, y)) {
+ touchedItem = -1;
+ if (super.touch(x, y) != null) {
for (int i = 0; i < entries.length; i++) {
- if (entries[i].touch(x, y)) {
+ final Element touched = entries[i].touch(x, y);
+ if (touched != null) {
touchedItem = i;
- return true;
+ return touched;
}
}
}
- return false;
+ return null;
}
@Override public void draw(Batch batch)
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Root.java b/core/src/ch/asynk/gdx/boardgame/ui/Root.java
index 3243b37..4690776 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Root.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Root.java
@@ -2,12 +2,37 @@ package ch.asynk.gdx.boardgame.ui;
import com.badlogic.gdx.math.Rectangle;
-public class Root extends Assembly
+import ch.asynk.gdx.boardgame.utils.IterableSet;
+
+public class Root extends Element
{
+ private Element touched;
+
public Root(int c)
{
- super(c);
this.alignment = Alignment.ABSOLUTE;
+ this.children = new IterableSet<Element>(c);
+ }
+
+ public Element touched()
+ {
+ return touched;
+ }
+
+ @Override public Element touch(float x, float y)
+ {
+ touched = super.touch(x, y);
+ return touched;
+ }
+
+ @Override public boolean drag(float x, float y, int dx, int dy)
+ {
+ if (touched != null && touched != this) {
+ if (touched.drag(x, y, dx, dy))
+ return true;
+ touched = null;
+ }
+ return false;
}
public void resize(Rectangle r)
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java
index fea1522..d07b3c7 100644
--- a/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java
@@ -47,17 +47,20 @@ public class Scrollable extends Element
child.computePosition();
}
- @Override public boolean touch(float x, float y)
+ @Override public Element touch(float x, float y)
{
if (clip.contains(x, y)) {
- return child.touch(x, y);
+ final Element touched = child.touch(x, y);
+ if (touched == child)
+ return this;
+ return touched;
}
- return false;
+ return null;
}
@Override public boolean drag(float x, float y, int dx, int dy)
{
- if (!touch(x, y)) return false;
+ if (touch(x, y) == null) return false;
float tx = 0;
float ty = 0;
if (vScroll) {