diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2016-02-16 16:44:36 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-02-16 16:44:36 +0100 |
commit | ad7e41ca99b71bddeb1a711d532f32eb1344b383 (patch) | |
tree | 7982a05217769efe089b28471716680fd206f588 /core/src/ch/asynk/rustanddust | |
parent | c7228cde6e2efb0e53c92f5da2b2e2f1f4283548 (diff) | |
download | RustAndDust-ad7e41ca99b71bddeb1a711d532f32eb1344b383.zip RustAndDust-ad7e41ca99b71bddeb1a711d532f32eb1344b383.tar.gz |
add ui/List
Diffstat (limited to 'core/src/ch/asynk/rustanddust')
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/List.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/core/src/ch/asynk/rustanddust/ui/List.java b/core/src/ch/asynk/rustanddust/ui/List.java new file mode 100644 index 0000000..ad1d786 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/List.java @@ -0,0 +1,90 @@ +package ch.asynk.rustanddust.ui; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.GL20; +import com.badlogic.gdx.graphics.glutils.HdpiUtils; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.GlyphLayout; + +import ch.asynk.rustanddust.RustAndDust; +import ch.asynk.rustanddust.engine.util.Collection; + +public class List extends Widget +{ + public interface ListElement + { + public String s(); + } + + private BitmapFont font; + private GlyphLayout layout; + private float itemHeight; + private Integer idx; + private Bg selected; + private Collection<ListElement> items; + + public List(RustAndDust game) + { + this(game, 15f); + } + + public List(RustAndDust game, float padding) + { + super(); + this.font = game.font; + this.padding = padding; + this.layout = new GlyphLayout(); + this.idx = null; + this.selected = new Bg(game.getUiRegion(game.UI_SELECT)); + } + + @Override + public boolean hit(float x, float y) + { + float t = (getTop() - (int) padding); + idx = (int) Math.floor((t - y) / itemHeight); + if ((idx >= 0) && (idx < items.size())) + selected.setPosition(rect.x, (t - itemHeight - (idx * itemHeight)), rect.width, (itemHeight + padding)); + return true; + } + + public void setItems(int clipN, Collection<ListElement> items) + { + this.items = items; + compute(); + } + + private void compute() + { + float w = 0f; + for (ListElement e: items) { + layout.setText(font, e.s()); + if (layout.width > w) w = layout.width; + } + itemHeight = (layout.height + padding); + + rect.width = w + (2 * padding); + rect.height = padding + (itemHeight * items.size()); + } + + @Override + public void dispose() + { + } + + @Override + public void draw(Batch batch) + { + if (!visible) return; + + if (idx != null) + selected.draw(batch); + float x = rect.x + padding; + float y = rect.y + rect.height - padding; + for (ListElement e : items) { + font.draw(batch, e.s(), x, y); + y -= itemHeight; + } + } +} |