From 2adf47d4577e8b50096b2329e88cd3a198084f30 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Fri, 3 Jul 2020 20:26:41 +0300 Subject: Add icon API --- gradle.properties | 2 +- .../cottonmc/cotton/gui/widget/icon/Icon.java | 26 ++++++++++++++ .../cottonmc/cotton/gui/widget/icon/ItemIcon.java | 42 ++++++++++++++++++++++ .../cotton/gui/widget/icon/TextureIcon.java | 29 +++++++++++++++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/icon/Icon.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/icon/ItemIcon.java create mode 100644 src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java diff --git a/gradle.properties b/gradle.properties index 07c188b..eba759b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check these on https://fabricmc.net/use minecraft_version=1.16.1 - yarn_mappings=1.16.1+build.1 + yarn_mappings=1.16.1+build.20 loader_version=0.8.8+build.202 # Mod Properties diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/Icon.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/Icon.java new file mode 100644 index 0000000..9fc89de --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/Icon.java @@ -0,0 +1,26 @@ +package io.github.cottonmc.cotton.gui.widget.icon; + +import com.google.common.annotations.Beta; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; + +/** + * A square icon for a widget such as a button. + * + * @see ItemIcon + * @see TextureIcon + * @since 2.2.0 + */ +public interface Icon { + /** + * Paints this icon. + * + * @param matrices the GUI matrix stack + * @param x the X coordinate + * @param y the Y coordinate + * @param size the size of this icon in pixels (size N means a N*N square) + */ + @Environment(EnvType.CLIENT) + void paint(MatrixStack matrices, int x, int y, int size); +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/ItemIcon.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/ItemIcon.java new file mode 100644 index 0000000..3ce8eaa --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/ItemIcon.java @@ -0,0 +1,42 @@ +package io.github.cottonmc.cotton.gui.widget.icon; + +import com.mojang.blaze3d.systems.RenderSystem; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.item.ItemRenderer; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; + +/** + * An icon that draws an item stack. + * + * @since 2.2.0 + */ +public class ItemIcon implements Icon { + private final ItemStack stack; + + /** + * Constructs an item icon. + * + * @param stack the drawn item stack + */ + public ItemIcon(ItemStack stack) { + this.stack = stack; + } + + @Environment(EnvType.CLIENT) + @Override + public void paint(MatrixStack matrices, int x, int y, int size) { + MinecraftClient client = MinecraftClient.getInstance(); + ItemRenderer renderer = client.getItemRenderer(); + + float scale = size != 16 ? ((float) size / 16f) : 1f; + + RenderSystem.pushMatrix(); + RenderSystem.translatef(x, y, 0); + RenderSystem.scalef(scale, scale, 1); + renderer.renderInGui(stack, 0, 0); + RenderSystem.popMatrix(); + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java new file mode 100644 index 0000000..e614e4b --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/icon/TextureIcon.java @@ -0,0 +1,29 @@ +package io.github.cottonmc.cotton.gui.widget.icon; + +import io.github.cottonmc.cotton.gui.client.ScreenDrawing; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.util.Identifier; + +/** + * An icon that draws a texture. + */ +public class TextureIcon implements Icon { + private final Identifier texture; + + /** + * Constructs a new texture icon. + * + * @param texture the identifier of the icon texture + */ + public TextureIcon(Identifier texture) { + this.texture = texture; + } + + @Environment(EnvType.CLIENT) + @Override + public void paint(MatrixStack matrices, int x, int y, int size) { + ScreenDrawing.texturedRect(x, y, size, size, texture, 0xFF_FFFFFF); + } +} -- cgit