diff --git a/build.gradle b/build.gradle index 2214ae16..21eb0e07 100644 --- a/build.gradle +++ b/build.gradle @@ -220,6 +220,8 @@ def setUpLibrary(project) { implementation "org.apache.logging.log4j:log4j-api:2.19.0" implementation "org.apache.logging.log4j:log4j-core:2.19.0" + implementation "org.jetbrains:annotations:13.0" + implementation "org.quiltmc.parsers:json:${project.rootProject.quilt_parsers_version}" libraries.each { libraryPath -> diff --git a/libraries/gui/README.md b/libraries/gui/README.md new file mode 100644 index 00000000..0ea04b21 --- /dev/null +++ b/libraries/gui/README.md @@ -0,0 +1,3 @@ +# GUI API + +The GUI API provides an Ornithe equivalent to the modern Minecraft concept of the GuiGraphics class, a one-stop-shop that lets you do all the rendering for your screens, toasts, and more. diff --git a/libraries/gui/build.gradle b/libraries/gui/build.gradle new file mode 100644 index 00000000..e98ba2c6 --- /dev/null +++ b/libraries/gui/build.gradle @@ -0,0 +1 @@ +setUpLibrary(project) diff --git a/libraries/gui/gradle.properties b/libraries/gui/gradle.properties new file mode 100644 index 00000000..18ad2422 --- /dev/null +++ b/libraries/gui/gradle.properties @@ -0,0 +1,7 @@ +library_id = gui +library_name = GUI +library_description = GUI rendering utils. +library_version = 0.1.0 + +osl_dependencies = core:0.9.0,text-components:0.1.0,lifecycle-events:0.6.1 +environment = client diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/FinchGraphics.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/FinchGraphics.java new file mode 100644 index 00000000..d8b2f894 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/FinchGraphics.java @@ -0,0 +1,13 @@ +package net.ornithemc.osl.gui.api; + +import net.ornithemc.osl.gui.api.pipelines.RenderPipeline; +import net.ornithemc.osl.gui.api.shape.Text; +import net.ornithemc.osl.gui.api.shape.texture.Texture; + +public interface FinchGraphics { + void drawTexture(RenderPipeline pipeline, Texture texture, int x, int y); + + void drawText(RenderPipeline pipeline, Text text, int x, int y); + + Transforms beginTransform(); +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/Transforms.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/Transforms.java new file mode 100644 index 00000000..f9e794af --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/Transforms.java @@ -0,0 +1,20 @@ +package net.ornithemc.osl.gui.api; + +/** + * A collection of transforms on rendered things that are reverted after they are no longer necessary. + */ +public interface Transforms extends AutoCloseable { + /** + * Reverts all transformations done, so that regular rendering may resume. + */ + void close(); + + /** + * + * @param x amount to horizontally translate screen rendering by + * @param y amount to vertically translate screen rendering by + */ + void translate(double x, double y); + + void enableBlend(); +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/color/Color.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/color/Color.java new file mode 100644 index 00000000..47fd3015 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/color/Color.java @@ -0,0 +1,23 @@ +package net.ornithemc.osl.gui.api.color; + +public interface Color { + int red(); + + int green(); + + int blue(); + + double alpha(); + + int hexNoAlpha(); + + int hexWithAlpha(); + + Color withRed(int r); + + Color withGreen(int g); + + Color withBlue(int b); + + Color withAlpha(double a); +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/color/Colors.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/color/Colors.java new file mode 100644 index 00000000..873367fb --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/color/Colors.java @@ -0,0 +1,34 @@ +package net.ornithemc.osl.gui.api.color; + +import net.ornithemc.osl.gui.impl.color.ColorImpl; + +import static net.ornithemc.osl.gui.impl.color.ColorUtil.percentToHexByte; +import static net.ornithemc.osl.gui.impl.color.ColorUtil.putHexByte; + +public abstract class Colors { + public static final double MAX_ALPHA = 1; + + public static final Color WHITE = Colors.hex(0xffffff); + public static final Color BLACK = Colors.hex(0x000000); + + public static Color rgb(int r, int g, int b) { + return rgba(r, g, b, MAX_ALPHA); + } + + public static Color hex(int hex) { + return hexWithAlpha(hex >> 8 | 0xff); + } + + public static Color rgba(int r, int g, int b, double a) { + return hexWithAlpha( + putHexByte(r, 3) | + putHexByte(g, 2) | + putHexByte(b, 1) | + putHexByte(percentToHexByte(a), 0) + ); + } + + public static Color hexWithAlpha(int hexWithAlpha) { + return new ColorImpl(hexWithAlpha); + } +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/pipelines/RenderPipeline.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/pipelines/RenderPipeline.java new file mode 100644 index 00000000..18daf8d6 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/pipelines/RenderPipeline.java @@ -0,0 +1,8 @@ +package net.ornithemc.osl.gui.api.pipelines; + +import net.ornithemc.osl.gui.api.FinchGraphics; +import net.ornithemc.osl.gui.api.Transforms; + +public interface RenderPipeline { + void transform(FinchGraphics gui, Transforms transforms); +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/pipelines/RenderPipelines.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/pipelines/RenderPipelines.java new file mode 100644 index 00000000..e0c53d46 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/pipelines/RenderPipelines.java @@ -0,0 +1,7 @@ +package net.ornithemc.osl.gui.api.pipelines; + +public class RenderPipelines { + public static RenderPipeline GUI_TEXTURED = (gui, transforms) -> { + transforms.enableBlend(); + }; +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/Text.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/Text.java new file mode 100644 index 00000000..c24c8bed --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/Text.java @@ -0,0 +1,21 @@ +package net.ornithemc.osl.gui.api.shape; + +import net.ornithemc.osl.gui.api.color.Color; +import net.ornithemc.osl.gui.impl.shape.TextImpl; +import net.ornithemc.osl.text.api.TextComponent; + +public interface Text { + static Text of(TextComponent value, Color color, boolean isShaded) { + return new TextImpl(value, color, isShaded); + } + + static Text of(TextComponent value, Color color) { + return of(value, color, true); + } + + TextComponent value(); + + Color color(); + + boolean isShaded(); +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/texture/Subsection.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/texture/Subsection.java new file mode 100644 index 00000000..2b10d221 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/texture/Subsection.java @@ -0,0 +1,17 @@ +package net.ornithemc.osl.gui.api.shape.texture; + +import net.ornithemc.osl.gui.impl.shape.SubsectionImpl; + +public interface Subsection { + static Subsection of(int startX, int startY, int width, int height) { + return new SubsectionImpl(startX, startY, width, height); + } + + int startX(); + + int startY(); + + int width(); + + int height(); +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/texture/Texture.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/texture/Texture.java new file mode 100644 index 00000000..1df40d10 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/api/shape/texture/Texture.java @@ -0,0 +1,33 @@ +package net.ornithemc.osl.gui.api.shape.texture; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.gui.api.color.Color; +import net.ornithemc.osl.gui.api.color.Colors; +import net.ornithemc.osl.gui.impl.shape.TextureImpl; +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; + +public interface Texture { + static Texture of(NamespacedIdentifier identifier, int width, int height) { + return new TextureImpl(identifier, width, height, null, null); + } + + NamespacedIdentifier identifier(); + + int width(); + + int height(); + + @Nullable Subsection subsection(); + + @Nullable Color filter(); + + Texture withSubsection(Subsection subsection); + + Texture withFilter(Color filter); + + default Texture withTransparency(double alpha) { + return this.withFilter(Optional.ofNullable(this.filter()).orElse(Colors.WHITE).withAlpha(alpha)); + } +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/color/ColorImpl.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/color/ColorImpl.java new file mode 100644 index 00000000..cc665cc4 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/color/ColorImpl.java @@ -0,0 +1,63 @@ +package net.ornithemc.osl.gui.impl.color; + +import net.ornithemc.osl.gui.api.color.Color; + +import static net.ornithemc.osl.gui.impl.color.ColorUtil.*; + +public class ColorImpl implements Color { + private final int hexWithAlpha; + + public ColorImpl(int hexWithAlpha) { + this.hexWithAlpha = hexWithAlpha; + } + + @Override + public int red() { + return getHexByte(this.hexWithAlpha, 3); + } + + @Override + public int green() { + return getHexByte(this.hexWithAlpha, 2); + } + + @Override + public int blue() { + return getHexByte(this.hexWithAlpha,1); + } + + @Override + public double alpha() { + return hexByteToPercent(getHexByte(this.hexWithAlpha,0)); + } + + @Override + public int hexNoAlpha() { + return this.hexWithAlpha >> 8; + } + + @Override + public int hexWithAlpha() { + return this.hexWithAlpha; + } + + @Override + public Color withRed(int r) { + return new ColorImpl(replaceHexByte(this.hexWithAlpha, r, 3)); + } + + @Override + public Color withGreen(int g) { + return new ColorImpl(replaceHexByte(this.hexWithAlpha, g, 2)); + } + + @Override + public Color withBlue(int b) { + return new ColorImpl(replaceHexByte(this.hexWithAlpha, b, 1)); + } + + @Override + public Color withAlpha(double a) { + return new ColorImpl(replaceHexByte(this.hexWithAlpha, percentToHexByte(a), 0)); + } +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/color/ColorUtil.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/color/ColorUtil.java new file mode 100644 index 00000000..dda8ae4a --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/color/ColorUtil.java @@ -0,0 +1,23 @@ +package net.ornithemc.osl.gui.impl.color; + +public class ColorUtil { + public static int getHexByte(int hexCode, int bytesFromRight) { + return hexCode >> (8 * bytesFromRight) & 0xff; + } + + public static int putHexByte(int hexByte, int bytesFromRight) { + return hexByte << (8 * bytesFromRight); + } + + public static int percentToHexByte(double percent) { + return (int) (0xff * percent) & 0xff; + } + + public static double hexByteToPercent(int hexByte) { + return (double) hexByte / (double) 0xff; + } + + public static int replaceHexByte(int hexCode, int newHexByte, int bytesFromRight) { + return hexCode & ~putHexByte(0xff, bytesFromRight) | putHexByte(newHexByte, bytesFromRight); + } +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/SubsectionImpl.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/SubsectionImpl.java new file mode 100644 index 00000000..893b7ebb --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/SubsectionImpl.java @@ -0,0 +1,37 @@ +package net.ornithemc.osl.gui.impl.shape; + +import net.ornithemc.osl.gui.api.shape.texture.Subsection; + +public class SubsectionImpl implements Subsection { + private final int startX; + private final int startY; + private final int width; + private final int height; + + public SubsectionImpl(int startX, int startY, int width, int height) { + this.startX = startX; + this.startY = startY; + this.width = width; + this.height = height; + } + + @Override + public int startX() { + return this.startX; + } + + @Override + public int startY() { + return this.startY; + } + + @Override + public int width() { + return this.width; + } + + @Override + public int height() { + return this.height; + } +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/TextImpl.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/TextImpl.java new file mode 100644 index 00000000..d5d339aa --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/TextImpl.java @@ -0,0 +1,32 @@ +package net.ornithemc.osl.gui.impl.shape; + +import net.ornithemc.osl.gui.api.color.Color; +import net.ornithemc.osl.gui.api.shape.Text; +import net.ornithemc.osl.text.api.TextComponent; + +public class TextImpl implements Text { + private final TextComponent value; + private final Color color; + private final boolean isShaded; + + public TextImpl(TextComponent value, Color color, boolean isShaded) { + this.value = value; + this.color = color; + this.isShaded = isShaded; + } + + @Override + public TextComponent value() { + return this.value; + } + + @Override + public Color color() { + return this.color; + } + + @Override + public boolean isShaded() { + return this.isShaded; + } +} diff --git a/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/TextureImpl.java b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/TextureImpl.java new file mode 100644 index 00000000..cfe6e7b5 --- /dev/null +++ b/libraries/gui/src/main/java/net/ornithemc/osl/gui/impl/shape/TextureImpl.java @@ -0,0 +1,60 @@ +package net.ornithemc.osl.gui.impl.shape; + +import net.ornithemc.osl.core.api.util.NamespacedIdentifier; +import net.ornithemc.osl.gui.api.color.Color; +import net.ornithemc.osl.gui.api.shape.texture.Texture; +import net.ornithemc.osl.gui.api.shape.texture.Subsection; +import org.jetbrains.annotations.Nullable; + +public class TextureImpl implements Texture { + private final NamespacedIdentifier identifier; + private final int width; + private final int height; + private final @Nullable Subsection subsection; + private final @Nullable Color filter; + + public TextureImpl(NamespacedIdentifier identifier, int width, int height, @Nullable Subsection subsection, @Nullable Color filter) { + this.identifier = identifier; + this.width = width; + this.height = height; + this.subsection = subsection; + this.filter = filter; + } + + @Override + public NamespacedIdentifier identifier() { + return this.identifier; + } + + @Override + public int width() { + return this.width; + } + + @Override + public int height() { + return this.height; + } + + @Nullable + @Override + public Subsection subsection() { + return this.subsection; + } + + @Nullable + @Override + public Color filter() { + return this.filter; + } + + @Override + public Texture withSubsection(Subsection subsection) { + return new TextureImpl(this.identifier, this.width, this.height, subsection, this.filter); + } + + @Override + public Texture withFilter(Color filter) { + return new TextureImpl(this.identifier, this.width, this.height, this.subsection, filter); + } +} diff --git a/settings.gradle b/settings.gradle index 71db3fc3..27fb5816 100644 --- a/settings.gradle +++ b/settings.gradle @@ -119,6 +119,8 @@ include ':libraries:executors:executors-mc14w07a-mc14w20b' include ':libraries:executors:executors-mc14w21a-mc1.13.2' include ':libraries:executors:executors-mc18w43a-mc1.14.4' +include ':libraries:gui' + include ':libraries:items' include ':libraries:items:items-mca1.0.1_01-mc1.6.4' include ':libraries:items:items-mc13w36a-mc14w25b'