aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMeredith Espinosa <goclonefilms@gmail.com>2019-07-09 22:11:36 -0700
committerMeredith Espinosa <goclonefilms@gmail.com>2019-07-09 22:11:36 -0700
commit59b8a6c53eeef4a9def7b2e47eba3e788fc5b1e9 (patch)
treed89e952bfab57a796054d689ef0efcec3794d4e9 /src
parentad4d0357d19d3dee50c28b7f5485a24deeeb4dde (diff)
downloadLibGui-59b8a6c53eeef4a9def7b2e47eba3e788fc5b1e9.tar.gz
LibGui-59b8a6c53eeef4a9def7b2e47eba3e788fc5b1e9.tar.bz2
LibGui-59b8a6c53eeef4a9def7b2e47eba3e788fc5b1e9.zip
add WSprite and WPlayerInvPanel, new slot bg painter1.1.0
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java13
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java28
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java4
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java25
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java72
5 files changed, 128 insertions, 14 deletions
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<Inventory
return propertyDelegate;
}
- public WPanel createPlayerInventoryPanel() {
- WPlainPanel inv = new WPlainPanel();
- inv.add(WItemSlot.ofPlayerStorage(playerInventory), 0, 0);
- inv.add(WItemSlot.of(playerInventory, 0, 9, 1), 0, 16*4 - 6);
- return inv;
+ public WPlayerInvPanel createPlayerInventoryPanel() {
+ return new WPlayerInvPanel(this.playerInventory);
}
public static Inventory getBlockInventory(BlockContext ctx) {
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java
index 38f9130..b95cdfc 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java
@@ -1,5 +1,6 @@
package io.github.cottonmc.cotton.gui.client;
+import io.github.cottonmc.cotton.gui.widget.WItemSlot;
import io.github.cottonmc.cotton.gui.widget.WWidget;
public interface BackgroundPainter {
@@ -14,11 +15,30 @@ public interface BackgroundPainter {
public static BackgroundPainter VANILLA = (left, top, panel) -> {
- 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;
+ }
+ }
+}