From 59b8a6c53eeef4a9def7b2e47eba3e788fc5b1e9 Mon Sep 17 00:00:00 2001 From: Meredith Espinosa Date: Tue, 9 Jul 2019 22:11:36 -0700 Subject: add WSprite and WPlayerInvPanel, new slot bg painter --- .../cotton/gui/CottonScreenController.java | 13 +--- .../cotton/gui/client/BackgroundPainter.java | 28 +++++++-- .../cottonmc/cotton/gui/widget/WItemSlot.java | 4 ++ .../cotton/gui/widget/WPlayerInvPanel.java | 25 ++++++++ .../github/cottonmc/cotton/gui/widget/WSprite.java | 72 ++++++++++++++++++++++ 5 files changed, 128 insertions(+), 14 deletions(-) create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java (limited to 'src') diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java index 47b56f6..902c59a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java @@ -5,11 +5,7 @@ import java.util.ArrayList; import javax.annotation.Nullable; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; -import io.github.cottonmc.cotton.gui.widget.WGridPanel; -import io.github.cottonmc.cotton.gui.widget.WItemSlot; -import io.github.cottonmc.cotton.gui.widget.WPanel; -import io.github.cottonmc.cotton.gui.widget.WPlainPanel; -import io.github.cottonmc.cotton.gui.widget.WWidget; +import io.github.cottonmc.cotton.gui.widget.*; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.block.Block; @@ -304,11 +300,8 @@ public abstract class CottonScreenController extends CraftingContainer { - ScreenDrawing.drawGuiPanel(left-8, top-8, panel.getWidth()+16, panel.getHeight()+16); - + ScreenDrawing.drawGuiPanel(left-8, top-8, panel.getWidth()+14, panel.getHeight()+14); + }; + + public static BackgroundPainter SLOT = (left, top, panel) -> { + if (!(panel instanceof WItemSlot)) { + ScreenDrawing.drawBeveledPanel(left-1, top-1, panel.getWidth(), panel.getHeight(), 0xFF373737, 0xFF8B8B8B, 0xFFFFFFFF); + } else { + WItemSlot slot = (WItemSlot)panel; + for(int x = 0; x < slot.getWidth()/18; ++x) { + for(int y = 0; y < slot.getHeight()/18; ++y) { + int lo = 0xFF373737; + int bg = 0xFF8B8B8B; + int hi = 0xFFFFFFFF; + if (slot.isBigSlot()) { + ScreenDrawing.drawBeveledPanel((x * 18) + left - 4, (y * 18) + top - 4, 24, 24, + lo, bg, hi); + } else { + ScreenDrawing.drawBeveledPanel((x * 18) + left - 1, (y * 18) + top - 1, 18, 18, + lo, bg, hi); + } + } + } + } }; - - public static BackgroundPainter createColorful(int panelColor) { return (left, top, panel) -> { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java index 0e6b981..5b9d500 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java @@ -83,6 +83,10 @@ public class WItemSlot extends WWidget { public int getHeight() { return slotsHigh * 18; } + + public boolean isBigSlot() { + return big; + } @Override public void createPeers(CottonScreenController c) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java new file mode 100644 index 0000000..0fda576 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java @@ -0,0 +1,25 @@ +package io.github.cottonmc.cotton.gui.widget; + +import io.github.cottonmc.cotton.gui.client.BackgroundPainter; +import net.minecraft.entity.player.PlayerInventory; + +public class WPlayerInvPanel extends WPlainPanel { + private WItemSlot inv; + private WItemSlot hotbar; + + public WPlayerInvPanel(PlayerInventory playerInventory) { + inv = WItemSlot.ofPlayerStorage(playerInventory); + hotbar = WItemSlot.of(playerInventory, 0, 9, 1); + this.add(inv, 0, 0); + this.add(hotbar, 0, 58); + } + + @Override + public WPanel setBackgroundPainter(BackgroundPainter painter) { + super.setBackgroundPainter(null); + inv.setBackgroundPainter(painter); + hotbar.setBackgroundPainter(painter); + return this; + } +} + 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 new file mode 100644 index 0000000..303f480 --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java @@ -0,0 +1,72 @@ +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; + +public class WSprite extends WWidget { + private int currentFrame= 0; + private long currentFrameTime = 0; + private Identifier[] frames; + private int frameTime; + private long lastFrame; + private boolean singleImage = false; + + /** + * 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; + } + + /** + * 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. + */ + public WSprite(int frameTime, Identifier... frames) { + this.frameTime = frameTime; + this.frames = frames; + } + + @Override + public boolean canResize() { + return true; + } + + @Environment(EnvType.CLIENT) + @Override + public void paintBackground(int x, int y) { + if (singleImage) { + ScreenDrawing.rect(frames[0], x, y, getWidth(), getHeight(), 0xFFFFFFFF); + } else { + //grab the system time at the very start of the frame. + long now = System.nanoTime() / 1_000_000L; + + //check bounds so the Identifier isn't passed a bad number + boolean inBounds = (currentFrame >= 0) && (currentFrame < frames.length); + if (!inBounds) currentFrame = 0; + //assemble and draw the frame calculated last iteration. + Identifier currentFrameTex = frames[currentFrame]; + ScreenDrawing.rect(currentFrameTex, x, y, getWidth(), getHeight(), 0xFFFFFFFF); + + //calculate how much time has elapsed since the last animation change, and change the frame if necessary. + long elapsed = now - lastFrame; + currentFrameTime += elapsed; + if (currentFrameTime >= frameTime) { + currentFrame++; + //if we've hit the end of the animation, go back to the beginning + if (currentFrame >= frames.length - 1) { + currentFrame = 0; + } + currentFrameTime = 0; + } + + //frame is over; this frame is becoming the last frame so write the time to lastFrame + this.lastFrame = now; + } + } +} -- cgit