aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFalkreon <falkreon@gmail.com>2019-08-28 17:57:00 -0500
committerFalkreon <falkreon@gmail.com>2019-08-28 17:57:00 -0500
commit40059d3145ac938e3e14eb011a07a405d99e5ffc (patch)
treeea27df65965d6ebad148bb267ca52e6d07e9b1c4
parenta9f2dd347f4da78c3740d9e0dddac9739f42d78c (diff)
downloadLibGui-40059d3145ac938e3e14eb011a07a405d99e5ffc.tar.gz
LibGui-40059d3145ac938e3e14eb011a07a405d99e5ffc.tar.bz2
LibGui-40059d3145ac938e3e14eb011a07a405d99e5ffc.zip
Test gui demo leading to list fixes
-rw-r--r--GuiTest/src/main/java/io/github/cottonmc/test/client/TestClientGui.java72
-rw-r--r--GuiTest/src/main/resources/assets/libgui-test/portal.pngbin0 -> 399 bytes
-rw-r--r--GuiTest/src/main/resources/assets/libgui-test/portal2.pngbin0 -> 441 bytes
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WListPanel.java5
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java1
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WSprite.java50
6 files changed, 109 insertions, 19 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 9bbd1fc..90beffd 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
@@ -3,38 +3,90 @@ package io.github.cottonmc.test.client;
import java.util.ArrayList;
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.WSprite;
import io.github.cottonmc.cotton.gui.widget.WTextField;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
import net.minecraft.text.LiteralText;
+import net.minecraft.util.Identifier;
public class TestClientGui extends LightweightGuiDescription {
+ @Environment(EnvType.CLIENT)
+ public static final BackgroundPainter PANEL = (x, y, panel)->{
+ ScreenDrawing.drawBeveledPanel(x, y, panel.getWidth(), panel.getHeight());
+ };
+
+ private static final Identifier PORTAL1 = new Identifier("libgui-test:portal.png");
+ private static final Identifier PORTAL2 = new Identifier("libgui-test:portal2.png");
+
public TestClientGui() {
- WGridPanel root = new WGridPanel(24);
+ WGridPanel root = new WGridPanel(22);
this.setRootPanel(root);
WLabel title = new WLabel(new LiteralText("Client Test Gui"), WLabel.DEFAULT_TEXT_COLOR);
root.add(title, 0, 0);
WTextField text = new WTextField();
- text.setSuggestion("Test Suggestion");
+ text.setSuggestion("Search");
root.add(text, 0, 1, 8, 1);
- text.setSize(8*18, 20);
+ text.setSize(7*18, 20);
ArrayList<String> data = new ArrayList<>();
- for(int i=0; i<100; i++) {
- data.add(""+i);
- }
+ data.add("Wolfram Alpha");
+ data.add("Strange Home");
+ data.add("Nether Base");
+ data.add("Death");
+ data.add("Cake");
+ data.add("Mushroom Island");
+ data.add("A List Item");
+ data.add("Notes");
+ data.add("Slime Island");
- BiConsumer<String, WLabel> configurator = (String s, WLabel label) -> {
- label.setText(new LiteralText(s));
+ BiConsumer<String, PortalDestination> configurator = (String s, PortalDestination destination) -> {
+ destination.label.setText(new LiteralText(s));
+
+ int hash = s.hashCode();
+ Identifier sprite = ((hash & 0x01) == 0) ? PORTAL1 : PORTAL2;
+ destination.sprite.setImage(sprite);
+
+ int cost = (hash >> 1) & 0x2FF;
+ destination.cost.setText(new LiteralText(""+cost+" XP"));
};
- WListPanel<String, WLabel> list = new WListPanel<String, WLabel>(data, WLabel.class, ()->new WLabel(""), configurator);
- root.add(list, 0, 2, 7, 5);
+ WListPanel<String, PortalDestination> list = new WListPanel<String, PortalDestination>(data, PortalDestination.class, PortalDestination::new, configurator);
+ list.setListItemHeight(2*18);
+ root.add(list, 0, 2, 7, 6);
+
+ root.add(new WButton(new LiteralText("Teleport")), 3,8,4,1);
root.validate(this);
}
+
+ public static class PortalDestination extends WPlainPanel {
+ WSprite sprite;
+ WLabel label;
+ WLabel cost;
+
+ public PortalDestination() {
+ sprite = new WSprite(new Identifier("libgui-test:portal"));
+ this.add(sprite, 2, 2, 18, 18);
+ label = new WLabel("Foo");
+ this.add(label, 18+ 4, 2, 5*18, 18);
+ cost = new WLabel("1000 Xp");
+ this.add(cost, 2, 20, 6*18, 18);
+
+ this.setSize(7*18, 2*18);
+
+ this.setBackgroundPainter(PANEL); //Would fail on a serverside gui
+ }
+ }
}
+
diff --git a/GuiTest/src/main/resources/assets/libgui-test/portal.png b/GuiTest/src/main/resources/assets/libgui-test/portal.png
new file mode 100644
index 0000000..cf64bcc
--- /dev/null
+++ b/GuiTest/src/main/resources/assets/libgui-test/portal.png
Binary files differ
diff --git a/GuiTest/src/main/resources/assets/libgui-test/portal2.png b/GuiTest/src/main/resources/assets/libgui-test/portal2.png
new file mode 100644
index 0000000..3a1c211
--- /dev/null
+++ b/GuiTest/src/main/resources/assets/libgui-test/portal2.png
Binary files differ
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;