Add checkbox in edit profile screen

This commit is contained in:
axolotlmaid 2024-06-29 16:55:37 +01:00
parent b6480365ad
commit 0b0a93dca7
10 changed files with 111 additions and 46 deletions

View file

@ -1,9 +1,11 @@
package com.axolotlmaid.optionsprofiles.gui;
import com.axolotlmaid.optionsprofiles.profiles.ProfileConfiguration;
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.Checkbox;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
@ -14,6 +16,7 @@ public class EditProfileScreen extends Screen {
private final Component profileName;
private EditBox profileNameEdit;
private Checkbox keybindingsOnlyCheckbox;
public EditProfileScreen(Screen screen, Component profileName) {
super(Component.literal(Component.translatable("gui.optionsprofiles.editing-profile-title").getString() + profileName.getString()));
@ -22,28 +25,65 @@ public class EditProfileScreen extends Screen {
}
protected void init() {
ProfileConfiguration profileConfiguration = ProfileConfiguration.get(profileName.getString());
this.profileNameEdit = new EditBox(this.font, this.width / 2 - 102, 116, 204, 20, Component.empty());
this.profileNameEdit.setValue(profileName.getString());
this.addWidget(this.profileNameEdit);
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.overwrite-options"), (button) -> {
Profiles.writeProfile(profileName.getString(), true);
this.minecraft.setScreen(this.lastScreen);
}).size(100, 20).pos(this.width / 2 - 50, 145).build());
this.keybindingsOnlyCheckbox = Checkbox.builder(
Component.translatable("gui.optionsprofiles.keybindings-only"),
this.font)
.pos(5, this.height - 45)
.selected(profileConfiguration.isKeybindingsOnly())
.build();
this.addRenderableWidget(this.keybindingsOnlyCheckbox);
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.rename-profile"), (button) -> {
Profiles.renameProfile(profileName.getString(), this.profileNameEdit.getValue());
this.minecraft.setScreen(new EditProfileScreen(lastScreen, Component.literal(this.profileNameEdit.getValue())));
}).size(100, 20).pos(this.width / 2 - 50, 166).build());
this.addRenderableWidget(
Button.builder(
Component.translatable("gui.optionsprofiles.overwrite-options"),
(button) -> {
Profiles.writeProfile(profileName.getString(), true);
this.minecraft.setScreen(this.lastScreen);
})
.size(100, 20)
.pos(this.width / 2 - 50, 145)
.build());
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.delete-profile").withStyle(ChatFormatting.RED), (button) -> {
Profiles.deleteProfile(profileName.getString());
this.minecraft.setScreen(this.lastScreen);
}).size(100, 20).pos(5, this.height - 25).build());
this.addRenderableWidget(
Button.builder(
Component.translatable("gui.optionsprofiles.rename-profile"),
(button) -> {
Profiles.renameProfile(profileName.getString(), this.profileNameEdit.getValue());
this.minecraft.setScreen(new EditProfileScreen(lastScreen, Component.literal(this.profileNameEdit.getValue())));
})
.size(100, 20)
.pos(this.width / 2 - 50, 166)
.build());
this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, (button) -> {
this.minecraft.setScreen(this.lastScreen);
}).size(100, 20).pos(this.width / 2 - 50, this.height - 40).build());
this.addRenderableWidget(
Button.builder(
Component.translatable("gui.optionsprofiles.delete-profile")
.withStyle(ChatFormatting.RED),
(button) -> {
Profiles.deleteProfile(profileName.getString());
this.minecraft.setScreen(this.lastScreen);
})
.size(100, 20)
.pos(5, this.height - 25)
.build());
this.addRenderableWidget(
Button.builder(
CommonComponents.GUI_DONE,
(button) -> {
profileConfiguration.setKeybindingsOnly(keybindingsOnlyCheckbox.selected());
profileConfiguration.save();
this.minecraft.setScreen(this.lastScreen);
})
.size(100, 20)
.pos(this.width / 2 - 50, this.height - 40)
.build());
}
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {

View file

@ -59,21 +59,27 @@ public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Entr
ProfileEntry(Component profileName) {
this.profileName = profileName;
this.editButton = Button.builder(Component.translatable("gui.optionsprofiles.edit-profile"), (button) -> {
minecraft.setScreen(new EditProfileScreen(profilesScreen, profileName));
}).size(75, 20).createNarration((supplier) -> Component.translatable("gui.optionsprofiles.edit-profile")).build();
this.editButton = Button.builder(
Component.translatable("gui.optionsprofiles.edit-profile"),
(button) -> minecraft.setScreen(new EditProfileScreen(profilesScreen, profileName)))
.size(75, 20)
.build();
this.loadButton = Button.builder(Component.translatable("gui.optionsprofiles.load-profile"), (button) -> {
Profiles.loadProfile(profileName.getString());
this.loadButton = Button.builder(
Component.translatable("gui.optionsprofiles.load-profile"),
(button) -> {
Profiles.loadProfile(profileName.getString());
minecraft.options.load();
minecraft.options.loadSelectedResourcePacks(minecraft.getResourcePackRepository());
minecraft.reloadResourcePacks();
minecraft.options.load();
minecraft.options.loadSelectedResourcePacks(minecraft.getResourcePackRepository());
minecraft.reloadResourcePacks();
minecraft.options.save();
minecraft.options.save();
button.active = false;
}).size(75, 20).createNarration((supplier) -> Component.translatable("gui.optionsprofiles.load-profile")).build();
button.active = false;
})
.size(75, 20)
.build();
this.loadButton.active = !Profiles.isProfileLoaded(profileName.getString());
}

View file

@ -20,15 +20,24 @@ public class ProfilesScreen extends Screen {
this.profilesList = new ProfilesList(this, this.minecraft);
this.addWidget(this.profilesList);
// buttons
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.save-current-options"), (button) -> {
Profiles.createProfile();
this.profilesList.refreshEntries();
}).size(150, 20).pos(this.width / 2 - 155, this.height - 29).build());
this.addRenderableWidget(
Button.builder(
Component.translatable("gui.optionsprofiles.save-current-options"),
(button) -> {
Profiles.createProfile();
this.profilesList.refreshEntries();
})
.size(150, 20)
.pos(this.width / 2 - 155, this.height - 29)
.build());
this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, (button) -> {
this.minecraft.setScreen(this.lastScreen);
}).size(150, 20).pos(this.width / 2 + 5, this.height - 29).build());
this.addRenderableWidget(
Button.builder(
CommonComponents.GUI_DONE,
(button) -> this.minecraft.setScreen(this.lastScreen))
.size(150, 20)
.pos(this.width / 2 + 5, this.height - 29)
.build());
}
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {

View file

@ -18,8 +18,12 @@ public class MixinOptionsScreen extends Screen {
@Inject(at = @At("HEAD"), method = "init")
private void init(CallbackInfo info) {
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.profiles-menu"), (button) -> {
this.minecraft.setScreen(new ProfilesScreen(this));
}).width(100).pos(5, 5).build());
this.addRenderableWidget(
Button.builder(
Component.translatable("gui.optionsprofiles.profiles-menu"),
(button) -> this.minecraft.setScreen(new ProfilesScreen(this)))
.width(100)
.pos(5, 5)
.build());
}
}

View file

@ -12,15 +12,13 @@ import java.nio.file.Path;
public class ProfileConfiguration {
private static final Path profilesDirectory = Profiles.PROFILES_DIRECTORY;
private static Path configurationFile;
private boolean keybindingsOnly = false;
public ProfileConfiguration save(String profileName) {
public ProfileConfiguration save() {
ProfileConfiguration configuration = new ProfileConfiguration();
Path profile = profilesDirectory.resolve(profileName);
Path configurationFile = profile.resolve("configuration.json");
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
@ -38,7 +36,11 @@ public class ProfileConfiguration {
ProfileConfiguration configuration = new ProfileConfiguration();
Path profile = profilesDirectory.resolve(profileName);
Path configurationFile = profile.resolve("configuration.json");
configurationFile = profile.resolve("configuration.json");
if (Files.notExists(configurationFile)) {
configuration.save();
}
try (BufferedReader reader = Files.newBufferedReader(configurationFile)) {
Gson gson = new Gson();

View file

@ -27,13 +27,13 @@ public class Profiles {
paths.filter(Files::isDirectory)
.forEach(path -> {
Path configurationFile = path.resolve("configuration.json");
if (!Files.exists(configurationFile)) {
if (Files.notExists(configurationFile)) {
String profileName = path.getFileName().toString();
// Create configuration.json
new ProfileConfiguration().save(profileName);
ProfileConfiguration.get(profileName);
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile updated", profileName);
OptionsProfilesMod.LOGGER.warn("[Profile '{}']: Profile configuration added", profileName);
}
});
} catch (IOException e) {
@ -89,7 +89,7 @@ public class Profiles {
}
} else {
// Create configuration.json
new ProfileConfiguration().save(profileName);
ProfileConfiguration.get(profileName);
}
copyOptionFile(profile, OPTIONS_FILE);

View file

@ -8,5 +8,6 @@
"gui.optionsprofiles.profile-name-text": "Profile Name",
"gui.optionsprofiles.overwrite-options": "Overwrite",
"gui.optionsprofiles.rename-profile": "Rename",
"gui.optionsprofiles.keybindings-only": "Keybindings Only",
"gui.optionsprofiles.delete-profile": "Delete"
}

View file

@ -8,6 +8,7 @@
"gui.optionsprofiles.profile-name-text": "Имя профиля",
"gui.optionsprofiles.overwrite-options": "Перезаписать",
"gui.optionsprofiles.rename-profile": "Переименовать",
"gui.optionsprofiles.keybindings-only": "Только элементы управления",
"gui.optionsprofiles.delete-profile": "удалить",
"modmenu.summaryTranslation.options-profiles": "Cохраняйте и загружайте профили настроек, не выходя из игры.",

View file

@ -8,6 +8,7 @@
"gui.optionsprofiles.profile-name-text": "Профиль исеме",
"gui.optionsprofiles.overwrite-options": "перезапись",
"gui.optionsprofiles.rename-profile": "переименовывать",
"gui.optionsprofiles.keybindings-only": "Контрольләр генә",
"gui.optionsprofiles.delete-profile": "бетерү",
"modmenu.summaryTranslation.options-profiles": "Хәзерге көйләүләр профильләрен саклагыз һәм уеннан чыгусыз йөкләгез.",

View file

@ -8,5 +8,6 @@
"gui.optionsprofiles.profile-name-text": "预设名",
"gui.optionsprofiles.overwrite-options": "覆盖",
"gui.optionsprofiles.rename-profile": "重命名",
"gui.optionsprofiles.keybindings-only": "仅按键绑定",
"gui.optionsprofiles.delete-profile": "删除"
}