ContainerEventHandler .java

net.minecraft.client.gui.components.events.ContainerEventHandler (interface): members: nextFocusPathVaguelyInDirection, nextFocusPathInDirection, excluded.

net.minecraft.client.gui.components.eventsinterface277 linesclientextends GuiEventListener10.1 KB

API Surface (0)

No public/protected methods detected (fields or constants only).

Fields & Constants (3)

focusedRectangle
direction
excluded

Full Source

ContainerEventHandler.java277 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.components.events;
2
3
import com.mojang.datafixers.util.Pair;
4
import java.util.ArrayList;
5
import java.util.Collections;
6
import java.util.Comparator;
7
import java.util.List;
8
import java.util.ListIterator;
9
import java.util.Optional;
10
import java.util.function.BooleanSupplier;
11
import java.util.function.Supplier;
12
import net.fabricmc.api.EnvType;
13
import net.fabricmc.api.Environment;
14
import net.minecraft.client.gui.ComponentPath;
15
import net.minecraft.client.gui.navigation.FocusNavigationEvent;
16
import net.minecraft.client.gui.navigation.ScreenAxis;
17
import net.minecraft.client.gui.navigation.ScreenDirection;
18
import net.minecraft.client.gui.navigation.ScreenPosition;
19
import net.minecraft.client.gui.navigation.ScreenRectangle;
20
import net.minecraft.client.input.CharacterEvent;
21
import net.minecraft.client.input.KeyEvent;
22
import net.minecraft.client.input.MouseButtonEvent;
23
import net.minecraft.client.input.PreeditEvent;
24
import org.joml.Vector2i;
25
import org.jspecify.annotations.Nullable;
26
27
@Environment(EnvType.CLIENT)
28
public interface ContainerEventHandler extends GuiEventListener {
29
	List<? extends GuiEventListener> children();
30
31
	default Optional<GuiEventListener> getChildAt(final double x, final double y) {
32
		for (GuiEventListener child : this.children()) {
33
			if (child.isMouseOver(x, y)) {
34
				return Optional.of(child);
35
			}
36
		}
37
38
		return Optional.empty();
39
	}
40
41
	@Override
42
	default boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
43
		Optional<GuiEventListener> child = this.getChildAt(event.x(), event.y());
44
		if (child.isEmpty()) {
45
			return false;
46
		}
47
48
		GuiEventListener widget = child.get();
49
		if (widget.mouseClicked(event, doubleClick) && widget.shouldTakeFocusAfterInteraction()) {
50
			this.setFocused(widget);
51
			if (event.button() == 1) {
52
				this.setDragging(true);
53
			}
54
		}
55
56
		return true;
57
	}
58
59
	@Override
60
	default boolean mouseReleased(final MouseButtonEvent event) {
61
		if (event.button() == 1 && this.isDragging()) {
62
			this.setDragging(false);
63
			if (this.getFocused() != null) {
64
				return this.getFocused().mouseReleased(event);
65
			}
66
		}
67
68
		return false;
69
	}
70
71
	@Override
72
	default boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
73
		return this.getFocused() != null && this.isDragging() && event.button() == 1 ? this.getFocused().mouseDragged(event, dx, dy) : false;
74
	}
75
76
	boolean isDragging();
77
78
	void setDragging(boolean dragging);
79
80
	@Override
81
	default boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
82
		return this.getChildAt(x, y).filter(child -> child.mouseScrolled(x, y, scrollX, scrollY)).isPresent();
83
	}
84
85
	@Override
86
	default boolean keyPressed(final KeyEvent event) {
87
		return this.getFocused() != null && this.getFocused().keyPressed(event);
88
	}
89
90
	@Override
91
	default boolean keyReleased(final KeyEvent event) {
92
		return this.getFocused() != null && this.getFocused().keyReleased(event);
93
	}
94
95
	@Override
96
	default boolean charTyped(final CharacterEvent event) {
97
		return this.getFocused() != null && this.getFocused().charTyped(event);
98
	}
99
100
	@Override
101
	default boolean preeditUpdated(final @Nullable PreeditEvent event) {
102
		return this.getFocused() != null && this.getFocused().preeditUpdated(event);
103
	}
104
105
	@Override
106
	default ScreenRectangle getBorderForArrowNavigation(final ScreenDirection opposite) {
107
		GuiEventListener focused = this.getFocused();
108
		return focused != null ? focused.getBorderForArrowNavigation(opposite) : GuiEventListener.super.getBorderForArrowNavigation(opposite);
109
	}
110
111
	@Nullable GuiEventListener getFocused();
112
113
	void setFocused(final @Nullable GuiEventListener focused);
114
115
	@Override
116
	default void setFocused(final boolean focused) {
117
		if (!focused) {
118
			this.setFocused(null);
119
		}
120
	}
121
122
	@Override
123
	default boolean isFocused() {
124
		return this.getFocused() != null;
125
	}
126
127
	@Override
128
	default @Nullable ComponentPath getCurrentFocusPath() {
129
		GuiEventListener focused = this.getFocused();
130
		return focused != null ? ComponentPath.path(this, focused.getCurrentFocusPath()) : null;
131
	}
132
133
	@Override
134
	default @Nullable ComponentPath nextFocusPath(final FocusNavigationEvent navigationEvent) {
135
		GuiEventListener focus = this.getFocused();
136
		if (focus != null) {
137
			ComponentPath focusPath = focus.nextFocusPath(navigationEvent);
138
			if (focusPath != null) {
139
				return ComponentPath.path(this, focusPath);
140
			}
141
		}
142
143
		if (navigationEvent instanceof FocusNavigationEvent.TabNavigation tabNavigation) {
144
			return this.handleTabNavigation(tabNavigation);
145
		} else {
146
			return navigationEvent instanceof FocusNavigationEvent.ArrowNavigation arrowNavigation ? this.handleArrowNavigation(arrowNavigation) : null;
147
		}
148
	}
149
150
	private @Nullable ComponentPath handleTabNavigation(final FocusNavigationEvent.TabNavigation tabNavigation) {
151
		boolean forward = tabNavigation.forward();
152
		GuiEventListener focus = this.getFocused();
153
		List<? extends GuiEventListener> sortedChildren = new ArrayList<>(this.children());
154
		Collections.sort(sortedChildren, Comparator.comparingInt(childx -> childx.getTabOrderGroup()));
155
		int index = sortedChildren.indexOf(focus);
156
		int newIndex;
157
		if (focus != null && index >= 0) {
158
			newIndex = index + (forward ? 1 : 0);
159
		} else if (forward) {
160
			newIndex = 0;
161
		} else {
162
			newIndex = sortedChildren.size();
163
		}
164
165
		ListIterator<? extends GuiEventListener> iterator = sortedChildren.listIterator(newIndex);
166
		BooleanSupplier test = forward ? iterator::hasNext : iterator::hasPrevious;
167
		Supplier<? extends GuiEventListener> getter = forward ? iterator::next : iterator::previous;
168
169
		while (test.getAsBoolean()) {
170
			GuiEventListener child = getter.get();
171
			ComponentPath focusPath = child.nextFocusPath(tabNavigation);
172
			if (focusPath != null) {
173
				return ComponentPath.path(this, focusPath);
174
			}
175
		}
176
177
		return null;
178
	}
179
180
	private @Nullable ComponentPath handleArrowNavigation(final FocusNavigationEvent.ArrowNavigation arrowNavigation) {
181
		GuiEventListener focus = this.getFocused();
182
		ScreenDirection direction = arrowNavigation.direction();
183
		if (focus == null) {
184
			if (arrowNavigation.previousFocus() != null) {
185
				return ComponentPath.path(this, this.nextFocusPathInDirection(arrowNavigation.previousFocus(), arrowNavigation.direction(), null, arrowNavigation));
186
			}
187
188
			ScreenRectangle borderRectangle = this.getBorderForArrowNavigation(direction.getOpposite());
189
			return ComponentPath.path(this, this.nextFocusPathInDirection(borderRectangle, direction, null, arrowNavigation));
190
		} else {
191
			ScreenRectangle focusedRectangle = focus.getBorderForArrowNavigation(direction);
192
			return ComponentPath.path(this, this.nextFocusPathInDirection(focusedRectangle, arrowNavigation.direction(), focus, arrowNavigation.with(focusedRectangle)));
193
		}
194
	}
195
196
	private @Nullable ComponentPath nextFocusPathInDirection(
197
		final ScreenRectangle focusedRectangle,
198
		final ScreenDirection direction,
199
		final @Nullable GuiEventListener excluded,
200
		final FocusNavigationEvent.ArrowNavigation navigationEvent
201
	) {
202
		ScreenAxis axis = direction.getAxis();
203
		ScreenAxis otherAxis = axis.orthogonal();
204
		ScreenDirection positiveDirectionOtherAxis = otherAxis.getPositive();
205
		int focusedFirstBound = focusedRectangle.getBoundInDirection(direction.getOpposite());
206
		List<GuiEventListener> potentialChildren = new ArrayList<>();
207
208
		for (GuiEventListener child : this.children()) {
209
			if (child != excluded) {
210
				ScreenRectangle childRectangle = child.getRectangle();
211
				if (childRectangle.overlapsInAxis(focusedRectangle, otherAxis)) {
212
					int childFirstBound = childRectangle.getBoundInDirection(direction.getOpposite());
213
					if (direction.isAfter(childFirstBound, focusedFirstBound)) {
214
						potentialChildren.add(child);
215
					} else if (childFirstBound == focusedFirstBound
216
						&& direction.isAfter(childRectangle.getBoundInDirection(direction), focusedRectangle.getBoundInDirection(direction))) {
217
						potentialChildren.add(child);
218
					}
219
				}
220
			}
221
		}
222
223
		Comparator<GuiEventListener> primaryComparator = Comparator.comparing(
224
			childx -> childx.getRectangle().getBoundInDirection(direction.getOpposite()), direction.coordinateValueComparator()
225
		);
226
		Comparator<GuiEventListener> secondaryComparator = Comparator.comparing(
227
			childx -> childx.getRectangle().getBoundInDirection(positiveDirectionOtherAxis.getOpposite()), positiveDirectionOtherAxis.coordinateValueComparator()
228
		);
229
		potentialChildren.sort(primaryComparator.thenComparing(secondaryComparator));
230
231
		for (GuiEventListener child : potentialChildren) {
232
			ComponentPath componentPath = child.nextFocusPath(navigationEvent);
233
			if (componentPath != null) {
234
				return componentPath;
235
			}
236
		}
237
238
		return this.nextFocusPathVaguelyInDirection(focusedRectangle, direction, excluded, navigationEvent);
239
	}
240
241
	private @Nullable ComponentPath nextFocusPathVaguelyInDirection(
242
		final ScreenRectangle focusedRectangle,
243
		final ScreenDirection direction,
244
		final @Nullable GuiEventListener excluded,
245
		final FocusNavigationEvent navigationEvent
246
	) {
247
		ScreenAxis axis = direction.getAxis();
248
		ScreenAxis otherAxis = axis.orthogonal();
249
		List<Pair<GuiEventListener, Long>> potentialChildren = new ArrayList<>();
250
		ScreenPosition focusedSideCenter = ScreenPosition.of(axis, focusedRectangle.getBoundInDirection(direction), focusedRectangle.getCenterInAxis(otherAxis));
251
252
		for (GuiEventListener child : this.children()) {
253
			if (child != excluded) {
254
				ScreenRectangle childRectangle = child.getRectangle();
255
				ScreenPosition childOpposingSideCenter = ScreenPosition.of(
256
					axis, childRectangle.getBoundInDirection(direction.getOpposite()), childRectangle.getCenterInAxis(otherAxis)
257
				);
258
				if (direction.isAfter(childOpposingSideCenter.getCoordinate(axis), focusedSideCenter.getCoordinate(axis))) {
259
					long distanceSquared = Vector2i.distanceSquared(focusedSideCenter.x(), focusedSideCenter.y(), childOpposingSideCenter.x(), childOpposingSideCenter.y());
260
					potentialChildren.add(Pair.of(child, distanceSquared));
261
				}
262
			}
263
		}
264
265
		potentialChildren.sort(Comparator.comparingDouble(Pair::getSecond));
266
267
		for (Pair<GuiEventListener, Long> child : potentialChildren) {
268
			ComponentPath componentPath = child.getFirst().nextFocusPath(navigationEvent);
269
			if (componentPath != null) {
270
				return componentPath;
271
			}
272
		}
273
274
		return null;
275
	}
276
}
277

Decompiled Minecraft 26.3 source of ContainerEventHandler (net/minecraft/client/gui/components/events/ContainerEventHandler.java) with Mojang official mappings, stored verbatim on the site.