Skip to content

Commit

Permalink
add WSprite and WPlayerInvPanel, new slot bg painter
Browse files Browse the repository at this point in the history
  • Loading branch information
LemmaEOF committed Jul 10, 2019
1 parent ad4d035 commit 59b8a6c
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.4.8+build.155

# Mod Properties
mod_version = 1.0.0
mod_version = 1.1.0
maven_group = io.github.cottonmc
archives_base_name = LibGui

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
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;
Expand Down Expand Up @@ -304,11 +300,8 @@ public PropertyDelegate getPropertyDelegate() {
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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public int getWidth() {
public int getHeight() {
return slotsHigh * 18;
}

public boolean isBigSlot() {
return big;
}

@Override
public void createPeers(CottonScreenController c) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

72 changes: 72 additions & 0 deletions src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 59b8a6c

Please sign in to comment.