diff options
Diffstat (limited to 'src/main')
7 files changed, 291 insertions, 51 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java index 5e07fbb..f382911 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java @@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.client; import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; +import io.github.cottonmc.cotton.gui.widget.data.Texture; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.BufferBuilder; import net.minecraft.client.render.Tessellator; @@ -74,6 +75,37 @@ public class ScreenDrawing { * @param y the y coordinate of the box on-screen * @param width the width of the box on-screen * @param height the height of the box on-screen + * @param texture the texture + * @param color a color to tint the texture. This can be transparent! Use 0xFF_FFFFFF if you don't want a color tint + * @since 3.0.0 + */ + public static void texturedRect(int x, int y, int width, int height, Texture texture, int color) { + texturedRect(x, y, width, height, texture, color, 1.0f); + } + + /** + * Draws a textured rectangle. + * + * @param x the x coordinate of the box on-screen + * @param y the y coordinate of the box on-screen + * @param width the width of the box on-screen + * @param height the height of the box on-screen + * @param texture the texture + * @param color a color to tint the texture. This can be transparent! Use 0xFF_FFFFFF if you don't want a color tint + * @param opacity opacity of the drawn texture. (0f is fully opaque and 1f is fully visible) + * @since 3.0.0 + */ + public static void texturedRect(int x, int y, int width, int height, Texture texture, int color, float opacity) { + texturedRect(x, y, width, height, texture.image, texture.u1, texture.v1, texture.u2, texture.v2, color, opacity); + } + + /** + * Draws a textured rectangle. + * + * @param x the x coordinate of the box on-screen + * @param y the y coordinate of the box on-screen + * @param width the width of the box on-screen + * @param height the height of the box on-screen * @param texture the Identifier for the texture * @param u1 the left edge of the texture * @param v1 the top edge of the texture diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java index 71973b4..1d332ea 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java @@ -1,5 +1,6 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.Texture; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; @@ -11,6 +12,7 @@ import net.minecraft.util.Identifier; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import net.minecraft.util.math.MathHelper; import javax.annotation.Nullable; @@ -25,13 +27,15 @@ public class WBar extends WWidget { * The background texture. If not null, it will be * drawn behind the bar contents. */ - protected final Identifier bg; + @Nullable + protected final Texture bg; /** * The bar texture. If not null, it will be * drawn to represent the current field. */ - protected final Identifier bar; + @Nullable + protected final Texture bar; /** * The ID of the displayed property in the {@link #properties}. @@ -58,11 +62,11 @@ public class WBar extends WWidget { protected String tooltipLabel; protected Text tooltipTextComponent; - public WBar(Identifier bg, Identifier bar, int field, int maxfield) { + public WBar(Texture bg, Texture bar, int field, int maxfield) { this(bg, bar, field, maxfield, Direction.UP); } - public WBar(Identifier bg, Identifier bar, int field, int maxfield, Direction dir) { + public WBar(Texture bg, Texture bar, int field, int maxfield, Direction dir) { this.bg = bg; this.bar = bar; this.field = field; @@ -71,6 +75,14 @@ public class WBar extends WWidget { this.direction = dir; } + public WBar(Identifier bg, Identifier bar, int field, int maxfield) { + this(bg, bar, field, maxfield, Direction.UP); + } + + public WBar(Identifier bg, Identifier bar, int field, int maxfield, Direction dir) { + this(new Texture(bg), new Texture(bar), field, maxfield, dir); + } + /** * Adds a tooltip to the WBar. * @@ -124,7 +136,7 @@ public class WBar extends WWidget { int top = y + getHeight(); top -= barSize; if (bar!=null) { - ScreenDrawing.texturedRect(left, top, getWidth(), barSize, bar, 0, 1 - percent, 1, 1, 0xFFFFFFFF); + ScreenDrawing.texturedRect(left, top, getWidth(), barSize, bar.image, bar.u1, MathHelper.lerp(percent, bar.v2, bar.v1), bar.u2, bar.v2, 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(left, top, getWidth(), barSize, ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); } @@ -132,7 +144,7 @@ public class WBar extends WWidget { } case RIGHT: { if (bar!=null) { - ScreenDrawing.texturedRect(x, y, barSize, getHeight(), bar, 0, 0, percent, 1, 0xFFFFFFFF); + ScreenDrawing.texturedRect(x, y, barSize, getHeight(), bar.image, bar.u1, bar.v1, MathHelper.lerp(percent, bar.u1, bar.u2), bar.v2, 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(x, y, barSize, getHeight(), ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); } @@ -140,7 +152,7 @@ public class WBar extends WWidget { } case DOWN: { if (bar!=null) { - ScreenDrawing.texturedRect(x, y, getWidth(), barSize, bar, 0, 0, 1, percent, 0xFFFFFFFF); + ScreenDrawing.texturedRect(x, y, getWidth(), barSize, bar.image, bar.u1, bar.v1, bar.u2, MathHelper.lerp(percent, bar.v1, bar.v2), 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(x, y, getWidth(), barSize, ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); } @@ -151,7 +163,7 @@ public class WBar extends WWidget { int top = y; left -= barSize; if (bar!=null) { - ScreenDrawing.texturedRect(left, top, barSize, getHeight(), bar, 1 - percent, 0, 1, 1, 0xFFFFFFFF); + ScreenDrawing.texturedRect(left, top, barSize, getHeight(), bar.image, MathHelper.lerp(percent, bar.u2, bar.u1), bar.v1, bar.u2, bar.v2, 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(left, top, barSize, getHeight(), ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); } 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 c91e011..9e6f038 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 @@ -1,5 +1,6 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.Texture; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; @@ -10,28 +11,28 @@ import io.github.cottonmc.cotton.gui.client.ScreenDrawing; public class WSprite extends WWidget { protected int currentFrame= 0; protected long currentFrameTime = 0; - protected Identifier[] frames; + protected Texture[] frames; protected int frameTime; protected long lastFrame; protected boolean singleImage = false; protected int tint = 0xFFFFFFFF; - /** The left edge of the texture as a fraction. */ - protected float u1 = 0; - /** The top edge of the texture as a fraction. */ - protected float v1 = 0; - /** The right edge of the texture as a fraction. */ - protected float u2 = 1; - /** The bottom edge of the texture as a fraction. */ - protected float v2 = 1; + /** + * Create a new sprite with a single image. + * @param texture The image texture to display. + * @since 3.0.0 + */ + public WSprite(Texture texture) { + this.frames = new Texture[]{texture}; + this.singleImage = true; + } /** * Create a new sprite with a single image. * @param image The location of the image to display. */ public WSprite(Identifier image) { - this.frames = new Identifier[]{image}; - this.singleImage = true; + this(new Texture(image)); } /** @@ -44,11 +45,7 @@ public class WSprite extends WWidget { * @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; + this(new Texture(image, u1, v1, u2, v2)); } /** @@ -58,19 +55,74 @@ public class WSprite extends WWidget { */ public WSprite(int frameTime, Identifier... frames) { this.frameTime = frameTime; + this.frames = new Texture[frames.length]; + + for (int i = 0; i < frames.length; i++) { + this.frames[i] = new Texture(frames[i]); + } + + if (frames.length==1) this.singleImage = true; + } + + /** + * 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. + * @since 3.0.0 + */ + public WSprite(int frameTime, Texture... frames) { + this.frameTime = frameTime; this.frames = frames; if (frames.length==1) this.singleImage = true; } + /** + * Sets the image of this sprite. + * + * @param image the new image + * @return this sprite + */ public WSprite setImage(Identifier image) { - this.frames = new Identifier[]{image}; + return setImage(new Texture(image)); + } + + /** + * Sets the animation frames of this sprite. + * + * @param frames the frames + * @return this sprite + */ + public WSprite setFrames(Identifier... frames) { + Texture[] textures = new Texture[frames.length]; + for (int i = 0; i < frames.length; i++) { + textures[i] = new Texture(frames[i]); + } + return setFrames(textures); + } + + /** + * Sets the image of this sprite. + * + * @param image the new image + * @return this sprite + * @since 3.0.0 + */ + public WSprite setImage(Texture image) { + this.frames = new Texture[]{image}; this.singleImage = true; this.currentFrame = 0; this.currentFrameTime = 0; return this; } - public WSprite setFrames(Identifier... frames) { + /** + * Sets the animation frames of this sprite. + * + * @param frames the frames + * @return this sprite + * @since 3.0.0 + */ + public WSprite setFrames(Texture... frames) { this.frames = frames; if (frames.length==1) singleImage = true; if (currentFrame>=frames.length) { @@ -83,12 +135,21 @@ public class WSprite extends WWidget { /** * 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. + * + * @param tint the new tint + * @return this sprite */ public WSprite setTint(int tint) { this.tint = tint; return this; } + /** + * Sets the tint for this sprite to the following opaque color. + * + * @param tint the new tint + * @return this sprite + */ public WSprite setOpaqueTint(int tint) { this.tint = tint | 0xFF000000; return this; @@ -106,12 +167,12 @@ public class WSprite extends WWidget { * @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; + Texture[] newFrames = new Texture[frames.length]; + for (int i = 0; i < frames.length; i++) { + newFrames[i] = frames[i].withUv(u1, v1, u2, v2); + } - return this; + return setFrames(newFrames); } @Override @@ -132,7 +193,7 @@ public class WSprite extends WWidget { boolean inBounds = (currentFrame >= 0) && (currentFrame < frames.length); if (!inBounds) currentFrame = 0; //assemble and draw the frame calculated last iteration. - Identifier currentFrameTex = frames[currentFrame]; + Texture currentFrameTex = frames[currentFrame]; paintFrame(x, y, currentFrameTex); //calculate how much time has elapsed since the last animation change, and change the frame if necessary. @@ -152,8 +213,15 @@ public class WSprite extends WWidget { } } + /** + * Paints a single frame for this sprite. + * + * @param x the X coordinate to draw it at + * @param y the Y coordinate to draw it at + * @param texture the texture to draw + */ @Environment(EnvType.CLIENT) - public void paintFrame(int x, int y, Identifier texture) { - ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), texture, u1, v1, u2, v2, tint); + protected void paintFrame(int x, int y, Texture texture) { + ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), texture, tint); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTiledSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTiledSprite.java index 516c876..2b7f3ca 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WTiledSprite.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTiledSprite.java @@ -1,6 +1,7 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Texture; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.util.Identifier; @@ -42,6 +43,35 @@ public class WTiledSprite extends WSprite { } /** + * Create a tiled sprite. + * + * @param tileWidth The width a tile + * @param tileHeight The height of a tile + * @param image The image to tile + * @since 3.0.0 + */ + public WTiledSprite(int tileWidth, int tileHeight, Texture image) { + super(image); + this.tileWidth = tileWidth; + this.tileHeight = tileHeight; + } + + /** + * Create a new animated tiled sprite. + * + * @param tileWidth The width a tile + * @param tileHeight The height of a tile + * @param frameTime How long in milliseconds to display for. (1 tick = 50 ms) + * @param frames The locations of the frames of the animation. + * @since 3.0.0 + */ + public WTiledSprite(int tileWidth, int tileHeight, int frameTime, Texture... frames) { + super(frameTime, frames); + this.tileWidth = tileWidth; + this.tileHeight = tileHeight; + } + + /** * Sets the tiling size. This determines how often the texture will repeat. * * @param width the new tiling width @@ -98,7 +128,7 @@ public class WTiledSprite extends WSprite { @Environment(EnvType.CLIENT) @Override - public void paintFrame(int x, int y, Identifier texture) { + public void paintFrame(int x, int y, Texture texture) { // Y Direction (down) for (int tileYOffset = 0; tileYOffset < height; tileYOffset += tileHeight) { // X Direction (right) @@ -112,8 +142,7 @@ public class WTiledSprite extends WSprite { getTileWidth(), getTileHeight(), // render the current texture texture, - // clips the texture if wanted - u1, v1, u2, v2, tint); + tint); } } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java index 29f8648..7c37aa5 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java @@ -1,5 +1,6 @@ package io.github.cottonmc.cotton.gui.widget; +import io.github.cottonmc.cotton.gui.widget.data.Texture; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; @@ -17,13 +18,13 @@ import java.util.function.Consumer; public class WToggleButton extends WWidget { // Default on/off images - protected final static Identifier DEFAULT_OFF_IMAGE = new Identifier("libgui:textures/widget/toggle_off.png"); - protected final static Identifier DEFAULT_ON_IMAGE = new Identifier("libgui:textures/widget/toggle_on.png"); - protected final static Identifier DEFAULT_FOCUS_IMAGE = new Identifier("libgui:textures/widget/toggle_focus.png"); + protected static final Texture DEFAULT_OFF_IMAGE = new Texture(new Identifier("libgui:textures/widget/toggle_off.png")); + protected static final Texture DEFAULT_ON_IMAGE = new Texture(new Identifier("libgui:textures/widget/toggle_on.png")); + protected static final Texture DEFAULT_FOCUS_IMAGE = new Texture(new Identifier("libgui:textures/widget/toggle_focus.png")); - protected Identifier onImage; - protected Identifier offImage; - protected Identifier focusImage = DEFAULT_FOCUS_IMAGE; + protected Texture onImage; + protected Texture offImage; + protected Texture focusImage = DEFAULT_FOCUS_IMAGE; @Nullable protected Text label = null; @@ -57,6 +58,28 @@ public class WToggleButton extends WWidget { * @param offImage the toggled off image */ public WToggleButton(Identifier onImage, Identifier offImage) { + this(new Texture(onImage), new Texture(offImage)); + } + + /** + * Constructs a toggle button with custom images. + * + * @param onImage the toggled on image + * @param offImage the toggled off image + * @param label the button label + */ + public WToggleButton(Identifier onImage, Identifier offImage, Text label) { + this(new Texture(onImage), new Texture(offImage), label); + } + + /** + * Constructs a toggle button with custom images and no label. + * + * @param onImage the toggled on image + * @param offImage the toggled off image + * @since 3.0.0 + */ + public WToggleButton(Texture onImage, Texture offImage) { this.onImage = onImage; this.offImage = offImage; } @@ -67,8 +90,9 @@ public class WToggleButton extends WWidget { * @param onImage the toggled on image * @param offImage the toggled off image * @param label the button label + * @since 3.0.0 */ - public WToggleButton(Identifier onImage, Identifier offImage, Text label) { + public WToggleButton(Texture onImage, Texture offImage, Text label) { this.onImage = onImage; this.offImage = offImage; this.label = label; @@ -151,29 +175,29 @@ public class WToggleButton extends WWidget { return this; } - public Identifier getOnImage() { + public Texture getOnImage() { return onImage; } - public WToggleButton setOnImage(Identifier onImage) { + public WToggleButton setOnImage(Texture onImage) { this.onImage = onImage; return this; } - public Identifier getOffImage() { + public Texture getOffImage() { return offImage; } - public WToggleButton setOffImage(Identifier offImage) { + public WToggleButton setOffImage(Texture offImage) { this.offImage = offImage; return this; } - public Identifier getFocusImage() { + public Texture getFocusImage() { return focusImage; } - public WToggleButton setFocusImage(Identifier focusImage) { + public WToggleButton setFocusImage(Texture focusImage) { this.focusImage = focusImage; return this; } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Texture.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Texture.java new file mode 100644 index 0000000..408a3cd --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/Texture.java @@ -0,0 +1,64 @@ +package io.github.cottonmc.cotton.gui.widget.data; + +import net.minecraft.util.Identifier; + +import java.util.Objects; + +/** + * Represents a texture for a widget. + * + * @since 3.0.0 + */ +public final class Texture { + /** + * The image of this texture. + */ + public final Identifier image; + + /** + * The UV coordinates of this texture, between 0 and 1. + */ + public final float u1, v1, u2, v2; + + /** + * Constructs a new texture that uses the full image. + * + * @param image the image + * @throws NullPointerException if the image is null + */ + public Texture(Identifier image) { + this(image, 0, 0, 1, 1); + } + + /** + * Constructs a new texture with custom UV values. + * + * @param image the image + * @param u1 the left U coordinate + * @param v1 the top V coordinate + * @param u2 the right U coordinate + * @param v2 the bottom V coordinate + * @throws NullPointerException if the image is null + */ + public Texture(Identifier image, float u1, float v1, float u2, float v2) { + this.image = Objects.requireNonNull(image, "image"); + + this.u1 = u1; + this.v1 = v1; + this.u2 = u2; + this.v2 = v2; + } + + /** + * Creates a new texture with different UV values. + * + * @param u1 the left U coordinate + * @param v1 the top V coordinate + * @param u2 the right U coordinate + * @param v2 the bottom V coordinate + * @return the created texture + */ + public Texture withUv(float u1, float v1, float u2, float v2) { + return new Texture(image, u1, v1, u2, v2); + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java index 0876cb5..1d7dcc9 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java @@ -1,6 +1,7 @@ package io.github.cottonmc.cotton.gui.widget.icon; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import io.github.cottonmc.cotton.gui.widget.data.Texture; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.util.math.MatrixStack; @@ -12,7 +13,7 @@ import net.minecraft.util.Identifier; * @since 2.2.0 */ public class TextureIcon implements Icon { - private final Identifier texture; + private final Texture texture; private float opacity = 1f; private int color = 0xFF_FFFFFF; @@ -22,6 +23,16 @@ public class TextureIcon implements Icon { * @param texture the identifier of the icon texture */ public TextureIcon(Identifier texture) { + this(new Texture(texture)); + } + + /** + * Constructs a new texture icon. + * + * @param texture the identifier of the icon texture + * @since 3.0.0 + */ + public TextureIcon(Texture texture) { this.texture = texture; } |