diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-07-03 20:26:41 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-07-10 18:37:32 +0300 |
commit | 2adf47d4577e8b50096b2329e88cd3a198084f30 (patch) | |
tree | 33d3bf3a06d46037af6a3fe53b8f583aed837490 /src/main/java/io | |
parent | 3b1698a72659efe8a243c70833df81366174e62a (diff) | |
download | LibGui-2adf47d4577e8b50096b2329e88cd3a198084f30.tar.gz LibGui-2adf47d4577e8b50096b2329e88cd3a198084f30.tar.bz2 LibGui-2adf47d4577e8b50096b2329e88cd3a198084f30.zip |
Add icon API
Diffstat (limited to 'src/main/java/io')
3 files changed, 97 insertions, 0 deletions
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); + } +} |