/
BookmarkOverlay.java
196 lines (168 loc) · 6.17 KB
/
BookmarkOverlay.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package mezz.jei.gui.overlay.bookmarks;
import com.mojang.blaze3d.matrix.MatrixStack;
import javax.annotation.Nullable;
import java.util.Set;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.Rectangle2d;
import net.minecraft.client.util.InputMappings;
import net.minecraft.item.ItemStack;
import mezz.jei.api.runtime.IBookmarkOverlay;
import mezz.jei.bookmarks.BookmarkList;
import mezz.jei.config.ClientConfig;
import mezz.jei.config.IWorldConfig;
import mezz.jei.gui.elements.GuiIconToggleButton;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.gui.overlay.IngredientGrid;
import mezz.jei.gui.overlay.IngredientGridWithNavigation;
import mezz.jei.gui.recipes.RecipesGui;
import mezz.jei.gui.textures.Textures;
import mezz.jei.input.IClickedIngredient;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.util.CommandUtil;
import mezz.jei.util.MathUtil;
import org.lwjgl.glfw.GLFW;
public class BookmarkOverlay implements IShowsRecipeFocuses, ILeftAreaContent, IBookmarkOverlay {
private static final int BUTTON_SIZE = 20;
// areas
private Rectangle2d parentArea = new Rectangle2d(0, 0, 0, 0);
private Rectangle2d displayArea = new Rectangle2d(0, 0, 0, 0);
// display elements
private final IngredientGridWithNavigation contents;
private final GuiIconToggleButton bookmarkButton;
// visibility
private boolean hasRoom = false;
// data
private final BookmarkList bookmarkList;
private final IWorldConfig worldConfig;
public BookmarkOverlay(BookmarkList bookmarkList, Textures textures, IngredientGridWithNavigation contents, IWorldConfig worldConfig) {
this.bookmarkList = bookmarkList;
this.worldConfig = worldConfig;
this.bookmarkButton = BookmarkButton.create(this, bookmarkList, textures, worldConfig);
this.contents = contents;
bookmarkList.addListener(() -> contents.updateLayout(false));
}
public boolean isListDisplayed() {
return worldConfig.isBookmarkOverlayEnabled() && hasRoom && !bookmarkList.isEmpty();
}
public boolean hasRoom() {
return hasRoom;
}
@Override
public void updateBounds(Rectangle2d area, Set<Rectangle2d> guiExclusionAreas) {
this.parentArea = area;
hasRoom = updateBounds(guiExclusionAreas);
}
@Override
public void drawScreen(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
if (this.isListDisplayed()) {
this.contents.draw(minecraft, matrixStack, mouseX, mouseY, partialTicks);
}
this.bookmarkButton.draw(matrixStack, mouseX, mouseY, partialTicks);
}
@Override
public void drawOnForeground(ContainerScreen<?> gui, int mouseX, int mouseY) {
}
@Override
public void drawTooltips(Minecraft minecraft, MatrixStack matrixStack, int mouseX, int mouseY) {
if (isListDisplayed()) {
this.contents.drawTooltips(minecraft, matrixStack, mouseX, mouseY);
}
bookmarkButton.drawTooltips(matrixStack, mouseX, mouseY);
}
private static int getMinWidth() {
return Math.max(4 * BUTTON_SIZE, ClientConfig.smallestNumColumns * IngredientGrid.INGREDIENT_WIDTH);
}
public boolean updateBounds(Set<Rectangle2d> guiExclusionAreas) {
displayArea = parentArea;
final int minWidth = getMinWidth();
if (displayArea.getWidth() < minWidth) {
return false;
}
Rectangle2d availableContentsArea = new Rectangle2d(
displayArea.getX(),
displayArea.getY(),
displayArea.getWidth(),
displayArea.getHeight() - (BUTTON_SIZE + 4)
);
boolean contentsHasRoom = this.contents.updateBounds(availableContentsArea, guiExclusionAreas, minWidth);
// update area to match contents size
Rectangle2d contentsArea = this.contents.getArea();
displayArea = new Rectangle2d(
contentsArea.getX(),
displayArea.getY(),
contentsArea.getWidth(),
displayArea.getHeight()
);
this.bookmarkButton.updateBounds(new Rectangle2d(
displayArea.getX(),
(int) Math.floor(displayArea.getY() + displayArea.getHeight()) - BUTTON_SIZE - 2,
BUTTON_SIZE,
BUTTON_SIZE
));
this.contents.updateLayout(false);
return contentsHasRoom;
}
@Override
@Nullable
public IClickedIngredient<?> getIngredientUnderMouse(double mouseX, double mouseY) {
if (isListDisplayed()) {
return this.contents.getIngredientUnderMouse(mouseX, mouseY);
}
return null;
}
@Override
public boolean canSetFocusWithMouse() {
return this.isListDisplayed() && this.contents.canSetFocusWithMouse();
}
@Override
public boolean handleMouseScrolled(double mouseX, double mouseY, double scrollDelta) {
return isListDisplayed() &&
MathUtil.contains(displayArea, mouseX, mouseY) &&
this.contents.handleMouseScrolled(mouseX, mouseY, scrollDelta);
}
@Override
public boolean handleMouseClicked(double mouseX, double mouseY, int mouseButton) {
if (isListDisplayed()) {
if (MathUtil.contains(displayArea, mouseX, mouseY)) {
Minecraft minecraft = Minecraft.getInstance();
Screen currentScreen = minecraft.currentScreen;
InputMappings.Input input = InputMappings.Type.MOUSE.getOrMakeInput(mouseButton);
if (currentScreen != null &&
!(currentScreen instanceof RecipesGui) &&
(mouseButton == GLFW.GLFW_MOUSE_BUTTON_1 || mouseButton == GLFW.GLFW_MOUSE_BUTTON_2 || minecraft.gameSettings.keyBindPickBlock.isActiveAndMatches(input))) {
IClickedIngredient<?> clicked = getIngredientUnderMouse(mouseX, mouseY);
if (clicked != null) {
if (worldConfig.isCheatItemsEnabled()) {
ItemStack itemStack = clicked.getCheatItemStack();
if (!itemStack.isEmpty()) {
CommandUtil.giveStack(itemStack, input);
}
clicked.onClickHandled();
return true;
}
}
}
}
if (contents.isMouseOver(mouseX, mouseY)) {
this.contents.handleMouseClicked(mouseX, mouseY, mouseButton);
}
}
if (bookmarkButton.isMouseOver(mouseX, mouseY)) {
return bookmarkButton.handleMouseClick(mouseX, mouseY, mouseButton);
}
return false;
}
@Nullable
@Override
public Object getIngredientUnderMouse() {
if (isListDisplayed()) {
IIngredientListElement<?> elementUnderMouse = this.contents.getElementUnderMouse();
if (elementUnderMouse != null) {
return elementUnderMouse.getIngredient();
}
}
return null;
}
}