aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java17
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java9
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WTiledSprite.java76
3 files changed, 92 insertions, 10 deletions
diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java
index b96a359..45426b4 100644
--- a/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java
+++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java
@@ -1,20 +1,15 @@
package io.github.cottonmc.test.client;
-import java.util.ArrayList;
import java.util.List;
-import java.util.function.BiConsumer;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
-import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WLabel;
-import io.github.cottonmc.cotton.gui.widget.WListPanel;
-import io.github.cottonmc.cotton.gui.widget.WPlainPanel;
import io.github.cottonmc.cotton.gui.widget.WSlider;
-import io.github.cottonmc.cotton.gui.widget.WSprite;
import io.github.cottonmc.cotton.gui.widget.WTextField;
+import io.github.cottonmc.cotton.gui.widget.WTiledSprite;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import io.github.cottonmc.cotton.gui.widget.data.Color;
@@ -42,13 +37,20 @@ public class TestClientGui extends LightweightGuiDescription {
public TestClientGui() {
WGridPanel root = new WGridPanel(22);
this.setRootPanel(root);
-
WLabel title = new WLabel(new LiteralText("Client Test Gui"), WLabel.DEFAULT_TEXT_COLOR) {
@Override
public void addTooltip(List<Text> tooltip) {
tooltip.add(new LiteralText("Radical!"));
}
};
+ WTiledSprite wood = new WTiledSprite(
+ 8, 8, // tile width and height
+ 500, // animation speed
+ new Identifier("minecraft:textures/block/birch_planks.png"),
+ new Identifier("minecraft:textures/block/dark_oak_planks.png"),
+ new Identifier("minecraft:textures/block/jungle_planks.png")
+ );
+ root.add(wood, 3, 3, 2, 2);
root.add(title, 0, 0);
WTextField text = new WTextField();
@@ -149,4 +151,3 @@ public class TestClientGui extends LightweightGuiDescription {
}
}
}
-
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);
+ }
+ }
+ }
+}