diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-03-19 20:43:23 +0200 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-03-19 20:46:18 +0200 |
commit | 756caa3d5941f44fac9fe15676501e16275cdd3b (patch) | |
tree | 6cf565f723ced30f060d954d112c8d032ed9c8f8 | |
parent | d335a633ccc9865d14f2d76a765ad180d5594d56 (diff) | |
download | LibGui-756caa3d5941f44fac9fe15676501e16275cdd3b.tar.gz LibGui-756caa3d5941f44fac9fe15676501e16275cdd3b.tar.bz2 LibGui-756caa3d5941f44fac9fe15676501e16275cdd3b.zip |
Add UV support for WSprite, document WLabel constructors
Closes #35.
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java | 25 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java | 51 |
2 files changed, 70 insertions, 6 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index ce4b921..d925c17 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -26,20 +26,43 @@ public class WLabel extends WWidget { public static final int DEFAULT_TEXT_COLOR = 0x404040; public static final int DEFAULT_DARKMODE_TEXT_COLOR = 0xbcbcbc; + /** + * Constructs a new label. + * + * @param text the text of the label + * @param color the color of the label + */ public WLabel(String text, int color) { this(new LiteralText(text), color); } - + + /** + * Constructs a new label. + * + * @param text the text of the label + * @param color the color of the label + */ public WLabel(Text text, int color) { this.text = text; this.color = color; this.darkmodeColor = (color==DEFAULT_TEXT_COLOR) ? DEFAULT_DARKMODE_TEXT_COLOR : color; } + /** + * Constructs a new label with the {@linkplain #DEFAULT_TEXT_COLOR default text color}. + * + * @param text the text of the label + */ public WLabel(String text) { this(text, DEFAULT_TEXT_COLOR); } + /** + * Constructs a new label with the {@linkplain #DEFAULT_TEXT_COLOR default text color}. + * + * @param text the text of the label + * @since 1.8.0 + */ public WLabel(Text text) { this(text, DEFAULT_TEXT_COLOR); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java index 44f8998..364761d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java @@ -13,6 +13,10 @@ public class WSprite extends WWidget { protected long lastFrame; protected boolean singleImage = false; protected int tint = 0xFFFFFFFF; + protected float u1 = 0; + protected float v1 = 0; + protected float u2 = 1; + protected float v2 = 1; /** * Create a new sprite with a single image. @@ -24,6 +28,23 @@ public class WSprite extends WWidget { } /** + * Create a new sprite with a single image and custom UV values. + * + * @param image The location of the image to display. + * @param u1 the left edge of the texture + * @param v1 the top edge of the texture + * @param u2 the right edge of the texture + * @param v2 the bottom edge of the texture + */ + public WSprite(Identifier image, float u1, float v1, float u2, float v2) { + this(image); + this.u1 = u1; + this.v1 = v1; + this.u2 = u2; + this.v2 = v2; + } + + /** * Create a new animated sprite. * @param frameTime How long in milliseconds to display for. (1 tick = 50 ms) * @param frames The locations of the frames of the animation. @@ -33,7 +54,7 @@ public class WSprite extends WWidget { this.frames = frames; if (frames.length==1) this.singleImage = true; } - + public WSprite setImage(Identifier image) { this.frames = new Identifier[]{image}; this.singleImage = true; @@ -41,7 +62,7 @@ public class WSprite extends WWidget { this.currentFrameTime = 0; return this; } - + public WSprite setFrames(Identifier... frames) { this.frames = frames; if (frames.length==1) singleImage = true; @@ -51,7 +72,7 @@ public class WSprite extends WWidget { } return this; } - + /** * Sets the tint for this sprite to the following color-with-alpha. If you don't want to specify * alpha, use {@link #setOpaqueTint(int)} instead. @@ -60,12 +81,32 @@ public class WSprite extends WWidget { this.tint = tint; return this; } - + public WSprite setOpaqueTint(int tint) { this.tint = tint | 0xFF000000; return this; } + /** + * Sets the UV values of this sprite. + * + * @param u1 the left edge of the texture + * @param v1 the top edge of the texture + * @param u2 the right edge of the texture + * @param v2 the bottom edge of the texture + * + * @return this sprite + * @since 1.8.0 + */ + public WSprite setUv(float u1, float v1, float u2, float v2) { + this.u1 = u1; + this.v1 = v1; + this.u2 = u2; + this.v2 = v2; + + return this; + } + @Override public boolean canResize() { return true; @@ -85,7 +126,7 @@ public class WSprite extends WWidget { if (!inBounds) currentFrame = 0; //assemble and draw the frame calculated last iteration. Identifier currentFrameTex = frames[currentFrame]; - ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), currentFrameTex, tint); + ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), currentFrameTex, u1, v1, u2, v2, tint); //calculate how much time has elapsed since the last animation change, and change the frame if necessary. long elapsed = now - lastFrame; |