diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-12-17 16:05:23 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-12-17 16:05:23 +0100 |
commit | b0fa4f8f26545d959762a6b41f35ed10c17cf5dc (patch) | |
tree | d908868ed3a7141424e1b50daa4dffe5fba1b18e /core/src/ch/asynk | |
parent | 9139f6aed1896130ec8b921091a5ff1eddf16a25 (diff) | |
download | RustAndDust-b0fa4f8f26545d959762a6b41f35ed10c17cf5dc.zip RustAndDust-b0fa4f8f26545d959762a6b41f35ed10c17cf5dc.tar.gz |
Widget: add padding, translate(), add parent and manage relative position
Diffstat (limited to 'core/src/ch/asynk')
-rw-r--r-- | core/src/ch/asynk/tankontank/game/hud/Widget.java | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/core/src/ch/asynk/tankontank/game/hud/Widget.java b/core/src/ch/asynk/tankontank/game/hud/Widget.java index d277cd0..08d90f5 100644 --- a/core/src/ch/asynk/tankontank/game/hud/Widget.java +++ b/core/src/ch/asynk/tankontank/game/hud/Widget.java @@ -10,13 +10,17 @@ 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; } @@ -26,15 +30,22 @@ public abstract class Widget implements Disposable, Drawable 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(Rectangle base) - { - rect.set(base); - } public void setPosition(float x, float y, float w, float h) { @@ -44,8 +55,27 @@ public abstract class Widget implements Disposable, Drawable public void setPosition(Position position) { this.position = position; - rect.x = position.getX(rect.width); - rect.y = position.getY(rect.height); + 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) |