Start adding a gui for the various mod options

This commit is contained in:
Sollace 2019-02-20 18:55:30 +02:00
parent fba999e0ff
commit 1614a1b8b0
10 changed files with 302 additions and 31 deletions

View file

@ -0,0 +1,64 @@
package com.minelittlepony.gui;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
public class Button extends GuiButton implements IGuiTooltipped<Button> {
private int tipX = 0;
private int tipY = 0;
protected IGuiAction<Button> action;
private List<String> tooltip = null;
@SuppressWarnings("unchecked")
public Button(int x, int y, int width, int height, String label, IGuiAction<? extends Button> callback) {
super(5000, x, y, width, height, GameGui.format(label));
action = (IGuiAction<Button>)callback;
}
public Button setEnabled(boolean enable) {
enabled = enable;
return this;
}
@Override
public Button setTooltip(List<String> tooltip) {
this.tooltip = tooltip;
return this;
}
protected List<String> getTooltip() {
return tooltip;
}
@Override
public void renderToolTip(Minecraft mc, int mouseX, int mouseY) {
List<String> tooltip = getTooltip();
if (visible && isMouseOver() && tooltip != null) {
mc.currentScreen.drawHoveringText(tooltip, mouseX + tipX, mouseY + tipY);
}
}
@Override
public Button setTooltipOffset(int x, int y) {
tipX = x;
tipY = y;
return this;
}
@Override
public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
if (super.mousePressed(mc, mouseX, mouseY)) {
action.perform(this);
return false;
}
return false;
}
}

View file

@ -0,0 +1,62 @@
package com.minelittlepony.gui;
import org.apache.commons.lang3.text.WordUtils;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.SoundEvent;
public abstract class GameGui extends GuiScreen {
protected static String format(String string, Object... pars) {
return string == null ? null : I18n.format(string, pars);
}
/**
* Formats a translation string and returns it in a list wrapped to a given width.
* This can be safely used in initGui, where the fontRenderer is often still null.
*/
protected List<String> formatMultiLine(String string, int width, Object...pars) {
FontRenderer fr = fontRenderer;
if (fr == null) {
fr = Minecraft.getMinecraft().fontRenderer;
}
return fr.listFormattedStringToWidth(format(string, pars), width);
}
protected void playSound(SoundEvent event) {
mc.getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(event, 1));
}
/**
* Converts a given string to title case regardless of initial case.
*/
protected static String toTitleCase(String string) {
return WordUtils.capitalize(string.toLowerCase());
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
drawContents(mouseX, mouseY, partialTicks);
postDrawContents(mouseX, mouseY, partialTicks);
}
protected void drawContents(int mouseX, int mouseY, float partialTicks) {
super.drawScreen(mouseX, mouseY, partialTicks);
}
protected void postDrawContents(int mouseX, int mouseY, float partialTicks) {
buttonList.forEach(button -> {
if (button instanceof IGuiTooltipped) {
((IGuiTooltipped<?>)button).renderToolTip(mc, mouseX, mouseY);
}
});
}
}

View file

@ -0,0 +1,15 @@
package com.minelittlepony.gui;
/**
* Response actions for UI events.
*/
@FunctionalInterface
public interface IGuiAction<T> {
/**
* Performs this action now.
*
* @param value New Value of the field being changed
* @return Adjusted value the field must take on
*/
void perform(T sender);
}

View file

@ -0,0 +1,15 @@
package com.minelittlepony.gui;
/**
* Response actions for UI events.
*/
@FunctionalInterface
public interface IGuiCallback<T> {
/**
* Performs this action now.
*
* @param value New Value of the field being changed
* @return Adjusted value the field must take on
*/
T perform(T value);
}

View file

@ -0,0 +1,20 @@
package com.minelittlepony.gui;
import net.minecraft.client.Minecraft;
import com.google.common.base.Splitter;
import java.util.List;
public interface IGuiTooltipped<T extends IGuiTooltipped<T>> {
T setTooltip(List<String> tooltip);
T setTooltipOffset(int x, int y);
default T setTooltip(String tooltip) {
return setTooltip(Splitter.onPattern("\r?\n|\\\\n").splitToList(GameGui.format(tooltip)));
}
void renderToolTip(Minecraft mc, int mouseX, int mouseY);
}

View file

@ -0,0 +1,44 @@
package com.minelittlepony.gui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
/**
* A simple label for drawing text to a gui screen.
*
* @author Sollace
*
*/
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;
}
@Override
public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
return false;
}
@Override
public void drawButton(Minecraft mc, int mouseX, int mouseY, float partialTicks) {
if (center) {
drawCenteredString(mc.fontRenderer, GameGui.format(text), x, y, color);
} else {
drawString(mc.fontRenderer, GameGui.format(text), x, y, color);
}
}
}

View file

@ -0,0 +1,44 @@
package com.minelittlepony.gui;
import net.minecraft.client.gui.GuiSlider;
import net.minecraft.client.gui.GuiPageButtonList.GuiResponder;
/**
* A slider for sliding.
*
* @author Sollace
*
*/
public class Slider extends GuiSlider {
private static Responder callback;
public Slider(int x, int y, float minIn, float maxIn, float defaultValue, GuiSlider.FormatHelper formatter, IGuiCallback<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 IGuiCallback<Float> action;
private Slider owner;
private Responder(IGuiCallback<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

@ -6,9 +6,11 @@ import java.util.UUID;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.minelittlepony.gui.Button;
import com.minelittlepony.jumpingcastle.api.Target; import com.minelittlepony.jumpingcastle.api.Target;
import com.minelittlepony.unicopia.entity.EntityFakeClientPlayer; import com.minelittlepony.unicopia.entity.EntityFakeClientPlayer;
import com.minelittlepony.unicopia.extern.MineLP; import com.minelittlepony.unicopia.extern.MineLP;
import com.minelittlepony.unicopia.gui.GuiScreenSettings;
import com.minelittlepony.unicopia.init.UEntities; import com.minelittlepony.unicopia.init.UEntities;
import com.minelittlepony.unicopia.init.UParticles; import com.minelittlepony.unicopia.init.UParticles;
import com.minelittlepony.unicopia.input.Keyboard; import com.minelittlepony.unicopia.input.Keyboard;
@ -18,17 +20,14 @@ import com.minelittlepony.unicopia.player.IPlayer;
import com.minelittlepony.unicopia.player.PlayerSpeciesList; import com.minelittlepony.unicopia.player.PlayerSpeciesList;
import com.minelittlepony.unicopia.render.DisguiseRenderer; import com.minelittlepony.unicopia.render.DisguiseRenderer;
import com.minelittlepony.util.gui.ButtonGridLayout; import com.minelittlepony.util.gui.ButtonGridLayout;
import com.minelittlepony.util.gui.UButton;
import com.minelittlepony.util.lang.ClientLocale; import com.minelittlepony.util.lang.ClientLocale;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.world.IInteractionObject; import net.minecraft.world.IInteractionObject;
import static com.minelittlepony.util.gui.ButtonGridLayout.*; import static com.minelittlepony.util.gui.ButtonGridLayout.*;
@ -62,11 +61,10 @@ public class UnicopiaClient extends UClient {
static void addUniButton(List<GuiButton> buttons) { static void addUniButton(List<GuiButton> buttons) {
ButtonGridLayout layout = new ButtonGridLayout(buttons); ButtonGridLayout layout = new ButtonGridLayout(buttons);
GuiButton uni = new UButton(layout.getNextButtonId(), 0, 0, 150, 20, ClientLocale.format("gui.unicopia"), b -> { GuiButton uni = new Button(0, 0, 150, 20, ClientLocale.format("gui.unicopia"), b -> {
Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.BLOCK_ANVIL_USE, 1)); Minecraft mc = Minecraft.getMinecraft();
b.displayString = "<< WIP >>";
return false; mc.displayGuiScreen(new GuiScreenSettings(mc.currentScreen));
}); });
List<Integer> possibleXCandidates = list(layout.getColumns()); List<Integer> possibleXCandidates = list(layout.getColumns());

View file

@ -0,0 +1,33 @@
package com.minelittlepony.unicopia.gui;
import com.minelittlepony.gui.Button;
import com.minelittlepony.gui.GameGui;
import com.minelittlepony.util.lang.ClientLocale;
import net.minecraft.client.gui.GuiScreen;
public class GuiScreenSettings extends GameGui {
private String title = "";
private final GuiScreen parent;
public GuiScreenSettings(GuiScreen parent) {
this.parent = parent;
}
@Override
public void initGui() {
title = ClientLocale.format("options.title");
addButton(new Button(width / 2 - 100, height / 6 + 168, 200, 20, "gui.done", sender -> mc.displayGuiScreen(parent)));
}
@Override
protected void drawContents(int mouseX, int mouseY, float partialTicks) {
drawDefaultBackground();
drawCenteredString(fontRenderer, title, width / 2, 15, 16777215);
super.drawScreen(mouseX, mouseY, partialTicks);
}
}

View file

@ -1,24 +0,0 @@
package com.minelittlepony.util.gui;
import java.util.function.Function;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
public class UButton extends GuiButton {
private final Function<GuiButton, Boolean> action;
public UButton(int id, int x, int y, int w, int h, String label, Function<GuiButton, Boolean> action) {
super(id, x, y, w, h, label);
this.action = action;
}
public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
if (super.mousePressed(mc, mouseX, mouseY)) {
return action.apply(this);
}
return false;
}
}