Added a dedicated lan settings screen

This commit is contained in:
Sollace 2021-08-07 22:31:09 +02:00
parent aa89e883ad
commit 3673d52511
3 changed files with 182 additions and 120 deletions

View file

@ -1,12 +1,14 @@
package com.minelittlepony.unicopia.client; package com.minelittlepony.unicopia.client;
import java.util.Optional; import java.util.Optional;
import com.minelittlepony.common.client.gui.element.Button;
import com.minelittlepony.common.event.ScreenInitCallback; import com.minelittlepony.common.event.ScreenInitCallback;
import com.minelittlepony.common.event.ScreenInitCallback.ButtonList; import com.minelittlepony.common.event.ScreenInitCallback.ButtonList;
import com.minelittlepony.unicopia.InteractionManager; import com.minelittlepony.unicopia.InteractionManager;
import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.client.gui.SettingsScreen; import com.minelittlepony.unicopia.client.gui.LanSettingsScreen;
import com.minelittlepony.unicopia.client.gui.UHud; import com.minelittlepony.unicopia.client.gui.UHud;
import com.minelittlepony.unicopia.client.minelittlepony.MineLPConnector; import com.minelittlepony.unicopia.client.minelittlepony.MineLPConnector;
import com.minelittlepony.unicopia.entity.player.PlayerCamera; import com.minelittlepony.unicopia.entity.player.PlayerCamera;
@ -15,9 +17,11 @@ import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback; import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.OpenToLanScreen;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.world.CreateWorldScreen; import net.minecraft.client.gui.screen.world.CreateWorldScreen;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.TranslatableText;
public class UnicopiaClient implements ClientModInitializer { public class UnicopiaClient implements ClientModInitializer {
@ -69,7 +73,12 @@ public class UnicopiaClient implements ClientModInitializer {
private void onScreenInit(Screen screen, ButtonList buttons) { private void onScreenInit(Screen screen, ButtonList buttons) {
if (screen instanceof CreateWorldScreen) { if (screen instanceof CreateWorldScreen) {
buttons.addButton(SettingsScreen.createRaceSelector(screen)); buttons.addButton(LanSettingsScreen.createRaceSelector(screen));
}
if (screen instanceof OpenToLanScreen) {
buttons.addButton(new Button(screen.width / 2 - 155, 130, 150, 20))
.onClick(b -> MinecraftClient.getInstance().openScreen(new LanSettingsScreen(screen)))
.getStyle().setText(new TranslatableText("unicopia.options.title"));
} }
} }

View file

@ -0,0 +1,169 @@
package com.minelittlepony.unicopia.client.gui;
import java.util.Set;
import com.minelittlepony.common.client.gui.GameGui;
import com.minelittlepony.common.client.gui.ScrollContainer;
import com.minelittlepony.common.client.gui.dimension.Bounds;
import com.minelittlepony.common.client.gui.element.Button;
import com.minelittlepony.common.client.gui.element.Cycler;
import com.minelittlepony.common.client.gui.element.Label;
import com.minelittlepony.common.client.gui.element.Toggle;
import com.minelittlepony.common.client.gui.packing.GridPacker;
import com.minelittlepony.common.client.gui.sprite.TextureSprite;
import com.minelittlepony.unicopia.Config;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper;
public class LanSettingsScreen extends GameGui {
private static final GridPacker WHITELIST_GRID_PACKER = new GridPacker()
.setItemSpacing(25)
.setListWidth(200)
.setItemWidth(30)
.setItemHeight(0);
private final Config config = Unicopia.getConfig();
private final ScrollContainer content = new ScrollContainer();
private boolean forceShowWhitelist;
private boolean forceHideWhitelist;
public LanSettingsScreen(Screen parent) {
super(new TranslatableText("unicopia.options.title"), parent);
content.margin.setVertical(30);
content.getContentPadding().setHorizontal(10);
content.getContentPadding().top = 10;
content.getContentPadding().bottom = 20;
}
@Override
public void init() {
content.init(this::rebuildContent);
}
private void rebuildContent() {
int LEFT = content.width / 2 - 100;
IntegratedServer server = client.getServer();
// In game
// Singleplayer server != null && !server.isRemote();
// Lan (hosting) server != null && server.isRemote();
// Multiplayer server == null
// Out of game
// server == null
final boolean canEditWhitelist = client.world == null || server != null;
getChildElements().add(content);
addButton(new Label(width / 2, 5).setCentered()).getStyle().setText(getTitle().getString());
addButton(new Button(width / 2 - 100, height - 25))
.onClick(sender -> finish())
.getStyle()
.setText("gui.done");
int row = 0;
content.addButton(new Label(LEFT, row += 20)).getStyle().setText("unicopia.options.lan");
Set<Race> whitelist = config.speciesWhiteList.get();
boolean whitelistEnabled = (forceShowWhitelist || !whitelist.isEmpty()) && !forceHideWhitelist;
if (whitelist.isEmpty() && forceShowWhitelist) {
for (Race r : Race.all()) {
if (!r.isDefault()) {
whitelist.add(r);
}
}
}
content.addButton(new Toggle(LEFT, row += 20, whitelistEnabled))
.onChange(v -> {
forceShowWhitelist = v;
forceHideWhitelist = !v;
init();
return v;
})
.setEnabled(canEditWhitelist)
.getStyle().setText("unicopia.options.whitelist");
if (whitelistEnabled) {
content.addButton(new Label(LEFT, row += 20)).getStyle().setText(new TranslatableText("unicopia.options.whitelist.details").formatted(Formatting.DARK_GREEN));
row += 20;
WHITELIST_GRID_PACKER.start();
for (Race race : Race.all()) {
if (!race.isDefault()) {
Bounds bound = WHITELIST_GRID_PACKER.next();
Button button = content.addButton(new Toggle(LEFT + bound.left + 10, row + bound.top, whitelist.contains(race)))
.onChange(v -> {
if (v) {
whitelist.add(race);
} else {
whitelist.remove(race);
}
return v;
})
.setEnabled(canEditWhitelist)
.setStyle(race.createStyle());
((TextureSprite)button.getStyle().getIcon()).setPosition(-20, 0);
}
}
}
}
@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta) {
renderBackground(matrices);
super.render(matrices, mouseX, mouseY, tickDelta);
content.render(matrices, mouseX, mouseY, tickDelta);
}
@Override
public void removed() {
if (forceHideWhitelist) {
config.speciesWhiteList.get().clear();
}
config.save();
}
public static Cycler createRaceSelector(Screen screen) {
return new Cycler(screen.width / 2 + 110, 60, 20, 20) {
@Override
protected void renderForground(MatrixStack matrices, MinecraftClient mc, int mouseX, int mouseY, int foreColor) {
super.renderForground(matrices, mc, mouseX, mouseY, foreColor);
if (isMouseOver(mouseX, mouseY)) {
renderToolTip(matrices, screen, mouseX, mouseY);
}
}
}.setStyles(
Race.EARTH.createStyle(),
Race.UNICORN.createStyle(),
Race.PEGASUS.createStyle(),
Race.BAT.createStyle(),
Race.ALICORN.createStyle(),
Race.CHANGELING.createStyle()
).onChange(i -> {
Unicopia.getConfig().preferredRace.set(Race.fromId(i + 1));
Unicopia.getConfig().save();
return i;
}).setValue(MathHelper.clamp(Unicopia.getConfig().preferredRace.get().ordinal() - 1, 0, 5));
}
}

View file

@ -1,22 +1,15 @@
package com.minelittlepony.unicopia.client.gui; package com.minelittlepony.unicopia.client.gui;
import java.util.Set;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.common.client.gui.GameGui; import com.minelittlepony.common.client.gui.GameGui;
import com.minelittlepony.common.client.gui.ScrollContainer; import com.minelittlepony.common.client.gui.ScrollContainer;
import com.minelittlepony.common.client.gui.dimension.Bounds;
import com.minelittlepony.common.client.gui.element.Button; import com.minelittlepony.common.client.gui.element.Button;
import com.minelittlepony.common.client.gui.element.Cycler;
import com.minelittlepony.common.client.gui.element.EnumSlider; import com.minelittlepony.common.client.gui.element.EnumSlider;
import com.minelittlepony.common.client.gui.element.Label; import com.minelittlepony.common.client.gui.element.Label;
import com.minelittlepony.common.client.gui.element.Toggle; import com.minelittlepony.common.client.gui.element.Toggle;
import com.minelittlepony.common.client.gui.packing.GridPacker;
import com.minelittlepony.common.client.gui.sprite.TextureSprite;
import com.minelittlepony.common.client.gui.style.Style; import com.minelittlepony.common.client.gui.style.Style;
import com.minelittlepony.unicopia.Config; import com.minelittlepony.unicopia.Config;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.WorldTribeManager; import com.minelittlepony.unicopia.WorldTribeManager;
import com.minelittlepony.unicopia.client.minelittlepony.MineLPConnector; import com.minelittlepony.unicopia.client.minelittlepony.MineLPConnector;
@ -30,16 +23,8 @@ import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper;
public class SettingsScreen extends GameGui { public class SettingsScreen extends GameGui {
private static final GridPacker WHITELIST_GRID_PACKER = new GridPacker()
.setItemSpacing(25)
.setListWidth(200)
.setItemWidth(30)
.setItemHeight(0);
private final Config config = Unicopia.getConfig(); private final Config config = Unicopia.getConfig();
private final ScrollContainer content = new ScrollContainer(); private final ScrollContainer content = new ScrollContainer();
@ -47,9 +32,6 @@ public class SettingsScreen extends GameGui {
@Nullable @Nullable
private Style mineLpStatus; private Style mineLpStatus;
private boolean forceShowWhitelist;
private boolean forceHideWhitelist;
public SettingsScreen(Screen parent) { public SettingsScreen(Screen parent) {
super(new TranslatableText("unicopia.options.title"), parent); super(new TranslatableText("unicopia.options.title"), parent);
@ -66,28 +48,7 @@ public class SettingsScreen extends GameGui {
private void rebuildContent() { private void rebuildContent() {
int LEFT = content.width / 2 - 210; int LEFT = content.width / 2 - 100;
int RIGHT = content.width / 2 + 10;
IntegratedServer server = client.getServer();
// In game
// Singleplayer server != null && !server.isRemote();
// Lan (hosting) server != null && server.isRemote();
// Multiplayer server == null
// Out of game
// server == null
final boolean showLanOptions = server == null || server.isRemote();
final boolean canEditWhitelist = client.world == null || server != null && server.isRemote();
boolean singleColumn = LEFT < 0 || !showLanOptions;
if (singleColumn) {
LEFT = content.width / 2 - 100;
RIGHT = LEFT;
}
getChildElements().add(content); getChildElements().add(content);
@ -117,6 +78,7 @@ public class SettingsScreen extends GameGui {
.onChange(config.preferredRace::set) .onChange(config.preferredRace::set)
.setTextFormat(v -> new TranslatableText("unicopia.options.preferred_race", v.getValue().getDisplayName())); .setTextFormat(v -> new TranslatableText("unicopia.options.preferred_race", v.getValue().getDisplayName()));
IntegratedServer server = client.getServer();
if (server != null) { if (server != null) {
row += 20; row += 20;
content.addButton(new Label(LEFT, row)).getStyle().setText("unicopia.options.world"); content.addButton(new Label(LEFT, row)).getStyle().setText("unicopia.options.world");
@ -128,57 +90,6 @@ public class SettingsScreen extends GameGui {
.setTextFormat(v -> new TranslatableText("unicopia.options.world.default_race", v.getValue().getDisplayName())) .setTextFormat(v -> new TranslatableText("unicopia.options.world.default_race", v.getValue().getDisplayName()))
.setEnabled(client.isInSingleplayer()); .setEnabled(client.isInSingleplayer());
} }
if (showLanOptions) {
if (RIGHT != LEFT) {
row = 0;
} else {
row += 20;
}
content.addButton(new Label(RIGHT, row)).getStyle().setText("unicopia.options.lan");
Set<Race> whitelist = config.speciesWhiteList.get();
boolean whitelistEnabled = (forceShowWhitelist || !whitelist.isEmpty()) && !forceHideWhitelist;
content.addButton(new Toggle(RIGHT, row += 20, whitelistEnabled))
.onChange(v -> {
forceShowWhitelist = v;
forceHideWhitelist = !v;
init();
return v;
})
.setEnabled(canEditWhitelist)
.getStyle().setText("unicopia.options.whitelist");
if (whitelistEnabled) {
content.addButton(new Label(RIGHT, row += 20)).getStyle().setText(new TranslatableText("unicopia.options.whitelist.details").formatted(Formatting.DARK_GREEN));
row += 20;
WHITELIST_GRID_PACKER.start();
for (Race race : Race.values()) {
if (!race.isDefault()) {
Bounds bound = WHITELIST_GRID_PACKER.next();
Button button = content.addButton(new Toggle(RIGHT + bound.left + 10, row + bound.top, whitelist.contains(race)))
.onChange(v -> {
if (v) {
whitelist.add(race);
} else {
whitelist.remove(race);
}
return v;
})
.setEnabled(canEditWhitelist)
.setStyle(race.createStyle());
((TextureSprite)button.getStyle().getIcon()).setPosition(-20, 0);
}
}
}
}
} }
private Text getMineLPStatus() { private Text getMineLPStatus() {
@ -204,33 +115,6 @@ public class SettingsScreen extends GameGui {
@Override @Override
public void removed() { public void removed() {
if (forceHideWhitelist) {
config.speciesWhiteList.get().clear();
}
config.save(); config.save();
} }
public static Cycler createRaceSelector(Screen screen) {
return new Cycler(screen.width / 2 + 110, 60, 20, 20) {
@Override
protected void renderForground(MatrixStack matrices, MinecraftClient mc, int mouseX, int mouseY, int foreColor) {
super.renderForground(matrices, mc, mouseX, mouseY, foreColor);
if (isMouseOver(mouseX, mouseY)) {
renderToolTip(matrices, screen, mouseX, mouseY);
}
}
}.setStyles(
Race.EARTH.createStyle(),
Race.UNICORN.createStyle(),
Race.PEGASUS.createStyle(),
Race.BAT.createStyle(),
Race.ALICORN.createStyle(),
Race.CHANGELING.createStyle()
).onChange(i -> {
Unicopia.getConfig().preferredRace.set(Race.fromId(i + 1));
Unicopia.getConfig().save();
return i;
}).setValue(MathHelper.clamp(Unicopia.getConfig().preferredRace.get().ordinal() - 1, 0, 5));
}
} }