Rewrite the settings gui and add it to the liteloader panel

This commit is contained in:
Sollace 2018-05-13 18:43:49 +02:00
parent 46180b9691
commit dabd9ecad0
11 changed files with 332 additions and 195 deletions

View file

@ -1,13 +1,17 @@
package com.minelittlepony; package com.minelittlepony;
import com.minelittlepony.gui.PonySettingsPanel;
import com.mumfrey.liteloader.Configurable;
import com.mumfrey.liteloader.InitCompleteListener; import com.mumfrey.liteloader.InitCompleteListener;
import com.mumfrey.liteloader.Tickable; import com.mumfrey.liteloader.Tickable;
import com.mumfrey.liteloader.core.LiteLoader; import com.mumfrey.liteloader.core.LiteLoader;
import com.mumfrey.liteloader.modconfig.ConfigPanel;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import java.io.File; import java.io.File;
public class LiteModMineLittlePony implements Tickable, InitCompleteListener { public class LiteModMineLittlePony implements Tickable, InitCompleteListener, Configurable {
private MineLittlePony mlp; private MineLittlePony mlp;
@ -39,4 +43,9 @@ public class LiteModMineLittlePony implements Tickable, InitCompleteListener {
public void onTick(Minecraft minecraft, float partialTicks, boolean inGame, boolean clock) { public void onTick(Minecraft minecraft, float partialTicks, boolean inGame, boolean clock) {
mlp.onTick(minecraft, inGame); mlp.onTick(minecraft, inGame);
} }
@Override
public Class<? extends ConfigPanel> getConfigPanelClass() {
return PonySettingsPanel.class;
}
} }

View file

@ -1,5 +1,6 @@
package com.minelittlepony; package com.minelittlepony;
import com.minelittlepony.gui.GuiPonySettings;
import com.minelittlepony.hdskins.gui.GuiSkinsMineLP; import com.minelittlepony.hdskins.gui.GuiSkinsMineLP;
import com.minelittlepony.pony.data.IPonyData; import com.minelittlepony.pony.data.IPonyData;
import com.minelittlepony.pony.data.PonyDataSerialzier; import com.minelittlepony.pony.data.PonyDataSerialzier;
@ -81,7 +82,7 @@ public class MineLittlePony {
void onTick(Minecraft minecraft, boolean inGame) { void onTick(Minecraft minecraft, boolean inGame) {
if (inGame && minecraft.currentScreen == null && SETTINGS_GUI.isPressed()) { if (inGame && minecraft.currentScreen == null && SETTINGS_GUI.isPressed()) {
minecraft.displayGuiScreen(new PonySettingPanel()); minecraft.displayGuiScreen(new GuiPonySettings());
} }
boolean skins = minecraft.currentScreen instanceof GuiSkins boolean skins = minecraft.currentScreen instanceof GuiSkins

View file

@ -1,192 +0,0 @@
package com.minelittlepony;
import com.minelittlepony.pony.data.PonyLevel;
import com.mumfrey.liteloader.client.gui.GuiCheckbox;
import com.mumfrey.liteloader.core.LiteLoader;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import java.io.IOException;
/**
* In-Game options menu.
*
* TODO: What a mess
*/
public class PonySettingPanel extends GuiScreen {
private static final String _PREFIX = "minelp.options.";
private static final String TITLE = _PREFIX + "title";
private static final String PONY_LEVEL = _PREFIX + "ponylevel";
private static final String PONY = PONY_LEVEL + ".ponies";
private static final String HUMAN = PONY_LEVEL + ".humans";
private static final String BOTH = PONY_LEVEL + ".both";
private static final String OPTIONS = _PREFIX + "options";
private static final String HD = _PREFIX + "hd";
private static final String SIZES = _PREFIX + "sizes";
private static final String SNUZZLES = _PREFIX + "snuzzles";
private static final String SHOW_SCALE = _PREFIX + "showscale";
private static final String MOB_PREFIX = "minelp.mobs.";
private static final String MOB_TITLE = MOB_PREFIX + "title";
private static final String VILLAGERS = MOB_PREFIX + "villagers";
private static final String ZOMBIES = MOB_PREFIX + "zombies";
private static final String ZOMBIE_PIGMEN = MOB_PREFIX + "zombiepigmen";
private static final String SKELETONS = MOB_PREFIX + "skeletons";
private static final String ILLAGERS = MOB_PREFIX + "illagers";
private static final String GUARDIANS = MOB_PREFIX + "guardians";
private static final int PONY_ID = 0;
private static final int HUMAN_ID = 1;
private static final int BOTH_ID = 2;
private static final int HD_ID = 3;
private static final int SIZES_ID = 4;
private static final int SNUZZLES_ID = 5;
private static final int SHOW_SCALE_ID = 6;
private static final int VILLAGERS_ID = 7;
private static final int ZOMBIES_ID = 8;
private static final int ZOMBIE_PIGMEN_ID = 9;
private static final int SKELETONS_ID = 10;
private static final int ILLAGER_ID = 11;
private static final int GUARDIAN_ID = 12;
private PonyConfig config;
private GuiCheckbox ponies;
private GuiCheckbox humans;
private GuiCheckbox both;
public PonySettingPanel() {
config = MineLittlePony.getConfig();
}
@SuppressWarnings("UnusedAssignment")
@Override
public void initGui() {
final int LEFT = width / 10 + 16;
GuiCheckbox pony, human, both, hd, sizes, snuzzles, showscale, villager, zombie, pigmen, skeleton, illager, guardian;
int row = 32;
buttonList.add(pony = ponies = new GuiCheckbox(PONY_ID, LEFT, row += 15, I18n.format(PONY)));
buttonList.add(human = humans = new GuiCheckbox(HUMAN_ID, LEFT, row += 15, I18n.format(HUMAN)));
buttonList.add(both = this.both = new GuiCheckbox(BOTH_ID, LEFT, row += 15, I18n.format(BOTH)));
row += 15;
buttonList.add(hd = new GuiCheckbox(HD_ID, LEFT, row += 15, I18n.format(HD)));
buttonList.add(snuzzles = new GuiCheckbox(SNUZZLES_ID, LEFT, row += 15, I18n.format(SNUZZLES)));
buttonList.add(sizes = new GuiCheckbox(SIZES_ID, LEFT, row += 15, I18n.format(SIZES)));
buttonList.add(showscale = new GuiCheckbox(SHOW_SCALE_ID, LEFT, row += 15, I18n.format(SHOW_SCALE)));
final int RIGHT = width - width / 3;
row = 32;
buttonList.add(villager = new GuiCheckbox(VILLAGERS_ID, RIGHT, row += 15, I18n.format(VILLAGERS)));
buttonList.add(zombie = new GuiCheckbox(ZOMBIES_ID, RIGHT, row += 15, I18n.format(ZOMBIES)));
buttonList.add(pigmen = new GuiCheckbox(ZOMBIE_PIGMEN_ID, RIGHT, row += 15, I18n.format(ZOMBIE_PIGMEN)));
buttonList.add(skeleton = new GuiCheckbox(SKELETONS_ID, RIGHT, row += 15, I18n.format(SKELETONS)));
buttonList.add(illager = new GuiCheckbox(ILLAGER_ID, RIGHT, row += 15, I18n.format(ILLAGERS)));
buttonList.add(guardian = new GuiCheckbox(GUARDIAN_ID, RIGHT, row += 15, I18n.format(GUARDIANS)));
switch (config.getPonyLevel()) {
default:
case PONIES:
pony.checked = true;
break;
case HUMANS:
human.checked = true;
break;
case BOTH:
both.checked = true;
break;
}
hd.checked = config.hd;
sizes.checked = config.sizes;
snuzzles.checked = config.snuzzles;
showscale.checked = config.showscale;
villager.checked = config.villagers;
zombie.checked = config.zombies;
pigmen.checked = config.pigzombies;
skeleton.checked = config.skeletons;
illager.checked = config.illagers;
guardian.checked = config.guardians;
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawDefaultBackground();
drawCenteredString(mc.fontRenderer, I18n.format(TITLE), width / 2, 12, -1);
drawString(mc.fontRenderer, I18n.format(MOB_TITLE), width - width / 3 - 16, 32, -1);
drawString(mc.fontRenderer, I18n.format(PONY_LEVEL), width / 10, 32, -1);
drawString(mc.fontRenderer, I18n.format(OPTIONS), width / 10, 94, -1);
super.drawScreen(mouseX, mouseY, partialTicks);
}
@Override
protected void actionPerformed(GuiButton button) throws IOException {
if (button instanceof GuiCheckbox) {
boolean checked = !((GuiCheckbox) button).checked;
((GuiCheckbox) button).checked = checked;
switch (button.id) {
case PONY_ID:
config.setPonyLevel(PonyLevel.PONIES);
ponies.checked = true;
humans.checked = false;
both.checked = false;
break;
case HUMAN_ID:
config.setPonyLevel(PonyLevel.HUMANS);
humans.checked = true;
ponies.checked = false;
both.checked = false;
break;
case BOTH_ID:
config.setPonyLevel(PonyLevel.BOTH);
both.checked = true;
ponies.checked = false;
humans.checked = false;
break;
case HD_ID:
config.hd = checked;
break;
case SIZES_ID:
config.sizes = checked;
break;
case SNUZZLES_ID:
config.snuzzles = checked;
break;
case SHOW_SCALE_ID:
config.showscale = checked;
break;
case VILLAGERS_ID:
config.villagers = checked;
break;
case ZOMBIES_ID:
config.zombies = checked;
break;
case ZOMBIE_PIGMEN_ID:
config.pigzombies = checked;
break;
case SKELETONS_ID:
config.skeletons = checked;
break;
case ILLAGER_ID:
config.illagers = checked;
break;
case GUARDIAN_ID:
config.guardians = checked;
break;
}
}
}
@Override
public void onGuiClosed() {
LiteLoader.getInstance().writeConfig(config);
MineLittlePony.getInstance().getRenderManager().initializeMobRenderers(mc.getRenderManager(), config);
}
}

View file

@ -0,0 +1,22 @@
package com.minelittlepony.gui;
import com.mumfrey.liteloader.client.gui.GuiCheckbox;
import net.minecraft.client.resources.I18n;
public class Checkbox extends GuiCheckbox implements IActionable {
private final IGUIAction<Boolean> action;
public Checkbox(int x, int y, String displayString, boolean value, IGUIAction<Boolean> callback) {
super(0, x, y, I18n.format(displayString));
action = callback;
checked = value;
}
@Override
public void perform() {
checked = action.perform(!checked);
}
}

View file

@ -0,0 +1,100 @@
package com.minelittlepony.gui;
import java.io.IOException;
import com.minelittlepony.MineLittlePony;
import com.minelittlepony.PonyConfig;
import com.minelittlepony.pony.data.PonyLevel;
import com.mumfrey.liteloader.core.LiteLoader;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
/**
* In-Game options menu.
*
*/
public class GuiPonySettings extends GuiScreen {
private static final String OPTIONS_PREFIX = "minelp.options.";
private static final String PONY_LEVEL = OPTIONS_PREFIX + "ponylevel";
private static final String MOB_PREFIX = "minelp.mobs.";
private PonyConfig config;
public GuiPonySettings() {
config = MineLittlePony.getConfig();
}
@Override
public void initGui() {
final int LEFT = width / 10;
final int RIGHT = mustScroll() ? LEFT : width - width / 3 - 16;
int row = mustScroll() ? 0 : 32;
if (!mustScroll()) {
addButton(new Label(width / 2, 12, getTitle(), -1, true));
}
addButton(new Label(LEFT, row += 15, PONY_LEVEL, -1));
addButton(new Slider(LEFT, row += 15, 0, 2, config.getPonyLevel().ordinal(), (int id, String name, float value) -> {
return I18n.format(PONY_LEVEL + "." + PonyLevel.valueFor(value).name().toLowerCase());
}, v -> {
PonyLevel level = PonyLevel.valueFor(v);
config.setPonyLevel(level);
return (float)level.ordinal();
}));
row += 15;
addButton(new Label(LEFT, row += 15, OPTIONS_PREFIX + "options", -1));
addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "hd", config.hd, v -> config.hd = v));
addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "snuzzles", config.snuzzles, v -> config.snuzzles = v));
addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "sizes", config.sizes, v -> config.sizes = v));
addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "showscale", config.showscale, v -> config.showscale = v));
if (mustScroll()) {
row += 15;
} else {
row = 32;
}
addButton(new Label(RIGHT, row += 15, MOB_PREFIX + "title", -1));
addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "villagers", config.villagers, v -> config.villagers = v));
addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "zombies", config.zombies, v -> config.zombies = v));
addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "zombiepigmen", config.pigzombies, v -> config.pigzombies = v));
addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "skeletons", config.skeletons, v -> config.skeletons = v));
addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "illagers", config.illagers, v -> config.illagers = v));
addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "guardians", config.guardians, v -> config.guardians = v));
}
@Override
protected void actionPerformed(GuiButton button) throws IOException {
if (button instanceof IActionable) {
((IActionable)button).perform();
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawDefaultBackground();
super.drawScreen(mouseX, mouseY, partialTicks);
}
@Override
public void onGuiClosed() {
LiteLoader.getInstance().writeConfig(config);
MineLittlePony.getInstance().getRenderManager().initializeMobRenderers(mc.getRenderManager(), config);
}
protected String getTitle() {
return OPTIONS_PREFIX + "title";
}
protected boolean mustScroll() {
return false;
}
}

View file

@ -0,0 +1,5 @@
package com.minelittlepony.gui;
public interface IActionable {
void perform();
}

View file

@ -0,0 +1,6 @@
package com.minelittlepony.gui;
@FunctionalInterface
public interface IGUIAction<T> {
T perform(T value);
}

View file

@ -0,0 +1,37 @@
package com.minelittlepony.gui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.resources.I18n;
public class Label extends GuiButton {
private boolean center;
private int color;
private String text;
public Label(int x, int y, String translationString, int color) {
this(x, y, translationString, color, false);
}
public Label(int x, int y, String translationString, int color, boolean center) {
super(0, x, y, "");
this.color = color;
this.center = center;
this.text = translationString;
}
public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
return false;
}
public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) {
if (center) {
drawCenteredString(mc.fontRenderer, I18n.format(text), x, y, color);
} else {
drawString(mc.fontRenderer, I18n.format(text), x, y, color);
}
}
}

View file

@ -0,0 +1,103 @@
package com.minelittlepony.gui;
import java.io.IOException;
import com.mumfrey.liteloader.modconfig.ConfigPanel;
import com.mumfrey.liteloader.modconfig.ConfigPanelHost;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.resources.I18n;
/**
* Boilerplate because LiteLoader has to be such a 'special flower' -_-
*/
public class PonySettingsPanel extends GuiPonySettings implements ConfigPanel {
private int contentHeight;
@Override
public String getPanelTitle() {
return I18n.format(getTitle());
}
@Override
public int getContentHeight() {
return contentHeight + 40;
}
@Override
protected <T extends GuiButton> T addButton(T button) {
if (button.y > contentHeight) {
contentHeight = button.y;
}
return super.addButton(button);
}
@Override
public void onPanelShown(ConfigPanelHost host) {
mc = Minecraft.getMinecraft();
width = host.getWidth();
initGui();
}
@Override
public void onPanelResize(ConfigPanelHost host) {
width = host.getWidth();
this.buttonList.clear();
initGui();
}
@Override
public void onPanelHidden() {
onGuiClosed();
}
@Override
public void onTick(ConfigPanelHost host) {
updateScreen();
}
@Override
public void drawPanel(ConfigPanelHost host, int mouseX, int mouseY, float partialTicks) {
drawScreen(mouseX, mouseY, partialTicks);
}
@Override
public void mousePressed(ConfigPanelHost host, int mouseX, int mouseY, int mouseButton) {
try {
mouseClicked(mouseX, mouseY, mouseButton);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void mouseReleased(ConfigPanelHost host, int mouseX, int mouseY, int mouseButton) {
mouseReleased(mouseX, mouseY, mouseButton);
}
@Override
public void mouseMoved(ConfigPanelHost host, int mouseX, int mouseY) {
}
@Override
public void keyPressed(ConfigPanelHost host, char keyChar, int keyCode) {
try {
keyTyped(keyChar, keyCode);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void drawWorldBackground(int tint) {
}
protected boolean mustScroll() {
return true;
}
}

View file

@ -0,0 +1,38 @@
package com.minelittlepony.gui;
import net.minecraft.client.gui.GuiSlider;
import net.minecraft.client.gui.GuiPageButtonList.GuiResponder;
public class Slider extends GuiSlider {
private static Responder callback;
public Slider(int x, int y, float minIn, float maxIn, float defaultValue, GuiSlider.FormatHelper formatter, IGUIAction<Float> action) {
super(callback = new Responder(action), 0, x, y, "", minIn, maxIn, defaultValue, formatter);
callback.owner = this;
callback = null;
}
private static final class Responder implements GuiResponder {
private final IGUIAction<Float> action;
private Slider owner;
private Responder(IGUIAction<Float> callback) {
action = callback;
}
@Override
public void setEntryValue(int id, boolean value) { }
@Override
public void setEntryValue(int id, float value) {
owner.setSliderValue(action.perform(value), false);
}
@Override
public void setEntryValue(int id, String value) { }
}
}

View file

@ -3,5 +3,13 @@ package com.minelittlepony.pony.data;
public enum PonyLevel { public enum PonyLevel {
PONIES, PONIES,
HUMANS, HUMANS,
BOTH BOTH;
public static PonyLevel valueFor(float index) {
PonyLevel[] values = values();
if (index < 0) {
index = 0;
}
return values[(int)Math.round(index) % values.length];
}
} }