Skip to content

Commit

Permalink
Refactored screen code, etc.
Browse files Browse the repository at this point in the history
* Also migrated to using GUI sprites
* Also made the name of the origin scroll if it's too long
  • Loading branch information
eggohito committed Oct 4, 2023
1 parent 7f38933 commit cc10de1
Show file tree
Hide file tree
Showing 15 changed files with 473 additions and 287 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ org.gradle.jvmargs=-Xmx4G
# Dependencies
fabric_version=0.89.2+1.20.2
cca_version=5.3.0
apoli_version=e749b05
apoli_version=2c3cd14
reach_version=2.4.0
clothconfig_version=10.0.96
modmenu_version=6.2.1
26 changes: 17 additions & 9 deletions src/main/java/io/github/apace100/origins/origin/Impact.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package io.github.apace100.origins.origin;

import io.github.apace100.origins.Origins;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

public enum Impact {

NONE(0, "none", Formatting.GRAY),
LOW(1, "low", Formatting.GREEN),
MEDIUM(2, "medium", Formatting.YELLOW),
HIGH(3, "high", Formatting.RED);
NONE(0, "none", Formatting.GRAY, Origins.identifier("choose_origin/impact/none")),
LOW(1, "low", Formatting.GREEN, Origins.identifier("choose_origin/impact/low")),
MEDIUM(2, "medium", Formatting.YELLOW, Origins.identifier("choose_origin/impact/medium")),
HIGH(3, "high", Formatting.RED, Origins.identifier("choose_origin/impact/high"));

private int impactValue;
private String translationKey;
private Formatting textStyle;
private final int impactValue;
private final String translationKey;
private final Formatting textStyle;
private final Identifier spriteId;

private Impact(int impactValue, String translationKey, Formatting textStyle) {
private Impact(int impactValue, String translationKey, Formatting textStyle, Identifier spriteId) {
this.translationKey = "origins.gui.impact." + translationKey;
this.impactValue = impactValue;
this.textStyle = textStyle;
this.spriteId = spriteId;
}


public Identifier getSpriteId() {
return spriteId;
}

public int getImpactValue() {
return impactValue;
}
Expand Down
179 changes: 119 additions & 60 deletions src/main/java/io/github/apace100/origins/screen/ChooseOriginScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import io.github.apace100.origins.Origins;
import io.github.apace100.origins.networking.packet.c2s.ChooseOriginC2SPacket;
import io.github.apace100.origins.networking.packet.c2s.ChooseRandomOriginC2SPacket;
import io.github.apace100.origins.origin.Impact;
import io.github.apace100.origins.origin.Origin;
import io.github.apace100.origins.origin.OriginLayer;
import io.github.apace100.origins.origin.OriginRegistry;
import io.github.apace100.origins.origin.*;
import io.github.apace100.origins.registry.ModItems;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.MinecraftClient;
Expand All @@ -20,51 +17,65 @@
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class ChooseOriginScreen extends OriginDisplayScreen {

private final ArrayList<OriginLayer> layerList;
private int currentLayerIndex = 0;
private int currentOriginIndex = 0;
private final List<Origin> originSelection;
private int maxSelection = 0;

private final int currentLayerIndex;

private Origin randomOrigin;

private int currentOriginIndex = 0;
private int maxSelection = 0;


public ChooseOriginScreen(ArrayList<OriginLayer> layerList, int currentLayerIndex, boolean showDirtBackground) {
super(Text.translatable(Origins.MODID + ".screen.choose_origin"), showDirtBackground);

this.layerList = layerList;
this.currentLayerIndex = currentLayerIndex;
this.originSelection = new ArrayList<>(10);
this.originSelection = new ArrayList<>(layerList.size());

PlayerEntity player = MinecraftClient.getInstance().player;
OriginLayer currentLayer = layerList.get(currentLayerIndex);
List<Identifier> originIdentifiers = currentLayer.getOrigins(player);
originIdentifiers.forEach(originId -> {
if (player == null) {
return;
}

OriginLayer currentLayer = getCurrentLayer();
currentLayer.getOrigins().forEach(originId -> {

Origin origin = OriginRegistry.get(originId);
if(origin.isChoosable()) {
ItemStack displayItem = origin.getDisplayItem();
if(displayItem.getItem() == Items.PLAYER_HEAD) {
if(!displayItem.hasNbt() || !displayItem.getNbt().contains("SkullOwner")) {
displayItem.getOrCreateNbt().putString("SkullOwner", player.getDisplayName().getString());
}
}
this.originSelection.add(origin);
if (!origin.isChoosable()) {
return;
}

ItemStack iconStack = origin.getDisplayItem();
if (iconStack.isOf(Items.PLAYER_HEAD) && (!iconStack.hasNbt() || !iconStack.getOrCreateNbt().contains("SkullOwner"))) {
iconStack.getOrCreateNbt().putString("SkullOwner", player.getName().getString());
}

originSelection.add(origin);

});
originSelection.sort((a, b) -> {
int impDelta = a.getImpact().getImpactValue() - b.getImpact().getImpactValue();
return impDelta == 0 ? a.getOrder() - b.getOrder() : impDelta;
});

originSelection.sort(Comparator.comparingInt((Origin o) -> o.getImpact().getImpactValue()).thenComparingInt(Origin::getOrder));
maxSelection = originSelection.size();
if(currentLayer.isRandomAllowed() && currentLayer.getRandomOrigins(player).size() > 0) {

if (currentLayer.isRandomAllowed() && !currentLayer.getRandomOrigins(player).isEmpty()) {
maxSelection += 1;
}
if(maxSelection == 0) {

if (maxSelection == 0) {
openNextLayerScreen();
}
Origin newOrigin = getCurrentOriginInternal();
showOrigin(newOrigin, layerList.get(currentLayerIndex), newOrigin == randomOrigin);

Origin newOrigin = getCurrentOrigin();
showOrigin(newOrigin, getCurrentLayer(), newOrigin == randomOrigin);

}

private void openNextLayerScreen() {
Expand All @@ -78,76 +89,124 @@ public boolean shouldCloseOnEsc() {

@Override
protected void init() {

super.init();
if(maxSelection > 1) {
addDrawableChild(ButtonWidget.builder(Text.of("<"), b -> {
if (maxSelection <= 0) {
return;
}

// Draw the previous origin button
addDrawableChild(ButtonWidget.builder(
Text.of("<"),
button -> {

currentOriginIndex = (currentOriginIndex - 1 + maxSelection) % maxSelection;
Origin newOrigin = getCurrentOriginInternal();
showOrigin(newOrigin, layerList.get(currentLayerIndex), newOrigin == randomOrigin);
}).dimensions(guiLeft - 40, this.height / 2 - 10, 20, 20).build());
addDrawableChild(ButtonWidget.builder(Text.of(">"), b -> {
Origin newOrigin = getCurrentOrigin();

showOrigin(newOrigin, getCurrentLayer(), newOrigin == randomOrigin);

}
).dimensions(guiLeft - 40, height / 2 - 10, 20, 20).build());

// Draw the next origin button
addDrawableChild(ButtonWidget.builder(
Text.of(">"),
button -> {

currentOriginIndex = (currentOriginIndex + 1) % maxSelection;
Origin newOrigin = getCurrentOriginInternal();
showOrigin(newOrigin, layerList.get(currentLayerIndex), newOrigin == randomOrigin);
}).dimensions(guiLeft + windowWidth + 20, this.height / 2 - 10, 20, 20).build());
}
addDrawableChild(ButtonWidget.builder(Text.translatable(Origins.MODID + ".gui.select"), b -> {
Origin newOrigin = getCurrentOrigin();

Identifier originId = getCurrentOrigin().getIdentifier();
Identifier layerId = layerList.get(currentLayerIndex).getIdentifier();
showOrigin(newOrigin, getCurrentLayer(), newOrigin == randomOrigin);

if (currentOriginIndex == originSelection.size()) {
ClientPlayNetworking.send(new ChooseRandomOriginC2SPacket(layerId));
} else {
ClientPlayNetworking.send(new ChooseOriginC2SPacket(layerId, originId));
}
).dimensions(guiLeft + WINDOW_WIDTH + 20, height / 2 - 10, 20, 20).build());

openNextLayerScreen();
// Draw the select origin button
addDrawableChild(ButtonWidget.builder(
Text.translatable(Origins.MODID + ".gui.select"),
button -> {

Identifier originId = super.getCurrentOrigin().getIdentifier();
Identifier layerId = getCurrentLayer().getIdentifier();

if (currentOriginIndex == originSelection.size()) {
ClientPlayNetworking.send(new ChooseRandomOriginC2SPacket(layerId));
} else {
ClientPlayNetworking.send(new ChooseOriginC2SPacket(layerId, originId));
}

openNextLayerScreen();

}
).dimensions(guiLeft + WINDOW_WIDTH / 2 - 50, guiTop + WINDOW_HEIGHT + 5, 100, 20).build());

}).dimensions(guiLeft + windowWidth / 2 - 50, guiTop + windowHeight + 5, 100, 20).build());
}

@Override
protected Text getTitleText() {
if (getCurrentLayer().shouldOverrideChooseOriginTitle()) {
return Text.translatable(getCurrentLayer().getTitleChooseOriginTranslationKey());
}
return Text.translatable(Origins.MODID + ".gui.choose_origin.title", Text.translatable(getCurrentLayer().getTranslationKey()));
public OriginLayer getCurrentLayer() {
return layerList.get(currentLayerIndex);
}

private Origin getCurrentOriginInternal() {
if(currentOriginIndex == originSelection.size()) {
if(randomOrigin == null) {
@Override
public Origin getCurrentOrigin() {

if (currentOriginIndex == originSelection.size()) {

if (randomOrigin == null) {
initRandomOrigin();
}

return randomOrigin;

}

return originSelection.get(currentOriginIndex);

}

@Override
protected Text getTitleText() {
if (getCurrentLayer().shouldOverrideChooseOriginTitle()) {
return Text.translatable(getCurrentLayer().getTitleChooseOriginTranslationKey());
}
return Text.translatable(Origins.MODID + ".gui.choose_origin.title", Text.translatable(getCurrentLayer().getTranslationKey()));
}

private void initRandomOrigin() {

this.randomOrigin = new Origin(Origins.identifier("random"), new ItemStack(ModItems.ORB_OF_ORIGIN), Impact.NONE, -1, Integer.MAX_VALUE);
MutableText randomOriginText = (MutableText)Text.of("");

MutableText randomOriginText = Text.of("").copy();
List<Identifier> randoms = layerList.get(currentLayerIndex).getRandomOrigins(MinecraftClient.getInstance().player);

randoms.sort((ia, ib) -> {

Origin a = OriginRegistry.get(ia);
Origin b = OriginRegistry.get(ib);
int impDelta = a.getImpact().getImpactValue() - b.getImpact().getImpactValue();
return impDelta == 0 ? a.getOrder() - b.getOrder() : impDelta;

int impactDelta = Integer.compare(a.getImpact().getImpactValue(), b.getImpact().getImpactValue());
return impactDelta != 0 ? impactDelta : Integer.compare(a.getOrder(), b.getOrder());

});

for(Identifier id : randoms) {
randomOriginText.append(OriginRegistry.get(id).getName());
randomOriginText.append(Text.of("\n"));
}

setRandomOriginText(randomOriginText);

}

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
if(maxSelection == 0) {

if (maxSelection == 0) {
openNextLayerScreen();
return;
} else {
super.render(context, mouseX, mouseY, delta);
}
super.render(context, mouseX, mouseY, delta);

}

}
Loading

0 comments on commit cc10de1

Please sign in to comment.