All Downloads are FREE. Search and download functionalities are using the official Maven repository.

personthecat.catlib.client.gui.CategorizedList Maven / Gradle / Ivy

Go to download

Utilities for serialization, commands, noise generation, IO, and some new data types.

The newest version!
package personthecat.catlib.client.gui;

import org.jetbrains.annotations.NotNull;
import personthecat.catlib.data.collections.MultiValueMap;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.class_2561;
import net.minecraft.class_2588;
import net.minecraft.class_310;
import net.minecraft.class_327;
import net.minecraft.class_339;
import net.minecraft.class_4185;
import net.minecraft.class_4280;
import net.minecraft.class_437;
import net.minecraft.class_4587;

public class CategorizedList extends class_4280{

    private final List buttons = new ArrayList<>();

    public CategorizedList(class_437 parent, int x0, int x1, MultiValueMap widgets) {
        super(class_310.method_1551(), x1 - x0, parent.field_22790, 35, parent.field_22790 - 50, 22);
        widgets.forEach((category, ws) -> {
            if (category != null && !category.isEmpty()) {
                this.method_25321(new Category(new class_2588(category)));
            }
            ws.forEach(this::addButton);
        });
        this.method_31322(false);
        this.method_31323(false);
        this.field_19088 = x0;
        this.field_19087 = x1;
    }

    public static class_4185 createButton(class_2561 display, class_4185.class_4241 onPress) {
        return new class_4185(0, 0, 0, 20, display, onPress);
    }

    public static class_4185 createButton(class_2561 display, class_4185.class_4241 onPress, class_4185.class_5316 onTooltip) {
        return new class_4185(0, 0, 0, 20, display, onPress, onTooltip);
    }

    private void addButton(class_339 widget) {
        final ButtonEntry entry = new ButtonEntry(widget);
        this.method_25321(entry);
        this.buttons.add(entry);
    }

    public int numButtons() {
        return this.buttons.size();
    }

    public void selectButton(int button) {
        this.buttons.get(button).widget.field_22763 = false;
    }

    public void deselectAll() {
        this.buttons.forEach(b -> b.widget.field_22763 = true);
    }

    public class_339 getButton(int button) {
        return this.buttons.get(button).widget;
    }

    public void renderTooltips(class_4587 stack, int x, int y) {
        final ListEntry entry = this.method_25308(x, y);
        if (entry != null) {
            entry.renderTooltip(stack, x, y);
        }
    }

    @Override
    protected int method_25329() {
        return this.field_19087 - 6;
    }

    @Override
    public int method_25322() {
        return this.field_22742;
    }

    static class Category extends ListEntry {
        private final class_2561 category;
        private final class_327 font = class_310.method_1551().field_1772;

        Category(class_2561 category) {
            this.category = category;
        }

        @Override
        public void method_25343(@NotNull class_4587 stack, int i, int top, int left, int w, int h, int x, int y, boolean over, float partial) {
            int height = top + (22 / 2) - (this.font.field_2000 / 2);
            method_27534(stack, this.font, this.category, left + (w / 2), height, 16777215);
        }

        @Override
        public class_2561 method_37006() {
            return new class_2588("narrator.select", this.category);
        }
    }

    static class ButtonEntry extends ListEntry {
        final class_339 widget;

        ButtonEntry(class_339 widget) {
            this.widget = widget;
        }

        @Override
        public void method_25343(@NotNull class_4587 stack, int i, int top, int left, int w, int h, int x, int y, boolean over, float partial) {
            this.widget.field_22760 = left + 4;
            this.widget.field_22761 = top;
            this.widget.method_25358(w - 18);
            this.widget.method_25394(stack, x, y, partial);
        }

        @Override
        public boolean method_25402(double x, double y, int button) {
            return this.widget.method_25402(x, y, button);
        }

        @Override
        public boolean method_25406(double x, double y, int button) {
            return this.widget.method_25406(x, y, button);
        }

        @Override
        void renderTooltip(class_4587 stack, int x, int y) {
            this.widget.method_25352(stack, x, y);
        }

        @Override
        public class_2561 method_37006() {
            return new class_2588("narrator.select", this.widget.method_25369());
        }
    }

    public static abstract class ListEntry extends class_4280.class_4281 {
        void renderTooltip(class_4587 stack, int x, int y) {}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy