| 1 | package nicotine.util.render;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 5 | import net.minecraft.client.renderer.texture.DynamicTexture;
|
| 6 | import net.minecraft.resources.Identifier;
|
| 7 | import nicotine.util.ColorUtil;
|
| 8 | import org.jspecify.annotations.Nullable;
|
| 9 |
|
| 10 | import javax.imageio.ImageIO;
|
| 11 | import javax.imageio.ImageReader;
|
| 12 | import javax.imageio.stream.ImageInputStream;
|
| 13 | import java.awt.image.BufferedImage;
|
| 14 | import java.io.ByteArrayOutputStream;
|
| 15 | import java.io.File;
|
| 16 | import java.io.IOException;
|
| 17 | import java.util.*;
|
| 18 |
|
| 19 | import static nicotine.util.Common.*;
|
| 20 |
|
| 21 | public class Render2D {
|
| 22 | public static final List<GIFEntry> gifImages = new ArrayList<>();
|
| 23 |
|
| 24 | @Nullable
|
| 25 | public static GIFEntry getGIFEntry() {
|
| 26 | if (gifImages.isEmpty())
|
| 27 | return null;
|
| 28 |
|
| 29 | GIFEntry gifEntry = gifImages.getFirst();
|
| 30 |
|
| 31 | gifImages.add(gifEntry);
|
| 32 | gifImages.removeFirst();
|
| 33 |
|
| 34 | return gifEntry;
|
| 35 | }
|
| 36 |
|
| 37 | public static void extractGIFImages(File file) {
|
| 38 | gifImages.forEach(gifEntry -> gifEntry.nativeImage.close());
|
| 39 | gifImages.clear();
|
| 40 |
|
| 41 | try {
|
| 42 | String formatName;
|
| 43 |
|
| 44 | int indexForExtension = file.getName().lastIndexOf('.');
|
| 45 |
|
| 46 | if (indexForExtension == -1)
|
| 47 | formatName = "gif";
|
| 48 | else
|
| 49 | formatName = file.getName().substring(indexForExtension + 1);
|
| 50 |
|
| 51 | ImageReader reader = ImageIO.getImageReadersByFormatName(formatName).next();
|
| 52 | ImageInputStream stream = ImageIO.createImageInputStream(file);
|
| 53 | reader.setInput(stream);
|
| 54 |
|
| 55 | int count = reader.getNumImages(true);
|
| 56 | for (int i = 0; i < count; i++) {
|
| 57 | BufferedImage frame = reader.read(i);
|
| 58 | ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
| 59 | ImageIO.write(frame, "png", byteArrayOutputStream);
|
| 60 |
|
| 61 | byte[] bytes = byteArrayOutputStream.toByteArray();
|
| 62 |
|
| 63 | String identifierPath = String.format("gifimage%d", i);
|
| 64 | Identifier identifier = Identifier.fromNamespaceAndPath("nicotine", identifierPath);
|
| 65 |
|
| 66 | NativeImage nativeImage = NativeImage.read(bytes);
|
| 67 |
|
| 68 | DynamicTexture dynamicTexture = new DynamicTexture(() -> identifierPath, nativeImage);
|
| 69 | mc.getTextureManager().register(identifier, dynamicTexture);
|
| 70 |
|
| 71 | gifImages.add(new GIFEntry(identifier, nativeImage, bytes));
|
| 72 | }
|
| 73 |
|
| 74 | stream.close();
|
| 75 | } catch (IOException e) {
|
| 76 | e.printStackTrace();
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public static void drawBorderAroundText(GuiGraphicsExtractor context, int x, int y, int width, int height, int padding, int color) {
|
| 81 | int borderX = x - padding - Math.round((float) padding / 2);
|
| 82 | int borderY = y - padding - Math.round((float) padding / 2);
|
| 83 |
|
| 84 | int borderWidth = width + 2 * padding;
|
| 85 | int borderHeight = height + padding * (padding > 1 ? 2 : 1);
|
| 86 |
|
| 87 | context.fill(borderX, borderY, borderX + borderWidth, borderY + borderHeight, ColorUtil.BACKGROUND_COLOR);
|
| 88 | Render2D.drawBorder(context, borderX, borderY, borderWidth, borderHeight, color);
|
| 89 | }
|
| 90 |
|
| 91 | public static void drawBorderAroundText(GuiGraphicsExtractor context, String text, int x, int y, int padding, int color) {
|
| 92 | drawBorderAroundText(context, x, y, mc.font.width(text), mc.font.lineHeight, padding, color);
|
| 93 | }
|
| 94 |
|
| 95 | public static void drawBorder(GuiGraphicsExtractor drawContext, int x, int y, int width, int height, int color) {
|
| 96 | drawBorderVertical(drawContext, x, y, width, height, color);
|
| 97 | drawBorderHorizontal(drawContext, x, y, width, height, color);
|
| 98 | }
|
| 99 |
|
| 100 | public static void drawBorderVertical(GuiGraphicsExtractor drawContext, int x, int y, int width, int height, int color) {
|
| 101 | drawContext.verticalLine(x, y , y + height, color);
|
| 102 | drawContext.verticalLine(x + width, y, y + height, color);
|
| 103 | }
|
| 104 |
|
| 105 | public static void drawBorderHorizontal(GuiGraphicsExtractor drawContext, int x, int y, int width, int height, int color) {
|
| 106 | drawContext.horizontalLine(x, x + width, y, color);
|
| 107 | drawContext.horizontalLine(x, x + width, y + height, color);
|
| 108 | }
|
| 109 |
|
| 110 | public static boolean mouseOver(int posX, int posY, int width, int height, double mouseX, double mouseY) {
|
| 111 | return (posX <= mouseX && mouseX <= posX + width && posY <= mouseY && mouseY <= posY + height);
|
| 112 | }
|
| 113 | }
|
| 114 |
|