From 40059d3145ac938e3e14eb011a07a405d99e5ffc Mon Sep 17 00:00:00 2001 From: Falkreon Date: Wed, 28 Aug 2019 17:57:00 -0500 Subject: Test gui demo leading to list fixes --- .../github/cottonmc/test/client/TestClientGui.java | 72 ++++++++++++++++++--- .../main/resources/assets/libgui-test/portal.png | Bin 0 -> 399 bytes .../main/resources/assets/libgui-test/portal2.png | Bin 0 -> 441 bytes .../cottonmc/cotton/gui/widget/WListPanel.java | 5 +- .../cottonmc/cotton/gui/widget/WScrollBar.java | 1 + .../github/cottonmc/cotton/gui/widget/WSprite.java | 50 +++++++++++--- 6 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 GuiTest/src/main/resources/assets/libgui-test/portal.png create mode 100644 GuiTest/src/main/resources/assets/libgui-test/portal2.png 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 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 configurator = (String s, WLabel label) -> { - label.setText(new LiteralText(s)); + BiConsumer 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 list = new WListPanel(data, WLabel.class, ()->new WLabel(""), configurator); - root.add(list, 0, 2, 7, 5); + WListPanel list = new WListPanel(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 Binary files /dev/null and b/GuiTest/src/main/resources/assets/libgui-test/portal.png 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 Binary files /dev/null and b/GuiTest/src/main/resources/assets/libgui-test/portal2.png 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 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 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 extends WPanel { if (presentCells>0) { for(int i=0; i=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