diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2019-12-27 14:40:52 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2019-12-27 14:40:52 +0100 |
commit | 828816db241467c296d3559226cd5fe5494e5761 (patch) | |
tree | 32b4822bd0688e60f2093c7c9c846f4580778035 | |
parent | 876fb5909fdb1924f4ee8355a0e7840911c6b9a2 (diff) | |
download | gdx-boardgame-828816db241467c296d3559226cd5fe5494e5761.zip gdx-boardgame-828816db241467c296d3559226cd5fe5494e5761.tar.gz |
ui : Assembly is dead, Element swallows touch() that now returns Element
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/Touchable.java | 4 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Assembly.java | 44 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Element.java | 17 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/List.java | 9 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Menu.java | 19 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Root.java | 29 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/ui/Scrollable.java | 11 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java | 2 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java | 4 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/UiScreen.java | 8 |
10 files changed, 72 insertions, 75 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 c61f810..0000000 --- a/core/src/ch/asynk/gdx/boardgame/ui/Assembly.java +++ /dev/null @@ -1,44 +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 -{ - private Element touched; - - public Assembly(int c) - { - this.children = new IterableSet<Element>(c); - } - - 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; - } -} diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Element.java b/core/src/ch/asynk/gdx/boardgame/ui/Element.java index db1735d..63670ac 100644 --- a/core/src/ch/asynk/gdx/boardgame/ui/Element.java +++ b/core/src/ch/asynk/gdx/boardgame/ui/Element.java @@ -119,10 +119,19 @@ public class Element implements Drawable, Paddable, Positionable, Touchable children.forEach( c -> c.drawDebug(shapeRenderer) ); } - @Override public boolean touch(float x, float y) - { - if (blocked || !visible) return false; - return rect.contains(x, y); + @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) 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) { diff --git a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java index 5357449..f609f2c 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java @@ -218,7 +218,7 @@ public class BoardScreen extends AbstractScreen { cam.unproject(x, y, boardTouch); cam.unprojectHud(x, y, hudTouch); - if (btn.touch(hudTouch.x, hudTouch.y)) { + if (btn.touch(hudTouch.x, hudTouch.y) != null) { setState(state.next()); } else { board.touch(boardTouch.x, boardTouch.y); diff --git a/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java index 23887f6..b0a312a 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java @@ -73,8 +73,10 @@ public class MenuScreen extends AbstractScreen { hudTouch.set(x, y, 0); camera.unproject(hudTouch); - if (root.touch(hudTouch.x, hudTouch.y)) { + if (root.touch(hudTouch.x, hudTouch.y) != root) { switch(menu.touched()) { + case -1: + break; case 0: app.switchToUi(); break; diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java index e0fcaf4..bdd8b88 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java @@ -134,10 +134,8 @@ public class UiScreen extends AbstractScreen { hudTouch.set(x, y, 0); camera.unproject(hudTouch); - if (root.touch(hudTouch.x, hudTouch.y)) { - if (root.touched() == next) - setState(state.next()); - } + if (root.touch(hudTouch.x, hudTouch.y) == next) + setState(state.next()); } @Override protected void onDragged(int dx, int dy) { @@ -213,7 +211,7 @@ class MyList extends Patch scrollable.setPosition(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight() - title.getHeight() - 15); } - @Override public boolean touch(float x, float y) + @Override public Element touch(float x, float y) { return scrollable.touch(x, y); } |