diff --git a/src/client/java/com/minelittlepony/client/gui/GuiPonySettings.java b/src/client/java/com/minelittlepony/client/gui/GuiPonySettings.java index c9f6b74d..78ba0bcc 100644 --- a/src/client/java/com/minelittlepony/client/gui/GuiPonySettings.java +++ b/src/client/java/com/minelittlepony/client/gui/GuiPonySettings.java @@ -4,12 +4,12 @@ import net.minecraft.client.gui.GuiScreen; import com.minelittlepony.MineLittlePony; import com.minelittlepony.client.render.entities.MobRenderers; -import com.minelittlepony.common.client.gui.Checkbox; import com.minelittlepony.common.client.gui.GameGui; import com.minelittlepony.common.client.gui.GuiHost; import com.minelittlepony.common.client.gui.IGuiGuest; import com.minelittlepony.common.client.gui.Label; import com.minelittlepony.common.client.gui.Slider; +import com.minelittlepony.common.client.gui.Toggle; import com.minelittlepony.settings.PonyConfig; import com.minelittlepony.settings.PonyLevel; import com.minelittlepony.settings.PonyConfig.PonySettings; @@ -44,28 +44,28 @@ public class GuiPonySettings implements IGuiGuest { } host.addButton(new Label(LEFT, row += 15, PONY_LEVEL, -1)); - host.addButton(new Slider(LEFT, row += 15, 0, 2, config.getPonyLevel().ordinal(), (int id, String name, float value) -> { - return GameGui.format(PONY_LEVEL + "." + PonyLevel.valueFor(value).name().toLowerCase()); - }, v -> { + host.addButton(new Slider(LEFT, row += 15, 0, 2, config.getPonyLevel().ordinal(), v -> { PonyLevel level = PonyLevel.valueFor(v); config.setPonyLevel(level); return (float)level.ordinal(); + }).setFormatter(value -> { + return GameGui.format(PONY_LEVEL + "." + PonyLevel.valueFor(value).name().toLowerCase()); })); if (GuiScreen.isCtrlKeyDown() && GuiScreen.isShiftKeyDown()) { host.addButton(new Label(LEFT, row += 30, "minelp.debug.scale", -1)); - host.addButton(new Slider(LEFT, row += 15, 0.1F, 3, config.getGlobalScaleFactor(), (int id, String name, float value) -> { - return GameGui.format("minelp.debug.scale.value", GameGui.format(describeCurrentScale(value))); - }, v -> { + host.addButton(new Slider(LEFT, row += 15, 0.1F, 3, config.getGlobalScaleFactor(), v -> { config.setGlobalScaleFactor(v); return config.getGlobalScaleFactor(); + }).setFormatter(value -> { + return GameGui.format("minelp.debug.scale.value", GameGui.format(describeCurrentScale(value))); })); } row += 15; host.addButton(new Label(LEFT, row += 15, OPTIONS_PREFIX + "options", -1)); for (PonySettings i : PonySettings.values()) { - host.addButton(new Checkbox(LEFT, row += 20, OPTIONS_PREFIX + i.name().toLowerCase(), i.get(), i)); + host.addButton(new Toggle(LEFT, row += 20, i.get(), OPTIONS_PREFIX + i.name().toLowerCase(), i)); } if (host.mustScroll()) { @@ -76,7 +76,7 @@ public class GuiPonySettings implements IGuiGuest { host.addButton(new Label(RIGHT, row += 15, MOB_PREFIX + "title", -1)); for (MobRenderers i : MobRenderers.values()) { - host.addButton(new Checkbox(RIGHT, row += 20, MOB_PREFIX + i.name().toLowerCase(), i.get(), i)); + host.addButton(new Toggle(RIGHT, row += 20, i.get(), MOB_PREFIX + i.name().toLowerCase(), i)); } } diff --git a/src/common/java/com/minelittlepony/common/client/gui/Checkbox.java b/src/common/java/com/minelittlepony/common/client/gui/Checkbox.java deleted file mode 100644 index b5bb7e11..00000000 --- a/src/common/java/com/minelittlepony/common/client/gui/Checkbox.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.minelittlepony.common.client.gui; - -import java.util.List; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.resources.I18n; -import net.minecraft.util.ResourceLocation; - -/** - * Checkbox that supports a gui action when it changes. - * - * @author Sollace - * - */ -public class Checkbox extends Slider implements IGuiTooltipped { - - protected static final ResourceLocation RECIPE_BOOK = new ResourceLocation("textures/gui/widgets.png"); - - private int tipX = 0; - private int tipY = 0; - - private List tooltip = null; - - private boolean checked; - - private IGuiCallback switchAction; - - public Checkbox(int x, int y, String displayString, boolean value, IGuiCallback callback) { - super(x, y, 0, 1, (value ? 1 : 0), (i, name, v) -> I18n.format(displayString), null); - - checked = value; - - width = 20; - height = 20; - - switchAction = callback; - action = this::perform; - } - - @Override - public void setSliderValue(float value, boolean notifyResponder) { - super.setSliderValue(value >= 0.5F ? 1 : 0, notifyResponder); - } - - protected float perform(float v) { - boolean value = v >= 0.5F; - - if (value != checked) { - checked = switchAction.perform(value); - } - - return checked ? 1 : 0; - } - - @Override - public Checkbox setTooltip(List tooltip) { - this.tooltip = tooltip; - return this; - } - - @Override - public void renderToolTip(Minecraft mc, int mouseX, int mouseY) { - if (visible && isMouseOver() && tooltip != null) { - mc.currentScreen.drawHoveringText(tooltip, mouseX + tipX, mouseY + tipY); - } - } - - @Override - public Checkbox setTooltipOffset(int x, int y) { - tipX = x; - tipY = y; - return this; - } - - @Override - public void drawCenteredString(FontRenderer fonts, String text, int x, int y, int color) { - super.drawString(fonts, text, x + width - 5, y, color); - } -} diff --git a/src/common/java/com/minelittlepony/common/client/gui/Slider.java b/src/common/java/com/minelittlepony/common/client/gui/Slider.java index 83d36c4b..d0f06863 100644 --- a/src/common/java/com/minelittlepony/common/client/gui/Slider.java +++ b/src/common/java/com/minelittlepony/common/client/gui/Slider.java @@ -1,49 +1,88 @@ package com.minelittlepony.common.client.gui; -import net.minecraft.client.gui.GuiSlider; +import net.minecraft.client.gui.GuiButton; +import javax.annotation.Nonnull; import javax.annotation.Nullable; -import net.minecraft.client.gui.GuiPageButtonList.GuiResponder; +import java.util.function.Function; /** * A slider for sliding. * * @author Sollace - * */ -// TODO: They tuk it. I's gone, capt'n! -public class Slider extends GuiSlider { +public class Slider extends GuiButton { - private static Responder callback; + private float min; + private float max; + + private float value; protected IGuiCallback action; - public Slider(int x, int y, float minIn, float maxIn, float defaultValue, GuiSlider.FormatHelper formatter, IGuiCallback action) { - super(callback = new Responder(), 0, x, y, "", minIn, maxIn, defaultValue, formatter); - callback.owner = this; - callback = null; + @Nullable + private Function formatter; + public Slider(int x, int y, float min, float max, float value, IGuiCallback action) { + super(0, x, y, ""); + + this.min = min; + this.max = max; + this.value = value; this.action = action; } - private static final class Responder implements GuiResponder { + public Slider setFormatter(@Nonnull Function formatter) { + this.formatter = formatter; + this.displayString = formatter.apply(getValue()); - @Nullable - private Slider owner; + return this; + } - @Override - public void setEntryValue(int id, boolean value) { } + public void setValue(float value) { + value = clamp(value, min, max); + value = (value - min) / (max - min); - @Override - public void setEntryValue(int id, float value) { - if (owner != null) { - owner.setSliderValue(owner.action.perform(value), false); - } + if (value != this.value) { + this.value = action.perform(value); } - @Override - public void setEntryValue(int id, String value) { } + if (formatter != null) { + displayString = formatter.apply(getValue()); + } + } + public float getValue() { + return value; + } + + @Override + public void onClick(double mouseX, double mouseY) { + super.onClick(mouseX, mouseY); + setValue((float)mouseX - (x + 4) / (float)(width - 8)); + } + + @Override + protected void onDrag(double mouseX, double mouseY, double mouseDX, double mouseDY) { + setValue((float)mouseX - (x + 4) / (float)(width - 8)); + } + + @Override + public void render(int mouseX, int mouseY, float partialTicks) { + super.render(mouseX, mouseY, partialTicks); + + int i = hovered ? 2 : 1; + drawTexturedModalRect(x + (int)(value * (width - 8)), y, 0, 46 + i * 20, 4, 20); + drawTexturedModalRect(x + (int)(value * (width - 8)) + 4, y, 196, 46 + i * 20, 4, 20); + } + + @Override + protected int getHoverState(boolean mouseOver) { + return 0; + } + + protected float clamp(float value, float min, float max) { + return value < min ? min : value > max ? max : value; } } diff --git a/src/common/java/com/minelittlepony/common/client/gui/Toggle.java b/src/common/java/com/minelittlepony/common/client/gui/Toggle.java new file mode 100644 index 00000000..df67dc50 --- /dev/null +++ b/src/common/java/com/minelittlepony/common/client/gui/Toggle.java @@ -0,0 +1,48 @@ +package com.minelittlepony.common.client.gui; + +import net.minecraft.client.gui.GuiButton; + +public class Toggle extends GuiButton { + + private boolean on; + + protected IGuiCallback action; + + public Toggle(int x, int y, boolean value, String label, IGuiCallback callback) { + super(0, x, y, 20, 20, label); + + on = value; + action = callback; + } + + public boolean getValue() { + return on; + } + + public void setValue(boolean value) { + if (value != on) { + on = action.perform(value); + } + } + + @Override + public void onClick(double mouseX, double mouseY) { + super.onClick(mouseX, mouseY); + setValue(!on); + } + + @Override + public void render(int mouseX, int mouseY, float partialTicks) { + super.render(mouseX, mouseY, partialTicks); + + int i = hovered ? 2 : 1; + float value = on ? 1 : 0; + drawTexturedModalRect(x + (int)(value * (width - 8)), y, 0, 46 + i * 20, 4, 20); + drawTexturedModalRect(x + (int)(value * (width - 8)) + 4, y, 196, 46 + i * 20, 4, 20); + } + + @Override + protected int getHoverState(boolean mouseOver) { + return 0; + } +}