diff options
author | Falkreon <falkreon@gmail.com> | 2019-08-30 12:25:56 -0500 |
---|---|---|
committer | Falkreon <falkreon@gmail.com> | 2019-08-30 12:25:56 -0500 |
commit | f31ed14fa9bcd45f1509c709ca51ce3c2aa4b6cf (patch) | |
tree | fd538cfc8f49ac4f505c546b8b077d3032179995 | |
parent | 3f01c837c12ee1c20d2ff568393a005226a1c940 (diff) | |
download | LibGui-f31ed14fa9bcd45f1509c709ca51ce3c2aa4b6cf.tar.gz LibGui-f31ed14fa9bcd45f1509c709ca51ce3c2aa4b6cf.tar.bz2 LibGui-f31ed14fa9bcd45f1509c709ca51ce3c2aa4b6cf.zip |
Rearrange drawing, deprecate paintForeground, add widget ticks
6 files changed, 95 insertions, 121 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 90beffd..d1b8bca 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 @@ -1,6 +1,7 @@ package io.github.cottonmc.test.client; import java.util.ArrayList; +import java.util.List; import java.util.function.BiConsumer; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; @@ -32,7 +33,12 @@ public class TestClientGui extends LightweightGuiDescription { WGridPanel root = new WGridPanel(22); this.setRootPanel(root); - WLabel title = new WLabel(new LiteralText("Client Test Gui"), WLabel.DEFAULT_TEXT_COLOR); + WLabel title = new WLabel(new LiteralText("Client Test Gui"), WLabel.DEFAULT_TEXT_COLOR) { + @Override + public void addInformation(List<String> information) { + information.add("Radical!"); + } + }; root.add(title, 0, 0); WTextField text = new WTextField(); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index a1b95fa..ea388d1 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java @@ -54,39 +54,49 @@ public class CottonClientScreen extends Screen { } } - @Override - public void render(int mouseX, int mouseY, float partialTicks) { - renderBackground(mouseX, mouseY); - - super.render(mouseX, mouseY, partialTicks); + public void paint(int mouseX, int mouseY) { + super.renderBackground(); if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { - root.paintForeground(left, top, mouseX, mouseY); + root.paintBackground(left, top, mouseX-left, mouseY-top); } } + + if (getTitle() != null) { + font.draw(getTitle().asFormattedString(), left, top, description.getTitleColor()); + } } - public void renderBackground(int mouseX, int mouseY) { - super.renderBackground(); + @SuppressWarnings("deprecation") + @Override + public void render(int mouseX, int mouseY, float partialTicks) { + paint(mouseX, mouseY); + + super.render(mouseX, mouseY, partialTicks); if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { - root.paintBackground(left, top, mouseX-left, mouseY-top); + root.paintForeground(left, top, mouseX, mouseY); + + WWidget hitChild = root.hit(mouseX-left, mouseY-top); + if (hitChild!=null) hitChild.renderTooltip(left, top, mouseX-left, mouseY-top); } } - - if (getTitle() != null) { - font.draw(getTitle().asFormattedString(), left, top, description.getTitleColor()); - } } @Override public void tick() { super.tick(); + if (description!=null) { + WPanel root = description.getRootPanel(); + if (root!=null) { + root.tick(); + } + } } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java index b7acb28..ad00784 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java @@ -5,20 +5,19 @@ import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen; +import net.minecraft.client.render.GuiLighting; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; -import net.minecraft.util.Nameable; public class CottonInventoryScreen<T extends CottonCraftingController> extends AbstractContainerScreen<T> { - protected CottonCraftingController container; + protected CottonCraftingController description; public static final int PADDING = 8; protected WWidget lastResponder = null; protected WWidget focus = null; public CottonInventoryScreen(T container, PlayerEntity player) { super(container, player.inventory, new LiteralText("")); - this.container = container; + this.description = container; width = 18*9; height = 18*9; this.containerWidth = 18*9; @@ -38,15 +37,15 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A public void init(MinecraftClient minecraftClient_1, int screenWidth, int screenHeight) { super.init(minecraftClient_1, screenWidth, screenHeight); - container.addPainters(); + description.addPainters(); reposition(); } public void reposition() { - WPanel basePanel = container.getRootPanel(); + WPanel basePanel = description.getRootPanel(); if (basePanel!=null) { - basePanel.validate(container); + basePanel.validate(description); containerWidth = basePanel.getWidth(); containerHeight = basePanel.getHeight(); @@ -54,8 +53,6 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A //DEBUG if (containerWidth<16) containerWidth=300; if (containerHeight<16) containerHeight=300; - //if (left<0 || left>300) left = 10; - //if (top<0 || top>300) top = 10; } left = (width / 2) - (containerWidth / 2); top = (height / 2) - (containerHeight / 2); @@ -74,23 +71,23 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A @Override public boolean charTyped(char ch, int keyCode) { - if (container.getFocus()==null) return false; - container.getFocus().onCharTyped(ch); + if (description.getFocus()==null) return false; + description.getFocus().onCharTyped(ch); return true; } @Override public boolean keyPressed(int ch, int keyCode, int modifiers) { if (super.keyPressed(ch, keyCode, modifiers)) return true; - if (container.getFocus()==null) return false; - container.getFocus().onKeyPressed(ch, keyCode, modifiers); + if (description.getFocus()==null) return false; + description.getFocus().onKeyPressed(ch, keyCode, modifiers); return true; } @Override public boolean keyReleased(int ch, int keyCode, int modifiers) { - if (container.getFocus()==null) return false; - container.getFocus().onKeyReleased(ch, keyCode, modifiers); + if (description.getFocus()==null) return false; + description.getFocus().onKeyReleased(ch, keyCode, modifiers); return true; } @@ -101,7 +98,7 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A int containerY = (int)mouseY-top; if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; if (lastResponder==null) { - lastResponder = container.doMouseDown(containerX, containerY, mouseButton); + lastResponder = description.doMouseDown(containerX, containerY, mouseButton); } else { //This is a drag instead } @@ -120,7 +117,7 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A lastResponder.onClick(containerX-lastResponder.getAbsoluteX(), containerY-lastResponder.getAbsoluteY(), mouseButton); } } else { - container.doMouseUp(containerX, containerY, mouseButton); + description.doMouseUp(containerX, containerY, mouseButton); } lastResponder = null; @@ -139,86 +136,58 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A return result; } else { if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; - container.doMouseDrag(containerX, containerY, mouseButton); + description.doMouseDrag(containerX, containerY, mouseButton); } return result; } - //Zapping this method may fix some positioning bugs - but may cause some backgroundPainter bugs. Will need to monitor. - /* @Override - public void resize(MinecraftClient minecraftClient_1, int int_1, int int_2) { - //super.onScaleChanged(minecraftClient_1, int_1, int_2); - this.width = int_1; - this.height = int_2; - reposition(); - }*/ + protected void drawBackground(float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. - /* - * SPECIAL FUNCTIONS: Where possible, we want to draw everything based on *actual GUI state and composition* rather - * than relying on pre-baked textures that the programmer then needs to carefully match up their GUI to. - */ - - private int multiplyColor(int color, float amount) { - int a = color & 0xFF000000; - float r = (color >> 16 & 255) / 255.0F; - float g = (color >> 8 & 255) / 255.0F; - float b = (color & 255) / 255.0F; - - r = Math.min(r*amount, 1.0f); - g = Math.min(g*amount, 1.0f); - b = Math.min(b*amount, 1.0f); - - int ir = (int)(r*255); - int ig = (int)(g*255); - int ib = (int)(b*255); + public void paint(int mouseX, int mouseY) { + super.renderBackground(); - return a | - (ir << 16) | - (ig << 8) | - ib; - } - - @Override - protected void drawBackground(float partialTicks, int mouseX, int mouseY) { - if (this.container==null) { - return; + if (description!=null) { + WPanel root = description.getRootPanel(); + if (root!=null) { + root.paintBackground(left, top, mouseX-left, mouseY-top); + } } - WPanel root = this.container.getRootPanel(); - if (root==null) return; - - root.paintBackground(left, top, mouseX-left, mouseY-top); - //TODO: Change this to a label that lives in the rootPanel instead? - if (container instanceof Nameable) { - Text name = ((Nameable)container).getDisplayName(); - font.draw(name.asFormattedString(), left, top, container.getTitleColor()); - } else if (getTitle() != null) { - font.draw(getTitle().asFormattedString(), left, top, container.getTitleColor()); + if (getTitle() != null) { + font.draw(getTitle().asFormattedString(), left, top, description.getTitleColor()); } } + @SuppressWarnings("deprecation") @Override - protected void drawForeground(int mouseX, int mouseY) { - if (this.container==null) { - System.out.println("CONTAINER IS NULL."); - return; - } + public void render(int mouseX, int mouseY, float partialTicks) { + paint(mouseX, mouseY); + + super.render(mouseX, mouseY, partialTicks); + GuiLighting.disable(); //Needed because super.render leaves dirty state - if (this.container.getRootPanel()!=null) { - this.container.getRootPanel().paintForeground(0, 0, mouseX-left, mouseY-top); + if (description!=null) { + WPanel root = description.getRootPanel(); + if (root!=null) { + root.paintForeground(left, top, mouseX, mouseY); + + WWidget hitChild = root.hit(mouseX-left, mouseY-top); + if (hitChild!=null) hitChild.renderTooltip(left, top, mouseX-left, mouseY-top); + } } + + drawMouseoverTooltip(mouseX, mouseY); //Draws the itemstack tooltips } @Override - public void render(int mouseX, int mouseY, float partialTicks) { - // Render the background shadow - this.renderBackground(); - - this.drawBackground(partialTicks, mouseX, mouseY); - - super.render(mouseX, mouseY, partialTicks); - drawMouseoverTooltip(mouseX, mouseY); + public void tick() { + super.tick(); + if (description!=null) { + WPanel root = description.getRootPanel(); + if (root!=null) { + root.tick(); + } + } } - } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java index 9c44e35..1973999 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ScreenDrawing.java @@ -204,10 +204,6 @@ public class ScreenDrawing { TextRenderer render = MinecraftClient.getInstance().getFontManager().getTextRenderer(MinecraftClient.DEFAULT_TEXT_RENDERER_ID); render.drawWithShadow(s, (float)(x - render.getStringWidth(s) / 2), (float)y, color); } - - public static void drawTooltip(String s, int x, int y) { - - } public static int colorAtOpacity(int opaque, float opacity) { if (opacity<0.0f) opacity=0.0f; diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 960a6e6..33ca28c 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -155,9 +155,15 @@ public abstract class WPanel extends WWidget { @Environment(EnvType.CLIENT) @Override + @Deprecated public void paintForeground(int x, int y, int mouseX, int mouseY) { for(WWidget child : children) { child.paintForeground(x + child.getX(), y + child.getY(), mouseX, mouseY); } } + + @Override + public void tick() { + for(WWidget child : children) child.tick(); + } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index 1314f88..1bbbf85 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -68,18 +68,6 @@ public class WWidget { } /** - * Draw this Widget at the specified coordinates. The coordinates provided are the top-level device coordinates of - * this widget's topleft corner, so don't translate by the widget X/Y! That's already been done. Your "valid" - * drawing space is from (x, y) to (x + width - 1, y + height - 1) inclusive. However, no scissor or depth masking - * is done, so please take care to respect your boundaries. - * @param x The X coordinate of the leftmost pixels of this widget in device (opengl) coordinates - * @param y The Y coordinate of the topmost pixels of this widget in device (opengl) coordinates - */ - public void paint(int x, int y) { - - } - - /** * Notifies this widget that the mouse has been pressed while inside its bounds * @param x The X coordinate of the event, in widget-space (0 is the left edge of this widget) * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) @@ -185,11 +173,12 @@ public class WWidget { public void paintBackground(int x, int y) { } + @Deprecated @Environment(EnvType.CLIENT) public void paintForeground(int x, int y, int mouseX, int mouseY) { - if (mouseX >= x && mouseX < x+getWidth() && mouseY >= y && mouseY < y+getHeight()) { - renderTooltip(mouseX, mouseY); - } + //if (mouseX >= x && mouseX < x+getWidth() && mouseY >= y && mouseY < y+getHeight()) { + // renderTooltip(mouseX, mouseY); + //} } public boolean isWithinBounds(int x, int y) { @@ -197,14 +186,11 @@ public class WWidget { } /** - * Internal method to conditionally render tooltip data. This requires an overriden {@link #addInformation(List) + * Internal method to render tooltip data. This requires an overriden {@link #addInformation(List) * addInformation} method to insert data into the tooltip - without this, the method returns early, because no work - * is needing to be done on an empty list. - * @param tX The adjusted X coordinate at which to render the tooltip. - * @param tY The adjusted X coordinate at which to render the tooltip. */ @Environment(EnvType.CLIENT) - protected void renderTooltip(int tX, int tY) { + public void renderTooltip(int x, int y, int tX, int tY) { List<String> info = new ArrayList<>(); addInformation(info); @@ -212,7 +198,7 @@ public class WWidget { return; Screen screen = MinecraftClient.getInstance().currentScreen; - screen.renderTooltip(info, tX, tY); + screen.renderTooltip(info, tX+x, tY+y); } /** @@ -224,8 +210,7 @@ public class WWidget { } /** - * Adds information to this widget's tooltip. This requires a call to {@link #setRenderTooltip(boolean) - * setRenderTooltip} (obviously passing in {@code true}), in order to enable the rendering of your tooltip. + * Adds information to this widget's tooltip. If information remains empty after this call, no tooltip will be drawn. * @param information List containing all previous tooltip data. */ public void addInformation(List<String> information) { @@ -237,4 +222,6 @@ public class WWidget { public WWidget hit(int x, int y) { return this; } + + public void tick() {} } |