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

personthecat.catlib.client.gui.SimpleTextPage 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 com.mojang.blaze3d.systems.RenderSystem;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.glfw.GLFW;
import personthecat.catlib.config.LibConfig;

import java.util.ArrayList;
import java.util.List;
import net.minecraft.class_2561;
import net.minecraft.class_2568;
import net.minecraft.class_2583;
import net.minecraft.class_437;
import net.minecraft.class_4587;
import net.minecraft.class_5481;

public class SimpleTextPage extends LibMenu {
    protected final class_2561 details;
    protected final List lines;
    public int left;
    public int right;
    public boolean wrap;
    protected int maxScroll;
    protected int scroll;

    public SimpleTextPage(@Nullable class_437 parent, class_2561 title, class_2561 details) {
        super(parent, title);
        this.details = details;
        this.lines = new ArrayList<>();
        this.left = 6;
        this.right = 6;
        this.wrap = LibConfig.wrapText();
        this.maxScroll = 0;
        this.scroll = 0;
    }

    @Override
    protected void method_25426() {
        super.method_25426();

        this.resetLines();

        final int menuHeight = this.field_22790 - Y1 - Y0;
        final int linesPerPage = menuHeight / (this.field_22793.field_2000 + 1);
        this.maxScroll = this.lines.size() - linesPerPage + 1;

        this.previous.field_22763 = false;
        this.next.field_22763 = false;
    }

    protected void resetLines() {
        this.lines.clear();
        this.lines.addAll(this.field_22793.method_1728(this.details, this.wrap ? this.field_22789 - 12 : 10000));
    }

    @Override
    protected void renderMenu(class_4587 stack, int x, int y, float partial) {
        final int h = this.field_22793.field_2000 + 1;
        int t = Y0 + 6;
        for (int i = this.scroll; i < this.lines.size(); i++) {
            final class_5481 chars = this.lines.get(i);

            RenderSystem.enableBlend();
            this.field_22793.method_27517(stack, chars, this.left, t, 0xFFFFFF);
            RenderSystem.disableBlend();

            if ((t += h) > this.field_22790 - Y1) {
                return;
            }
        }
    }

    @Override
    protected void renderDetails(class_4587 stack, int x, int y, float partial) {
        super.renderDetails(stack, x, y, partial);
        if (y < Y0 + 6 || y > this.field_22790 - Y1 || x < 6 || x > this.field_22789 - 6) {
            return;
        }

        final int o = Y0 + 6;
        final int d = y - o;
        final int h = this.field_22793.field_2000 + 1;
        final int l = d / h;
        final int a = this.scroll + l;

        if (a >= this.lines.size()) {
            return;
        }
        final class_5481 chars = this.lines.get(a);
        final class_2583 s = this.field_22793.method_27527().method_30876(chars, x - 6);
        if (s == null) {
            return;
        }
        final class_2568 hover = s.method_10969();
        if (hover == null) {
            return;
        }
        final class_2561 tooltip = hover.method_10891(class_2568.class_5247.field_24342);
        if (tooltip != null) {
            this.method_25424(stack, tooltip, x, y);
        }
    }

    @Override
    public boolean method_25401(double x, double y, double d) {
        if (d > 0.0) {
            if (this.scroll > 0) {
                this.scroll--;
                return true;
            }
        } else {
            if (this.scroll <= this.maxScroll) {
                this.scroll++;
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean method_25404(int key, int scan, int modifiers) {
        if (super.method_25404(key, scan, modifiers)) {
            return true;
        }
        if (key == GLFW.GLFW_KEY_DOWN) {
            if (this.scroll <= this.maxScroll) {
                this.scroll++;
                return true;
            }
        } else if (key == GLFW.GLFW_KEY_UP) {
            if (this.scroll > 0) {
                this.scroll--;
                return true;
            }
        } else if (key == GLFW.GLFW_KEY_PAGE_DOWN) {
            if (this.scroll <= this.maxScroll) {
                this.scroll += 10;
                if (this.scroll > this.maxScroll) {
                    this.scroll = this.maxScroll;
                }
                return true;
            }
        } else if (key == GLFW.GLFW_KEY_PAGE_UP) {
            if (this.scroll > 0) {
                this.scroll -= 10;
                if (this.scroll < 0) {
                    this.scroll = 0;
                }
                return true;
            }
        } else if (key == GLFW.GLFW_KEY_SPACE || key == GLFW.GLFW_KEY_W) {
            this.wrap = !this.wrap;
            this.resetLines();
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy