Skip to content

Commit

Permalink
Add support for translucency
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Jan 22, 2025
1 parent b5fbaf3 commit f3007d0
Show file tree
Hide file tree
Showing 18 changed files with 119 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public int alignY(int x, int y) {

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
super.drawGuiTextureSubsection(path, width, height, this.alignX(x, y), this.alignY(x, y), subsectionWidth,
subsectionHeight, offsetX, offsetY);
subsectionHeight, offsetX, offsetY, alpha);
}

@Override
Expand All @@ -40,8 +40,8 @@ public void drawText(Component text, Color color, int x, int y, boolean shadowed
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
super.drawTexture(path, width, height, this.alignX(x, y), this.alignY(x, y));
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
super.drawTexture(path, width, height, this.alignX(x, y), this.alignY(x, y), alpha);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import net.pixaurora.kitten_cube.impl.ui.widget.text.TextBox;

public interface GuiDisplay {
public void drawTexture(ResourcePath path, int width, int height, int x, int y);
public static double DEFAULT_ALPHA = 1.0;

public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha);

public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY);
int subsectionHeight, int offsetX, int offsetY, double alpha);

public void drawText(Component text, Color color, int x, int y, boolean shadowed);

Expand All @@ -24,14 +26,18 @@ public default void drawTextBox(TextBox box) {
this.drawTextBox(box, Alignment.TOP_LEFT, Size.ZERO);
}

public default void drawTexture(ResourcePath path, Size size, Point pos, double alpha) {
this.drawTexture(path, size.width(), size.height(), pos.x(), pos.y(), alpha);
}

public default void drawTexture(ResourcePath path, Size size, Point pos) {
this.drawTexture(path, size.width(), size.height(), pos.x(), pos.y());
this.drawTexture(path, size, pos, DEFAULT_ALPHA);
}

public default void drawGuiTextureSubsection(ResourcePath path, Size size, Point pos, Size subsection,
Point offset) {
Point offset, double alpha) {
this.drawGuiTextureSubsection(path, size.width(), size.height(), pos.x(), pos.y(), subsection.width(),
subsection.height(), offset.x(), offset.y());
subsection.height(), offset.x(), offset.y(), alpha);
}

public default void drawText(Component text, Color color, Point pos, boolean shadowed) {
Expand All @@ -46,12 +52,20 @@ public default void draw(Texture texture, Point pos) {
this.drawTexture(texture.path(), texture.size(), pos);
}

public default void drawGui(GuiTexture texture, Point pos, double alpha) {
this.drawGuiTextureSubsection(texture.path(), texture.size(), pos, texture.size(), Point.ZERO, alpha);
}

public default void drawGui(GuiTexture texture, Point pos) {
this.drawGuiTextureSubsection(texture.path(), texture.size(), pos, texture.size(), Point.ZERO);
this.drawGui(texture, pos, DEFAULT_ALPHA);
}

public default void drawGui(GuiTexture texture, Point pos, Size subsection, Point offset, double alpha) {
this.drawGuiTextureSubsection(texture.path(), texture.size(), pos, subsection, offset, alpha);
}

public default void drawGui(GuiTexture texture, Point pos, Size subsection, Point offset) {
this.drawGuiTextureSubsection(texture.path(), texture.size(), pos, subsection, offset);
this.drawGui(texture, pos, subsection, offset, DEFAULT_ALPHA);
}

public default int alignX(int x, int y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public GuiDisplay parent() {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
this.parent.drawTexture(path, width, height, x, y);
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.parent.drawTexture(path, width, height, x, y, alpha);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
this.parent.drawGuiTextureSubsection(path, width, height, x, y, subsectionWidth, subsectionHeight, offsetX,
offsetY);
offsetY, alpha);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public TiledBackground(GuiTexture texture) {
@Override
public void draw(GuiDisplay gui, Point mousePos) {
for (Point pos : posToDrawAt) {
gui.drawGui(this.texture, pos);
gui.drawGui(this.texture, pos, 0.5);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ public GuiDisplayImpl(GuiElement element, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0);
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0, alpha);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
GL11.glEnable(GL11.GL_BLEND);

GL11.glBindTexture(GL11.GL_TEXTURE_2D, Minecraft.INSTANCE.textureManager.load(this.conversions.convert(path)));

float u = offsetX;
Expand All @@ -44,7 +46,7 @@ public void drawGuiTextureSubsection(ResourcePath path, int width, int height, i

BufferBuilder bufferBuilder = BufferBuilder.INSTANCE;
bufferBuilder.start();
bufferBuilder.color(255, 255, 255, 255);
bufferBuilder.color(255, 255, 255, (int) (255.0 * alpha));
bufferBuilder.vertex(x, y + subsectionHeight, 0.0, u * invertedTexWidth,
(v + (float) subsectionHeight) * invertedTexHeight);
bufferBuilder.vertex(x + subsectionWidth, y + subsectionHeight, 0.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ public GuiDisplayImpl(PoseStack poseStack, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0);
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0, alpha);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();

RenderSystem.setShaderTexture(0, conversions.convert(path));
GuiComponent.blit(poseStack, x, y, offsetX, offsetY, subsectionWidth, subsectionHeight, width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void init() {

@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float delta) {
this.renderDirtBackground(0);
super.render(poseStack, mouseX, mouseY, delta);

GuiDisplay display = new GuiDisplayImpl(poseStack, this.conversions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ public GuiDisplayImpl(PoseStack poseStack, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0);
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0, alpha);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();

RenderSystem.setShaderTexture(0, conversions.convert(path));
GuiComponent.blit(poseStack, x, y, offsetX, offsetY, subsectionWidth, subsectionHeight, width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void init() {

@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float delta) {
this.renderDirtBackground(0);
super.render(poseStack, mouseX, mouseY, delta);

GuiDisplay display = new GuiDisplayImpl(poseStack, this.conversions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ public GuiDisplayImpl(PoseStack poseStack, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0);
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0, alpha);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();

RenderSystem.setShaderTexture(0, conversions.convert(path));
GuiComponent.blit(poseStack, x, y, offsetX, offsetY, subsectionWidth, subsectionHeight, width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void init() {

@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float delta) {
this.renderDirtBackground(0);
super.render(poseStack, mouseX, mouseY, delta);

GuiDisplay display = new GuiDisplayImpl(poseStack, this.conversions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ public GuiDisplayImpl(PoseStack poseStack, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0);
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0, alpha);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();

RenderSystem.setShaderTexture(0, conversions.convert(path));
GuiComponent.blit(poseStack, x, y, offsetX, offsetY, subsectionWidth, subsectionHeight, width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public void init() {

@Override
public void render(PoseStack poseStack, int mouseX, int mouseY, float delta) {
this.renderDirtBackground(poseStack);
super.render(poseStack, mouseX, mouseY, delta);

GuiDisplay display = new GuiDisplayImpl(poseStack, this.conversions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.pixaurora.kitten_square.impl.ui.display;

import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.util.FormattedCharSequence;
Expand All @@ -25,13 +27,18 @@ public GuiDisplayImpl(GuiGraphics graphics, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0);
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.drawGuiTextureSubsection(path, width, height, x, y, width, height, 0, 0, alpha);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
RenderSystem.enableDepthTest();

this.graphics.blit(conversions.convert(path), x, y, offsetX, offsetY, subsectionWidth, subsectionHeight, width,
height);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.pixaurora.kitten_square.impl.ui.display;

import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.util.FormattedCharSequence;
Expand All @@ -25,15 +27,25 @@ public GuiDisplayImpl(GuiGraphics graphics, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.graphics.setColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.enableDepthTest();

this.graphics.blit(conversions.convert(path), x, y, 0, 0.0F, 0.0F, width, height, width, height);
this.graphics.setColor(1.0f, 1.0f, 1.0f, 1.0f);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
this.graphics.setColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.enableDepthTest();

this.graphics.blitSprite(conversions.convert(path), width, height, offsetX, offsetY, x, y, subsectionWidth,
subsectionHeight);
this.graphics.setColor(1.0f, 1.0f, 1.0f, 1.0f);
}

@SuppressWarnings("resource")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.pixaurora.kitten_square.impl.ui.display;

import com.mojang.blaze3d.systems.RenderSystem;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.util.FormattedCharSequence;
Expand All @@ -25,15 +27,27 @@ public GuiDisplayImpl(GuiGraphics graphics, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.graphics.setColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.enableDepthTest();

this.graphics.blit(conversions.convert(path), x, y, 0, 0.0F, 0.0F, width, height, width, height);

this.graphics.setColor(1.0f, 1.0f, 1.0f, 1.0f);
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
int subsectionHeight, int offsetX, int offsetY, double alpha) {
this.graphics.setColor(1.0f, 1.0f, 1.0f, (float) alpha);
RenderSystem.enableBlend();
RenderSystem.enableDepthTest();

this.graphics.blitSprite(conversions.convert(path), width, height, offsetX, offsetY, x, y, subsectionWidth,
subsectionHeight);

this.graphics.setColor(1.0f, 1.0f, 1.0f, 1.0f);
}

@SuppressWarnings("resource")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.toasts.TutorialToast;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.ARGB;
import net.minecraft.util.FormattedCharSequence;
import net.pixaurora.kit_tunes.api.resource.ResourcePath;
import net.pixaurora.kitten_cube.impl.MinecraftClient;
Expand All @@ -29,16 +31,17 @@ public GuiDisplayImpl(GuiGraphics graphics, ConversionCacheImpl conversions) {
}

@Override
public void drawTexture(ResourcePath path, int width, int height, int x, int y) {
public void drawTexture(ResourcePath path, int width, int height, int x, int y, double alpha) {
this.graphics.blit(RenderType::guiTextured, conversions.convert(path), x, y, 0.0F, 0.0F, width, height, width,
height);
height, ARGB.white((float) alpha));
}

@Override
public void drawGuiTextureSubsection(ResourcePath path, int width, int height, int x, int y, int subsectionWidth,
int subsectionHeight, int offsetX, int offsetY) {
this.graphics.blitSprite(RenderType::guiTextured, conversions.convert(path), width, height, offsetX, offsetY, x,
y, subsectionWidth, subsectionHeight);
int subsectionHeight, int offsetX, int offsetY, double alpha) {
TextureAtlasSprite sprite = this.graphics.sprites.getSprite(conversions.convert(path));
this.graphics.blitSprite(RenderType::guiTextured, sprite, width, height, offsetX, offsetY, x,
y, subsectionWidth, subsectionHeight, ARGB.white((float) alpha));
}

@SuppressWarnings("resource")
Expand Down
Loading

0 comments on commit f3007d0

Please sign in to comment.