nageyqma·git
HTTPS https://git.nageyqma.icu/nageyqma/blade-addons-next.git SSH git@git.nageyqma.icu:nageyqma/blade-addons-next.git

HTTPS 链接可直接 git clone;推送代码请使用 SSH。

3.7 KB · 100 行raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
package blade.addon.features.notifications;

import com.mojang.datafixers.util.Pair;
import config.practical.utilities.Constants;
import config.practical.utilities.DrawHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.client.renderer.RenderPipelines;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;

class NotificationEntry extends AbstractWidget {

    private static final Identifier CROSS = Identifier.fromNamespaceAndPath(blade.addon.utils.Constants.NAMESPACE, "cross");

    private static final int PADDING = 5;
    private static final int TEXT_SPACE = 125;
    private static final int MAX_TEXT_WIDTH = 100;

    private static final int SPRITE_SIZE = 16;
    private static final int SPRITE_WIDTH_AREA = SPRITE_SIZE + 10;

    private final NotificationList parent ;
    private final Notification notification;

    public NotificationEntry(NotificationList parent, Notification notification) {
        super(0, 0, Constants.WIDGET_WIDTH, 28, Component.empty());
        this.parent = parent;
        this.notification = notification;
    }

    @Override
    protected void extractWidgetRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float deltaTicks) {
        int x = getX();
        int y = getY();
        int width = getWidth();
        int height = getHeight();
        DrawHelper.drawBackground(graphics, x, y, width - SPRITE_WIDTH_AREA, height);

        Minecraft mc = Minecraft.getInstance();
        Font textRenderer = mc.font;

        drawText(graphics, textRenderer, notification.getMatchString(), x + PADDING, y);
        drawText(graphics, textRenderer, notification.getNotificationString(), x + PADDING * 2 + TEXT_SPACE, y);

        Pair<Integer, Integer> pos = getRemovePos();
        graphics.blitSprite(RenderPipelines.GUI_TEXTURED, CROSS, pos.getFirst(), pos.getSecond(), SPRITE_SIZE, SPRITE_SIZE, 0xffffffff);
    }

    private void drawText(GuiGraphicsExtractor context, Font textRenderer, String string, int x, int y) {

        Component text;
        if (string.isEmpty()) {
            text = Component.literal("Empty");
        } else {
            text = Component.literal(textRenderer.plainSubstrByWidth(string, MAX_TEXT_WIDTH - 2));
        }

        int textY = (30 - Constants.TEXT_HEIGHT) / 2 + y;

        context.enableScissor(x, y, x + MAX_TEXT_WIDTH - 1, y + height);
        context.text(textRenderer, text, x + 1, textY, 0xffffffff, true);
        context.disableScissor();
    }

    private Pair<Integer, Integer> getRemovePos() {
        return Pair.of(getX() + width - SPRITE_SIZE - 5, getY() + (height - SPRITE_SIZE) / 2);
    }

    private boolean inRemovalBounds(double x, double y) {
        Pair<Integer, Integer> pos = getRemovePos();
        return x >= pos.getFirst() && x <=  pos.getFirst() + SPRITE_SIZE && y >= pos.getSecond() && y <= pos.getSecond() + SPRITE_SIZE;
    }

    @Override
    public void onClick(MouseButtonEvent click, boolean doubled) {
        double x = click.x();
        double y = click.y();

        if (inRemovalBounds(x, y)) {
            parent.removeNotification(notification);
            return;
        }

        //make sure you cant click around the button accidentally
        if (width + getX() - SPRITE_WIDTH_AREA < x) return;

        Minecraft.getInstance().gui.setScreen(new NotificationEditScreen(notification));
    }

    @Override
    protected void updateWidgetNarration(NarrationElementOutput builder) {

    }
}