|
3 | 3 | import dev.architectury.platform.Platform; |
4 | 4 | import io.github.jamalam360.jamlib.JamLib; |
5 | 5 | import io.github.jamalam360.jamlib.client.config.gui.entry.ConfigEntry; |
| 6 | +import io.github.jamalam360.jamlib.client.gui.SpriteButton; |
6 | 7 | import io.github.jamalam360.jamlib.client.gui.WidgetList; |
7 | 8 | import io.github.jamalam360.jamlib.config.ConfigExtensions; |
8 | 9 | import io.github.jamalam360.jamlib.config.ConfigManager; |
|
12 | 13 | import net.minecraft.client.gui.GuiGraphics; |
13 | 14 | import net.minecraft.client.gui.components.AbstractWidget; |
14 | 15 | import net.minecraft.client.gui.components.Button; |
15 | | -import net.minecraft.client.gui.components.SpriteIconButton; |
16 | 16 | import net.minecraft.client.gui.screens.Screen; |
17 | 17 | import net.minecraft.client.resources.language.I18n; |
18 | 18 | import net.minecraft.network.chat.CommonComponents; |
|
30 | 30 | @ApiStatus.Internal |
31 | 31 | public class ConfigScreen<T> extends Screen { |
32 | 32 |
|
33 | | - protected final ConfigManager<T> manager; |
34 | | - private final Screen parent; |
35 | | - private final List<ConfigEntry<T, ?>> entries; |
36 | | - private WidgetList widgetList; |
37 | | - private Button doneButton; |
38 | | - |
39 | | - public ConfigScreen(ConfigManager<T> manager, Screen parent) { |
40 | | - super(createTitle(manager)); |
41 | | - this.manager = manager; |
42 | | - this.parent = parent; |
43 | | - this.entries = new ArrayList<>(); |
44 | | - } |
45 | | - |
46 | | - @ApiStatus.Internal |
47 | | - public static String createTranslationKey(String modId, String configName, String path) { |
48 | | - if (modId.equals(configName)) { |
49 | | - return "config." + modId + "." + path; |
50 | | - } else { |
51 | | - return "config." + modId + "." + configName + "." + path; |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - protected static Component createTitle(ConfigManager<?> manager) { |
56 | | - String translationKey = createTranslationKey(manager.getModId(), manager.getConfigName(), "title"); |
57 | | - |
58 | | - if (I18n.exists(translationKey)) { |
59 | | - return Component.translatable(translationKey); |
60 | | - } else { |
61 | | - return Component.literal(Platform.getMod(manager.getModId()).getName()); |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - @Override |
66 | | - protected void init() { |
67 | | - super.init(); |
68 | | - |
69 | | - this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> { |
70 | | - this.manager.reloadFromDisk(); |
71 | | - Objects.requireNonNull(this.minecraft).setScreen(this.parent); |
72 | | - }).pos(this.width / 2 - 154, this.height - 28).size(150, 20).build()); |
73 | | - |
74 | | - this.doneButton = this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> { |
75 | | - if (this.hasChanges()) { |
76 | | - this.manager.save(); |
77 | | - } |
78 | | - |
79 | | - Objects.requireNonNull(this.minecraft).setScreen(this.parent); |
80 | | - }).pos(this.width / 2 + 4, this.height - 28).size(150, 20).build()); |
81 | | - |
82 | | - SpriteIconButton editManuallyButton = this.addRenderableWidget( |
83 | | - SpriteIconButton.builder(Component.translatable("config.jamlib.edit_manually"), button -> { |
84 | | - if (this.hasChanges()) { |
85 | | - this.manager.save(); |
86 | | - } |
87 | | - |
88 | | - Util.getPlatform().openFile(Platform.getConfigFolder().resolve(this.manager.getConfigName() + ".json5").toFile()); |
89 | | - Objects.requireNonNull(this.minecraft).setScreen(this.parent); |
90 | | - }, true).sprite(JamLib.id("writable_book"), 16, 16).size(20, 20).build() |
91 | | - ); |
92 | | - editManuallyButton.setX(7); |
93 | | - editManuallyButton.setY(7); |
94 | | - this.widgetList = new WidgetList(this.minecraft, this.width, this.height - 64, 32); |
95 | | - |
96 | | - if (this.entries.isEmpty()) { |
97 | | - for (Field field : this.manager.getConfigClass().getFields()) { |
98 | | - if (field.isAnnotationPresent(HiddenInGui.class)) { |
99 | | - continue; |
100 | | - } |
101 | | - |
102 | | - this.entries.add(ConfigEntry.createFromField(this.manager.getModId(), this.manager.getConfigName(), field)); |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - for (ConfigEntry<T, ?> entry : this.entries) { |
107 | | - this.widgetList.addWidgetGroup(entry.createWidgets(this.width)); |
108 | | - } |
109 | | - |
110 | | - this.addRenderableWidget(this.widgetList); |
111 | | - |
112 | | - if (this.manager.get() instanceof ConfigExtensions<?> ext) { |
113 | | - List<ConfigExtensions.Link> links = ext.getLinks(); |
114 | | - |
115 | | - for (int i = 0; i < links.size(); i++) { |
116 | | - ConfigExtensions.Link link = links.get(i); |
117 | | - SpriteIconButton linkButton = this.addRenderableWidget( |
118 | | - SpriteIconButton.builder(link.getTooltip(), button -> { |
119 | | - try { |
120 | | - Util.getPlatform().openUri(link.getUrl().toURI()); |
121 | | - } catch (Exception e) { |
122 | | - JamLib.LOGGER.error("Failed to open link", e); |
123 | | - } |
124 | | - }, true).sprite(link.getTexture(), 16, 16).size(20, 20).build() |
125 | | - |
126 | | - ); |
127 | | - linkButton.setX(this.width - 30 - (28 * i)); |
128 | | - linkButton.setY(5); |
129 | | - } |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - @Override |
134 | | - public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { |
135 | | - super.render(graphics, mouseX, mouseY, delta); |
136 | | - graphics.drawCenteredString(Minecraft.getInstance().font, this.title, this.width / 2, 12, 0xFFFFFF); |
137 | | - } |
138 | | - |
139 | | - private boolean canExit() { |
140 | | - return this.entries.stream().allMatch(ConfigEntry::isValid); |
141 | | - } |
142 | | - |
143 | | - private boolean hasChanges() { |
144 | | - return this.entries.stream().anyMatch(ConfigEntry::hasChanged); |
145 | | - } |
146 | | - |
147 | | - @Override |
148 | | - public void tick() { |
149 | | - super.tick(); |
150 | | - boolean canExit = this.canExit(); |
151 | | - |
152 | | - if (this.doneButton.active != canExit) { |
153 | | - this.doneButton.active = canExit; |
154 | | - } |
155 | | - |
156 | | - for (int i = 0; i < this.entries.size(); i++) { |
157 | | - ConfigEntry<T, ?> entry = this.entries.get(i); |
158 | | - List<AbstractWidget> widgets = entry.getNewWidgets(this.width); |
159 | | - if (widgets != null) { |
160 | | - this.widgetList.updateWidgetGroup(i, widgets); |
161 | | - } |
162 | | - } |
163 | | - } |
| 33 | + protected final ConfigManager<T> manager; |
| 34 | + private final Screen parent; |
| 35 | + private final List<ConfigEntry<T, ?>> entries; |
| 36 | + private WidgetList widgetList; |
| 37 | + private Button doneButton; |
| 38 | + |
| 39 | + public ConfigScreen(ConfigManager<T> manager, Screen parent) { |
| 40 | + super(createTitle(manager)); |
| 41 | + this.manager = manager; |
| 42 | + this.parent = parent; |
| 43 | + this.entries = new ArrayList<>(); |
| 44 | + } |
| 45 | + |
| 46 | + @ApiStatus.Internal |
| 47 | + public static String createTranslationKey(String modId, String configName, String path) { |
| 48 | + if (modId.equals(configName)) { |
| 49 | + return "config." + modId + "." + path; |
| 50 | + } else { |
| 51 | + return "config." + modId + "." + configName + "." + path; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected static Component createTitle(ConfigManager<?> manager) { |
| 56 | + String translationKey = createTranslationKey(manager.getModId(), manager.getConfigName(), "title"); |
| 57 | + |
| 58 | + if (I18n.exists(translationKey)) { |
| 59 | + return Component.translatable(translationKey); |
| 60 | + } else { |
| 61 | + return Component.literal(Platform.getMod(manager.getModId()).getName()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void init() { |
| 67 | + super.init(); |
| 68 | + this.widgetList = new WidgetList(this.minecraft, this.width, this.height); |
| 69 | + |
| 70 | + if (this.entries.isEmpty()) { |
| 71 | + for (Field field : this.manager.getConfigClass().getFields()) { |
| 72 | + if (field.isAnnotationPresent(HiddenInGui.class)) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + this.entries.add(ConfigEntry.createFromField(this.manager.getModId(), this.manager.getConfigName(), field)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + for (ConfigEntry<T, ?> entry : this.entries) { |
| 81 | + this.widgetList.addWidgetGroup(entry.createWidgets(this.width)); |
| 82 | + } |
| 83 | + |
| 84 | + this.addRenderableWidget(this.widgetList); |
| 85 | + |
| 86 | + if (this.manager.get() instanceof ConfigExtensions<?> ext) { |
| 87 | + List<ConfigExtensions.Link> links = ext.getLinks(); |
| 88 | + |
| 89 | + for (int i = 0; i < links.size(); i++) { |
| 90 | + ConfigExtensions.Link link = links.get(i); |
| 91 | + |
| 92 | + this.addRenderableWidget( |
| 93 | + new SpriteButton(this.width - 30 - (28 * i), 5, 20, 20, link.getTooltip().copy(), link.getTexture(), 16, 16, button -> { |
| 94 | + try { |
| 95 | + Util.getPlatform().openUri(link.getUrl().toURI()); |
| 96 | + } catch (Exception e) { |
| 97 | + JamLib.LOGGER.error("Failed to open link", e); |
| 98 | + } |
| 99 | + }) |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL, button -> { |
| 105 | + this.manager.reloadFromDisk(); |
| 106 | + Objects.requireNonNull(this.minecraft).setScreen(this.parent); |
| 107 | + }).pos(this.width / 2 - 154, this.height - 28).size(150, 20).build()); |
| 108 | + |
| 109 | + this.doneButton = this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, button -> { |
| 110 | + if (this.hasChanges()) { |
| 111 | + this.manager.save(); |
| 112 | + } |
| 113 | + |
| 114 | + Objects.requireNonNull(this.minecraft).setScreen(this.parent); |
| 115 | + }).pos(this.width / 2 + 4, this.height - 28).size(150, 20).build()); |
| 116 | + |
| 117 | + this.addRenderableWidget( |
| 118 | + new SpriteButton(7, 7, 20, 20, Component.translatable("config.jamlib.edit_manually"), JamLib.id("textures/gui/writable_book.png"), 16, 16, button -> { |
| 119 | + if (this.hasChanges()) { |
| 120 | + this.manager.save(); |
| 121 | + } |
| 122 | + |
| 123 | + Util.getPlatform().openFile(Platform.getConfigFolder().resolve(this.manager.getConfigName() + ".json5").toFile()); |
| 124 | + Objects.requireNonNull(this.minecraft).setScreen(this.parent); |
| 125 | + }) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) { |
| 131 | + super.render(graphics, mouseX, mouseY, delta); |
| 132 | + graphics.drawCenteredString(Minecraft.getInstance().font, this.title, this.width / 2, 12, 0xFFFFFF); |
| 133 | + } |
| 134 | + |
| 135 | + private boolean canExit() { |
| 136 | + return this.entries.stream().allMatch(ConfigEntry::isValid); |
| 137 | + } |
| 138 | + |
| 139 | + private boolean hasChanges() { |
| 140 | + return this.entries.stream().anyMatch(ConfigEntry::hasChanged); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void tick() { |
| 145 | + super.tick(); |
| 146 | + boolean canExit = this.canExit(); |
| 147 | + |
| 148 | + if (this.doneButton.active != canExit) { |
| 149 | + this.doneButton.active = canExit; |
| 150 | + } |
| 151 | + |
| 152 | + for (int i = 0; i < this.entries.size(); i++) { |
| 153 | + ConfigEntry<T, ?> entry = this.entries.get(i); |
| 154 | + List<AbstractWidget> widgets = entry.getNewWidgets(this.width); |
| 155 | + if (widgets != null) { |
| 156 | + this.widgetList.updateWidgetGroup(i, widgets); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
164 | 160 | } |
0 commit comments