/
GuiIconToggleButton.java
67 lines (53 loc) · 2.18 KB
/
GuiIconToggleButton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package mezz.jei.gui.elements;
import com.mojang.blaze3d.matrix.MatrixStack;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.renderer.Rectangle2d;
import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.config.Constants;
import mezz.jei.gui.HoverChecker;
import mezz.jei.gui.TooltipRenderer;
import net.minecraft.util.text.ITextComponent;
public abstract class GuiIconToggleButton {
private final IDrawable offIcon;
private final IDrawable onIcon;
private final GuiIconButton button;
private final HoverChecker hoverChecker;
public GuiIconToggleButton(IDrawable offIcon, IDrawable onIcon) {
this.offIcon = offIcon;
this.onIcon = onIcon;
this.button = new GuiIconButton(new DrawableBlank(0, 0), b -> {
});
this.hoverChecker = new HoverChecker();
this.hoverChecker.updateBounds(this.button);
}
public void updateBounds(Rectangle2d area) {
this.button.setWidth(area.getWidth());
this.button.setHeight(area.getHeight());
this.button.x = area.getX();
this.button.y = area.getY();
this.hoverChecker.updateBounds(this.button);
}
public void draw(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
this.button.render(matrixStack, mouseX, mouseY, partialTicks);
IDrawable icon = isIconToggledOn() ? this.onIcon : this.offIcon;
icon.draw(matrixStack, this.button.x + 2, this.button.y + 2);
}
public final boolean isMouseOver(double mouseX, double mouseY) {
return this.hoverChecker.checkHover(mouseX, mouseY);
}
public final boolean handleMouseClick(double mouseX, double mouseY, int mouseButton) {
return button.mouseClicked(mouseX, mouseY, mouseButton) &&
onMouseClicked(mouseX, mouseY, mouseButton);
}
public final void drawTooltips(MatrixStack matrixStack, int mouseX, int mouseY) {
if (isMouseOver(mouseX, mouseY)) {
List<ITextComponent> tooltip = new ArrayList<>();
getTooltips(tooltip);
TooltipRenderer.drawHoveringText(tooltip, mouseX, mouseY, Constants.MAX_TOOLTIP_WIDTH, matrixStack);
}
}
protected abstract void getTooltips(List<ITextComponent> tooltip);
protected abstract boolean isIconToggledOn();
protected abstract boolean onMouseClicked(double mouseX, double mouseY, int mouseButton);
}