|
| 1 | +package com.coflnet.gui.cofl; |
| 2 | + |
| 3 | +import com.coflnet.gui.RenderUtils; |
| 4 | +import com.coflnet.gui.widget.ItemWidget; |
| 5 | +import net.minecraft.client.MinecraftClient; |
| 6 | +import net.minecraft.client.gui.DrawContext; |
| 7 | +import net.minecraft.client.gui.screen.Screen; |
| 8 | +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; |
| 9 | +import net.minecraft.client.gui.widget.ClickableWidget; |
| 10 | +import net.minecraft.client.gui.widget.ScrollableTextWidget; |
| 11 | +import net.minecraft.client.gui.widget.TextWidget; |
| 12 | +import net.minecraft.inventory.Inventory; |
| 13 | +import net.minecraft.inventory.InventoryChangedListener; |
| 14 | +import net.minecraft.item.Item; |
| 15 | +import net.minecraft.item.ItemStack; |
| 16 | +import net.minecraft.item.Items; |
| 17 | +import net.minecraft.screen.GenericContainerScreenHandler; |
| 18 | +import net.minecraft.screen.ScreenHandler; |
| 19 | +import net.minecraft.screen.ScreenHandlerListener; |
| 20 | +import net.minecraft.text.Text; |
| 21 | + |
| 22 | +public class CoflBinGUI extends Screen implements InventoryChangedListener { |
| 23 | + private int width; |
| 24 | + private int height; |
| 25 | + private int p; |
| 26 | + private int r; |
| 27 | + public GenericContainerScreenHandler gcsh; |
| 28 | + public Item item = Items.AIR; |
| 29 | + public String title = ""; |
| 30 | + public String lore = RenderUtils.lorem(); |
| 31 | + public String buttonRText = ""; |
| 32 | + |
| 33 | + private TextWidget titleTextWidget; |
| 34 | + private ItemWidget itemWidget; |
| 35 | + private ScrollableTextWidget loreScrollableTextWidget; |
| 36 | + private ClickableWidget rightClickableWidget; |
| 37 | + private ClickableWidget leftClickableWidget; |
| 38 | + |
| 39 | + public CoflBinGUI(Item item, GenericContainerScreenHandler gcsh){ |
| 40 | + super(Text.literal("Cofl Bin Gui")); |
| 41 | + |
| 42 | + int screenWidth = MinecraftClient.getInstance().currentScreen.width; |
| 43 | + int screenHeight = MinecraftClient.getInstance().currentScreen.height; |
| 44 | + |
| 45 | + this.item = item; |
| 46 | + this.gcsh = gcsh; |
| 47 | + |
| 48 | + this.width = screenWidth / 2; |
| 49 | + if (width < 300) this.width = 300; |
| 50 | + |
| 51 | + this.height = screenHeight / 3 * 2; |
| 52 | + if (height < 225) this.height = 225; |
| 53 | + |
| 54 | + this.p = 5; |
| 55 | + this.r = 4; |
| 56 | + |
| 57 | + gcsh.addListener(new ScreenHandlerListener() { |
| 58 | + @Override |
| 59 | + public void onSlotUpdate(ScreenHandler handler, int slotId, ItemStack stack) { |
| 60 | + if (stack.getItem() != Items.AIR) System.out.println("slotid: "+slotId); |
| 61 | + switch (slotId){ |
| 62 | + case 13: |
| 63 | + setItem(stack.getItem()); |
| 64 | + break; |
| 65 | + case 31: |
| 66 | + break; |
| 67 | + case 41: |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onPropertyUpdate(ScreenHandler handler, int property, int value) {} |
| 74 | + }); |
| 75 | + |
| 76 | + leftClickableWidget = new ClickableWidget( |
| 77 | + screenWidth / 2 - width / 2 + p, |
| 78 | + screenHeight / 2 + height / 2 - p - (225 - 150 - 12 - p * 5) - screenHeight / 15, |
| 79 | + width / 5 * 2 - p, |
| 80 | + 225 - 150 - 12 - p*5 + screenHeight / 15, |
| 81 | + Text.of("Cancel") |
| 82 | + ){ |
| 83 | + @Override |
| 84 | + protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) { |
| 85 | + RenderUtils.drawRoundedRect(context, getX(), getY(), getWidth(), getHeight(), r, CoflColConfig.CANCEL); |
| 86 | + RenderUtils.drawString(context, this.getMessage().getLiteralString(), getX() + 6, getY() + 4, 0xFFEEEEEE); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected void appendClickableNarrations(NarrationMessageBuilder builder) {} |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onClick(double mouseX, double mouseY) {close();} |
| 94 | + }; |
| 95 | + |
| 96 | + rightClickableWidget = new ClickableWidget( |
| 97 | + screenWidth / 2 - width / 2, //screenWidth / 2 - width / 2 + p + width / 5 * 2, |
| 98 | + screenHeight / 2 - height / 2, //screenHeight / 2 + height / 2 - p - (225 - 150 - 12 - p*5) - screenHeight / 15, |
| 99 | + width, //width / 5 * 3 - p*2, |
| 100 | + height, //225 - 150 - 12 - p*5 + screenHeight / 15, |
| 101 | + Text.of(buttonRText) |
| 102 | + ){ |
| 103 | + @Override |
| 104 | + protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) { |
| 105 | + RenderUtils.drawRoundedRect(context, |
| 106 | + screenWidth / 2 - width / 2 + p + width / 5 * 2, |
| 107 | + screenHeight / 2 + height / 2 - p - (225 - 150 - 12 - p*5) - screenHeight / 15, |
| 108 | + width / 5 * 3 - p*2, |
| 109 | + 225 - 150 - 12 - p*5 + screenHeight / 15, |
| 110 | + r, CoflColConfig.CONFIRM |
| 111 | + ); |
| 112 | + RenderUtils.drawString(context, |
| 113 | + this.getMessage().getString(), |
| 114 | + screenWidth / 2 - width / 2 + p + width / 5 * 2 + 6, |
| 115 | + screenHeight / 2 + height / 2 - p - (225 - 150 - 12 - p*5) - screenHeight / 15 + 4, |
| 116 | + 0xFFEEEEEE |
| 117 | + ); |
| 118 | + } |
| 119 | + @Override |
| 120 | + protected void appendClickableNarrations(NarrationMessageBuilder builder) {} |
| 121 | + |
| 122 | + @Override |
| 123 | + public void onClick(double mouseX, double mouseY) { |
| 124 | + if (leftClickableWidget.isMouseOver(mouseX, mouseY)) { |
| 125 | + leftClickableWidget.onClick(mouseX, mouseY); |
| 126 | + } else MinecraftClient.getInstance().player.sendMessage(Text.of("Confim clicked")); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + titleTextWidget = new TextWidget( |
| 131 | + screenWidth / 2 - width / 2 + p + 3, |
| 132 | + screenHeight / 2 - height / 2 + p + 2, |
| 133 | + width - p*2 - 4, 10, |
| 134 | + Text.of(title), |
| 135 | + MinecraftClient.getInstance().textRenderer |
| 136 | + ).alignLeft(); |
| 137 | + |
| 138 | + loreScrollableTextWidget = new ScrollableTextWidget( |
| 139 | + screenWidth / 2 - width / 2 + p + 20 + p + 4, |
| 140 | + screenHeight / 2 - height / 2 + p + 12 + p + 2, |
| 141 | + width - 20 - p*4 - 4, height - 75 - 2 - screenHeight / 15, |
| 142 | + Text.of(lore), MinecraftClient.getInstance().textRenderer |
| 143 | + ){ |
| 144 | + @Override |
| 145 | + protected void drawBox(DrawContext context) {} |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onClick(double mouseX, double mouseY) { |
| 149 | + super.onClick(mouseX, mouseY); |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + itemWidget = new ItemWidget( |
| 154 | + screenWidth / 2 - width / 2 + p + 2, |
| 155 | + screenHeight / 2 - height / 2 + p + 12 + p + 2, |
| 156 | + this.item.getDefaultStack() |
| 157 | + ); |
| 158 | + |
| 159 | + this.addDrawableChild(titleTextWidget); |
| 160 | + this.addDrawableChild(loreScrollableTextWidget); |
| 161 | + this.addDrawableChild(itemWidget); |
| 162 | + this.addDrawableChild(rightClickableWidget); |
| 163 | + this.addDrawableChild(leftClickableWidget); |
| 164 | + } |
| 165 | + |
| 166 | + public void setItem(Item item) { |
| 167 | + this.item = item; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public boolean shouldPause() { |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void render(DrawContext context, int mouseX, int mouseY, float delta) { |
| 177 | + super.render(context, mouseX, mouseY, delta); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void renderBackground(DrawContext drawContext, int mouseX, int mouseY, float delta) { |
| 182 | + int screenWidth = MinecraftClient.getInstance().currentScreen.width; |
| 183 | + int screenHeight = MinecraftClient.getInstance().currentScreen.height; |
| 184 | + |
| 185 | + // Background |
| 186 | + RenderUtils.drawRoundedRect(drawContext, screenWidth / 2 - width / 2,screenHeight / 2 - height / 2, width, height, r, CoflColConfig.BACKGROUND_PRIMARY); |
| 187 | + |
| 188 | + // Title Background |
| 189 | + RenderUtils.drawRoundedRect(drawContext, screenWidth / 2 - width / 2 + p,screenHeight / 2 - height / 2 + p, width - p*2, 12, r, CoflColConfig.BACKGROUND_SECONDARY); |
| 190 | + |
| 191 | + // Item Background |
| 192 | + RenderUtils.drawRoundedRect(drawContext, screenWidth / 2 - width / 2 + p,screenHeight / 2 - height / 2 + p + 12 + p, 20, 20, r, CoflColConfig.BACKGROUND_SECONDARY); |
| 193 | + |
| 194 | + // Description Background |
| 195 | + RenderUtils.drawRoundedRect(drawContext,screenWidth / 2 - width / 2 + p + 20 + p, screenHeight / 2 - height / 2 + p + 12+ p, width - 20 - p*3, height - 75 - screenHeight / 15, r, CoflColConfig.BACKGROUND_SECONDARY); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void onInventoryChanged(Inventory sender) { |
| 200 | + System.out.println("Inv changed"); |
| 201 | + } |
| 202 | +} |
0 commit comments