diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2015-07-19 13:20:33 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2015-07-19 13:20:33 +0200 |
commit | de0463bcf0f76ef8b07f2719679c9e0d72745c5d (patch) | |
tree | 9a33df947ceeea16a3e20b400585b1d3c304e77e /core/src/ch/asynk/rustanddust/ui | |
parent | e66f9f2a61d3dab4545e996046486de0d44e2901 (diff) | |
download | RustAndDust-de0463bcf0f76ef8b07f2719679c9e0d72745c5d.zip RustAndDust-de0463bcf0f76ef8b07f2719679c9e0d72745c5d.tar.gz |
welcome RustAndDust
Diffstat (limited to 'core/src/ch/asynk/rustanddust/ui')
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Bg.java | 28 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Label.java | 72 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/LabelImage.java | 72 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/LabelStack.java | 72 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Menu.java | 93 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Msg.java | 79 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/OkCancel.java | 115 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Patch.java | 28 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Position.java | 239 | ||||
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Widget.java | 93 |
10 files changed, 891 insertions, 0 deletions
diff --git a/core/src/ch/asynk/rustanddust/ui/Bg.java b/core/src/ch/asynk/rustanddust/ui/Bg.java new file mode 100644 index 0000000..cac3ddc --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Bg.java @@ -0,0 +1,28 @@ +package ch.asynk.rustanddust.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/rustanddust/ui/Label.java b/core/src/ch/asynk/rustanddust/ui/Label.java new file mode 100644 index 0000000..6a6d809 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Label.java @@ -0,0 +1,72 @@ +package ch.asynk.rustanddust.ui; + +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.GlyphLayout; + +public class Label extends Widget +{ + private BitmapFont font; + private GlyphLayout layout; + 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; + this.layout = new GlyphLayout(); + } + + @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) + { + this.layout.setText(font, (text == null) ? "" : text); + setPosition(x, y, (layout.width + (2 * padding)), (layout.height + (2 * padding))); + this.dx = (x + padding); + this.dy = (y + padding + layout.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.draw(batch, layout, dx, dy); + } +} diff --git a/core/src/ch/asynk/rustanddust/ui/LabelImage.java b/core/src/ch/asynk/rustanddust/ui/LabelImage.java new file mode 100644 index 0000000..0fb6ecb --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/LabelImage.java @@ -0,0 +1,72 @@ +package ch.asynk.rustanddust.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/rustanddust/ui/LabelStack.java b/core/src/ch/asynk/rustanddust/ui/LabelStack.java new file mode 100644 index 0000000..92933e3 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/LabelStack.java @@ -0,0 +1,72 @@ +package ch.asynk.rustanddust.ui; + +import java.util.ArrayDeque; + +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; + +import ch.asynk.rustanddust.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/rustanddust/ui/Menu.java b/core/src/ch/asynk/rustanddust/ui/Menu.java new file mode 100644 index 0000000..2fe93a7 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Menu.java @@ -0,0 +1,93 @@ +package ch.asynk.rustanddust.ui; + +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.NinePatch; + +public class Menu extends Patch +{ + public static int PADDING = 40; + public static int VSPACING = 8; + + protected Label []labels; + + public interface MenuItem + { + public int last(); + public int i(); + }; + + protected MenuItem menuItem; + + public Menu(MenuItem menuItem, BitmapFont font, NinePatch ninePatch) + { + super(ninePatch); + this.menuItem = menuItem; + this.labels = new Label[menuItem.last()]; + for (int i = 0; i< menuItem.last(); i ++) + labels[i] = new Label(font, 10); + } + + protected Label label(MenuItem m) + { + return labels[m.i()]; + } + + protected float widestLabel() + { + float w = 0f; + for (int i = 0; i< menuItem.last(); i ++) { + float t = labels[i].getWidth(); + if (t> w) + w = t; + } + return w; + } + + protected float highestLabel() + { + float h = 0f; + for (int i = 0; i< menuItem.last(); i ++) { + float t = labels[i].getHeight(); + if (t> h) + h = t; + } + return h; + } + + public void setPosition() + { + float lh = highestLabel(); + float h = ((menuItem.last() * lh) + (2 * PADDING) + ((menuItem.last() - 1) * VSPACING)); + float w = (widestLabel() + (2 * PADDING)); + float x = position.getX(w); + float y = position.getY(h); + setPosition(x, y, w, h); + + y += PADDING; + x += PADDING; + float dy = (VSPACING + lh); + + for (int i = 0; i< menuItem.last(); i ++) { + labels[i].setPosition(x, y); + y += dy; + } + } + + @Override + public void dispose() + { + super.dispose(); + for (int i = 0; i < menuItem.last(); i ++) + labels[i].dispose(); + } + + @Override + public void draw(Batch batch) + { + if (!visible) return; + super.draw(batch); + for (int i = 0; i < menuItem.last(); i ++) + labels[i].draw(batch); + } +} diff --git a/core/src/ch/asynk/rustanddust/ui/Msg.java b/core/src/ch/asynk/rustanddust/ui/Msg.java new file mode 100644 index 0000000..e1e7c13 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Msg.java @@ -0,0 +1,79 @@ +package ch.asynk.rustanddust.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/rustanddust/ui/OkCancel.java b/core/src/ch/asynk/rustanddust/ui/OkCancel.java new file mode 100644 index 0000000..f30a65b --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/OkCancel.java @@ -0,0 +1,115 @@ +package ch.asynk.rustanddust.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 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) + { + show(msg, Position.MIDDLE_CENTER); + } + + public void show(String msg, Position position) + { + 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((x + PADDING), okBtn.getY()); + label.setPosition((x + PADDING), (y + PADDING + okBtn.getHeight() + VSPACING)); + cancelBtn.visible = true; + visible = true; + ok = false; + } + + public void noCancel() + { + cancelBtn.visible = false; + } + + @Override + public boolean hit(float x, float y) + { + if (!cancelBtn.visible && super.hit(x, y)) { + ok = true; + return true; + } + 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/rustanddust/ui/Patch.java b/core/src/ch/asynk/rustanddust/ui/Patch.java new file mode 100644 index 0000000..acff791 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Patch.java @@ -0,0 +1,28 @@ +package ch.asynk.rustanddust.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/rustanddust/ui/Position.java b/core/src/ch/asynk/rustanddust/ui/Position.java new file mode 100644 index 0000000..d8c6096 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Position.java @@ -0,0 +1,239 @@ +package ch.asynk.rustanddust.ui; + +import com.badlogic.gdx.Gdx; +import ch.asynk.rustanddust.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 hudLeft = 0; + private static int hudBottom = 0; + private static int hudWidth = Gdx.graphics.getWidth(); + private static int hudHeight = Gdx.graphics.getHeight(); + + public static void update(int width, int height) + { + update(0, 0, width, height); + } + + public static void update(int left, int bottom, int width, int height) + { + hudLeft = left; + hudBottom = bottom; + hudWidth = width; + hudHeight = height; + } + + public float getX(float width) + { + float x = hudLeft; + 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 = hudBottom; + 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 = 0; + 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 = 0; + 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/rustanddust/ui/Widget.java b/core/src/ch/asynk/rustanddust/ui/Widget.java new file mode 100644 index 0000000..4ae8afd --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Widget.java @@ -0,0 +1,93 @@ +package ch.asynk.rustanddust.ui; + +import com.badlogic.gdx.utils.Disposable; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; + +import ch.asynk.rustanddust.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); + } +} |