DebugOptionsScreen .java

net.minecraft.client.gui.screens.debug.DebugOptionsScreen (class, 327 lines): members: AbstractOptionEntry, NOT_ALLOWED_TOOLTIP, IN_OVERLAY_TEXT, SUBTITLE.

net.minecraft.client.gui.screens.debugclass327 linesclientextends Screen13.4 KB

API Surface (16)

void init()
void extractBlurredBackground(final GuiGraphicsExtractor graphics)
void setInitialFocus()
void repositionElements()
void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a)
List<? extends GuiEventListener> children()
List<? extends NarratableEntry> narratables()
void refreshEntry()
List<? extends GuiEventListener> children()
List<? extends NarratableEntry> narratables()
void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a)
void refreshEntry()
void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
int getRowWidth()
void refreshEntries()
void updateSearch(final String value)

Fields & Constants (11)

TITLE
SUBTITLE
ENABLED_TEXT
IN_OVERLAY_TEXT
DISABLED_TEXT
NOT_ALLOWED_TOOLTIP
SEARCH
layout
optionList
searchBox
profileButtons

String literals (7)

"debug.options.title"
"debug.options.warning"
"debug.entry.always"
"debug.entry.overlay"
"debug.options.notAllowed.tooltip"
"debug.options.search"
"debug.entry.currently."

Full Source

DebugOptionsScreen.java327 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.debug;
2
3
import com.google.common.collect.Lists;
4
import it.unimi.dsi.fastutil.floats.FloatComparators;
5
import java.util.ArrayList;
6
import java.util.Comparator;
7
import java.util.List;
8
import java.util.Map;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.ChatFormatting;
12
import net.minecraft.client.Minecraft;
13
import net.minecraft.client.gui.GuiGraphicsExtractor;
14
import net.minecraft.client.gui.components.AbstractWidget;
15
import net.minecraft.client.gui.components.Button;
16
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
17
import net.minecraft.client.gui.components.CycleButton;
18
import net.minecraft.client.gui.components.EditBox;
19
import net.minecraft.client.gui.components.FocusableTextWidget;
20
import net.minecraft.client.gui.components.MultiLineTextWidget;
21
import net.minecraft.client.gui.components.StringWidget;
22
import net.minecraft.client.gui.components.debug.DebugEntryCategory;
23
import net.minecraft.client.gui.components.debug.DebugScreenEntries;
24
import net.minecraft.client.gui.components.debug.DebugScreenEntry;
25
import net.minecraft.client.gui.components.debug.DebugScreenEntryStatus;
26
import net.minecraft.client.gui.components.debug.DebugScreenProfile;
27
import net.minecraft.client.gui.components.events.GuiEventListener;
28
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
29
import net.minecraft.client.gui.layouts.LayoutSettings;
30
import net.minecraft.client.gui.layouts.LinearLayout;
31
import net.minecraft.client.gui.layouts.SpacerElement;
32
import net.minecraft.client.gui.narration.NarratableEntry;
33
import net.minecraft.client.gui.screens.Screen;
34
import net.minecraft.network.chat.CommonComponents;
35
import net.minecraft.network.chat.Component;
36
import net.minecraft.network.chat.MutableComponent;
37
import net.minecraft.resources.Identifier;
38
import org.jspecify.annotations.Nullable;
39
40
@Environment(EnvType.CLIENT)
41
public class DebugOptionsScreen extends Screen {
42
	private static final Component TITLE = Component.translatable("debug.options.title");
43
	private static final Component SUBTITLE = Component.translatable("debug.options.warning").withColor(-2142128);
44
	private static final Component ENABLED_TEXT = Component.translatable("debug.entry.always");
45
	private static final Component IN_OVERLAY_TEXT = Component.translatable("debug.entry.overlay");
46
	private static final Component DISABLED_TEXT = CommonComponents.OPTION_OFF;
47
	private static final Component NOT_ALLOWED_TOOLTIP = Component.translatable("debug.options.notAllowed.tooltip");
48
	private static final Component SEARCH = Component.translatable("debug.options.search").withStyle(EditBox.SEARCH_HINT_STYLE);
49
	private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 61, 33);
50
	private DebugOptionsScreen.@Nullable OptionList optionList;
51
	private EditBox searchBox;
52
	private final List<Button> profileButtons = new ArrayList<>();
53
54
	public DebugOptionsScreen() {
55
		super(TITLE);
56
	}
57
58
	@Override
59
	protected void init() {
60
		LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(8));
61
		this.optionList = new DebugOptionsScreen.OptionList();
62
		int optionListWidth = this.optionList.getRowWidth();
63
		LinearLayout title = LinearLayout.horizontal().spacing(8);
64
		title.addChild(new SpacerElement(optionListWidth / 3, 1));
65
		title.addChild(new StringWidget(TITLE, this.font), title.newCellSettings().alignVerticallyMiddle());
66
		this.searchBox = new EditBox(this.font, 0, 0, optionListWidth / 3, 20, this.searchBox, SEARCH);
67
		this.searchBox.setResponder(value -> this.optionList.updateSearch(value));
68
		this.searchBox.setHint(SEARCH);
69
		title.addChild(this.searchBox);
70
		header.addChild(title, LayoutSettings::alignHorizontallyCenter);
71
		header.addChild(new MultiLineTextWidget(SUBTITLE, this.font).setMaxWidth(optionListWidth).setCentered(true), LayoutSettings::alignHorizontallyCenter);
72
		this.layout.addToContents(this.optionList);
73
		LinearLayout bottomButtons = this.layout.addToFooter(LinearLayout.horizontal().spacing(4));
74
		this.addProfileButton(DebugScreenProfile.DEFAULT, bottomButtons);
75
		this.addProfileButton(DebugScreenProfile.PERFORMANCE, bottomButtons);
76
		AbstractWidget guiScaleButton = this.minecraft.options.debugGuiScale().createButton(this.minecraft.options);
77
		guiScaleButton.setWidth(110);
78
		bottomButtons.addChild(guiScaleButton);
79
		bottomButtons.addChild(Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).width(60).build());
80
		this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
81
		this.repositionElements();
82
	}
83
84
	@Override
85
	public void extractBlurredBackground(final GuiGraphicsExtractor graphics) {
86
		this.minecraft.gui.hud.extractDebugOverlay(graphics);
87
		super.extractBlurredBackground(graphics);
88
	}
89
90
	@Override
91
	protected void setInitialFocus() {
92
		this.setInitialFocus(this.searchBox);
93
	}
94
95
	private void addProfileButton(final DebugScreenProfile profile, final LinearLayout bottomButtons) {
96
		Button profileButton = Button.builder(Component.translatable(profile.translationKey()), button -> {
97
			this.minecraft.debugEntries.loadProfile(profile);
98
			this.minecraft.debugEntries.save();
99
			this.optionList.refreshEntries();
100
101
			for (Button listButton : this.profileButtons) {
102
				listButton.active = true;
103
			}
104
105
			button.active = false;
106
		}).width(120).build();
107
		profileButton.active = !this.minecraft.debugEntries.isUsingProfile(profile);
108
		this.profileButtons.add(profileButton);
109
		bottomButtons.addChild(profileButton);
110
	}
111
112
	@Override
113
	protected void repositionElements() {
114
		this.layout.arrangeElements();
115
		if (this.optionList != null) {
116
			this.optionList.updateSize(this.width, this.layout);
117
		}
118
	}
119
120
	public DebugOptionsScreen.@Nullable OptionList getOptionList() {
121
		return this.optionList;
122
	}
123
124
	@Environment(EnvType.CLIENT)
125
	public abstract static class AbstractOptionEntry extends ContainerObjectSelectionList.Entry<DebugOptionsScreen.AbstractOptionEntry> {
126
		public abstract void refreshEntry();
127
	}
128
129
	@Environment(EnvType.CLIENT)
130
	private class CategoryEntry extends DebugOptionsScreen.AbstractOptionEntry {
131
		private final FocusableTextWidget widget;
132
133
		public CategoryEntry(final Component category) {
134
			this.widget = FocusableTextWidget.builder(category, DebugOptionsScreen.this.minecraft.font)
135
				.alwaysShowBorder(false)
136
				.backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
137
				.build();
138
		}
139
140
		@Override
141
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
142
			this.widget
143
				.setPosition(this.getContentX() + this.getContentWidth() / 2 - this.widget.getWidth() / 2, this.getContentYMiddle() - this.widget.getHeight() / 2);
144
			this.widget.extractRenderState(graphics, mouseX, mouseY, a);
145
		}
146
147
		@Override
148
		public List<? extends GuiEventListener> children() {
149
			return List.of(this.widget);
150
		}
151
152
		@Override
153
		public List<? extends NarratableEntry> narratables() {
154
			return List.of(this.widget);
155
		}
156
157
		@Override
158
		public void refreshEntry() {
159
		}
160
	}
161
162
	@Environment(EnvType.CLIENT)
163
	private class OptionEntry extends DebugOptionsScreen.AbstractOptionEntry {
164
		private static final int BUTTON_WIDTH = 60;
165
		private final Identifier location;
166
		protected final List<AbstractWidget> children = Lists.newArrayList();
167
		private final CycleButton<Boolean> always;
168
		private final CycleButton<Boolean> overlay;
169
		private final CycleButton<Boolean> never;
170
		private final String name;
171
		private final boolean isAllowed;
172
173
		public OptionEntry(final Identifier location) {
174
			this.location = location;
175
			DebugScreenEntry entry = DebugScreenEntries.getEntry(location);
176
			this.isAllowed = entry != null && entry.isAllowed(DebugOptionsScreen.this.minecraft.showOnlyReducedInfo());
177
			String name = location.getPath();
178
			if (this.isAllowed) {
179
				this.name = name;
180
			} else {
181
				this.name = ChatFormatting.ITALIC + name;
182
			}
183
184
			this.always = CycleButton.booleanBuilder(
185
					DebugOptionsScreen.ENABLED_TEXT.copy().withColor(-2142128), DebugOptionsScreen.ENABLED_TEXT.copy().withColor(-4539718), false
186
				)
187
				.displayOnlyValue()
188
				.withCustomNarration(this::narrateButton)
189
				.create(10, 5, 60, 16, Component.literal(name), (button, newValue) -> this.setValue(location, DebugScreenEntryStatus.ALWAYS_ON));
190
			this.overlay = CycleButton.booleanBuilder(
191
					DebugOptionsScreen.IN_OVERLAY_TEXT.copy().withColor(-171), DebugOptionsScreen.IN_OVERLAY_TEXT.copy().withColor(-4539718), false
192
				)
193
				.displayOnlyValue()
194
				.withCustomNarration(this::narrateButton)
195
				.create(10, 5, 60, 16, Component.literal(name), (button, newValue) -> this.setValue(location, DebugScreenEntryStatus.IN_OVERLAY));
196
			this.never = CycleButton.booleanBuilder(
197
					DebugOptionsScreen.DISABLED_TEXT.copy().withColor(-1), DebugOptionsScreen.DISABLED_TEXT.copy().withColor(-4539718), false
198
				)
199
				.displayOnlyValue()
200
				.withCustomNarration(this::narrateButton)
201
				.create(10, 5, 60, 16, Component.literal(name), (button, newValue) -> this.setValue(location, DebugScreenEntryStatus.NEVER));
202
			this.children.add(this.never);
203
			this.children.add(this.overlay);
204
			this.children.add(this.always);
205
			this.refreshEntry();
206
		}
207
208
		private MutableComponent narrateButton(final CycleButton<Boolean> booleanCycleButton) {
209
			DebugScreenEntryStatus status = DebugOptionsScreen.this.minecraft.debugEntries.getStatus(this.location);
210
			MutableComponent current = Component.translatable("debug.entry.currently." + status.getSerializedName(), this.name);
211
			return CommonComponents.optionNameValue(current, booleanCycleButton.getMessage());
212
		}
213
214
		private void setValue(final Identifier location, final DebugScreenEntryStatus never) {
215
			DebugOptionsScreen.this.minecraft.debugEntries.setStatus(location, never);
216
217
			for (Button profileButton : DebugOptionsScreen.this.profileButtons) {
218
				profileButton.active = true;
219
			}
220
221
			this.refreshEntry();
222
		}
223
224
		@Override
225
		public List<? extends GuiEventListener> children() {
226
			return this.children;
227
		}
228
229
		@Override
230
		public List<? extends NarratableEntry> narratables() {
231
			return this.children;
232
		}
233
234
		@Override
235
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
236
			int x = this.getContentX();
237
			int y = this.getContentY();
238
			graphics.text(DebugOptionsScreen.this.minecraft.font, this.name, x, y + 5, this.isAllowed ? -1 : -8355712);
239
			int buttonsStartX = x + this.getContentWidth() - this.never.getWidth() - this.overlay.getWidth() - this.always.getWidth();
240
			if (!this.isAllowed && hovered && mouseX < buttonsStartX) {
241
				graphics.setTooltipForNextFrame(DebugOptionsScreen.NOT_ALLOWED_TOOLTIP, mouseX, mouseY);
242
			}
243
244
			this.never.setX(buttonsStartX);
245
			this.overlay.setX(this.never.getX() + this.never.getWidth());
246
			this.always.setX(this.overlay.getX() + this.overlay.getWidth());
247
			this.always.setY(y);
248
			this.overlay.setY(y);
249
			this.never.setY(y);
250
			this.always.extractRenderState(graphics, mouseX, mouseY, a);
251
			this.overlay.extractRenderState(graphics, mouseX, mouseY, a);
252
			this.never.extractRenderState(graphics, mouseX, mouseY, a);
253
		}
254
255
		@Override
256
		public void refreshEntry() {
257
			DebugScreenEntryStatus status = DebugOptionsScreen.this.minecraft.debugEntries.getStatus(this.location);
258
			this.always.setValue(status == DebugScreenEntryStatus.ALWAYS_ON);
259
			this.overlay.setValue(status == DebugScreenEntryStatus.IN_OVERLAY);
260
			this.never.setValue(status == DebugScreenEntryStatus.NEVER);
261
			this.always.active = !this.always.getValue();
262
			this.overlay.active = !this.overlay.getValue();
263
			this.never.active = !this.never.getValue();
264
		}
265
	}
266
267
	@Environment(EnvType.CLIENT)
268
	public class OptionList extends ContainerObjectSelectionList<DebugOptionsScreen.AbstractOptionEntry> {
269
		private static final Comparator<Map.Entry<Identifier, DebugScreenEntry>> COMPARATOR = (o1, o2) -> {
270
			int byCategory = FloatComparators.NATURAL_COMPARATOR.compare(o1.getValue().category().sortKey(), o2.getValue().category().sortKey());
271
			return byCategory != 0 ? byCategory : o1.getKey().compareTo(o2.getKey());
272
		};
273
		private static final int ITEM_HEIGHT = 20;
274
275
		public OptionList() {
276
			super(
277
				Minecraft.getInstance(),
278
				DebugOptionsScreen.this.width,
279
				DebugOptionsScreen.this.layout.getContentHeight(),
280
				DebugOptionsScreen.this.layout.getHeaderHeight(),
281
				20
282
			);
283
			this.updateSearch("");
284
		}
285
286
		@Override
287
		public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
288
			super.extractWidgetRenderState(graphics, mouseX, mouseY, a);
289
		}
290
291
		@Override
292
		public int getRowWidth() {
293
			return 350;
294
		}
295
296
		public void refreshEntries() {
297
			this.children().forEach(DebugOptionsScreen.AbstractOptionEntry::refreshEntry);
298
		}
299
300
		public void updateSearch(final String value) {
301
			this.clearEntries();
302
			List<Map.Entry<Identifier, DebugScreenEntry>> all = new ArrayList<>(DebugScreenEntries.allEntries().entrySet());
303
			all.sort(COMPARATOR);
304
			DebugEntryCategory currentCategory = null;
305
306
			for (Map.Entry<Identifier, DebugScreenEntry> entry : all) {
307
				if (entry.getKey().getPath().contains(value)) {
308
					DebugEntryCategory newCategory = entry.getValue().category();
309
					if (!newCategory.equals(currentCategory)) {
310
						this.addEntry(DebugOptionsScreen.this.new CategoryEntry(newCategory.label()));
311
						currentCategory = newCategory;
312
					}
313
314
					this.addEntry(DebugOptionsScreen.this.new OptionEntry(entry.getKey()));
315
				}
316
			}
317
318
			this.notifyListUpdated();
319
		}
320
321
		private void notifyListUpdated() {
322
			this.refreshScrollAmount();
323
			DebugOptionsScreen.this.triggerImmediateNarration(true);
324
		}
325
	}
326
}
327

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