Skip to content

Commit

Permalink
Overhaul poses
Browse files Browse the repository at this point in the history
It's now a separate screen
  • Loading branch information
Mrbysco committed Feb 21, 2024
1 parent 83107eb commit f69d75d
Show file tree
Hide file tree
Showing 16 changed files with 650 additions and 122 deletions.
53 changes: 29 additions & 24 deletions common/src/main/java/com/mrbysco/armorposer/Reference.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.mrbysco.armorposer;

import com.mojang.logging.LogUtils;
import com.mrbysco.armorposer.util.PoseData;
import net.minecraft.resources.ResourceLocation;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Reference {
Expand All @@ -21,30 +24,32 @@ public class Reference {

private static Map<String, String> initializePoseMap() {
Map<String, String> poseMap = new LinkedHashMap<>();
poseMap.put("attention", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[0.0f,0.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("walking", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-20.0f,0.0f,-10.0f],LeftLeg:[20.0f,0.0f,0.0f],RightArm:[20.0f,0.0f,10.0f],RightLeg:[-20.0f,0.0f,0.0f]}}");
poseMap.put("running", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[40.0f,0.0f,-10.0f],LeftLeg:[-40.0f,0.0f,0.0f],RightArm:[-40.0f,0.0f,10.0f],RightLeg:[40.0f,0.0f,0.0f]}}");
poseMap.put("pointing", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,20.0f,0.0f],LeftArm:[0.0f,0.0f,-10.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-90.0f,18.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("blocking", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-50.0f,50.0f,0.0f],LeftLeg:[20.0f,0.0f,0.0f],RightArm:[-20.0f,-20.0f,0.0f],RightLeg:[-20.0f,0.0f,0.0f]}}");
poseMap.put("lunging", "{Pose:{Body:[15.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[10.0f,0.0f,-10.0f],LeftLeg:[30.0f,0.0f,0.0f],RightArm:[-60.0f,-10.0f,0.0f],RightLeg:[-15.0f,0.0f,0.0f]}}");
poseMap.put("winning", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[-15.0f,0.0f,0.0f],LeftArm:[10.0f,0.0f,-10.0f],LeftLeg:[15.0f,0.0f,0.0f],RightArm:[-120.0f,-10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("sitting", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-80.0f,-20.0f,0.0f],LeftLeg:[-90.0f,-10.0f,0.0f],RightArm:[-80.0f,20.0f,0.0f],RightLeg:[-90.0f,10.0f,0.0f]}}");
poseMap.put("arabesque", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[-15.0f,0.0f,0.0f],LeftArm:[70.0f,0.0f,-10.0f],LeftLeg:[75.0f,0.0f,0.0f],RightArm:[-140.0f,-10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("cupid", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-75.0f,0.0f,10.0f],LeftLeg:[75.0f,0.0f,0.0f],RightArm:[-90.0f,-10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("point_and_laugh", "{Pose:{Body:[10.0f,7.0f,8.0f],Head:[25.0f,17.0f,-8.0f],LeftArm:[-90.0f,0.0f,20.0f],LeftLeg:[20.0f,30.0f,-10.0f],RightArm:[-8.0f,0.0f,-77.0f],RightLeg:[20.0f,-10.0f,20.0f]}}");
poseMap.put("confident", "{Pose:{Body:[-2.0f,0.0f,0.0f],Head:[-10.0f,20.0f,0.0f],LeftArm:[5.0f,0.0f,0.0f],LeftLeg:[0.0f,-10.0f,-4.0f],RightArm:[5.0f,0.0f,0.0f],RightLeg:[16.0f,2.0f,10.0f]}}");
poseMap.put("salute", "{Pose:{Body:[5.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[29.0f,0.0f,25.0f],LeftLeg:[0.0f,4.0f,2.0f],RightArm:[-124.0f,-51.0f,-35.0f],RightLeg:[0.0f,-4.0f,-2.0f]}}");
poseMap.put("death", "{Pose:{Body:[-90.0f,0.0f,0.0f],Head:[-85.0f,0.0f,0.0f],LeftArm:[-90.0f,-10.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-90.0f,10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("facepalm", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[45.0f,-4.0f,1.0f],LeftArm:[-72.0f,24.0f,47.0f],LeftLeg:[-4.0f,-6.0f,-2.0f],RightArm:[18.0f,-14.0f,0.0f],RightLeg:[25.0f,-2.0f,0.0f]}}");
poseMap.put("lazing", "{Pose:{Body:[5.0f,0.0f,0.0f],Head:[14.0f,-12.0f,6.0f],LeftArm:[-4.0f,-20.0f,-10.0f],LeftLeg:[-88.0f,46.0f,0.0f],RightArm:[-40.0f,20.0f,0.0f],RightLeg:[-88.0f,71.0f,0.0f]}}");
poseMap.put("confused", "{Pose:{Body:[0.0f,13.0f,0.0f],Head:[0.0f,30.0f,0.0f],LeftArm:[145.0f,22.0f,-49.0f],LeftLeg:[-6.0f,0.0f,0.0f],RightArm:[-22.0f,31.0f,10.0f],RightLeg:[6.0f,-20.0f,0.0f]}}");
poseMap.put("formal", "{Pose:{Body:[4.0f,0.0f,0.0f],Head:[4.0f,0.0f,0.0f],LeftArm:[30.0f,-20.0f,21.0f],LeftLeg:[0.0f,0.0f,-5.0f],RightArm:[30.0f,22.0f,-20.0f],RightLeg:[0.0f,0.0f,5.0f]}}");
poseMap.put("sad", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[63.0f,0.0f,0.0f],LeftArm:[-5.0f,0.0f,-5.0f],LeftLeg:[-5.0f,16.0f,-5.0f],RightArm:[-5.0f,0.0f,5.0f],RightLeg:[-5.0f,-10.0f,5.0f]}}");
poseMap.put("joyous", "{Pose:{Body:[-4.0f,0.0f,0.0f],Head:[-11.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,-100.0f],LeftLeg:[-8.0f,0.0f,-60.0f],RightArm:[0.0f,0.0f,100.0f],RightLeg:[-8.0f,0.0f,60.0f]}}");
poseMap.put("stargazing", "{Pose:{Body:[-4.0f,10.0f,0.0f],Head:[-22.0f,25.0f,0.0f],LeftArm:[4.0f,18.0f,0.0f],LeftLeg:[6.0f,24.0f,0.0f],RightArm:[-153.0f,34.0f,-3.0f],RightLeg:[-4.0f,17.0f,2.0f]}}");
poseMap.put("block", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-15.0f,-45.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("item", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-90.0f,0.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]}}");
poseMap.put("random", "{Pose:{Body:[0.0f,90.0f,0.0f],Head:[25.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,-50.0f],LeftLeg:[0.0f,0.0f,-50.0f],RightArm:[0.0f,0.0f,50.0f],RightLeg:[0.0f,0.0f,50.0f]}");
poseMap.put("attention", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[0.0f,0.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("walking", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-20.0f,0.0f,-10.0f],LeftLeg:[20.0f,0.0f,0.0f],RightArm:[20.0f,0.0f,10.0f],RightLeg:[-20.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("running", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[40.0f,0.0f,-10.0f],LeftLeg:[-40.0f,0.0f,0.0f],RightArm:[-40.0f,0.0f,10.0f],RightLeg:[40.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("pointing", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,20.0f,0.0f],LeftArm:[0.0f,0.0f,-10.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-90.0f,18.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("blocking", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-50.0f,50.0f,0.0f],LeftLeg:[20.0f,0.0f,0.0f],RightArm:[-20.0f,-20.0f,0.0f],RightLeg:[-20.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("lunging", "{Pose:{Body:[15.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[10.0f,0.0f,-10.0f],LeftLeg:[30.0f,0.0f,0.0f],RightArm:[-60.0f,-10.0f,0.0f],RightLeg:[-15.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("winning", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[-15.0f,0.0f,0.0f],LeftArm:[10.0f,0.0f,-10.0f],LeftLeg:[15.0f,0.0f,0.0f],RightArm:[-120.0f,-10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("sitting", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-80.0f,-20.0f,0.0f],LeftLeg:[-90.0f,-10.0f,0.0f],RightArm:[-80.0f,20.0f,0.0f],RightLeg:[-90.0f,10.0f,0.0f]},ShowArms:1b}");
poseMap.put("arabesque", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[-15.0f,0.0f,0.0f],LeftArm:[70.0f,0.0f,-10.0f],LeftLeg:[75.0f,0.0f,0.0f],RightArm:[-140.0f,-10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("cupid", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[-75.0f,0.0f,10.0f],LeftLeg:[75.0f,0.0f,0.0f],RightArm:[-90.0f,-10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("point_and_laugh", "{Pose:{Body:[10.0f,7.0f,8.0f],Head:[25.0f,17.0f,-8.0f],LeftArm:[-90.0f,0.0f,20.0f],LeftLeg:[20.0f,30.0f,-10.0f],RightArm:[-8.0f,0.0f,-77.0f],RightLeg:[20.0f,-10.0f,20.0f]},ShowArms:1b}");
poseMap.put("confident", "{Pose:{Body:[-2.0f,0.0f,0.0f],Head:[-10.0f,20.0f,0.0f],LeftArm:[5.0f,0.0f,0.0f],LeftLeg:[0.0f,-10.0f,-4.0f],RightArm:[5.0f,0.0f,0.0f],RightLeg:[16.0f,2.0f,10.0f]},ShowArms:1b}");
poseMap.put("salute", "{Pose:{Body:[5.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[29.0f,0.0f,25.0f],LeftLeg:[0.0f,4.0f,2.0f],RightArm:[-124.0f,-51.0f,-35.0f],RightLeg:[0.0f,-4.0f,-2.0f]},ShowArms:1b}");
poseMap.put("death", "{Pose:{Body:[-90.0f,0.0f,0.0f],Head:[-85.0f,0.0f,0.0f],LeftArm:[-90.0f,-10.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-90.0f,10.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("facepalm", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[45.0f,-4.0f,1.0f],LeftArm:[-72.0f,24.0f,47.0f],LeftLeg:[-4.0f,-6.0f,-2.0f],RightArm:[18.0f,-14.0f,0.0f],RightLeg:[25.0f,-2.0f,0.0f]},ShowArms:1b}");
poseMap.put("lazing", "{Pose:{Body:[5.0f,0.0f,0.0f],Head:[14.0f,-12.0f,6.0f],LeftArm:[-4.0f,-20.0f,-10.0f],LeftLeg:[-88.0f,46.0f,0.0f],RightArm:[-40.0f,20.0f,0.0f],RightLeg:[-88.0f,71.0f,0.0f]},ShowArms:1b}");
poseMap.put("confused", "{Pose:{Body:[0.0f,13.0f,0.0f],Head:[0.0f,30.0f,0.0f],LeftArm:[145.0f,22.0f,-49.0f],LeftLeg:[-6.0f,0.0f,0.0f],RightArm:[-22.0f,31.0f,10.0f],RightLeg:[6.0f,-20.0f,0.0f]},ShowArms:1b}");
poseMap.put("formal", "{Pose:{Body:[4.0f,0.0f,0.0f],Head:[4.0f,0.0f,0.0f],LeftArm:[30.0f,-20.0f,21.0f],LeftLeg:[0.0f,0.0f,-5.0f],RightArm:[30.0f,22.0f,-20.0f],RightLeg:[0.0f,0.0f,5.0f]},ShowArms:1b}");
poseMap.put("sad", "{Pose:{Body:[10.0f,0.0f,0.0f],Head:[63.0f,0.0f,0.0f],LeftArm:[-5.0f,0.0f,-5.0f],LeftLeg:[-5.0f,16.0f,-5.0f],RightArm:[-5.0f,0.0f,5.0f],RightLeg:[-5.0f,-10.0f,5.0f]},ShowArms:1b}");
poseMap.put("joyous", "{Pose:{Body:[-4.0f,0.0f,0.0f],Head:[-11.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,-100.0f],LeftLeg:[-8.0f,0.0f,-60.0f],RightArm:[0.0f,0.0f,100.0f],RightLeg:[-8.0f,0.0f,60.0f]},ShowArms:1b}");
poseMap.put("stargazing", "{Pose:{Body:[-4.0f,10.0f,0.0f],Head:[-22.0f,25.0f,0.0f],LeftArm:[4.0f,18.0f,0.0f],LeftLeg:[6.0f,24.0f,0.0f],RightArm:[-153.0f,34.0f,-3.0f],RightLeg:[-4.0f,17.0f,2.0f]},ShowArms:1b}");
poseMap.put("block", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-15.0f,-45.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("item", "{Pose:{Body:[0.0f,0.0f,0.0f],Head:[0.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,0.0f],LeftLeg:[0.0f,0.0f,0.0f],RightArm:[-90.0f,0.0f,0.0f],RightLeg:[0.0f,0.0f,0.0f]},ShowArms:1b}");
poseMap.put("random", "{Pose:{Body:[0.0f,90.0f,0.0f],Head:[25.0f,0.0f,0.0f],LeftArm:[0.0f,0.0f,-50.0f],LeftLeg:[0.0f,0.0f,-50.0f],RightArm:[0.0f,0.0f,50.0f],RightLeg:[0.0f,0.0f,50.0f]},ShowArms:1b}");
return poseMap;
}

public static final List<PoseData> userPoses = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
package com.mrbysco.armorposer.client.gui;

import com.mrbysco.armorposer.Reference;
import com.mrbysco.armorposer.client.gui.widgets.PoseEntry;
import com.mrbysco.armorposer.client.gui.widgets.PoseListWidget;
import com.mrbysco.armorposer.poses.UserPoseHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ArmorPosesScreen extends Screen {
private enum SortType {
NORMAL,
A_TO_Z,
Z_TO_A;

Button button;

Component getButtonText() {
return Component.translatable("armorposer.gui.poses.search." + name().toLowerCase(Locale.ROOT));
}
}

private static final int PADDING = 6;

private PoseListWidget[] poseListWidget = new PoseListWidget[2];
private PoseListWidget.ListEntry selected = null;
private List<PoseEntry> poses;
private final List<PoseEntry> unsortedPoses;
private List<PoseEntry> userPoses;
private final List<PoseEntry> unsortedUserPoses;
private Button applyButton;

private final int buttonMargin = 1;
private final int numButtons = SortType.values().length;
private String lastFilterText = "";

private EditBox search;
private boolean sorted = false;
private SortType sortType = SortType.NORMAL;

public ArmorStandScreen parentScreen;

public ArmorPosesScreen(ArmorStandScreen parent) {
super(Component.translatable("armorposer.gui.poses.title"));
this.parentScreen = parent;

//Add default poses
List<PoseEntry> rawPoses = Reference.defaultPoseMap.entrySet().stream()
.map(entry -> new PoseEntry(entry.getKey(), entry.getValue(), false)).collect(Collectors.toList());
this.unsortedPoses = Collections.unmodifiableList(rawPoses);
Collections.sort(rawPoses);
this.poses = Collections.unmodifiableList(rawPoses);

//Add user added poses
UserPoseHandler.loadUserPoses();
List<PoseEntry> rawUserPoses = Reference.userPoses.stream().map(entry -> new PoseEntry(entry, true)).collect(Collectors.toList());
this.unsortedUserPoses = Collections.unmodifiableList(rawUserPoses);
Collections.sort(rawUserPoses);
this.userPoses = Collections.unmodifiableList(rawUserPoses);
}

@Override
public boolean isPauseScreen() {
return false;
}

@Override
protected void init() {
int centerWidth = this.width / 2;
int listWidth = this.width / 4 + 20;
int structureWidth = this.width - listWidth - (PADDING * 3);
int closeButtonWidth = Math.min(structureWidth, 160);
int y = this.height - 20 - PADDING;
this.addRenderableWidget(Button.builder(Component.translatable("gui.cancel"), b -> ArmorPosesScreen.this.onClose())
.bounds(centerWidth - (closeButtonWidth / 2) + PADDING, y, closeButtonWidth, 20).build());

y -= 18 + PADDING;
this.addRenderableWidget(this.applyButton = Button.builder(Component.translatable("armorposer.gui.poses.selection.apply"), b -> {
if (selected != null) {
if (!selected.userAdded() && selected.rawName().equals("random")) {
//Randomize all fields but the last 3 (as those are position) but don't make the rotations too crazy
for (int i = 0; i < this.parentScreen.poseTextFields.length - 3; i++) {
//generate a random number between -35 and 35
float randomRotation = (float) (Math.random() * 70 - 35);
this.parentScreen.poseTextFields[i].setValue(String.valueOf((int) randomRotation));
}
} else {
this.parentScreen.readFieldsFromNBT(selected.getTag());
}
this.parentScreen.updateEntity(selected.getTag());
}
this.onClose();
}).bounds(centerWidth - (closeButtonWidth / 2) + PADDING, y, closeButtonWidth, 20).build());

y -= 14 + PADDING;
search = new EditBox(getScreenFont(), centerWidth - listWidth / 2 + PADDING + 1, y, listWidth - 2, 14,
Component.translatable("armorposer.gui.poses.search"));

int fullButtonHeight = PADDING + 20 + PADDING;
this.poseListWidget[0] = new PoseListWidget(this, Component.translatable("armorposer.gui.poses.default"), false, listWidth, fullButtonHeight, search.getY() - getScreenFont().lineHeight - PADDING);
this.poseListWidget[0].setX(0);
this.poseListWidget[0].setY(10);
this.poseListWidget[0].setHeight(this.height);

this.poseListWidget[1] = new PoseListWidget(this, Component.translatable("armorposer.gui.poses.user"), true, listWidth, fullButtonHeight, search.getY() - getScreenFont().lineHeight - PADDING);
this.poseListWidget[1].setX(width - listWidth);
this.poseListWidget[1].setY(10);
this.poseListWidget[1].setHeight(this.height);

addWidget(search);
addWidget(poseListWidget[0]);
addWidget(poseListWidget[1]);
search.setFocused(false);
search.setCanLoseFocus(true);

final int width = listWidth / numButtons;
int x = centerWidth + PADDING - width;
addRenderableWidget(SortType.A_TO_Z.button = Button.builder(SortType.A_TO_Z.getButtonText(), b ->
resortPoses(SortType.A_TO_Z))
.bounds(x, PADDING, width - buttonMargin, 20).build());
x += width + buttonMargin;
addRenderableWidget(SortType.Z_TO_A.button = Button.builder(SortType.Z_TO_A.getButtonText(), b ->
resortPoses(SortType.Z_TO_A))
.bounds(x, PADDING, width - buttonMargin, 20).build());

resortPoses(SortType.A_TO_Z);
updateCache();
}

@Override
public void tick() {
if (poseListWidget[0].children().contains(selected)) {
poseListWidget[0].setSelected(selected);
poseListWidget[1].setSelected(null);
} else if (poseListWidget[1].children().contains(selected)) {
poseListWidget[0].setSelected(null);
poseListWidget[1].setSelected(selected);
}

if (!search.getValue().equals(lastFilterText)) {
reloadPoses();
sorted = false;
}

if (!sorted) {
reloadPoses();
if (sortType == SortType.A_TO_Z) {
Collections.sort(poses);
Collections.sort(userPoses);
} else if (sortType == SortType.Z_TO_A) {
poses.sort(Collections.reverseOrder());
userPoses.sort(Collections.reverseOrder());
}
poseListWidget[0].refreshList(false);
poseListWidget[1].refreshList(true);
if (selected != null) {
selected = poseListWidget[0].children().stream().filter(e -> e == selected).findFirst()
.orElse(poseListWidget[1].children().stream().filter(e -> e == selected).findFirst().orElse(null));
}
sorted = true;
}
}

private void reloadPoses() {
this.poses = this.unsortedPoses.stream().
filter(entry -> entry.getName().toLowerCase(Locale.ROOT).contains(search.getValue().toLowerCase(Locale.ROOT)))
.collect(Collectors.toList());

this.userPoses = this.unsortedUserPoses.stream().
filter(entry -> entry.getName().toLowerCase(Locale.ROOT).contains(search.getValue().toLowerCase(Locale.ROOT)))
.collect(Collectors.toList());

lastFilterText = search.getValue();
}

public <T extends ObjectSelectionList.Entry<T>> void buildPoseList(Consumer<T> ListViewConsumer, Function<PoseEntry, T> newEntry) {
poses.forEach(mod -> ListViewConsumer.accept(newEntry.apply(mod)));
}

public <T extends ObjectSelectionList.Entry<T>> void buildUserPoseList(Consumer<T> ListViewConsumer, Function<PoseEntry, T> newEntry) {
userPoses.forEach(mod -> ListViewConsumer.accept(newEntry.apply(mod)));
}

private void resortPoses(SortType newSort) {
this.sortType = newSort;

for (SortType sort : SortType.values()) {
if (sort.button != null)
sort.button.active = sortType != sort;
}
sorted = false;
}

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
this.poseListWidget[0].render(guiGraphics, mouseX, mouseY, partialTicks);
this.poseListWidget[1].render(guiGraphics, mouseX, mouseY, partialTicks);
super.render(guiGraphics, mouseX, mouseY, partialTicks);

Component text = Component.translatable("armorposer.gui.poses.search");
guiGraphics.drawCenteredString(getScreenFont(), text, this.width / 2 + PADDING,
search.getY() - getScreenFont().lineHeight - 2, 0xFFFFFF);

this.search.render(guiGraphics, mouseX, mouseY, partialTicks);
}

@Override
public void renderBackground(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
//Nope
}

public void setSelected(PoseListWidget.ListEntry entry) {
this.selected = entry == this.selected ? null : entry;
updateCache();
}

private void updateCache() {
this.applyButton.active = selected != null;
}

/**
* Clear the search field when right-clicked on it
*/
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
boolean flag = super.mouseClicked(mouseX, mouseY, button);
if (button == 1 && search.isMouseOver(mouseX, mouseY)) {
search.setValue("");
}
return flag;
}

@Override
public void resize(Minecraft mc, int width, int height) {
String s = this.search.getValue();
SortType sort = this.sortType;
PoseListWidget.ListEntry selected = this.selected;
this.init(mc, width, height);
this.search.setValue(s);
this.selected = selected;
if (!this.search.getValue().isEmpty())
reloadPoses();
if (sort != SortType.NORMAL)
resortPoses(sort);
updateCache();
}

@Override
public void onClose() {
this.minecraft.setScreen(parentScreen);
}

public Minecraft getScreenMinecraft() {
return this.minecraft;
}

public Font getScreenFont() {
return this.font;
}
}
Loading

0 comments on commit f69d75d

Please sign in to comment.