add EntityListSetting with entity picker screen

This commit is contained in:
froslanz
2026-10-30 15:17:31 -04:00
parent 4adb232811
commit 06386c9bbc
5 changed files with 402 additions and 0 deletions
@@ -0,0 +1,289 @@
package cc.fros.features.gui;
import cc.fros.Fros;
import cc.fros.features.modules.client.ClickGuiModule;
import cc.fros.features.settings.EntityListSetting;
import cc.fros.util.ColorUtil;
import cc.fros.util.render.FrosFont;
import cc.fros.util.render.RenderUtil;
import cc.fros.util.render.ScissorUtil;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.input.CharacterEvent;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.EntityType;
import org.lwjgl.glfw.GLFW;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
/**
* Multi-select picker for {@link EntityListSetting}s, opened from the ClickGUI. Shows every
* registered entity type as a checkbox in a scrollable grid, filtered by the search field.
* Renders the ClickGUI behind itself and returns to it on close.
*/
public class EntityPickerScreen extends Screen {
private static final int BAR = 18;
private static final int CONTROLS = 19;
private static final int CELL_H = 14;
private static final int MIN_CELL_W = 120;
private static final int BUTTON_W = 42;
private static final int BUTTON_H = 13;
private final Screen parent;
private final EntityListSetting setting;
private final List<EntityType<?>> all = BuiltInRegistries.ENTITY_TYPE.stream()
.sorted(Comparator.comparing(type -> EntityType.getKey(type).toString()))
.toList();
private List<EntityType<?>> visible = all;
private String search = "";
// window layout, computed in init()
private int winX, winY, winW, winH;
private int listX, listY, listW, listH;
private int searchX, searchW;
private int clearX, doneX;
// grid layout, recomputed by layoutGrid()
private int cols, rows, gridW, cellW;
private double scroll;
public EntityPickerScreen(Screen parent, EntityListSetting setting) {
super(Component.literal("Entity Picker"));
this.parent = parent;
this.setting = setting;
}
@Override
protected void init() {
this.winW = Math.min(380, this.width - 40);
this.winH = Math.min(250, this.height - 40);
this.winX = (this.width - this.winW) / 2;
this.winY = (this.height - this.winH) / 2;
this.searchW = Math.min(150, this.winW / 3);
this.searchX = this.winX + 5;
this.doneX = this.winX + this.winW - 5 - BUTTON_W;
this.clearX = this.doneX - 4 - BUTTON_W;
this.listX = this.winX + 5;
this.listY = this.winY + BAR + CONTROLS + 2;
this.listW = this.winW - 10;
this.listH = this.winH - BAR - CONTROLS - 9;
refresh();
}
private void refresh() {
String needle = this.search.toLowerCase(Locale.ROOT);
this.visible = needle.isEmpty()
? this.all
: this.all.stream().filter(type -> EntityType.getKey(type).toString().contains(needle)).toList();
layoutGrid();
}
/** One-pass grid layout shared by rendering and click handling so both agree on cells. */
private void layoutGrid() {
int cols0 = Math.max(1, this.listW / MIN_CELL_W);
int rows0 = (this.visible.size() + cols0 - 1) / cols0;
this.gridW = this.listW - (rows0 * CELL_H > this.listH ? 5 : 0);
this.cols = Math.max(1, this.gridW / MIN_CELL_W);
this.cellW = this.gridW / this.cols;
this.rows = (this.visible.size() + this.cols - 1) / this.cols;
double maxScroll = Math.max(0, this.rows * (double) CELL_H - this.listH);
this.scroll = Math.max(0, Math.min(this.scroll, maxScroll));
}
@Override
public void render(GuiGraphics context, int mouseX, int mouseY, float delta) {
if (this.parent != null) {
this.parent.renderBackground(context, Integer.MIN_VALUE, Integer.MIN_VALUE, delta);
this.parent.render(context, Integer.MIN_VALUE, Integer.MIN_VALUE, delta);
context.nextStratum();
}
context.fill(0, 0, this.width, this.height, 0x78000000);
int accent = Fros.colorManager.getColorAsInt();
ClickGuiModule gui = ClickGuiModule.getInstance();
int headerColor = gui.rainbow.getValue()
? ColorUtil.rainbow(gui.rainbowHue.getValue()).getRGB()
: gui.topColor.getValue().getRGB();
// window: shadow, header bar, body
context.fill(this.winX + 2, this.winY + 2, this.winX + this.winW + 2, this.winY + this.winH + 2, 0x50000000);
context.fill(this.winX, this.winY, this.winX + this.winW, this.winY + BAR, headerColor);
RenderUtil.rect(context, this.winX, this.winY + BAR, this.winX + this.winW, this.winY + this.winH, 0xE6141414);
// header: title left, selection count right
FrosFont.draw(context, "Entity Picker", this.winX + 5, (int) (this.winY + BAR / 2f - FrosFont.visualCenter()), -1);
String count = this.setting.getValue().size() + " selected";
FrosFont.draw(context, count, this.winX + this.winW - 5 - FrosFont.width(count),
(int) (this.winY + BAR / 2f - FrosFont.visualCenter()), -1);
// controls row: search box, Clear, Done
int boxY = this.winY + BAR + 3;
RenderUtil.rect(context, this.searchX, boxY, this.searchX + this.searchW, boxY + BUTTON_H, 0x44000000);
RenderUtil.rect(context, this.searchX + 0.5f, boxY + 0.5f, this.searchX + this.searchW - 0.5f,
boxY + BUTTON_H - 0.5f, 0x66FFFFFF, 0.75f);
boolean empty = this.search.isEmpty();
String text = empty ? "search..." : truncate(this.search, this.searchW - 10);
FrosFont.draw(context, text, this.searchX + 4, (int) (boxY + BUTTON_H / 2f - FrosFont.visualCenter()),
empty ? 0x88AAAAAA : -1);
if ((System.currentTimeMillis() / 400) % 2 == 0) {
int caret = this.searchX + 4 + FrosFont.width(text) + 1;
RenderUtil.rect(context, caret, boxY + 2, caret + 1, boxY + BUTTON_H - 2, accent);
}
drawButton(context, mouseX, mouseY, this.clearX, boxY, "Clear");
drawButton(context, mouseX, mouseY, this.doneX, boxY, "Done");
// grid
layoutGrid();
int offsetY = (int) this.scroll;
ScissorUtil.enable(context, this.listX, this.listY, this.listX + this.listW, this.listY + this.listH);
for (int i = 0; i < this.visible.size(); i++) {
int cx = this.listX + (i % this.cols) * this.cellW;
int cy = this.listY + (i / this.cols) * CELL_H - offsetY;
if (cy + CELL_H < this.listY || cy > this.listY + this.listH) continue;
boolean selected = this.setting.contains(this.visible.get(i));
boolean hover = inCell(mouseX, mouseY, cx, cy);
if (hover) context.fill(cx + 1, cy + 1, cx + this.cellW - 4, cy + CELL_H - 1, 0x22FFFFFF);
int boxSize = 8;
int by = cy + (CELL_H - boxSize) / 2;
if (selected) {
RenderUtil.rect(context, cx + 3, by, cx + 3 + boxSize, by + boxSize, accent);
} else {
RenderUtil.rect(context, cx + 3.5f, by + 0.5f, cx + 3 + boxSize - 0.5f, by + boxSize - 0.5f,
0x80FFFFFF, 0.75f);
}
String id = EntityType.getKey(this.visible.get(i)).toString();
FrosFont.draw(context, truncate(id, this.cellW - 17), cx + 14,
(int) (cy + CELL_H / 2f - FrosFont.visualCenter()), selected ? -1 : 0xFFAAAAAA);
}
ScissorUtil.disable(context);
// scrollbar
int contentH = this.rows * CELL_H;
if (contentH > this.listH) {
double maxScroll = contentH - (double) this.listH;
int handleH = Math.max(12, (int) (this.listH * (this.listH / (double) contentH)));
int handleY = this.listY + (int) ((this.listH - handleH) * (this.scroll / maxScroll));
RenderUtil.rect(context, this.listX, this.listY, this.listX + this.listW, this.listY + this.listH,
0x11000000);
RenderUtil.rect(context, this.listX + this.listW - 3, handleY, this.listX + this.listW - 1,
handleY + handleH, 0x66FFFFFF);
}
}
private void drawButton(GuiGraphics context, int mouseX, int mouseY, int x, int y, String label) {
boolean hover = inRect(mouseX, mouseY, x, y, BUTTON_W, BUTTON_H);
RenderUtil.rect(context, x, y, x + BUTTON_W, y + BUTTON_H, hover ? 0x33FFFFFF : 0x22000000);
FrosFont.draw(context, label, x + (BUTTON_W - FrosFont.width(label)) / 2,
(int) (y + BUTTON_H / 2f - FrosFont.visualCenter()), -1);
}
@Override
public boolean mouseClicked(MouseButtonEvent click, boolean doubled) {
int mx = (int) click.x();
int my = (int) click.y();
if (click.button() == 0) {
int boxY = this.winY + BAR + 3;
if (inRect(mx, my, this.clearX, boxY, BUTTON_W, BUTTON_H)) {
this.setting.clearAll();
this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1f));
return true;
}
if (inRect(mx, my, this.doneX, boxY, BUTTON_W, BUTTON_H)) {
onClose();
return true;
}
if (mx >= this.listX && mx < this.listX + this.listW && my >= this.listY && my < this.listY + this.listH) {
layoutGrid();
int offsetY = (int) this.scroll;
int col = (mx - this.listX) / this.cellW;
int row = (my - this.listY + offsetY) / CELL_H;
int index = row * this.cols + col;
if (col < this.cols && index >= 0 && index < this.visible.size()) {
this.setting.toggle(this.visible.get(index));
return true;
}
}
}
return super.mouseClicked(click, doubled);
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
layoutGrid();
double maxScroll = Math.max(0, this.rows * (double) CELL_H - this.listH);
this.scroll = Math.max(0, Math.min(this.scroll - verticalAmount * CELL_H * 2, maxScroll));
return true;
}
@Override
public boolean charTyped(CharacterEvent input) {
this.search += input.codepointAsString();
refresh();
return true;
}
@Override
public boolean keyPressed(KeyEvent input) {
if (input.input() == GLFW.GLFW_KEY_BACKSPACE && !this.search.isEmpty()) {
this.search = this.search.substring(0, this.search.length() - 1);
refresh();
return true;
}
// ESC clears the search first so it never silently closes the picker
if (input.input() == GLFW.GLFW_KEY_ESCAPE && !this.search.isEmpty()) {
this.search = "";
refresh();
return true;
}
if (input.input() == GLFW.GLFW_KEY_ENTER || input.input() == GLFW.GLFW_KEY_KP_ENTER) {
onClose();
return true;
}
return super.keyPressed(input);
}
@Override
public void onClose() {
this.minecraft.setScreen(this.parent);
}
@Override
public boolean isPauseScreen() {
return false;
}
@Override
public void renderBackground(GuiGraphics context, int mouseX, int mouseY, float delta) {
}//ignore 1.21.8 blur thing
private boolean inCell(int mx, int my, int cx, int cy) {
return mx >= cx && mx < cx + this.cellW - 4 && my >= cy && my < cy + CELL_H;
}
private static boolean inRect(int mx, int my, int x, int y, int w, int h) {
return mx >= x && mx < x + w && my >= y && my < y + h;
}
private static String truncate(String text, int maxWidth) {
if (FrosFont.width(text) <= maxWidth) return text;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
if (FrosFont.width(sb + "..." + text.charAt(i)) > maxWidth) break;
sb.append(text.charAt(i));
}
return sb + "...";
}
}
@@ -0,0 +1,48 @@
package cc.fros.features.gui.items.buttons;
import cc.fros.Fros;
import cc.fros.features.gui.EntityPickerScreen;
import cc.fros.features.gui.FrosGui;
import cc.fros.features.modules.client.ClickGuiModule;
import cc.fros.features.settings.EntityListSetting;
import cc.fros.util.render.RenderUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.sounds.SoundEvents;
/**
* Opens the entity picker screen for an {@link EntityListSetting}; the label shows how many
* entity types are currently selected.
*/
public class EntityListButton
extends Button {
private final EntityListSetting setting;
public EntityListButton(EntityListSetting setting) {
super(setting.getName());
this.setting = setting;
this.width = 15;
}
@Override
public void drawScreen(GuiGraphics context, int mouseX, int mouseY, float partialTicks) {
RenderUtil.rect(context, this.x, this.y, this.x + (float) this.width + 7.4f, this.y + (float) this.height - 0.5f,
!this.isHovering(mouseX, mouseY)
? Fros.colorManager.getColorWithAlpha(y, ClickGuiModule.getInstance().color.getValue().getAlpha())
: Fros.colorManager.getColorWithAlpha(y, ClickGuiModule.getInstance().topColor.getValue().getAlpha()));
drawString(context, this.getName() + " " + ChatFormatting.GRAY + this.setting.getValue().size(),
this.x + 2.3f, textY(), -1);
}
@Override
public void update() {
this.setHidden(!this.setting.isVisible());
}
@Override
public void onMouseClick() {
mc.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1f));
mc.setScreen(new EntityPickerScreen(FrosGui.getClickGui(), this.setting));
}
}
@@ -3,6 +3,7 @@ package cc.fros.features.gui.items.buttons;
import cc.fros.features.gui.items.Item;
import cc.fros.features.modules.Module;
import cc.fros.features.settings.Bind;
import cc.fros.features.settings.EntityListSetting;
import cc.fros.features.settings.Setting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
@@ -28,6 +29,10 @@ public class ModuleButton
ArrayList<Item> newItems = new ArrayList<>();
if (!this.module.getSettings().isEmpty()) {
for (Setting<?> setting : this.module.getSettings()) {
if (setting instanceof EntityListSetting entityList) {
newItems.add(new EntityListButton(entityList));
continue;
}
if (setting.getValue() instanceof Boolean && !setting.getName().equals("Enabled")) {
newItems.add(new BooleanButton((Setting<Boolean>) setting));
}
@@ -0,0 +1,58 @@
package cc.fros.features.settings;
import net.minecraft.world.entity.EntityType;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
/**
* A multi-select setting holding a set of {@link EntityType}s, opened from the ClickGUI through
* {@link cc.fros.features.gui.EntityPickerScreen}. Serialised as a comma-separated list of
* registry ids.
*/
public class EntityListSetting extends Setting<Set<EntityType<?>>> {
public EntityListSetting(String name, Set<EntityType<?>> defaultValue) {
super(name, new LinkedHashSet<>(defaultValue));
// Make the live value independent from the default so toggling doesn't mutate it.
setValueNoEvent(new LinkedHashSet<>(getDefaultValue()));
}
@Override
public String getType() {
return "EntityList";
}
public boolean contains(EntityType<?> type) {
return getValue().contains(type);
}
public void toggle(EntityType<?> type) {
Set<EntityType<?>> copy = new LinkedHashSet<>(getValue());
if (!copy.remove(type)) {
copy.add(type);
}
setValue(copy);
}
public void clearAll() {
setValue(new LinkedHashSet<>());
}
@Override
public String getValueAsString() {
return getValue().stream()
.map(type -> EntityType.getKey(type).toString())
.collect(Collectors.joining(","));
}
public void fromString(String value) {
Set<EntityType<?>> types = new LinkedHashSet<>();
if (value != null && !value.isBlank()) {
for (String part : value.split(",")) {
EntityType.byString(part.trim()).ifPresent(types::add);
}
}
setValueNoEvent(types);
}
}
@@ -7,6 +7,7 @@ import com.google.gson.JsonParser;
import cc.fros.features.Feature;
import cc.fros.features.settings.Bind;
import cc.fros.features.settings.EnumConverter;
import cc.fros.features.settings.EntityListSetting;
import cc.fros.features.settings.Setting;
import cc.fros.util.traits.Jsonable;
import net.fabricmc.loader.api.FabricLoader;
@@ -117,6 +118,7 @@ public class ConfigManager {
LOGGER.error("Error parsing enum for {}.{}: {}", feature.getName(), setting.getName(), exception);
}
}
case "EntityList" -> ((EntityListSetting) setting).fromString(element.getAsString());
default -> LOGGER.error("Unknown Setting type for: {} : {}", feature.getName(), setting.getName());
}
}