|
| 1 | +package com.fox2code.foxloader.client.gui; |
| 2 | + |
| 3 | +import com.fox2code.foxloader.config.ConfigEntry; |
| 4 | +import com.fox2code.foxloader.config.ConfigKey; |
| 5 | +import com.fox2code.foxloader.config.ConfigMenu; |
| 6 | +import com.fox2code.foxloader.config.ConfigStructure; |
| 7 | +import com.fox2code.foxloader.loader.ModContainer; |
| 8 | +import net.minecraft.client.Minecraft; |
| 9 | +import net.minecraft.src.client.gui.*; |
| 10 | +import org.lwjgl.input.Keyboard; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | + |
| 14 | +final class GuiModConfig extends GuiScreen { |
| 15 | + private final ArrayList<GuiTextFieldModOption> modOptionsTextFields = new ArrayList<>(); |
| 16 | + private String screenTitle = "Options"; |
| 17 | + private final GuiScreen parentScreen; |
| 18 | + private final ModContainer modContainer; |
| 19 | + private final ConfigMenu configMenu; |
| 20 | + private final Object rootInstance; |
| 21 | + private final Object curInstance; |
| 22 | + |
| 23 | + public GuiModConfig(GuiScreen parentScreen, ModContainer modContainer) { |
| 24 | + this(parentScreen, modContainer, modContainer.getConfigObject()); |
| 25 | + } |
| 26 | + |
| 27 | + public GuiModConfig(GuiScreen parentScreen, ModContainer modContainer, Object rootInstance) { |
| 28 | + this(parentScreen, modContainer, ConfigStructure.parseFromClass( |
| 29 | + rootInstance.getClass(), modContainer).rootConfigMenu, rootInstance, rootInstance); |
| 30 | + } |
| 31 | + |
| 32 | + private GuiModConfig(GuiScreen parentScreen, ModContainer modContainer, ConfigMenu configMenu, |
| 33 | + Object rootInstance, Object curInstance) { |
| 34 | + this.parentScreen = parentScreen; |
| 35 | + this.modContainer = modContainer; |
| 36 | + this.configMenu = configMenu; |
| 37 | + this.rootInstance = rootInstance; |
| 38 | + this.curInstance = curInstance; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void initGui() { |
| 43 | + this.controlList.clear(); |
| 44 | + this.modOptionsTextFields.clear(); |
| 45 | + StringTranslate st= StringTranslate.getInstance(); |
| 46 | + this.screenTitle = st.translateKey(this.configMenu.menuName); |
| 47 | + for(int i = 0; i < this.configMenu.configKeys.size(); ++i) { |
| 48 | + ConfigKey configKey = configMenu.configKeys.get(i); |
| 49 | + final int x = this.width / 2 - 155 + i % 2 * 160; |
| 50 | + final int y = this.height / 6 + 24 * (i >> 1); |
| 51 | + String translation = configKey.translation; |
| 52 | + String translated; |
| 53 | + if (translation.equals(translated = st.translateKey(translation))) { |
| 54 | + String name = configKey.configEntry.configName(); |
| 55 | + if (name != null && !name.isEmpty()) { |
| 56 | + translated = name; |
| 57 | + } |
| 58 | + } |
| 59 | + switch (configKey.configElement) { |
| 60 | + case DUPLICATE: |
| 61 | + case BUTTON: { |
| 62 | + GuiSmallButtonModConfig guiSmallButton = new GuiSmallButtonModConfig( |
| 63 | + i, |
| 64 | + this.width / 2 - 155 + i % 2 * 160, |
| 65 | + this.height / 6 + 24 * (i >> 1), |
| 66 | + translated |
| 67 | + ); |
| 68 | + if (configKey.configElement == ConfigKey.ConfigElement.DUPLICATE || |
| 69 | + (configKey.configEntry.type() == ConfigEntry.ConfigEntryType.SUBMENU && |
| 70 | + configKey.getField(this.curInstance) == null)) { |
| 71 | + guiSmallButton.enabled = false; |
| 72 | + } |
| 73 | + if (configKey.configEntry.type() == ConfigEntry.ConfigEntryType.CONFIG) { |
| 74 | + Object value = configKey.getField(this.curInstance); |
| 75 | + if (value instanceof Boolean) { |
| 76 | + value = st.translateKey(((Boolean) value) ? "gui.yes" : "gui.no"); |
| 77 | + } |
| 78 | + guiSmallButton.displayString = translated + ": " + value; |
| 79 | + } |
| 80 | + this.controlList.add(guiSmallButton); |
| 81 | + break; |
| 82 | + } |
| 83 | + case SLIDER: { |
| 84 | + this.controlList.add(new GuiSliderModConfig( |
| 85 | + i, x, y, translated, this.curInstance, configKey)); |
| 86 | + break; |
| 87 | + } |
| 88 | + case TEXT: { |
| 89 | + this.modOptionsTextFields.add(new GuiTextFieldModOption(x, y, this.curInstance, configKey)); |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + if (!this.modOptionsTextFields.isEmpty()) { |
| 95 | + Keyboard.enableRepeatEvents(true); |
| 96 | + } |
| 97 | + |
| 98 | + this.controlList.add(new GuiButton(200, this.width / 2 - 100, this.height / 6 + 168, st.translateKey("gui.done"))); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + protected void actionPerformed(GuiButton button) { |
| 103 | + if (button.enabled) { |
| 104 | + if (button.id < 200 && button instanceof GuiSmallButtonModConfig) { |
| 105 | + GuiSmallButtonModConfig guiSmallButtonModConfig = |
| 106 | + (GuiSmallButtonModConfig) button; |
| 107 | + ConfigKey configKey = this.configMenu.configKeys.get(button.id); |
| 108 | + Class<?> type = configKey.field.getType(); |
| 109 | + switch (configKey.configEntry.type()) { |
| 110 | + case CONFIG: { |
| 111 | + boolean updated = false; |
| 112 | + if (type == boolean.class) { |
| 113 | + configKey.setField(this.curInstance, !(Boolean) |
| 114 | + configKey.getField(this.curInstance)); |
| 115 | + updated = true; |
| 116 | + } else if (type.isEnum()) { |
| 117 | + int ordinal = ((Enum<?>) configKey.getField(this.curInstance)).ordinal(); |
| 118 | + Enum<?>[] enums = (Enum<?>[]) type.getEnumConstants(); |
| 119 | + configKey.setField(this.curInstance, |
| 120 | + enums[(ordinal + 1) % enums.length]); |
| 121 | + updated = true; |
| 122 | + } |
| 123 | + if (updated) { |
| 124 | + configKey.callHandler(this.curInstance); |
| 125 | + Object newValue = configKey.getField(this.curInstance); |
| 126 | + if (newValue instanceof Boolean) { |
| 127 | + newValue = StringTranslate.getInstance().translateKey( |
| 128 | + ((Boolean) newValue) ? "gui.yes" : "gui.no"); |
| 129 | + } |
| 130 | + guiSmallButtonModConfig.displayString = |
| 131 | + guiSmallButtonModConfig.text + ": " + newValue; |
| 132 | + } |
| 133 | + break; |
| 134 | + } |
| 135 | + case SUBMENU: { |
| 136 | + configKey.callHandler(this.curInstance); |
| 137 | + Object instance = configKey.getField(this.curInstance); |
| 138 | + if (this.mc.currentScreen == this && instance != null) { |
| 139 | + if (instance instanceof GuiScreen) { |
| 140 | + this.mc.displayGuiScreen((GuiScreen) instance); |
| 141 | + } else { |
| 142 | + this.mc.displayGuiScreen(new GuiModConfig(this, this.modContainer, |
| 143 | + configKey.configMenu, this.rootInstance, instance)); |
| 144 | + } |
| 145 | + } |
| 146 | + break; |
| 147 | + } |
| 148 | + case LINK: { |
| 149 | + configKey.callHandler(this.curInstance); |
| 150 | + Object link = configKey.getField(this.curInstance); |
| 151 | + if (this.mc.currentScreen == this && link != null) { |
| 152 | + this.mc.displayGuiScreen(new GuiLinkConfirm(this, link.toString())); |
| 153 | + } |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + if (button.id == 200) { |
| 160 | + this.mc.displayGuiScreen(this.parentScreen); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + protected void mouseClicked(int x, int y, int click) { |
| 167 | + super.mouseClicked(x, y, click); |
| 168 | + for (GuiTextFieldModOption guiTextFieldModOption : |
| 169 | + this.modOptionsTextFields) { |
| 170 | + guiTextFieldModOption.mouseClicked(x, y, click); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + protected void keyTyped(char c, int i) { |
| 175 | + for (GuiTextFieldModOption guiTextFieldModOption : |
| 176 | + this.modOptionsTextFields) { |
| 177 | + if (guiTextFieldModOption.isFocused) { |
| 178 | + guiTextFieldModOption.textboxKeyTyped(c, i); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public void drawScreen(int mouseX, int mouseY, float deltaTicks) { |
| 184 | + this.drawDefaultBackground(); |
| 185 | + this.drawCenteredString(this.fontRenderer, this.screenTitle, this.width / 2, 20, 16777215); |
| 186 | + for (GuiTextFieldModOption guiTextFieldModOption : |
| 187 | + this.modOptionsTextFields) { |
| 188 | + guiTextFieldModOption.drawTextBox(); |
| 189 | + } |
| 190 | + |
| 191 | + super.drawScreen(mouseX, mouseY, deltaTicks); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void onGuiClosed() { |
| 196 | + Keyboard.enableRepeatEvents(false); |
| 197 | + if (this.rootInstance == this.modContainer.getConfigObject()) { |
| 198 | + this.modContainer.saveModConfig(); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private static class GuiSmallButtonModConfig extends GuiSmallButton { |
| 203 | + public final String text; |
| 204 | + |
| 205 | + public GuiSmallButtonModConfig(int id, int x, int y, String text) { |
| 206 | + super(id, x, y, text); |
| 207 | + this.text = text; |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | + } |
| 212 | + private static class GuiSliderModConfig extends GuiSlider { |
| 213 | + private final Object curInstance; |
| 214 | + private final ConfigKey configKey; |
| 215 | + private final boolean clamp; |
| 216 | + private final double lowerBounds, upperBounds, boundsSize; |
| 217 | + |
| 218 | + public GuiSliderModConfig(int id, int x, int y, String translate, |
| 219 | + Object curInstance, ConfigKey configKey) { |
| 220 | + super(id, x, y, translate, 0f); |
| 221 | + this.curInstance = curInstance; |
| 222 | + this.configKey = configKey; |
| 223 | + Class<?> cls = configKey.field.getType(); |
| 224 | + this.clamp = cls == byte.class || cls == short.class || cls == int.class || cls == long.class; |
| 225 | + if (this.clamp) { |
| 226 | + this.lowerBounds = Math.ceil(configKey.configEntry.lowerBounds()); |
| 227 | + this.upperBounds = Math.floor(configKey.configEntry.upperBounds()); |
| 228 | + } else { |
| 229 | + this.lowerBounds = configKey.configEntry.lowerBounds(); |
| 230 | + this.upperBounds = configKey.configEntry.upperBounds(); |
| 231 | + } |
| 232 | + this.boundsSize = this.upperBounds -this.lowerBounds; |
| 233 | + if (this.boundsSize <= 0) { |
| 234 | + this.enabled = false; |
| 235 | + } else { |
| 236 | + this.updateSliderValue(); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + private double getModConfigValue() { |
| 241 | + Number number = null; |
| 242 | + try { |
| 243 | + number = (Number) this.configKey.field.get(curInstance); |
| 244 | + } catch (Exception ignored) {} |
| 245 | + return number == null ? this.lowerBounds : number.doubleValue(); |
| 246 | + } |
| 247 | + |
| 248 | + private void setModConfigValue(double value) { |
| 249 | + try { |
| 250 | + this.configKey.field.set(this.curInstance, ConfigStructure.Internal |
| 251 | + .correctNumber(value, this.configKey.field.getType())); |
| 252 | + this.configKey.callHandler(this.curInstance); |
| 253 | + } catch (Exception ignored) {} |
| 254 | + } |
| 255 | + |
| 256 | + @Override |
| 257 | + protected void mouseDragged(Minecraft var1, int var2, int var3) { |
| 258 | + super.mouseDragged(var1, var2, var3); |
| 259 | + if (this.visible && this.dragging) { |
| 260 | + this.applySliderChanges(); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + @Override |
| 265 | + public boolean mousePressed(Minecraft var1, int var2, int var3) { |
| 266 | + boolean b = super.mousePressed(var1, var2, var3); |
| 267 | + if (this.visible && this.dragging) { |
| 268 | + this.applySliderChanges(); |
| 269 | + } |
| 270 | + return b; |
| 271 | + } |
| 272 | + |
| 273 | + private void applySliderChanges() { |
| 274 | + if (this.enabled) { |
| 275 | + this.setModConfigValue((this.sliderValue * this.boundsSize) + this.lowerBounds); |
| 276 | + if (this.clamp) { |
| 277 | + this.updateSliderValue(); |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + private void updateSliderValue() { |
| 283 | + if (this.enabled) { |
| 284 | + this.sliderValue = (float) ((getModConfigValue() - this.lowerBounds) / this.boundsSize); |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + private static class GuiTextFieldModOption extends GuiTextField { |
| 290 | + private final Object curInstance; |
| 291 | + private final ConfigKey configKey; |
| 292 | + |
| 293 | + public GuiTextFieldModOption(int x, int y, Object curInstance, ConfigKey configKey) { |
| 294 | + super(x, y, 150, 20, ""); |
| 295 | + this.curInstance = curInstance; |
| 296 | + this.configKey = configKey; |
| 297 | + String text = this.getModConfigValue(); |
| 298 | + this.setText(text); |
| 299 | + this.setCursorPosition(text.length()); |
| 300 | + int i = (int) configKey.configEntry.upperBounds(); |
| 301 | + this.setMaxStringLength(i > 1 ? i : 255); |
| 302 | + } |
| 303 | + |
| 304 | + private String getModConfigValue() { |
| 305 | + String string = null; |
| 306 | + try { |
| 307 | + string = (String) this.configKey.field.get(curInstance); |
| 308 | + } catch (Exception ignored) {} |
| 309 | + return string == null ? "" : string; |
| 310 | + } |
| 311 | + |
| 312 | + private void setModConfigValue(String value) { |
| 313 | + try { |
| 314 | + this.configKey.field.set(this.curInstance, value); |
| 315 | + this.configKey.callHandler(this.curInstance); |
| 316 | + } catch (Exception ignored) {} |
| 317 | + } |
| 318 | + |
| 319 | + @Override |
| 320 | + public void textboxKeyTyped(char eventChar, int eventKey) { |
| 321 | + super.textboxKeyTyped(eventChar, eventKey); |
| 322 | + if (this.isEnabled && this.isFocused) { |
| 323 | + this.setModConfigValue(this.text); |
| 324 | + } |
| 325 | + } |
| 326 | + } |
| 327 | +} |
0 commit comments