/
BookmarkOverlay.java
210 lines (184 loc) · 7.19 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package mezz.jei.gui.overlay.bookmarks;
import com.mojang.blaze3d.vertex.PoseStack;
import mezz.jei.api.gui.handlers.IGuiProperties;
import mezz.jei.api.ingredients.IIngredientType;
import mezz.jei.api.ingredients.ITypedIngredient;
import mezz.jei.api.runtime.IBookmarkOverlay;
import mezz.jei.api.runtime.IScreenHelper;
import mezz.jei.common.gui.textures.Textures;
import mezz.jei.common.input.IClickableIngredientInternal;
import mezz.jei.common.input.IInternalKeyMappings;
import mezz.jei.common.network.IConnectionToServer;
import mezz.jei.common.util.ImmutableRect2i;
import mezz.jei.core.config.IWorldConfig;
import mezz.jei.gui.bookmarks.BookmarkList;
import mezz.jei.gui.config.IClientConfig;
import mezz.jei.gui.elements.GuiIconToggleButton;
import mezz.jei.gui.input.IDragHandler;
import mezz.jei.gui.input.IRecipeFocusSource;
import mezz.jei.gui.input.IUserInputHandler;
import mezz.jei.gui.input.MouseUtil;
import mezz.jei.gui.input.handlers.CheatInputHandler;
import mezz.jei.gui.input.handlers.CombinedInputHandler;
import mezz.jei.gui.input.handlers.NullDragHandler;
import mezz.jei.gui.input.handlers.ProxyDragHandler;
import mezz.jei.gui.input.handlers.ProxyInputHandler;
import mezz.jei.gui.overlay.IngredientGridWithNavigation;
import mezz.jei.gui.overlay.ScreenPropertiesCache;
import mezz.jei.gui.util.CheatUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
public class BookmarkOverlay implements IRecipeFocusSource, IBookmarkOverlay {
private static final int BORDER_MARGIN = 6;
private static final int INNER_PADDING = 2;
private static final int BUTTON_SIZE = 20;
// input
private final CheatInputHandler cheatInputHandler;
// areas
private final ScreenPropertiesCache screenPropertiesCache;
// display elements
private final IngredientGridWithNavigation contents;
private final GuiIconToggleButton bookmarkButton;
// data
private final BookmarkList bookmarkList;
private final IWorldConfig worldConfig;
public BookmarkOverlay(
BookmarkList bookmarkList,
Textures textures,
IngredientGridWithNavigation contents,
IClientConfig clientConfig,
IWorldConfig worldConfig,
IScreenHelper screenHelper,
IConnectionToServer serverConnection,
IInternalKeyMappings keyBindings,
CheatUtil cheatUtil
) {
this.bookmarkList = bookmarkList;
this.worldConfig = worldConfig;
this.bookmarkButton = BookmarkButton.create(this, bookmarkList, textures, worldConfig, keyBindings);
this.cheatInputHandler = new CheatInputHandler(this, worldConfig, clientConfig, serverConnection, cheatUtil);
this.contents = contents;
this.screenPropertiesCache = new ScreenPropertiesCache(screenHelper);
bookmarkList.addSourceListChangedListener(() -> {
worldConfig.setBookmarkEnabled(!bookmarkList.isEmpty());
Minecraft minecraft = Minecraft.getInstance();
Screen screen = minecraft.screen;
this.updateScreen(screen, null);
});
}
public boolean isListDisplayed() {
return worldConfig.isBookmarkOverlayEnabled() &&
screenPropertiesCache.hasValidScreen() &&
contents.hasRoom() &&
!bookmarkList.isEmpty();
}
public boolean hasRoom() {
return contents.hasRoom();
}
public void updateScreen(@Nullable Screen guiScreen, @Nullable Set<ImmutableRect2i> updatedGuiExclusionAreas) {
this.screenPropertiesCache.updateScreen(guiScreen, updatedGuiExclusionAreas, this::onScreenPropertiesChanged);
}
private void onScreenPropertiesChanged() {
this.screenPropertiesCache.getGuiProperties()
.ifPresentOrElse(guiProperties -> {
Set<ImmutableRect2i> guiExclusionAreas = this.screenPropertiesCache.getGuiExclusionAreas();
updateBounds(guiProperties, guiExclusionAreas);
}, this.contents::close);
}
private void updateBounds(IGuiProperties guiProperties, Set<ImmutableRect2i> guiExclusionAreas) {
ImmutableRect2i displayArea = new ImmutableRect2i(0, 0, guiProperties.getGuiLeft(), guiProperties.getScreenHeight());
ImmutableRect2i availableContentsArea = displayArea.cropBottom(BUTTON_SIZE + INNER_PADDING);
this.contents.updateBounds(availableContentsArea, guiExclusionAreas);
this.contents.updateLayout(false);
if (contents.hasRoom()) {
ImmutableRect2i contentsArea = this.contents.getBackgroundArea();
ImmutableRect2i bookmarkButtonArea = displayArea
.insetBy(BORDER_MARGIN)
.matchWidthAndX(contentsArea)
.keepBottom(BUTTON_SIZE)
.keepLeft(BUTTON_SIZE);
this.bookmarkButton.updateBounds(bookmarkButtonArea);
} else {
ImmutableRect2i bookmarkButtonArea = displayArea
.insetBy(BORDER_MARGIN)
.keepBottom(BUTTON_SIZE)
.keepLeft(BUTTON_SIZE);
this.bookmarkButton.updateBounds(bookmarkButtonArea);
}
}
public void drawScreen(Minecraft minecraft, PoseStack poseStack, int mouseX, int mouseY, float partialTicks) {
if (isListDisplayed()) {
this.contents.draw(minecraft, poseStack, mouseX, mouseY, partialTicks);
}
if (this.screenPropertiesCache.hasValidScreen()) {
this.bookmarkButton.draw(poseStack, mouseX, mouseY, partialTicks);
}
}
public void drawTooltips(Minecraft minecraft, PoseStack poseStack, int mouseX, int mouseY) {
if (isListDisplayed()) {
this.contents.drawTooltips(minecraft, poseStack, mouseX, mouseY);
}
if (this.screenPropertiesCache.hasValidScreen()) {
bookmarkButton.drawTooltips(poseStack, mouseX, mouseY);
}
}
@Override
public Stream<IClickableIngredientInternal<?>> getIngredientUnderMouse(double mouseX, double mouseY) {
if (isListDisplayed()) {
return this.contents.getIngredientUnderMouse(mouseX, mouseY);
}
return Stream.empty();
}
@Override
public Optional<ITypedIngredient<?>> getIngredientUnderMouse() {
double mouseX = MouseUtil.getX();
double mouseY = MouseUtil.getY();
return getIngredientUnderMouse(mouseX, mouseY)
.<ITypedIngredient<?>>map(IClickableIngredientInternal::getTypedIngredient)
.findFirst();
}
@Nullable
@Override
public <T> T getIngredientUnderMouse(IIngredientType<T> ingredientType) {
double mouseX = MouseUtil.getX();
double mouseY = MouseUtil.getY();
return getIngredientUnderMouse(mouseX, mouseY)
.map(IClickableIngredientInternal::getTypedIngredient)
.map(i -> i.getIngredient(ingredientType))
.flatMap(Optional::stream)
.findFirst()
.orElse(null);
}
public IUserInputHandler createInputHandler() {
final IUserInputHandler bookmarkButtonInputHandler = this.bookmarkButton.createInputHandler();
final IUserInputHandler displayedInputHandler = new CombinedInputHandler(
this.cheatInputHandler,
this.contents.createInputHandler(),
bookmarkButtonInputHandler
);
return new ProxyInputHandler(() -> {
if (isListDisplayed()) {
return displayedInputHandler;
}
return bookmarkButtonInputHandler;
});
}
public IDragHandler createDragHandler() {
final IDragHandler displayedDragHandler = this.contents.createDragHandler();
return new ProxyDragHandler(() -> {
if (isListDisplayed()) {
return displayedDragHandler;
}
return NullDragHandler.INSTANCE;
});
}
public void drawOnForeground(Minecraft minecraft, PoseStack poseStack, int mouseX, int mouseY) {
if (isListDisplayed()) {
this.contents.drawOnForeground(minecraft, poseStack, mouseX, mouseY);
}
}
}