diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2019-12-18 10:04:48 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2019-12-18 10:04:48 +0100 |
commit | dd535dc97129dbac3e0521826343e5002f43777b (patch) | |
tree | 35ace6a75a033d7ffacc585047090831713a93ab /test/src/ch/asynk/gdx/boardgame | |
parent | 941588bdd70d985398537e331df56b6b599efb8d (diff) | |
download | gdx-boardgame-dd535dc97129dbac3e0521826343e5002f43777b.zip gdx-boardgame-dd535dc97129dbac3e0521826343e5002f43777b.tar.gz |
test : add ui.List test
Diffstat (limited to 'test/src/ch/asynk/gdx/boardgame')
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/Assets.java | 3 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/UiScreen.java | 126 |
2 files changed, 113 insertions, 16 deletions
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..a0167dc 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,94 @@ 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(); + 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); + } +} |