Skip to content

Commit 4f5ef0e

Browse files
committed
Remove some gui related capturing lambdas
1 parent f26d90a commit 4f5ef0e

File tree

3 files changed

+114
-26
lines changed

3 files changed

+114
-26
lines changed

src/main/java/mekanism/client/gui/GuiMekanism.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.util.Collection;
66
import java.util.List;
77
import java.util.Optional;
8+
import java.util.function.BiFunction;
89
import java.util.function.BooleanSupplier;
9-
import java.util.function.Function;
1010
import java.util.function.Supplier;
1111
import mekanism.api.text.ILangEntry;
1212
import mekanism.client.gui.element.GuiElement;
@@ -194,7 +194,8 @@ public ItemStack getCarriedItem() {
194194
}
195195

196196
@Nullable
197-
private ComponentPath handleNavigationWithWindows(Function<ContainerEventHandler, @Nullable ComponentPath> handleNavigation) {
197+
private <NAVIGATION extends FocusNavigationEvent> ComponentPath handleNavigationWithWindows(NAVIGATION navigation,
198+
BiFunction<ContainerEventHandler, NAVIGATION, @Nullable ComponentPath> handleNavigation) {
198199
GuiWindow topWindow = windows.head();
199200
List<GuiEventListener> combinedChildren;
200201
//Note: As allowContainer wise only allows interacting with slots, which we don't have navigation for, we check against allowAll.
@@ -239,7 +240,7 @@ public ScreenRectangle getRectangle() {
239240
return GuiMekanism.this.getRectangle();
240241
}
241242
};
242-
ComponentPath componentPath = handleNavigation.apply(handlerWithWindows);
243+
ComponentPath componentPath = handleNavigation.apply(handlerWithWindows, navigation);
243244
if (componentPath == null) {
244245
return null;
245246
} else if (componentPath instanceof ComponentPath.Path path) {
@@ -259,7 +260,7 @@ public ComponentPath handleTabNavigation(@NotNull FocusNavigationEvent.TabNaviga
259260
if (windows.isEmpty()) {
260261
return super.handleTabNavigation(navigation);
261262
}
262-
return handleNavigationWithWindows(handlerWithWindows -> handlerWithWindows.handleTabNavigation(navigation));
263+
return handleNavigationWithWindows(navigation, ContainerEventHandler::handleTabNavigation);
263264
}
264265

265266
@Nullable
@@ -268,7 +269,7 @@ public ComponentPath handleArrowNavigation(@NotNull FocusNavigationEvent.ArrowNa
268269
if (windows.isEmpty()) {
269270
return super.handleArrowNavigation(navigation);
270271
}
271-
return handleNavigationWithWindows(handlerWithWindows -> handlerWithWindows.handleArrowNavigation(navigation));
272+
return handleNavigationWithWindows(navigation, ContainerEventHandler::handleArrowNavigation);
272273
}
273274

274275
@Override
@@ -376,7 +377,7 @@ protected void renderLabels(@NotNull GuiGraphics guiGraphics, int mouseX, int mo
376377
// then render tooltips, translating above max z offset to prevent clashing
377378
GuiElement tooltipElement = getWindowHovering(mouseX, mouseY);
378379
if (tooltipElement == null) {
379-
tooltipElement = (GuiElement) GuiUtils.findChild(children(), child -> child instanceof GuiElement && child.isMouseOver(mouseX, mouseY));
380+
tooltipElement = (GuiElement) GuiUtils.findChild(children(), mouseX, mouseY, (child, x, y) -> child instanceof GuiElement && child.isMouseOver(x, y));
380381
}
381382

382383
// translate forwards using RenderSystem. this should never have to happen as we do all the necessary translations with MatrixStacks,
@@ -458,7 +459,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
458459
}
459460
// otherwise, we send it to the current element (this is the same as super.super [ContainerEventHandler#mouseClicked], but in reverse order)
460461
//TODO: Why do we do this in reverse order?
461-
GuiEventListener clickedChild = GuiUtils.findChild(children(), child -> child.mouseClicked(mouseX, mouseY, button));
462+
GuiEventListener clickedChild = GuiUtils.findChild(children(), mouseX, mouseY, button, GuiEventListener::mouseClicked);
462463
if (clickedChild != null) {
463464
setFocused(clickedChild);
464465
if (button == GLFW.GLFW_MOUSE_BUTTON_LEFT) {
@@ -491,7 +492,7 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
491492
return true;
492493
}
493494
}
494-
return GuiUtils.checkChildren(children(), child -> child instanceof GuiElement && child.keyPressed(keyCode, scanCode, modifiers)) ||
495+
return GuiUtils.checkChildren(children(), keyCode, scanCode, modifiers, (child, k, s, m) -> child instanceof GuiElement && child.keyPressed(k, s, m)) ||
495496
super.keyPressed(keyCode, scanCode, modifiers);
496497
}
497498

@@ -502,7 +503,7 @@ public boolean charTyped(char c, int keyCode) {
502503
return true;
503504
}
504505
}
505-
return GuiUtils.checkChildren(children(), child -> child instanceof GuiElement && child.charTyped(c, keyCode)) || super.charTyped(c, keyCode);
506+
return GuiUtils.checkChildrenChar(children(), c, keyCode, (child, ch, k) -> child instanceof GuiElement && child.charTyped(ch, k)) || super.charTyped(c, keyCode);
506507
}
507508

508509
/**
@@ -550,8 +551,12 @@ protected Slot findSlot(double mouseX, double mouseY) {
550551
if (overNoButtons && slot.isActive()) {
551552
if (window == null) {
552553
return slot;
553-
} else if (virtual && window.childrenContainsElement(element -> element instanceof GuiVirtualSlot v && v.isElementForSlot((IVirtualSlot) slot))) {
554-
return slot;
554+
} else if (virtual) {
555+
for (GuiElement child : window.children()) {
556+
if (child instanceof GuiVirtualSlot v && v.isElementForSlot((IVirtualSlot) slot)) {
557+
return slot;
558+
}
559+
}
555560
}
556561
}
557562
}
@@ -570,8 +575,14 @@ protected boolean isMouseOverSlot(@NotNull Slot slot, double mouseX, double mous
570575
if (super.isHovering(xPos, yPos, 16, 16, mouseX, mouseY)) {
571576
GuiWindow window = getWindowHovering(mouseX, mouseY);
572577
//If we are hovering over a window, check if the virtual slot is a child of the window
573-
if (window == null || window.childrenContainsElement(element -> element instanceof GuiVirtualSlot v && v.isElementForSlot(virtualSlot))) {
574-
return overNoButtons(window, mouseX, mouseY);
578+
if (window == null) {
579+
return overNoButtons((GuiWindow) null, mouseX, mouseY);
580+
} else {
581+
for (GuiElement child : window.children()) {
582+
if (child instanceof GuiVirtualSlot v && v.isElementForSlot(virtualSlot)) {
583+
return overNoButtons(window, mouseX, mouseY);
584+
}
585+
}
575586
}
576587
}
577588
}
@@ -582,14 +593,18 @@ protected boolean isMouseOverSlot(@NotNull Slot slot, double mouseX, double mous
582593

583594
private boolean overNoButtons(@Nullable GuiWindow window, double mouseX, double mouseY) {
584595
if (window == null) {
585-
for (GuiEventListener child : children()) {
586-
if (child.isMouseOver(mouseX, mouseY)) {
587-
return false;
588-
}
596+
return overNoButtons(children(), mouseX, mouseY);
597+
}
598+
return overNoButtons(window.children(), mouseX, mouseY);
599+
}
600+
601+
private static boolean overNoButtons(List<? extends GuiEventListener> children, double mouseX, double mouseY) {
602+
for (GuiEventListener child : children) {
603+
if (child.isMouseOver(mouseX, mouseY)) {
604+
return false;
589605
}
590-
return true;
591606
}
592-
return !window.childrenContainsElement(e -> e.isMouseOver(mouseX, mouseY));
607+
return true;
593608
}
594609

595610
private boolean isVirtualSlotAvailable(IVirtualSlot virtualSlot) {

src/main/java/mekanism/client/gui/GuiUtils.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,73 @@ public static <CHILD extends GuiEventListener> CHILD findChild(List<? extends CH
180180
return null;
181181
}
182182

183+
// reverse-order iteration over children w/ built-in GuiElement check, runs a basic anyMatch with checker
184+
public static <CHILD extends GuiEventListener> boolean checkChildren(List<? extends CHILD> children, double mouseX, double mouseY, MouseOverPredicate<CHILD> checker) {
185+
return findChild(children, mouseX, mouseY, checker) != null;
186+
}
187+
188+
@Nullable
189+
public static <CHILD extends GuiEventListener> CHILD findChild(List<? extends CHILD> children, double mouseX, double mouseY, MouseOverPredicate<CHILD> checker) {
190+
for (int i = children.size() - 1; i >= 0; i--) {
191+
CHILD child = children.get(i);
192+
if (checker.test(child, mouseX, mouseY)) {
193+
return child;
194+
}
195+
}
196+
return null;
197+
}
198+
199+
@Nullable
200+
public static <CHILD extends GuiEventListener> CHILD findChild(List<? extends CHILD> children, double mouseX, double mouseY, int button, MouseClickedPredicate<CHILD> checker) {
201+
for (int i = children.size() - 1; i >= 0; i--) {
202+
CHILD child = children.get(i);
203+
if (checker.test(child, mouseX, mouseY, button)) {
204+
return child;
205+
}
206+
}
207+
return null;
208+
}
209+
210+
public static <CHILD extends GuiEventListener> boolean checkChildren(List<? extends CHILD> children, int keyCode, int scanCode, int modifiers, KeyPressedPredicate<CHILD> checker) {
211+
for (int i = children.size() - 1; i >= 0; i--) {
212+
CHILD child = children.get(i);
213+
if (checker.test(child, keyCode, scanCode, modifiers)) {
214+
return true;
215+
}
216+
}
217+
return false;
218+
}
219+
220+
public static <CHILD extends GuiEventListener> boolean checkChildrenChar(List<? extends CHILD> children, char c, int keyCode, CharTypedPredicate<CHILD> checker) {
221+
for (int i = children.size() - 1; i >= 0; i--) {
222+
CHILD child = children.get(i);
223+
if (checker.test(child, c, keyCode)) {
224+
return true;
225+
}
226+
}
227+
return false;
228+
}
229+
230+
public interface MouseOverPredicate<ELEMENT> {
231+
232+
boolean test(ELEMENT element, double mouseX, double mouseY);
233+
}
234+
235+
public interface MouseClickedPredicate<ELEMENT> {
236+
237+
boolean test(ELEMENT element, double mouseX, double mouseY, int button);
238+
}
239+
240+
public interface KeyPressedPredicate<ELEMENT> {
241+
242+
boolean test(ELEMENT element, int keyCode, int scanCode, int modifiers);
243+
}
244+
245+
public interface CharTypedPredicate<ELEMENT> {
246+
247+
boolean test(ELEMENT element, char c, int keyCode);
248+
}
249+
183250
public static int drawString(GuiGraphics guiGraphics, Font font, Component component, float x, float y, int color, boolean drawShadow) {
184251
return guiGraphics.drawString(font, component.getVisualOrderText(), x, y, color, drawShadow);
185252
}

src/main/java/mekanism/client/gui/element/GuiElement.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void resize(int prevLeft, int prevTop, int left, int top) {
187187
}
188188
}
189189

190-
public boolean childrenContainsElement(Predicate<GuiElement> checker) {
190+
public final boolean childrenContainsElement(Predicate<GuiElement> checker) {
191191
for (GuiElement child : children) {
192192
if (child.containsElement(checker)) {
193193
return true;
@@ -320,7 +320,7 @@ public final void setDragging(boolean dragging) {
320320
public Optional<GuiEventListener> getChildAt(double mouseX, double mouseY) {
321321
if (checkWindows(mouseX, mouseY)) {
322322
//If we are are not covered by a window, try to locate which child we are over
323-
return Optional.ofNullable(GuiUtils.findChild(children, child -> child.isMouseOver(mouseX, mouseY)));
323+
return Optional.ofNullable(GuiUtils.findChild(children, mouseX, mouseY, GuiElement::isMouseOver));
324324
}
325325
return Optional.empty();
326326
}
@@ -405,7 +405,7 @@ protected boolean supportsArrowNavigation() {
405405
//TODO - 1.20: Do we want things like the merged bars/gauges to have setFocused also mark the "children" as focused?
406406
@Override
407407
public boolean mouseClicked(double mouseX, double mouseY, int button) {
408-
GuiElement clickedChild = GuiUtils.findChild(children, child -> child.mouseClicked(mouseX, mouseY, button));
408+
GuiElement clickedChild = GuiUtils.findChild(children, mouseX, mouseY, button, GuiElement::mouseClicked);
409409
//Note: This setFocused call is outside the clickedChild find, so that if we couldn't find one
410410
// then we un-focus whatever child is currently focused
411411
if (clickedChild != null) {
@@ -420,12 +420,12 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
420420

421421
@Override
422422
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
423-
return GuiUtils.checkChildren(children, child -> child.keyPressed(keyCode, scanCode, modifiers)) || super.keyPressed(keyCode, scanCode, modifiers);
423+
return GuiUtils.checkChildren(children, keyCode, scanCode, modifiers, GuiElement::keyPressed) || super.keyPressed(keyCode, scanCode, modifiers);
424424
}
425425

426426
@Override
427427
public boolean charTyped(char c, int keyCode) {
428-
return GuiUtils.checkChildren(children, child -> child.charTyped(c, keyCode)) || super.charTyped(c, keyCode);
428+
return GuiUtils.checkChildrenChar(children, c, keyCode, GuiElement::charTyped) || super.charTyped(c, keyCode);
429429
}
430430

431431
@Override
@@ -449,7 +449,13 @@ public void onRelease(double mouseX, double mouseY) {
449449

450450
@Override
451451
public boolean mouseScrolled(double mouseX, double mouseY, double xDelta, double yDelta) {
452-
return GuiUtils.checkChildren(children, child -> child.mouseScrolled(mouseX, mouseY, xDelta, yDelta)) || super.mouseScrolled(mouseX, mouseY, xDelta, yDelta);
452+
for (int i = children.size() - 1; i >= 0; i--) {
453+
GuiElement child = children.get(i);
454+
if (child.mouseScrolled(mouseX, mouseY, xDelta, yDelta)) {
455+
return true;
456+
}
457+
}
458+
return super.mouseScrolled(mouseX, mouseY, xDelta, yDelta);
453459
}
454460

455461
@Override
@@ -511,7 +517,7 @@ protected boolean resetColorBeforeRender() {
511517

512518
@Override
513519
public boolean isMouseOver(double mouseX, double mouseY) {
514-
return super.isMouseOver(mouseX, mouseY) || GuiUtils.checkChildren(children, child -> child.isMouseOver(mouseX, mouseY));
520+
return super.isMouseOver(mouseX, mouseY) || GuiUtils.checkChildren(children, mouseX, mouseY, GuiElement::isMouseOver);
515521
}
516522

517523
/**

0 commit comments

Comments
 (0)