|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2025 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.clickgui.components; |
| 9 | + |
| 10 | +import org.lwjgl.glfw.GLFW; |
| 11 | + |
| 12 | +import net.minecraft.client.font.TextRenderer; |
| 13 | +import net.minecraft.client.gui.Click; |
| 14 | +import net.minecraft.client.gui.DrawContext; |
| 15 | +import net.wurstclient.clickgui.ClickGui; |
| 16 | +import net.wurstclient.clickgui.ClickGuiIcons; |
| 17 | +import net.wurstclient.clickgui.Component; |
| 18 | +import net.wurstclient.settings.PlantTypeSetting; |
| 19 | +import net.wurstclient.util.RenderUtils; |
| 20 | +import net.wurstclient.util.text.WText; |
| 21 | + |
| 22 | +public final class PlantTypeComponent extends Component |
| 23 | +{ |
| 24 | + private static final ClickGui GUI = WURST.getGui(); |
| 25 | + private static final TextRenderer TR = MC.textRenderer; |
| 26 | + private static final int BOX_SIZE = 11; |
| 27 | + private static final int ICON_SIZE = 24; |
| 28 | + private static final String HARVEST = "Harvest"; |
| 29 | + private static final String REPLANT = "Replant"; |
| 30 | + |
| 31 | + private final PlantTypeSetting setting; |
| 32 | + |
| 33 | + public PlantTypeComponent(PlantTypeSetting setting) |
| 34 | + { |
| 35 | + this.setting = setting; |
| 36 | + setWidth(getDefaultWidth()); |
| 37 | + setHeight(getDefaultHeight()); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void handleMouseClick(double mouseX, double mouseY, int mouseButton, |
| 42 | + Click context) |
| 43 | + { |
| 44 | + if(mouseX < getX() + ICON_SIZE) |
| 45 | + return; |
| 46 | + |
| 47 | + if(mouseY < getY() + getHeight() - BOX_SIZE) |
| 48 | + return; |
| 49 | + |
| 50 | + boolean hHarvest = |
| 51 | + mouseX < getX() + ICON_SIZE + BOX_SIZE + TR.getWidth(HARVEST) + 4; |
| 52 | + |
| 53 | + switch(mouseButton) |
| 54 | + { |
| 55 | + case GLFW.GLFW_MOUSE_BUTTON_LEFT: |
| 56 | + if(hHarvest) |
| 57 | + setting.toggleHarvestingEnabled(); |
| 58 | + else |
| 59 | + setting.toggleReplantingEnabled(); |
| 60 | + break; |
| 61 | + |
| 62 | + case GLFW.GLFW_MOUSE_BUTTON_RIGHT: |
| 63 | + if(hHarvest) |
| 64 | + setting.resetHarvestingEnabled(); |
| 65 | + else |
| 66 | + setting.resetReplantingEnabled(); |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void render(DrawContext context, int mouseX, int mouseY, |
| 73 | + float partialTicks) |
| 74 | + { |
| 75 | + int harvestWidth = TR.getWidth(HARVEST); |
| 76 | + |
| 77 | + int x1 = getX(); |
| 78 | + int x2 = x1 + getWidth(); |
| 79 | + int x3 = x1 + ICON_SIZE; |
| 80 | + int x4 = x3 + BOX_SIZE; |
| 81 | + int x5 = x4 + harvestWidth + 4; |
| 82 | + int x6 = x5 + BOX_SIZE; |
| 83 | + int y1 = getY(); |
| 84 | + int y2 = y1 + getHeight(); |
| 85 | + int y3 = y2 - BOX_SIZE; |
| 86 | + |
| 87 | + boolean hovering = isHovering(mouseX, mouseY); |
| 88 | + boolean hIcon = hovering && mouseX < x3; |
| 89 | + boolean hName = hovering && mouseX >= x3 && mouseY < y3; |
| 90 | + boolean hHarvest = |
| 91 | + hovering && mouseX >= x3 && mouseX < x5 && mouseY >= y3; |
| 92 | + boolean hReplant = hovering && mouseX >= x5 && mouseY >= y3; |
| 93 | + |
| 94 | + if(hIcon) |
| 95 | + GUI.setTooltip(setting.getIcon().getName().getString()); |
| 96 | + else if(hName) |
| 97 | + GUI.setTooltip(setting.getWrappedDescription(200)); |
| 98 | + else if(hHarvest) |
| 99 | + GUI.setTooltip("" + WText.translated("gui.wurst.autofarm.harvest")); |
| 100 | + else if(hReplant) |
| 101 | + GUI.setTooltip("" + WText.translated("gui.wurst.autofarm.replant")); |
| 102 | + |
| 103 | + // background |
| 104 | + int bgColor = getFillColor(false); |
| 105 | + context.fill(x1, y1, x2, y3, bgColor); |
| 106 | + context.fill(x1, y3, x3, y2, bgColor); |
| 107 | + context.fill(x4, y3, x5, y2, bgColor); |
| 108 | + context.fill(x6, y3, x2, y2, bgColor); |
| 109 | + |
| 110 | + // icon |
| 111 | + RenderUtils.drawItem(context, setting.getIcon(), x1, y1, true); |
| 112 | + |
| 113 | + // boxes |
| 114 | + context.fill(x3, y3, x4, y2, getFillColor(hHarvest)); |
| 115 | + context.fill(x5, y3, x6, y2, getFillColor(hReplant)); |
| 116 | + int outlineColor = RenderUtils.toIntColor(GUI.getAcColor(), 0.5F); |
| 117 | + RenderUtils.drawBorder2D(context, x3, y3, x4, y2, outlineColor); |
| 118 | + RenderUtils.drawBorder2D(context, x5, y3, x6, y2, outlineColor); |
| 119 | + |
| 120 | + // checks |
| 121 | + if(setting.isHarvestingEnabled()) |
| 122 | + ClickGuiIcons.drawCheck(context, x3, y3, x4, y2, hHarvest, false); |
| 123 | + if(setting.isReplantingEnabled()) |
| 124 | + ClickGuiIcons.drawCheck(context, x5, y3, x6, y2, hReplant, false); |
| 125 | + |
| 126 | + // text |
| 127 | + String name = setting.getName(); |
| 128 | + context.drawText(TR, name, x3 + 2, y1 + 3, GUI.getTxtColor(), false); |
| 129 | + context.drawText(TR, HARVEST, x4 + 2, y3 + 2, GUI.getTxtColor(), false); |
| 130 | + context.drawText(TR, REPLANT, x6 + 2, y3 + 2, GUI.getTxtColor(), false); |
| 131 | + } |
| 132 | + |
| 133 | + private int getFillColor(boolean hovering) |
| 134 | + { |
| 135 | + float opacity = GUI.getOpacity() * (hovering ? 1.5F : 1); |
| 136 | + return RenderUtils.toIntColor(GUI.getBgColor(), opacity); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int getDefaultWidth() |
| 141 | + { |
| 142 | + int nameWidth = TR.getWidth(setting.getName()); |
| 143 | + int boxesWidth = |
| 144 | + 2 * BOX_SIZE + TR.getWidth(HARVEST) + TR.getWidth(REPLANT) + 6; |
| 145 | + return ICON_SIZE + Math.max(nameWidth, boxesWidth); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int getDefaultHeight() |
| 150 | + { |
| 151 | + return ICON_SIZE; |
| 152 | + } |
| 153 | +} |
0 commit comments