Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
3 changes: 3 additions & 0 deletions libraries/gui/README.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions libraries/gui/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
setUpLibrary(project)
7 changes: 7 additions & 0 deletions libraries/gui/gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.ornithemc.osl.gui.api.pipelines;

public class RenderPipelines {
public static RenderPipeline GUI_TEXTURED = (gui, transforms) -> {
transforms.enableBlend();
};
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading