Added an options button. It doesn't do much, yet...

This commit is contained in:
Sollace 2019-02-02 22:34:27 +02:00
parent 23522e4f18
commit eb92e7f92e
4 changed files with 167 additions and 0 deletions

View file

@ -1,5 +1,6 @@
package com.minelittlepony.unicopia; package com.minelittlepony.unicopia;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -12,14 +13,23 @@ import com.minelittlepony.unicopia.network.MsgRequestCapabilities;
import com.minelittlepony.unicopia.player.IPlayer; import com.minelittlepony.unicopia.player.IPlayer;
import com.minelittlepony.unicopia.player.IView; import com.minelittlepony.unicopia.player.IView;
import com.minelittlepony.unicopia.player.PlayerSpeciesList; import com.minelittlepony.unicopia.player.PlayerSpeciesList;
import com.minelittlepony.util.gui.ButtonGridLayout;
import com.minelittlepony.util.gui.UButton;
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.GuiOptions;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.resources.I18n;
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 net.minecraftforge.client.event.ColorHandlerEvent; import net.minecraftforge.client.event.ColorHandlerEvent;
import net.minecraftforge.client.event.EntityViewRenderEvent; import net.minecraftforge.client.event.EntityViewRenderEvent;
import net.minecraftforge.client.event.FOVUpdateEvent; import net.minecraftforge.client.event.FOVUpdateEvent;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType; import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.client.event.RenderLivingEvent; import net.minecraftforge.client.event.RenderLivingEvent;
@ -32,6 +42,8 @@ import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import static com.minelittlepony.util.gui.ButtonGridLayout.*;
@EventBusSubscriber(Side.CLIENT) @EventBusSubscriber(Side.CLIENT)
public class UnicopiaClient extends UClient { public class UnicopiaClient extends UClient {
@ -55,6 +67,7 @@ public class UnicopiaClient extends UClient {
} }
} }
return UConfig.getInstance().getPrefferedRace(); return UConfig.getInstance().getPrefferedRace();
} }
@ -106,6 +119,44 @@ public class UnicopiaClient extends UClient {
return Minecraft.getMinecraft().gameSettings.thirdPersonView; return Minecraft.getMinecraft().gameSettings.thirdPersonView;
} }
@SideOnly(Side.CLIENT)
@SubscribeEvent
public static void onDisplayGui(GuiScreenEvent.InitGuiEvent.Post event) {
if (event.getGui() instanceof GuiOptions) {
addUniButton(event);
}
}
static void addUniButton(GuiScreenEvent.InitGuiEvent.Post event) {
ButtonGridLayout layout = new ButtonGridLayout(event.getButtonList());
GuiButton uni = new UButton(layout.getNextButtonId(), 0, 0, 150, 20, I18n.format("gui.unicopia"), b -> {
Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.BLOCK_ANVIL_USE, 1));
b.displayString = "<< WIP >>";
return false;
});
List<Integer> possibleXCandidates = list(layout.getColumns());
List<Integer> possibleYCandidates = list(layout.getRows());
uni.y = last(possibleYCandidates, 1);
if (layout.getRows()
.filter(y -> layout.getRow(y).size() == 1).count() < 2) {
uni.y += 25;
uni.x = first(possibleXCandidates, 0);
layout.getRow(last(possibleYCandidates, 0)).forEach(button -> {
button.y = Math.max(button.y, uni.y + uni.height + 13);
});
} else {
uni.x = first(possibleXCandidates, 2);
}
layout.getElements().add(uni);
}
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
@SubscribeEvent @SubscribeEvent
public static void preEntityRender(RenderLivingEvent.Pre<?> event) { public static void preEntityRender(RenderLivingEvent.Pre<?> event) {

View file

@ -0,0 +1,90 @@
package com.minelittlepony.util.gui;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.minecraft.client.gui.GuiButton;
public class ButtonGridLayout {
private List<GuiButton> elements;
private Map<Integer, List<GuiButton>> leftLookup = new HashMap<>();
private Map<Integer, List<GuiButton>> topLookup = new HashMap<>();
private Map<Integer, List<GuiButton>> idLookup = new HashMap<>();
private int nextButtonId = -1;
public ButtonGridLayout(List<GuiButton> elements) {
elements.forEach(this::addElement);
this.elements = elements;
}
public int getNextButtonId() {
return nextButtonId++;
}
public GuiButton addElement(GuiButton button) {
if (elements != null) {
elements.add(button);
}
getColumn(button.x).add(button);
getRow(button.y).add(button);
getButton(button.id).add(button);
while (button.id >= nextButtonId) {
getNextButtonId();
}
return button;
}
public List<GuiButton> getElements() {
return elements;
}
public List<GuiButton> getRow(int y) {
return topLookup.computeIfAbsent(y, ArrayList::new);
}
public List<GuiButton> getColumn(int x) {
return leftLookup.computeIfAbsent(x, ArrayList::new);
}
public List<GuiButton> getButton(int id) {
return idLookup.computeIfAbsent(id, ArrayList::new);
}
public Stream<Integer> getButtonIds() {
return idLookup.keySet().stream().sorted();
}
public Stream<Integer> getColumns() {
return leftLookup.keySet().stream().sorted();
}
public Stream<Integer> getRows() {
return topLookup.keySet().stream().sorted();
}
public Stream<Integer> getIds() {
return idLookup.keySet().stream().sorted();
}
public static <T> List<T> list(Stream<T> s) {
return s.collect(Collectors.toList());
}
public static <T> T first(List<T> c, int i) {
return c.get(i);
}
public static <T> T last(List<T> c, int i) {
return first(c, (c.size() - 1) - i);
}
}

View file

@ -0,0 +1,24 @@
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;
}
}

View file

@ -101,6 +101,8 @@ toxicity.fair.name=Fairly Toxic
toxicity.severe.name=Toxic toxicity.severe.name=Toxic
toxicity.lethal.name=Lethal toxicity.lethal.name=Lethal
gui.unicopia=Unicopia...
commands.race.success.self=Your race has been updated commands.race.success.self=Your race has been updated
commands.race.success.otherself=%s changed race to %s commands.race.success.otherself=%s changed race to %s
commands.race.success.other=Changed %s's race to %s commands.race.success.other=Changed %s's race to %s