diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java | 9 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WTiledSprite.java | 76 |
2 files changed, 83 insertions, 2 deletions
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 a5555fc..c91e011 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 @@ -123,7 +123,7 @@ public class WSprite extends WWidget { @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { if (singleImage) { - ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), frames[0], u1, v1, u2, v2, tint); + paintFrame(x, y, frames[0]); } else { //grab the system time at the very start of the frame. long now = System.nanoTime() / 1_000_000L; @@ -133,7 +133,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, u1, v1, u2, v2, tint); + paintFrame(x, y, currentFrameTex); //calculate how much time has elapsed since the last animation change, and change the frame if necessary. long elapsed = now - lastFrame; @@ -151,4 +151,9 @@ public class WSprite extends WWidget { this.lastFrame = now; } } + + @Environment(EnvType.CLIENT) + public void paintFrame(int x, int y, Identifier texture) { + ScreenDrawing.texturedRect(x, y, getWidth(), getHeight(), texture, u1, v1, u2, v2, 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 new file mode 100644 index 0000000..d49074f --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WTiledSprite.java @@ -0,0 +1,76 @@ +package io.github.cottonmc.cotton.gui.widget; + +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.util.Identifier; + +/** + * A sprite whose texture will be tiled. + * + * @since 2.0.0 + */ +public class WTiledSprite extends WSprite { + private int tileWidth; + private int tileHeight; + + /** + * Create a tiled sprite. + * + * @param tileWidth The width a tile + * @param tileHeight The height of a tile + * @param image The image to tile + */ + public WTiledSprite(int tileWidth, int tileHeight, Identifier 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. + */ + public WTiledSprite(int tileWidth, int tileHeight, int frameTime, Identifier... 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 + * @param height the new tiling height + */ + public void setTileSize(int width, int height) { + tileWidth = width; + tileHeight = height; + } + + @Environment(EnvType.CLIENT) + @Override + public void paintFrame(int x, int y, Identifier texture) { + // Y Direction (down) + for (int tileYOffset = 0; tileYOffset < height; tileYOffset += tileHeight) { + // X Direction (right) + for (int tileXOffset = 0; tileXOffset < width; tileXOffset += tileWidth) { + // draw the texture + ScreenDrawing.texturedRect( + // at the correct position using tileXOffset and tileYOffset + x + tileXOffset, y + tileYOffset, + // but using the set tileWidth and tileHeight instead of the full height and + // width + tileWidth, tileHeight, + // render the current texture + texture, + // clips the texture if wanted + u1, v1, u2, v2, tint); + } + } + } +} |