From 40059d3145ac938e3e14eb011a07a405d99e5ffc Mon Sep 17 00:00:00 2001
From: Falkreon <falkreon@gmail.com>
Date: Wed, 28 Aug 2019 17:57:00 -0500
Subject: Test gui demo leading to list fixes

---
 .../cottonmc/cotton/gui/widget/WListPanel.java     |  5 ++-
 .../cottonmc/cotton/gui/widget/WScrollBar.java     |  1 +
 .../github/cottonmc/cotton/gui/widget/WSprite.java | 50 ++++++++++++++++++----
 3 files changed, 47 insertions(+), 9 deletions(-)

(limited to 'src/main/java')

diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java
index abb403e..155f979 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java
@@ -53,7 +53,7 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
 		}
 		
 		for(WWidget child : children) {
-			child.paintBackground(x + child.getX(), y + child.getY());
+			child.paintBackground(x + child.getX(), y + child.getY(), mouseX - child.getX(), mouseY - child.getY());
 		}
 	}
 	
@@ -87,6 +87,7 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
 				if (!exemplar.canResize()) cellHeight = exemplar.getHeight();
 			}
 		}
+		System.out.println("CellHeight: "+cellHeight);
 		if (cellHeight<4) cellHeight=4;
 		
 		int layoutHeight = this.getHeight()-(margin*2);
@@ -111,6 +112,8 @@ public class WListPanel<D, W extends WWidget> extends WPanel {
 		if (presentCells>0) {
 			for(int i=0; i<presentCells; i++) {
 				int index = i+scrollOffset;
+				if (index>=data.size()) break;
+				if (index<0) continue; //THIS IS A THING THAT IS HAPPENING >:(
 				D d = data.get(index);
 				W w = configured.get(d);
 				if (w==null) {
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java
index 524b0d2..39c416c 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java
@@ -126,6 +126,7 @@ public class WScrollBar extends WWidget {
 		if (this.value>maxValue-window) {
 			this.value = maxValue-window;
 		}
+		if (this.value<0) this.value = 0;
 		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
index 303f480..b3ce4cf 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
@@ -6,12 +6,13 @@ 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;
+	protected int currentFrame= 0;
+	protected long currentFrameTime = 0;
+	protected Identifier[] frames;
+	protected int frameTime;
+	protected long lastFrame;
+	protected boolean singleImage = false;
+	protected int tint = 0xFFFFFFFF;
 
 	/**
 	 * Create a new sprite with a single image.
@@ -30,6 +31,39 @@ public class WSprite extends WWidget {
 	public WSprite(int frameTime, Identifier... frames) {
 		this.frameTime = frameTime;
 		this.frames = frames;
+		if (frames.length==1) this.singleImage = true;
+	}
+	
+	public WSprite setImage(Identifier image) {
+		this.frames = new Identifier[]{image};
+		this.singleImage = true;
+		this.currentFrame = 0;
+		this.currentFrameTime = 0;
+		return this;
+	}
+	
+	public WSprite setFrames(Identifier... frames) {
+		this.frames = frames;
+		if (frames.length==1) singleImage = true;
+		if (currentFrame>=frames.length) {
+			currentFrame = 0;
+			currentFrameTime = 0;
+		}
+		return this;
+	}
+	
+	/**
+	 * Sets the tint for this sprite to the following color-with-alpha. If you don't want to specify
+	 * alpha, use {@link #setOpaqueTint(int)} instead.
+	 */
+	public WSprite setTint(int tint) {
+		this.tint = tint;
+		return this;
+	}
+	
+	public WSprite setOpaqueTint(int tint) {
+		this.tint = tint | 0xFF000000;
+		return this;
 	}
 
 	@Override
@@ -41,7 +75,7 @@ public class WSprite extends WWidget {
 	@Override
 	public void paintBackground(int x, int y) {
 		if (singleImage) {
-			ScreenDrawing.rect(frames[0], x, y, getWidth(), getHeight(), 0xFFFFFFFF);
+			ScreenDrawing.rect(frames[0], x, y, getWidth(), getHeight(), tint);
 		} else {
 			//grab the system time at the very start of the frame.
 			long now = System.nanoTime() / 1_000_000L;
@@ -51,7 +85,7 @@ public class WSprite extends WWidget {
 			if (!inBounds) currentFrame = 0;
 			//assemble and draw the frame calculated last iteration.
 			Identifier currentFrameTex = frames[currentFrame];
-			ScreenDrawing.rect(currentFrameTex, x, y, getWidth(), getHeight(), 0xFFFFFFFF);
+			ScreenDrawing.rect(currentFrameTex, x, y, getWidth(), getHeight(), tint);
 
 			//calculate how much time has elapsed since the last animation change, and change the frame if necessary.
 			long elapsed = now - lastFrame;
-- 
cgit