diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2016-02-16 16:58:28 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-02-16 16:58:28 +0100 |
commit | ebc82f8e2bfc9f6ecbf8c6ae31f097c410b06534 (patch) | |
tree | a245803d0ed5ab40392c5780ee251a4b7bf1411a /core/src/ch | |
parent | cd84e7b8ae6efa66fb1796c6eda95e4c4fd863f4 (diff) | |
download | RustAndDust-ebc82f8e2bfc9f6ecbf8c6ae31f097c410b06534.zip RustAndDust-ebc82f8e2bfc9f6ecbf8c6ae31f097c410b06534.tar.gz |
add ui/Scrollable
Diffstat (limited to 'core/src/ch')
-rw-r--r-- | core/src/ch/asynk/rustanddust/ui/Scrollable.java | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/core/src/ch/asynk/rustanddust/ui/Scrollable.java b/core/src/ch/asynk/rustanddust/ui/Scrollable.java new file mode 100644 index 0000000..7548239 --- /dev/null +++ b/core/src/ch/asynk/rustanddust/ui/Scrollable.java @@ -0,0 +1,87 @@ +package ch.asynk.rustanddust.ui; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.math.Rectangle; +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.NinePatch; + +public class Scrollable extends Widget +{ + private float offset; + private Widget child; + private NinePatch patch; + private Rectangle clip; + + public Scrollable(Widget child) + { + this(child, null); + } + + public Scrollable(Widget child, NinePatch patch) + { + super(); + this.offset = 0f; + this.padding = 10f; + this.child = child; + this.patch = patch; + this.clip = new Rectangle(); + if (patch != null) + setPosition(0, 0, patch.getTotalWidth(), patch.getTotalHeight()); + } + + public Widget getChild() { return child; } + + @Override + public boolean hit(float x, float y) + { + if (!super.hit(x, y)) return false; + child.hit(x, y); + return true; + } + + public float getBestWidth() + { + return (child.getWidth() + (2 * padding)); + } + + public boolean drag(float x, float y, int dx, int dy) + { + child.translate(0, dy); + if (child.getY() > (getY() + padding)) + child.rect.y = (getY() + padding); + if (child.getTop() < (getTop() - padding)) + child.rect.y = (getTop() - padding - child.getHeight()); + return true; + } + + @Override + public void dispose() + { + child.dispose(); + } + + @Override + public void setPosition(float x, float y, float w, float h) + { + rect.set(x, y, w, h); + child.setPosition((x + padding), (getTop() - padding - child.getHeight())); + clip.set((getX() + padding), (getY() + padding), (getWidth() - (2 * padding)), (getHeight() - (2 * padding))); + } + + @Override + public void draw(Batch batch) + { + if (!visible) return; + + if (patch != null) + patch.draw(batch, rect.x, rect.y, rect.width, rect.height); + batch.flush(); + HdpiUtils.glScissor((int) clip.x, (int) clip.y, (int) clip.width, (int) clip.height); + Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST); + child.draw(batch); + batch.flush(); + Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST); + } +} |