| 1 | package net.minecraft.client.gui.screens.options;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Locale;
|
| 5 | import java.util.SortedMap;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.Options;
|
| 10 | import net.minecraft.client.gui.components.Button;
|
| 11 | import net.minecraft.client.gui.components.EditBox;
|
| 12 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 13 | import net.minecraft.client.gui.components.StringWidget;
|
| 14 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 15 | import net.minecraft.client.gui.screens.Screen;
|
| 16 | import net.minecraft.client.gui.screens.options.LanguageSelectScreen.LanguageSelectionList.Entry;
|
| 17 | import net.minecraft.client.resources.language.LanguageInfo;
|
| 18 | import net.minecraft.client.resources.language.LanguageManager;
|
| 19 | import net.minecraft.network.chat.CommonComponents;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class LanguageSelectScreen extends OptionsSubScreen {
|
| 25 | private static final Component WARNING_LABEL = Component.translatable("options.languageAccuracyWarning").withColor(-4539718);
|
| 26 | private static final int FOOTER_HEIGHT = 53;
|
| 27 | private static final Component SEARCH_HINT = Component.translatable("gui.language.search").withStyle(EditBox.SEARCH_HINT_STYLE);
|
| 28 | private static final int SEARCH_BOX_HEIGHT = 15;
|
| 29 | private final LanguageManager languageManager;
|
| 30 | private LanguageSelectScreen.@Nullable LanguageSelectionList languageSelectionList;
|
| 31 | private @Nullable EditBox search;
|
| 32 |
|
| 33 | public LanguageSelectScreen(final Screen lastScreen, final Options options, final LanguageManager languageManager) {
|
| 34 | super(lastScreen, options, Component.translatable("options.language.title"));
|
| 35 | this.languageManager = languageManager;
|
| 36 | this.layout.setFooterHeight(53);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected void addTitle() {
|
| 41 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(4));
|
| 42 | header.defaultCellSetting().alignHorizontallyCenter();
|
| 43 | header.addChild(new StringWidget(this.title, this.font));
|
| 44 | this.search = header.addChild(new EditBox(this.font, 0, 0, 200, 15, Component.empty()));
|
| 45 | this.search.setHint(SEARCH_HINT);
|
| 46 | this.search.setResponder(string -> {
|
| 47 | if (this.languageSelectionList != null) {
|
| 48 | this.languageSelectionList.filterEntries(string);
|
| 49 | }
|
| 50 | });
|
| 51 | this.layout.setHeaderHeight((int)(12.0 + 9.0 + 15.0));
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected void setInitialFocus() {
|
| 56 | if (this.search != null) {
|
| 57 | this.setInitialFocus(this.search);
|
| 58 | } else {
|
| 59 | super.setInitialFocus();
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | @Override
|
| 64 | protected void addContents() {
|
| 65 | this.languageSelectionList = this.layout.addToContents(new LanguageSelectScreen.LanguageSelectionList(this.minecraft));
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected void addOptions() {
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void addFooter() {
|
| 74 | LinearLayout footer = this.layout.addToFooter(LinearLayout.vertical()).spacing(8);
|
| 75 | footer.defaultCellSetting().alignHorizontallyCenter();
|
| 76 | footer.addChild(new StringWidget(WARNING_LABEL, this.font));
|
| 77 | LinearLayout bottomButtons = footer.addChild(LinearLayout.horizontal().spacing(8));
|
| 78 | bottomButtons.addChild(
|
| 79 | Button.builder(Component.translatable("options.font"), var1x -> this.minecraft.gui.setScreen(new FontOptionsScreen(this, this.options))).build()
|
| 80 | );
|
| 81 | bottomButtons.addChild(Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).build());
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | protected void repositionElements() {
|
| 86 | super.repositionElements();
|
| 87 | if (this.languageSelectionList != null) {
|
| 88 | this.languageSelectionList.updateSize(this.width, this.layout);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | private void onDone() {
|
| 93 | if (this.languageSelectionList != null && this.languageSelectionList.getSelected() != null) {
|
| 94 | Entry selectedEntry = this.languageSelectionList.getSelected();
|
| 95 | if (!selectedEntry.code.equals(this.languageManager.getSelected())) {
|
| 96 | this.languageManager.setSelected(selectedEntry.code);
|
| 97 | this.options.languageCode = selectedEntry.code;
|
| 98 | this.minecraft.reloadResourcePacks();
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 103 | }
|
| 104 |
|
| 105 | @Environment(EnvType.CLIENT)
|
| 106 | private class LanguageSelectionList extends ObjectSelectionList<net.minecraft.client.gui.screens.options.LanguageSelectScreen.LanguageSelectionList.Entry> {
|
| 107 | public LanguageSelectionList(final Minecraft minecraft) {
|
| 108 | super(minecraft, LanguageSelectScreen.this.width, LanguageSelectScreen.this.height - 33 - 53, 33, 18);
|
| 109 | String selectedLanguage = LanguageSelectScreen.this.languageManager.getSelected();
|
| 110 | LanguageSelectScreen.this.languageManager
|
| 111 | .getLanguages()
|
| 112 | .forEach(
|
| 113 | (code, info) -> {
|
| 114 | net.minecraft.client.gui.screens.options.LanguageSelectScreen.LanguageSelectionList.Entry entry = new net.minecraft.client.gui.screens.options.LanguageSelectScreen.LanguageSelectionList.Entry(
|
| 115 | this, code, info
|
| 116 | );
|
| 117 | this.addEntry(entry);
|
| 118 | if (selectedLanguage.equals(code)) {
|
| 119 | this.setSelected(entry);
|
| 120 | }
|
| 121 | }
|
| 122 | );
|
| 123 | if (this.getSelected() != null) {
|
| 124 | this.centerScrollOn(this.getSelected());
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | private void filterEntries(final String filter) {
|
| 129 | SortedMap<String, LanguageInfo> languages = LanguageSelectScreen.this.languageManager.getLanguages();
|
| 130 | List<net.minecraft.client.gui.screens.options.LanguageSelectScreen.LanguageSelectionList.Entry> filteredEntries = languages.entrySet()
|
| 131 | .stream()
|
| 132 | .filter(
|
| 133 | entry -> filter.isEmpty()
|
| 134 | || entry.getValue().name().toLowerCase(Locale.ROOT).contains(filter.toLowerCase(Locale.ROOT))
|
| 135 | || entry.getValue().region().toLowerCase(Locale.ROOT).contains(filter.toLowerCase(Locale.ROOT))
|
| 136 | )
|
| 137 | .map(entry -> new net.minecraft.client.gui.screens.options.LanguageSelectScreen.LanguageSelectionList.Entry(this, entry.getKey(), entry.getValue()))
|
| 138 | .toList();
|
| 139 | this.replaceEntries(filteredEntries);
|
| 140 | this.refreshScrollAmount();
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public int getRowWidth() {
|
| 145 | return super.getRowWidth() + 50;
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 |
|