summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/ui
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2014-12-27 17:15:28 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2014-12-27 17:15:28 +0100
commit8c4e1b6e2c1e013de9ee4ff079cc61dbf3f0591c (patch)
tree67b170fed9f5a540a266dcb680b4c7ee649eaae5 /core/src/ch/asynk/tankontank/ui
parent06462e18ec111a0662322ef221991dd0b547eded (diff)
downloadRustAndDust-8c4e1b6e2c1e013de9ee4ff079cc61dbf3f0591c.zip
RustAndDust-8c4e1b6e2c1e013de9ee4ff079cc61dbf3f0591c.tar.gz
move base ui widgets into core/src/ch/asynk/tankontank/ui
Diffstat (limited to 'core/src/ch/asynk/tankontank/ui')
-rw-r--r--core/src/ch/asynk/tankontank/ui/Bg.java28
-rw-r--r--core/src/ch/asynk/tankontank/ui/Label.java70
-rw-r--r--core/src/ch/asynk/tankontank/ui/LabelImage.java72
-rw-r--r--core/src/ch/asynk/tankontank/ui/LabelStack.java72
-rw-r--r--core/src/ch/asynk/tankontank/ui/Msg.java79
-rw-r--r--core/src/ch/asynk/tankontank/ui/OkCancel.java121
-rw-r--r--core/src/ch/asynk/tankontank/ui/Patch.java28
-rw-r--r--core/src/ch/asynk/tankontank/ui/Position.java230
-rw-r--r--core/src/ch/asynk/tankontank/ui/Widget.java93
9 files changed, 793 insertions, 0 deletions
diff --git a/core/src/ch/asynk/tankontank/ui/Bg.java b/core/src/ch/asynk/tankontank/ui/Bg.java
new file mode 100644
index 0000000..7a87c26
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/Bg.java
@@ -0,0 +1,28 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.TextureRegion;
+
+public class Bg extends Widget
+{
+ private TextureRegion region;
+
+ public Bg(TextureRegion region)
+ {
+ super();
+ this.region = region;
+ setPosition(0, 0, region.getRegionWidth(), region.getRegionHeight());
+ }
+
+ @Override
+ public void dispose()
+ {
+ }
+
+ @Override
+ public void draw(Batch batch)
+ {
+ if (!visible) return;
+ batch.draw(region, rect.x, rect.y, rect.width, rect.height);
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/Label.java b/core/src/ch/asynk/tankontank/ui/Label.java
new file mode 100644
index 0000000..eae4b15
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/Label.java
@@ -0,0 +1,70 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
+
+public class Label extends Widget
+{
+ private BitmapFont font;
+ private float dx;
+ private float dy;
+ protected String text;
+
+ public Label(BitmapFont font)
+ {
+ this(font, 0f);
+ }
+
+ public Label(BitmapFont font, float padding)
+ {
+ this(font, padding, Position.MIDDLE_CENTER);
+ }
+
+ public Label(BitmapFont font, float padding, Position position)
+ {
+ super();
+ this.font = font;
+ this.padding = padding;
+ this.position = position;
+ }
+
+ @Override
+ public void dispose()
+ {
+ }
+
+ @Override
+ public void translate(float dx, float dy)
+ {
+ setPosition((rect.x + dx), (rect.y + dy));
+ }
+
+ @Override
+ public void setPosition(float x, float y)
+ {
+ TextBounds b = font.getMultiLineBounds((text == null) ? "" : text);
+ setPosition(x, y, (b.width + (2 * padding)), (b.height + (2 * padding)));
+ this.dx = (x + padding);
+ this.dy = (y + padding + b.height);
+ }
+
+ public void write(String text)
+ {
+ this.text = text;
+ setPosition(position);
+ }
+
+ public void write(String text, float x, float y)
+ {
+ this.text = text;
+ setPosition(x, y);
+ }
+
+ @Override
+ public void draw(Batch batch)
+ {
+ if (!visible) return;
+ font.drawMultiLine(batch, text, dx, dy);
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/LabelImage.java b/core/src/ch/asynk/tankontank/ui/LabelImage.java
new file mode 100644
index 0000000..4f28924
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/LabelImage.java
@@ -0,0 +1,72 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+import com.badlogic.gdx.graphics.g2d.TextureRegion;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+
+public class LabelImage extends Bg
+{
+ private Label label;
+
+ public LabelImage(TextureRegion region, BitmapFont font)
+ {
+ this(region, font, 0f);
+ }
+
+ public LabelImage(TextureRegion region, BitmapFont font, float padding)
+ {
+ this(region, font, padding, Position.MIDDLE_CENTER);
+ }
+
+ public LabelImage(TextureRegion region, BitmapFont font, float padding, Position position)
+ {
+ super(region);
+ this.label = new Label(font, padding, position);
+ }
+
+ @Override
+ public void dispose()
+ {
+ label.dispose();
+ }
+
+ @Override
+ public void translate(float dx, float dy)
+ {
+ super.translate(dx, dy);
+ label.translate(dx, dy);
+ }
+
+ public void setPosition(float x, float y)
+ {
+ super.setPosition(x, y);
+ label.setPosition(x, y);
+ }
+
+ public void setLabelPosition(Position position)
+ {
+ label.setPosition(position, this);
+ }
+
+ public void write(String text)
+ {
+ this.label.write(text);
+ }
+
+ @Override
+ public void draw(Batch batch)
+ {
+ if (!visible) return;
+ super.draw(batch);
+ label.draw(batch);
+ }
+
+ @Override
+ public void drawDebug(ShapeRenderer shapes)
+ {
+ if (!visible) return;
+ super.drawDebug(shapes);
+ label.drawDebug(shapes);
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/LabelStack.java b/core/src/ch/asynk/tankontank/ui/LabelStack.java
new file mode 100644
index 0000000..f730d0d
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/LabelStack.java
@@ -0,0 +1,72 @@
+package ch.asynk.tankontank.ui;
+
+import java.util.ArrayDeque;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+
+import ch.asynk.tankontank.engine.gfx.Animation;
+
+public class LabelStack extends Label implements Animation
+{
+ class MsgInfo
+ {
+ String text;
+ float duration;
+ Position position;
+ MsgInfo(String text, float duration, Position position)
+ {
+ this.text = text;
+ this.duration = duration;
+ this.position = position;
+ }
+ }
+
+ private float duration;
+ private float elapsed;
+ private ArrayDeque<MsgInfo> stack;
+
+ public LabelStack(BitmapFont font, float padding)
+ {
+ super(font, padding);
+ this.visible = false;
+ this.stack = new ArrayDeque<MsgInfo>();
+ }
+
+ public void pushWrite(String text, float duration, Position position)
+ {
+ if (visible)
+ stack.push(new MsgInfo(text, duration, position));
+ else
+ write(text, duration, position);
+ }
+
+ public void write(String text, float duration, Position position)
+ {
+ this.position = position;
+ write(text, duration);
+ }
+
+ public void write(String text, float duration)
+ {
+ this.duration = duration;
+ this.visible = true;
+ this.elapsed = 0f;
+ write(text);
+ }
+
+ @Override
+ public boolean animate(float delta)
+ {
+ if (!visible) return true;
+ elapsed += delta;
+ if (elapsed >= duration) {
+ visible = false;
+ if (stack.size() > 0) {
+ MsgInfo info = stack.pop();
+ write(info.text, info.duration, info.position);
+ }
+ }
+ return false;
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/Msg.java b/core/src/ch/asynk/tankontank/ui/Msg.java
new file mode 100644
index 0000000..c2069b7
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/Msg.java
@@ -0,0 +1,79 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+
+public class Msg extends Patch
+{
+ private LabelStack label;
+
+ public Msg(BitmapFont font, TextureAtlas atlas)
+ {
+ super(atlas.createPatch("typewriter"));
+ label = new LabelStack(font, 20f);
+ }
+
+ @Override
+ public void dispose()
+ {
+ super.dispose();
+ label.dispose();
+ }
+
+ public void updatePosition()
+ {
+ if (!visible) return;
+ float dx = (position.getX(rect.width) - rect.x);
+ float dy = (position.getY(rect.height) - rect.y);
+ translate(dx, dy);
+ label.translate(dx, dy);
+ }
+
+ public void write(String text, float duration)
+ {
+ label.write(text, duration);
+ resize();
+ }
+
+ public void write(String text, float duration, Position position)
+ {
+ this.position = position;
+ label.write(text, duration, position);
+ resize();
+ }
+
+ public void pushWrite(String text, float duration, Position position)
+ {
+ this.position = position;
+ label.pushWrite(text, duration, position);
+ resize();
+ }
+
+ private void resize()
+ {
+ setPosition(label.getX(), label.getY(), label.getWidth(), label.getHeight());
+ }
+
+ public boolean animate(float delta)
+ {
+ return label.animate(delta);
+ }
+
+ @Override
+ public void draw(Batch batch)
+ {
+ if (!label.visible) return;
+ super.draw(batch);
+ label.draw(batch);
+ }
+
+ @Override
+ public void drawDebug(ShapeRenderer shapes)
+ {
+ if (!label.visible) return;
+ super.drawDebug(shapes);
+ label.drawDebug(shapes);
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/OkCancel.java b/core/src/ch/asynk/tankontank/ui/OkCancel.java
new file mode 100644
index 0000000..4a8507f
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/OkCancel.java
@@ -0,0 +1,121 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.BitmapFont;
+import com.badlogic.gdx.graphics.g2d.TextureAtlas;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+
+public class OkCancel extends Patch
+{
+ public static int PADDING = 20;
+ public static int VSPACING = 10;
+ public static int HSPACING = 10;
+
+ public boolean ok;
+ protected Label label;
+ protected Bg okBtn;
+ protected Bg cancelBtn;
+ public Action action;
+
+ public enum Action
+ {
+ EXIT_BOARD,
+ ABORT_TURN,
+ END_TURN,
+ END_DEPLOYMENT,
+ }
+
+ public OkCancel(BitmapFont font, TextureAtlas atlas)
+ {
+ super(atlas.createPatch("typewriter"));
+ this.label = new Label(font);
+ this.okBtn = new Bg(atlas.findRegion("ok"));
+ this.cancelBtn = new Bg(atlas.findRegion("cancel"));
+ this.visible = false;
+ }
+
+ public void updatePosition()
+ {
+ if (!visible) return;
+ float dx = (position.getX(rect.width) - rect.x);
+ float dy = (position.getY(rect.height) - rect.y);
+ translate(dx, dy);
+ label.translate(dx, dy);
+ okBtn.translate(dx, dy);
+ cancelBtn.translate(dx, dy);
+ }
+
+ public void show(String msg, Action action)
+ {
+ show(msg, action, Position.MIDDLE_CENTER);
+ }
+
+ public void show(String msg, Action action, Position position)
+ {
+ this.action = action;
+
+ label.write(msg);
+
+ float height = (label.getHeight() + okBtn.getHeight() + (2 * PADDING) + (2 * VSPACING));
+ float width = (label.getWidth() + (2 * PADDING));
+ float w2 = (okBtn.getWidth() + cancelBtn.getWidth() + (2 * PADDING) + (1 * HSPACING));
+ if (w2 > width)
+ width = w2;
+ float x = position.getX(width);
+ float y = position.getY(height);
+ setPosition(x, y, width, height);
+
+ okBtn.setPosition((x + width - okBtn.getWidth() - PADDING), (y + PADDING));
+ cancelBtn.setPosition((okBtn.getX() - cancelBtn.getWidth() - HSPACING), okBtn.getY());
+ label.setPosition((x + PADDING), (y + PADDING + okBtn.getHeight() + VSPACING));
+ cancelBtn.visible = true;
+ visible = true;
+ ok = false;
+ }
+
+ public void noCancel()
+ {
+ cancelBtn.visible = false;
+ }
+
+ public boolean hit(float x, float y)
+ {
+ if (okBtn.hit(x, y)) {
+ ok = true;
+ return true;
+ } else if (cancelBtn.hit(x, y)) {
+ ok = false;
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public void dispose()
+ {
+ super.dispose();
+ label.dispose();
+ okBtn.dispose();
+ cancelBtn.dispose();
+ }
+
+ @Override
+ public void draw(Batch batch)
+ {
+ if (!visible) return;
+ super.draw(batch);
+ label.draw(batch);
+ okBtn.draw(batch);
+ cancelBtn.draw(batch);
+ }
+
+ @Override
+ public void drawDebug(ShapeRenderer shapes)
+ {
+ if (!visible) return;
+ super.drawDebug(shapes);
+ label.drawDebug(shapes);
+ okBtn.drawDebug(shapes);
+ cancelBtn.drawDebug(shapes);
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/Patch.java b/core/src/ch/asynk/tankontank/ui/Patch.java
new file mode 100644
index 0000000..4056a3c
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/Patch.java
@@ -0,0 +1,28 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.graphics.g2d.NinePatch;
+
+public class Patch extends Widget
+{
+ private NinePatch patch;
+
+ public Patch(NinePatch patch)
+ {
+ super();
+ this.patch = patch;
+ setPosition(0, 0, patch.getTotalWidth(), patch.getTotalHeight());
+ }
+
+ @Override
+ public void dispose()
+ {
+ }
+
+ @Override
+ public void draw(Batch batch)
+ {
+ if (!visible) return;
+ patch.draw(batch, rect.x, rect.y, rect.width, rect.height);
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/Position.java b/core/src/ch/asynk/tankontank/ui/Position.java
new file mode 100644
index 0000000..f1708c6
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/Position.java
@@ -0,0 +1,230 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.Gdx;
+import ch.asynk.tankontank.game.Hud;
+
+public enum Position
+{
+ TOP_LEFT,
+ TOP_RIGHT,
+ TOP_CENTER,
+ MIDDLE_LEFT,
+ MIDDLE_RIGHT,
+ MIDDLE_CENTER,
+ BOTTOM_LEFT,
+ BOTTOM_RIGHT,
+ BOTTOM_CENTER;
+
+ public Position verticalMirror()
+ {
+ Position p = this;
+ switch(this) {
+ case TOP_LEFT:
+ p = TOP_RIGHT;
+ break;
+ case MIDDLE_LEFT:
+ p = MIDDLE_RIGHT;
+ break;
+ case BOTTOM_LEFT:
+ p = BOTTOM_RIGHT;
+ break;
+ case TOP_RIGHT:
+ p = TOP_LEFT;
+ break;
+ case MIDDLE_RIGHT:
+ p = MIDDLE_LEFT;
+ break;
+ case BOTTOM_RIGHT:
+ p = BOTTOM_LEFT;
+ break;
+ }
+ return p;
+ }
+
+ public Position horizontalMirror()
+ {
+ Position p = this;
+ switch(this) {
+ case TOP_LEFT:
+ p = BOTTOM_LEFT;
+ break;
+ case TOP_CENTER:
+ p = BOTTOM_CENTER;
+ break;
+ case TOP_RIGHT:
+ p = BOTTOM_RIGHT;
+ break;
+ case BOTTOM_LEFT:
+ p = TOP_LEFT;
+ break;
+ case BOTTOM_CENTER:
+ p = TOP_CENTER;
+ break;
+ case BOTTOM_RIGHT:
+ p = TOP_RIGHT;
+ break;
+ }
+ return p;
+ }
+
+ public boolean isLeft()
+ {
+ boolean r = false;
+ switch(this) {
+ case TOP_LEFT:
+ case MIDDLE_LEFT:
+ case BOTTOM_LEFT:
+ r = true;
+ break;
+ default:
+ r = false;
+ break;
+ }
+ return r;
+ }
+
+ public boolean isRight()
+ {
+ boolean r = false;
+ switch(this) {
+ case TOP_RIGHT:
+ case MIDDLE_RIGHT:
+ case BOTTOM_RIGHT:
+ r = true;
+ break;
+ default:
+ r = false;
+ break;
+ }
+ return r;
+ }
+
+ public boolean isCenter()
+ {
+ boolean r = false;
+ switch(this) {
+ case TOP_CENTER:
+ case MIDDLE_CENTER:
+ case BOTTOM_CENTER:
+ r = true;
+ break;
+ default:
+ r = false;
+ break;
+ }
+ return r;
+ }
+
+ private static int hudWidth = Gdx.graphics.getWidth();
+ private static int hudHeight = Gdx.graphics.getHeight();
+
+ public static void update(int width, int height)
+ {
+ hudWidth = width;
+ hudHeight = height;
+ }
+
+ public float getX(float width)
+ {
+ float x;
+ switch(this) {
+ case TOP_LEFT:
+ case MIDDLE_LEFT:
+ case BOTTOM_LEFT:
+ x = Hud.OFFSET;
+ break;
+ case TOP_CENTER:
+ case MIDDLE_CENTER:
+ case BOTTOM_CENTER:
+ x = ((hudWidth - width) / 2);
+ break;
+ case TOP_RIGHT:
+ case MIDDLE_RIGHT:
+ case BOTTOM_RIGHT:
+ x = (hudWidth - width - Hud.OFFSET);
+ break;
+ default:
+ x = ((hudWidth - width) / 2);
+ break;
+ }
+ return x;
+ }
+
+ public float getY(float height)
+ {
+ float y;
+ switch(this) {
+ case TOP_LEFT:
+ case TOP_CENTER:
+ case TOP_RIGHT:
+ y = (hudHeight - height - Hud.OFFSET);
+ break;
+ case MIDDLE_LEFT:
+ case MIDDLE_CENTER:
+ case MIDDLE_RIGHT:
+ y = ((hudHeight - height) / 2);
+ break;
+ case BOTTOM_LEFT:
+ case BOTTOM_CENTER:
+ case BOTTOM_RIGHT:
+ y = Hud.OFFSET;
+ break;
+ default:
+ y = ((hudHeight - height) / 2);
+ break;
+ }
+ return y;
+ }
+
+ public float getX(Widget widget, float width)
+ {
+ float x;
+ switch(this) {
+ case TOP_LEFT:
+ case MIDDLE_LEFT:
+ case BOTTOM_LEFT:
+ x = widget.getX();
+ break;
+ case TOP_CENTER:
+ case MIDDLE_CENTER:
+ case BOTTOM_CENTER:
+ x = (widget.getX() + ((widget.getWidth() - width) / 2));
+ break;
+ case TOP_RIGHT:
+ case MIDDLE_RIGHT:
+ case BOTTOM_RIGHT:
+ x = (widget.getX() + widget.getWidth() - width);
+ break;
+ default:
+ x = (widget.getX() + ((widget.getWidth() - width) / 2));
+ break;
+ }
+ return x;
+ }
+
+ public float getY(Widget widget, float height)
+ {
+ float y;
+ switch(this) {
+ case TOP_LEFT:
+ case TOP_CENTER:
+ case TOP_RIGHT:
+ y = (widget.getY() + widget.getHeight() - height);
+ break;
+ case MIDDLE_LEFT:
+ case MIDDLE_CENTER:
+ case MIDDLE_RIGHT:
+ y = (widget.getY() + ((widget.getHeight() - height) / 2));
+ break;
+ case BOTTOM_LEFT:
+ case BOTTOM_CENTER:
+ case BOTTOM_RIGHT:
+ y = widget.getY();
+ break;
+ default:
+ y = (widget.getY() + ((widget.getHeight() - height) / 2));
+ break;
+ }
+ return y;
+ }
+}
diff --git a/core/src/ch/asynk/tankontank/ui/Widget.java b/core/src/ch/asynk/tankontank/ui/Widget.java
new file mode 100644
index 0000000..e49833a
--- /dev/null
+++ b/core/src/ch/asynk/tankontank/ui/Widget.java
@@ -0,0 +1,93 @@
+package ch.asynk.tankontank.ui;
+
+import com.badlogic.gdx.utils.Disposable;
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
+
+import ch.asynk.tankontank.engine.gfx.Drawable;
+
+public abstract class Widget implements Disposable, Drawable
+{
+ public boolean blocked;
+ public boolean visible;
+ protected float padding;
+ protected Rectangle rect;
+ protected Position position;
+ protected Widget parent;
+
+ protected Widget()
+ {
+ this.parent = null;
+ this.blocked = false;
+ this.visible = true;
+ this.padding = 0f;
+ this.rect = new Rectangle(0, 0, 0, 0);
+ this.position = Position.MIDDLE_CENTER;
+ }
+
+ public float getX() { return rect.x; }
+ public float getY() { return rect.y; }
+ public float getWidth() { return rect.width; }
+ public float getHeight() { return rect.height; }
+
+ public void translate(float dx, float dy)
+ {
+ rect.x += dx;
+ rect.y += dy;
+ }
+
+ public void setPosition(Rectangle r)
+ {
+ rect.set(r);
+ }
+
+ public void setPosition(float x, float y)
+ {
+ rect.x = x;
+ rect.y = y;
+ }
+
+ public void setPosition(float x, float y, float w, float h)
+ {
+ rect.set(x, y, w, h);
+ }
+
+ public void setPosition(Position position)
+ {
+ this.position = position;
+ setParent(this.parent);
+ }
+
+ public void setPosition(Position position, Widget parent)
+ {
+ this.position = position;
+ setParent(parent);
+ }
+
+ public void setParent(Widget parent)
+ {
+ this.parent = parent;
+ if (parent == null) {
+ rect.x = position.getX(rect.width);
+ rect.y = position.getY(rect.height);
+ } else {
+ rect.x = position.getX(parent, rect.width);
+ rect.y = position.getY(parent, rect.height);
+ }
+ // might trigger something if overriden
+ setPosition(rect.x, rect.y);
+ }
+
+ public boolean hit(float x, float y)
+ {
+ if (blocked || !visible) return false;
+ return rect.contains(x, y);
+ }
+
+ @Override
+ public void drawDebug(ShapeRenderer shapes)
+ {
+ if (!visible) return;
+ shapes.rect(rect.x, rect.y, rect.width, rect.height);
+ }
+}