| 1 | package net.minecraft.client.gui.screens.options.controls;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.InputConstants;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.KeyMapping;
|
| 7 | import net.minecraft.client.Options;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.gui.components.Button;
|
| 10 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 11 | import net.minecraft.client.gui.screens.Screen;
|
| 12 | import net.minecraft.client.gui.screens.options.OptionsSubScreen;
|
| 13 | import net.minecraft.client.input.KeyEvent;
|
| 14 | import net.minecraft.client.input.MouseButtonEvent;
|
| 15 | import net.minecraft.network.chat.CommonComponents;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.util.Util;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class KeyBindsScreen extends OptionsSubScreen {
|
| 22 | private static final Component TITLE = Component.translatable("controls.keybinds.title");
|
| 23 | public @Nullable KeyMapping selectedKey;
|
| 24 | public long lastKeySelection;
|
| 25 | private KeyBindsList keyBindsList;
|
| 26 | private Button resetButton;
|
| 27 |
|
| 28 | public KeyBindsScreen(final Screen lastScreen, final Options options) {
|
| 29 | super(lastScreen, options, TITLE);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | protected void addContents() {
|
| 34 | this.keyBindsList = this.layout.addToContents(new KeyBindsList(this, this.minecraft));
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | protected void addOptions() {
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void addFooter() {
|
| 43 | this.resetButton = Button.builder(Component.translatable("controls.resetAll"), button -> {
|
| 44 | for (KeyMapping key : this.options.keyMappings) {
|
| 45 | key.setKey(key.getDefaultKey());
|
| 46 | }
|
| 47 |
|
| 48 | this.keyBindsList.resetMappingAndUpdateButtons();
|
| 49 | }).build();
|
| 50 | LinearLayout bottomButtons = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 51 | bottomButtons.addChild(this.resetButton);
|
| 52 | bottomButtons.addChild(Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).build());
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void repositionElements() {
|
| 57 | this.layout.arrangeElements();
|
| 58 | this.keyBindsList.updateSize(this.width, this.layout);
|
| 59 | }
|
| 60 |
|
| 61 | public void refreshKeybindLabels() {
|
| 62 | this.keyBindsList.refreshEntries();
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 67 | if (this.selectedKey != null) {
|
| 68 | this.selectedKey.setKey(InputConstants.Type.MOUSE.getOrCreate(event.button()));
|
| 69 | this.selectedKey = null;
|
| 70 | this.keyBindsList.resetMappingAndUpdateButtons();
|
| 71 | return true;
|
| 72 | } else {
|
| 73 | return super.mouseClicked(event, doubleClick);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public boolean keyPressed(final KeyEvent event) {
|
| 79 | if (this.selectedKey != null) {
|
| 80 | if (event.isEscape()) {
|
| 81 | this.selectedKey.setKey(InputConstants.UNKNOWN);
|
| 82 | } else {
|
| 83 | this.selectedKey.setKey(InputConstants.getKey(event));
|
| 84 | }
|
| 85 |
|
| 86 | this.selectedKey = null;
|
| 87 | this.lastKeySelection = Util.getMillis();
|
| 88 | this.keyBindsList.resetMappingAndUpdateButtons();
|
| 89 | return true;
|
| 90 | } else {
|
| 91 | return super.keyPressed(event);
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 97 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 98 | boolean canReset = false;
|
| 99 |
|
| 100 | for (KeyMapping key : this.options.keyMappings) {
|
| 101 | if (!key.isDefault()) {
|
| 102 | canReset = true;
|
| 103 | break;
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | this.resetButton.active = canReset;
|
| 108 | }
|
| 109 | }
|
| 110 |
|