summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2019-12-18 10:02:33 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2019-12-18 10:02:33 +0100
commit941588bdd70d985398537e331df56b6b599efb8d (patch)
treec4114e951b2725d5ae261f52a9f81732f32e56b4 /core
parent6b46e61ecefb120b186247ac01c6e2afac6337bb (diff)
downloadgdx-boardgame-941588bdd70d985398537e331df56b6b599efb8d.zip
gdx-boardgame-941588bdd70d985398537e331df56b6b599efb8d.tar.gz
UI : add List
Diffstat (limited to 'core')
-rw-r--r--core/src/ch/asynk/gdx/boardgame/ui/List.java82
1 files changed, 82 insertions, 0 deletions
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..19f08f2
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/ui/List.java
@@ -0,0 +1,82 @@
+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;
+ 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;
+ }
+
+ @Override public void draw(Batch batch)
+ {
+ if (!visible) return;
+
+ float x = getInnerX();
+ float y = getInnerTop();
+ for (Item e : items) {
+ font.draw(batch, e.s(), x, y);
+ y -= itemHeight;
+ }
+ selected.draw(batch);
+ }
+}