KeyBindsList .java

net.minecraft.client.gui.screens.options.controls.KeyBindsList (class): members: resetMappingAndUpdateButtons, keyBindsScreen, refreshEntries, KeyEntry.

net.minecraft.client.gui.screens.options.controlsclass208 linesclientextends ContainerObjectSelectionList<KeyBindsList.Entry>7.4 KB

API Surface (11)

void resetMappingAndUpdateButtons()
void refreshEntries()
int getRowWidth()
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()
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()

Fields & Constants (3)

ITEM_HEIGHT
keyBindsScreen
maxNameWidth

String literals (5)

"controls.reset"
"narrator.controls.unbound"
"narrator.controls.bound"
"narrator.controls.reset"
"controls.keybinds.duplicateKeybinds"

Full Source

KeyBindsList.java208 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.options.controls;
2
3
import com.google.common.collect.ImmutableList;
4
import java.util.Arrays;
5
import java.util.List;
6
import net.fabricmc.api.EnvType;
7
import net.fabricmc.api.Environment;
8
import net.minecraft.ChatFormatting;
9
import net.minecraft.client.KeyMapping;
10
import net.minecraft.client.Minecraft;
11
import net.minecraft.client.gui.GuiGraphicsExtractor;
12
import net.minecraft.client.gui.components.Button;
13
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
14
import net.minecraft.client.gui.components.FocusableTextWidget;
15
import net.minecraft.client.gui.components.Tooltip;
16
import net.minecraft.client.gui.components.events.GuiEventListener;
17
import net.minecraft.client.gui.narration.NarratableEntry;
18
import net.minecraft.network.chat.Component;
19
import net.minecraft.network.chat.MutableComponent;
20
import org.apache.commons.lang3.ArrayUtils;
21
22
@Environment(EnvType.CLIENT)
23
public class KeyBindsList extends ContainerObjectSelectionList<KeyBindsList.Entry> {
24
	private static final int ITEM_HEIGHT = 20;
25
	private final KeyBindsScreen keyBindsScreen;
26
	private int maxNameWidth;
27
28
	public KeyBindsList(final KeyBindsScreen keyBindsScreen, final Minecraft minecraft) {
29
		super(minecraft, keyBindsScreen.width, keyBindsScreen.layout.getContentHeight(), keyBindsScreen.layout.getHeaderHeight(), 20);
30
		this.keyBindsScreen = keyBindsScreen;
31
		KeyMapping[] keyMappings = ArrayUtils.clone(minecraft.options.keyMappings);
32
		Arrays.sort(keyMappings);
33
		KeyMapping.Category previousCategory = null;
34
35
		for (KeyMapping key : keyMappings) {
36
			KeyMapping.Category category = key.getCategory();
37
			if (category != previousCategory) {
38
				previousCategory = category;
39
				this.addEntry(new KeyBindsList.CategoryEntry(category));
40
			}
41
42
			Component name = Component.translatable(key.getName());
43
			int width = minecraft.font.width(name);
44
			if (width > this.maxNameWidth) {
45
				this.maxNameWidth = width;
46
			}
47
48
			this.addEntry(new KeyBindsList.KeyEntry(key, name));
49
		}
50
	}
51
52
	public void resetMappingAndUpdateButtons() {
53
		KeyMapping.resetMapping();
54
		this.refreshEntries();
55
	}
56
57
	public void refreshEntries() {
58
		this.children().forEach(KeyBindsList.Entry::refreshEntry);
59
	}
60
61
	@Override
62
	public int getRowWidth() {
63
		return 340;
64
	}
65
66
	@Environment(EnvType.CLIENT)
67
	public class CategoryEntry extends KeyBindsList.Entry {
68
		private final FocusableTextWidget categoryName;
69
70
		public CategoryEntry(final KeyMapping.Category category) {
71
			this.categoryName = FocusableTextWidget.builder(
72
					category.label().copy().withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD), KeyBindsList.this.minecraft.font
73
				)
74
				.alwaysShowBorder(false)
75
				.backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
76
				.build();
77
		}
78
79
		@Override
80
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
81
			this.categoryName.setPosition(KeyBindsList.this.width / 2 - this.categoryName.getWidth() / 2, this.getContentBottom() - this.categoryName.getHeight());
82
			this.categoryName.extractRenderState(graphics, mouseX, mouseY, a);
83
		}
84
85
		@Override
86
		public List<? extends GuiEventListener> children() {
87
			return List.of(this.categoryName);
88
		}
89
90
		@Override
91
		public List<? extends NarratableEntry> narratables() {
92
			return List.of(this.categoryName);
93
		}
94
95
		@Override
96
		public void refreshEntry() {
97
		}
98
	}
99
100
	@Environment(EnvType.CLIENT)
101
	public abstract static class Entry extends ContainerObjectSelectionList.Entry<KeyBindsList.Entry> {
102
		public abstract void refreshEntry();
103
	}
104
105
	@Environment(EnvType.CLIENT)
106
	public class KeyEntry extends KeyBindsList.Entry {
107
		private static final Component RESET_BUTTON_TITLE = Component.translatable("controls.reset");
108
		private static final int PADDING = 10;
109
		private final KeyMapping key;
110
		private final Component name;
111
		private final Button changeButton;
112
		private final Button resetButton;
113
		private boolean hasCollision = false;
114
115
		private KeyEntry(final KeyMapping key, final Component name) {
116
			this.key = key;
117
			this.name = name;
118
			this.changeButton = Button.builder(name, button -> {
119
					KeyBindsList.this.keyBindsScreen.selectedKey = key;
120
					KeyBindsList.this.resetMappingAndUpdateButtons();
121
				})
122
				.bounds(0, 0, 75, 20)
123
				.createNarration(
124
					defaultNarrationSupplier -> key.isUnbound()
125
						? Component.translatable("narrator.controls.unbound", name)
126
						: Component.translatable("narrator.controls.bound", name, defaultNarrationSupplier.get())
127
				)
128
				.build();
129
			this.resetButton = Button.builder(RESET_BUTTON_TITLE, button -> {
130
				key.setKey(key.getDefaultKey());
131
				KeyBindsList.this.resetMappingAndUpdateButtons();
132
			}).bounds(0, 0, 50, 20).createNarration(defaultNarrationSupplier -> Component.translatable("narrator.controls.reset", name)).build();
133
			this.refreshEntry();
134
		}
135
136
		@Override
137
		public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
138
			this.updateChangeButtonMessage();
139
			int resetButtonX = KeyBindsList.this.scrollBarX() - this.resetButton.getWidth() - 10;
140
			int buttonY = this.getContentY() - 2;
141
			this.resetButton.setPosition(resetButtonX, buttonY);
142
			this.resetButton.extractRenderState(graphics, mouseX, mouseY, a);
143
			int changeButtonX = resetButtonX - 5 - this.changeButton.getWidth();
144
			this.changeButton.setPosition(changeButtonX, buttonY);
145
			this.changeButton.extractRenderState(graphics, mouseX, mouseY, a);
146
			graphics.text(KeyBindsList.this.minecraft.font, this.name, this.getContentX(), this.getContentYMiddle() - 9 / 2, -1);
147
			if (this.hasCollision) {
148
				int stripeWidth = 3;
149
				int stripeLeft = this.changeButton.getX() - 6;
150
				graphics.fill(stripeLeft, this.getContentY() - 1, stripeLeft + 3, this.getContentBottom(), -256);
151
			}
152
		}
153
154
		@Override
155
		public List<? extends GuiEventListener> children() {
156
			return ImmutableList.of(this.changeButton, this.resetButton);
157
		}
158
159
		@Override
160
		public List<? extends NarratableEntry> narratables() {
161
			return ImmutableList.of(this.changeButton, this.resetButton);
162
		}
163
164
		@Override
165
		public void refreshEntry() {
166
			this.resetButton.active = !this.key.isDefault();
167
			this.hasCollision = false;
168
			MutableComponent tooltip = Component.empty();
169
			if (!this.key.isUnbound()) {
170
				for (KeyMapping otherKey : KeyBindsList.this.minecraft.options.keyMappings) {
171
					if (otherKey != this.key && this.key.same(otherKey) && (!otherKey.isDefault() || !this.key.isDefault())) {
172
						if (this.hasCollision) {
173
							tooltip.append(", ");
174
						}
175
176
						this.hasCollision = true;
177
						tooltip.append(Component.translatable(otherKey.getName()));
178
					}
179
				}
180
			}
181
182
			if (this.hasCollision) {
183
				this.changeButton.setTooltip(Tooltip.create(Component.translatable("controls.keybinds.duplicateKeybinds", tooltip)));
184
			} else {
185
				this.changeButton.setTooltip(null);
186
			}
187
188
			this.updateChangeButtonMessage();
189
		}
190
191
		private void updateChangeButtonMessage() {
192
			Component message = this.key.getTranslatedKeyMessage();
193
			if (this.hasCollision) {
194
				message = Component.literal("[ ").append(message.copy().withStyle(ChatFormatting.WHITE)).append(" ]").withStyle(ChatFormatting.YELLOW);
195
			}
196
197
			if (KeyBindsList.this.keyBindsScreen.selectedKey == this.key) {
198
				message = Component.literal("> ")
199
					.append(message.copy().withStyle(ChatFormatting.WHITE, ChatFormatting.UNDERLINE))
200
					.append(" <")
201
					.withStyle(ChatFormatting.YELLOW);
202
			}
203
204
			this.changeButton.setMessage(message);
205
		}
206
	}
207
}
208

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