summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--assets/data/ui-selected.pngbin0 -> 904 bytes
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/Bg.java22
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/List.java89
-rw-r--r--test/src/ch/asynk/gdx/boardgame/test/Assets.java3
-rw-r--r--test/src/ch/asynk/gdx/boardgame/test/UiScreen.java127
5 files changed, 225 insertions, 16 deletions
diff --git a/assets/data/ui-selected.png b/assets/data/ui-selected.png
new file mode 100644
index 0000000..57cfd1d
--- /dev/null
+++ b/assets/data/ui-selected.png
Binary files differ
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/Bg.java b/core/src/ch/asynk/gdx/boardgame/ui/Bg.java
new file mode 100644
index 0000000..18cfa7c
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/ui/Bg.java
@@ -0,0 +1,22 @@
+package ch.asynk.gdx.boardgame.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.TextureRegion;
+
+public class Bg extends Element
+{
+ private TextureRegion region;
+
+ public Bg(TextureRegion region)
+ {
+ this.region = region;
+ rect.set(0, 0, region.getRegionWidth(), region.getRegionHeight());
+ }
+
+ @Override public void draw(Batch batch)
+ {
+ if (!visible) return;
+ if (tainted) computeGeometry();
+ batch.draw(region, rect.x, rect.y, rect.width, rect.height);
+ }
+}
diff --git a/core/src/ch/asynk/gdx/boardgame/ui/List.java b/core/src/ch/asynk/gdx/boardgame/ui/List.java
new file mode 100644
index 0000000..e9f874a
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/ui/List.java
@@ -0,0 +1,89 @@
+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.graphics.g2d.TextureRegion;
+
+import ch.asynk.gdx.boardgame.utils.Collection;
+
+public class List extends Element
+{
+ public interface Item
+ {
+ public String s();
+ }
+
+ private BitmapFont font;
+ private GlyphLayout layout;
+ private float spacing;
+ private float itemHeight;
+ private Integer idx;
+ private Bg selected;
+ private Collection<Item> items;
+
+ public List(BitmapFont font, TextureRegion selected, float padding, float spacing)
+ {
+ this.font = font;
+ this.padding = padding;
+ this.spacing = spacing;
+ this.layout = new GlyphLayout();
+ this.idx = null;
+ this.selected = new Bg(selected);
+ this.selected.visible = false;
+ }
+
+ public void unselect() { idx = null; }
+ public Integer getIdx() { return idx; }
+ public Item getSelected() { return ((idx == null) ? null : items.get(idx)); }
+
+ @Override public boolean touch(float x, float y)
+ {
+ if (super.touch(x, y)) {
+ 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;
+ }
+ }
+ idx = null;
+ selected.visible = false;
+ return false;
+ }
+
+ public void setItems(Collection<Item> items)
+ {
+ unselect();
+ this.items = items;
+ taint();
+ }
+
+ @Override public void computeGeometry()
+ {
+ float w = 0f;
+ for (Item e: items) {
+ layout.setText(font, e.s());
+ if (layout.width > w) w = layout.width;
+ }
+ itemHeight = (layout.height + spacing);
+
+ rect.width = w + (2 * padding);
+ rect.height = (itemHeight * items.size()) + (2 * padding) - spacing;
+ super.computeGeometry();
+ }
+
+ @Override public void draw(Batch batch)
+ {
+ if (!visible) return;
+
+ if (tainted) computeGeometry();
+ float x = getInnerX();
+ float y = getInnerTop();
+ for (Item e : items) {
+ font.draw(batch, e.s(), x, y);
+ y -= itemHeight;
+ }
+ selected.draw(batch);
+ }
+}
diff --git a/test/src/ch/asynk/gdx/boardgame/test/Assets.java b/test/src/ch/asynk/gdx/boardgame/test/Assets.java
index 2bdad29..b3a56b9 100644
--- a/test/src/ch/asynk/gdx/boardgame/test/Assets.java
+++ b/test/src/ch/asynk/gdx/boardgame/test/Assets.java
@@ -23,6 +23,7 @@ public class Assets extends ch.asynk.gdx.boardgame.Assets
public static final String FONT_20 = "size20.ttf";
public static final String FONT_25 = "size25.ttf";
public static final String PATCH = "ui-patch.png";
+ public static final String SELECTED = "ui-selected.png";
public static final String PANZER = "panzer.png";
public static final String ENGINEER = "engineer.png";
public static final String HEX_OVERLAYS = "hex-overlays.atlas";
@@ -78,6 +79,7 @@ public class Assets extends ch.asynk.gdx.boardgame.Assets
load(TRI, Texture.class);
load(CORNER, Texture.class);
load(PATCH, Texture.class);
+ load(SELECTED, Texture.class);
load(PANZER, Texture.class);
load(ENGINEER, Texture.class);
load(FONT_20, BitmapFont.class, params20);
@@ -102,6 +104,7 @@ public class Assets extends ch.asynk.gdx.boardgame.Assets
unload(TRI);
unload(CORNER);
unload(PATCH);
+ unload(SELECTED);
unload(PANZER);
unload(ENGINEER);
unload(FONT_20);
diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
index 14d282c..ec06a3f 100644
--- a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
+++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java
@@ -1,40 +1,36 @@
package ch.asynk.gdx.boardgame.test;
+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.g2d.SpriteBatch;
+import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.OrthographicCamera;
import ch.asynk.gdx.boardgame.ui.Alignment;
import ch.asynk.gdx.boardgame.ui.Button;
-
-class MyButton extends Button
-{
- public MyButton(BitmapFont font, NinePatch patch, float padding, float spacing)
- {
- super(font, patch, padding, spacing);
- }
-
- @Override public void computeGeometry()
- {
- super.computeGeometry();
- label.write(String.format("%04d;%04d", (int)getX(), (int)getY()));
- }
-}
+import ch.asynk.gdx.boardgame.ui.Label;
+import ch.asynk.gdx.boardgame.ui.List;
+import ch.asynk.gdx.boardgame.ui.Patch;
+import ch.asynk.gdx.boardgame.utils.Collection;
+import ch.asynk.gdx.boardgame.utils.IterableArray;
public class UiScreen extends AbstractScreen
{
private final Button next;
private final Button[] buttons = new Button[8];
+ private final MyList list;
public enum State
{
- POSITIONS, DONE;
+ POSITIONS, SCROLL, DONE;
public State next()
{
switch(this) {
case POSITIONS:
+ return SCROLL;
+ case SCROLL:
return DONE;
default:
return POSITIONS;
@@ -60,6 +56,9 @@ public class UiScreen extends AbstractScreen
this.buttons[6] = buildButton(font, patch, 10, 15, Alignment.BOTTOM_LEFT, Alignment.TOP_RIGHT);
this.buttons[7] = buildButton(font, patch, 10, 15, Alignment.BOTTOM_CENTER, Alignment.TOP_CENTER);
+ this.list = new MyList(font, patch, app.assets.getTextureRegion(app.assets.SELECTED));
+ this.list.setAlignment(Alignment.MIDDLE_CENTER);
+
this.next = new Button(font, patch, 10, 15);
this.next.write("Next");
this.next.setAlignment(Alignment.BOTTOM_RIGHT);
@@ -86,8 +85,12 @@ public class UiScreen extends AbstractScreen
case POSITIONS:
setButtons(true);
break;
- case DONE:
+ case SCROLL:
setButtons(false);
+ root.add(list);
+ break;
+ case DONE:
+ root.remove(list);
app.switchToMenu();
break;
}
@@ -131,3 +134,95 @@ public class UiScreen extends AbstractScreen
}
}
}
+
+class MyButton extends Button
+{
+ public MyButton(BitmapFont font, NinePatch patch, float padding, float spacing)
+ {
+ super(font, patch, padding, spacing);
+ }
+
+ @Override public void computeGeometry()
+ {
+ super.computeGeometry();
+ label.write(String.format("%04d;%04d", (int)getX(), (int)getY()));
+ }
+}
+
+class MyList extends Patch
+{
+ private Label title;
+ private List list;
+
+ class Item implements List.Item
+ {
+ private String s;
+ public Item(String s)
+ {
+ this.s = s;
+ }
+ public String s() { return s; }
+ }
+
+ public MyList(BitmapFont font, NinePatch patch, TextureRegion textureRegion)
+ {
+ super(patch);
+ this.padding = 10;
+ this.title = new Label(font, 10);
+ this.title.write("My List Title");
+ this.title.setAlignment(alignment.TOP_CENTER);
+ this.title.setParent(this);
+
+ Collection<List.Item> items = new IterableArray<List.Item>(15);
+ for (int i = 0; i < 15; i++) {
+ items.add(new Item(String.format("%02d : is just another list item", i)));
+ }
+ this.list = new List(font, textureRegion, 10, 15);
+ this.list.setItems(items);
+ this.list.setParent(this);
+ }
+
+ @Override public void computeGeometry()
+ {
+ float pp = (2 * padding);
+
+ // update dimensions
+ title.computeGeometry();
+ list.computeGeometry();
+ rect.width = title.getWidth() + pp;
+ rect.height = title.getHeight() + pp + list.getHeight() + 15;
+ if ((list.getWidth() + pp) > rect.width) rect.width = list.getWidth() + pp;
+
+ // update position
+ super.computeGeometry();
+ title.computeGeometry();
+ list.computeGeometry();
+ }
+
+ @Override public boolean touch(float x, float y)
+ {
+ if (super.touch(x, y)) {
+ if (list.touch(x, y)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override public void draw(Batch batch)
+ {
+ if (!visible) return;
+ if (tainted) computeGeometry();
+ super.draw(batch);
+ title.draw(batch);
+ list.draw(batch);
+ }
+
+ @Override public void drawDebug(ShapeRenderer shapeRenderer)
+ {
+ if (!visible) return;
+ super.drawDebug(shapeRenderer);
+ title.drawDebug(shapeRenderer);
+ list.drawDebug(shapeRenderer);
+ }
+}