Skip to content

Commit

Permalink
add overlay renderer for lamp & try to fix tool bar renderer (#1571)
Browse files Browse the repository at this point in the history
Co-authored-by: YoungOnion <[email protected]>
  • Loading branch information
qwer523 and YoungOnionMC authored Jul 14, 2024
1 parent 7367103 commit 1a4e0c9
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 72 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/api/gui/GuiTextures.java
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ public class GuiTextures {
}
}

// Lamp item overlay
public static final ResourceTexture LAMP_NO_BLOOM = new ResourceTexture(
"gtceu:textures/gui/item_overlay/lamp_no_bloom.png");
public static final ResourceTexture LAMP_NO_LIGHT = new ResourceTexture(
"gtceu:textures/gui/item_overlay/lamp_no_light.png");

// ME hatch/bus
public static final ResourceTexture NUMBER_BACKGROUND = new ResourceTexture(
"gtceu:textures/gui/widget/number_background.png");
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/com/gregtechceu/gtceu/api/item/LampBlockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;

import static com.gregtechceu.gtceu.common.block.LampBlock.isBloomEnabled;
import static com.gregtechceu.gtceu.common.block.LampBlock.isInverted;
import static com.gregtechceu.gtceu.common.block.LampBlock.isLightEnabled;

@ParametersAreNonnullByDefault
public class LampBlockItem extends BlockItem implements IItemRendererProvider {

public LampBlockItem(LampBlock block, Properties properties) {
Expand All @@ -33,10 +40,11 @@ protected BlockState getPlacementState(BlockPlaceContext context) {
BlockState returnValue = super.getPlacementState(context);
ItemStack handItem = context.getItemInHand();
if (returnValue != null && handItem.hasTag()) {
var tag = handItem.getTag();
returnValue = returnValue
.setValue(LampBlock.INVERTED, handItem.getTag().getBoolean(LampBlock.TAG_INVERTED))
.setValue(LampBlock.BLOOM, handItem.getTag().getBoolean(LampBlock.TAG_BLOOM))
.setValue(LampBlock.LIGHT, handItem.getTag().getBoolean(LampBlock.TAG_LIT));
.setValue(LampBlock.INVERTED, isInverted(tag))
.setValue(LampBlock.BLOOM, isBloomEnabled(tag))
.setValue(LampBlock.LIGHT, isLightEnabled(tag));
}
return returnValue;
}
Expand All @@ -52,10 +60,11 @@ public void fillItemCategory(CreativeModeTab category, NonNullList<ItemStack> it
public IRenderer getRenderer(ItemStack stack) {
BlockState state = getBlock().defaultBlockState();
if (stack.hasTag()) {
var tag = stack.getTag();
state = state
.setValue(LampBlock.INVERTED, stack.getTag().getBoolean(LampBlock.TAG_INVERTED))
.setValue(LampBlock.BLOOM, stack.getTag().getBoolean(LampBlock.TAG_BLOOM))
.setValue(LampBlock.LIGHT, stack.getTag().getBoolean(LampBlock.TAG_LIT));
.setValue(LampBlock.INVERTED, isInverted(tag))
.setValue(LampBlock.BLOOM, isBloomEnabled(tag))
.setValue(LampBlock.LIGHT, isLightEnabled(tag));
}
return getBlock().getRenderer(state);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.gregtechceu.gtceu.client.renderer.item;

import com.gregtechceu.gtceu.api.gui.GuiTextures;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import com.mojang.blaze3d.systems.RenderSystem;

import static com.gregtechceu.gtceu.common.block.LampBlock.isBloomEnabled;
import static com.gregtechceu.gtceu.common.block.LampBlock.isLightEnabled;

@OnlyIn(Dist.CLIENT)
public class LampItemOverlayRenderer {

private LampItemOverlayRenderer() {}

public static OverlayType getOverlayType(boolean light, boolean bloom) {
if (light) {
return bloom ? OverlayType.NONE : OverlayType.NO_BLOOM;
} else {
return bloom ? OverlayType.NO_LIGHT : OverlayType.NO_BLOOM_NO_LIGHT;
}
}

public static void renderOverlay(GuiGraphics graphics, ItemStack stack, int xPosition,
int yPosition) {
if (stack.hasTag()) {
var tag = stack.getTag();
var overlayType = getOverlayType(isLightEnabled(tag), isBloomEnabled(tag));
if (overlayType == OverlayType.NONE) {
return;
}

RenderSystem.disableDepthTest();
if (overlayType.noBloom()) {
GuiTextures.LAMP_NO_BLOOM.draw(graphics, 0, 0, xPosition, yPosition, 16, 16);
}

if (overlayType.noLight()) {
GuiTextures.LAMP_NO_LIGHT.draw(graphics, 0, 0, xPosition, yPosition, 16, 16);
}
RenderSystem.enableDepthTest();
}
}

public enum OverlayType {

NONE,
NO_BLOOM,
NO_LIGHT,
NO_BLOOM_NO_LIGHT;

public boolean noLight() {
return this == NO_LIGHT || this == NO_BLOOM_NO_LIGHT;
}

public boolean noBloom() {
return this == NO_BLOOM || this == NO_BLOOM_NO_LIGHT;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gregtechceu.gtceu.client.util;
package com.gregtechceu.gtceu.client.renderer.item;

import com.gregtechceu.gtceu.api.capability.GTCapabilityHelper;
import com.gregtechceu.gtceu.api.capability.IElectricItem;
Expand All @@ -7,16 +7,15 @@
import com.gregtechceu.gtceu.api.item.component.IDurabilityBar;
import com.gregtechceu.gtceu.api.item.component.IItemComponent;
import com.gregtechceu.gtceu.api.item.tool.ToolHelper;
import com.gregtechceu.gtceu.client.util.DrawUtil;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.FastColor;
import net.minecraft.world.item.ItemStack;

import com.mojang.blaze3d.vertex.VertexConsumer;
import org.apache.commons.lang3.tuple.Pair;
import org.joml.Matrix4f;

public final class ToolChargeBarRenderer {

Expand Down Expand Up @@ -44,7 +43,7 @@ public static void render(GuiGraphics graphics, int level, int xPosition, int yP
int x = xPosition + 2;
int y = yPosition + 13 - offset;
graphics.fill(RenderType.guiOverlay(), x, y, x + 13, y + (shadow ? 2 : 1), 400, colorShadow);
fillHorizontalGradient(graphics, RenderType.guiOverlay(), x, y, x + level, y + 1, left, right, 400);
DrawUtil.fillHorizontalGradient(graphics, RenderType.guiOverlay(), x, y, x + level, y + 1, left, right, 400);
// graphics.fill(RenderType.guiOverlay(), x + BAR_W, y, x + BAR_W - level, y - 1, colorBG);
}

Expand Down Expand Up @@ -109,56 +108,5 @@ private static boolean renderDurabilityBar(GuiGraphics graphics, int level, int
return true;
}

/**
* Fills a rectangle with a gradient color from colorFrom to colorTo at the specified z-level using the given render
* type and coordinates as the boundaries.
*
* @param y2 the y-coordinate of the second corner of the rectangle.
* @param x2 the x-coordinate of the second corner of the rectangle.
* @param y1 the y-coordinate of the first corner of the rectangle.
* @param x1 the x-coordinate of the first corner of the rectangle.
* @param renderType the render type to use.
* @param z the z-level of the rectangle.
* @param colorTo the ending color of the gradient.
* @param colorFrom the starting color of the gradient.
*/
public static void fillHorizontalGradient(GuiGraphics graphics, RenderType renderType, int x1, int y1, int x2,
int y2, int colorFrom, int colorTo, int z) {
VertexConsumer vertexconsumer = graphics.bufferSource().getBuffer(renderType);
fillHorizontalGradient(graphics, vertexconsumer, x1, y1, x2, y2, z, colorFrom, colorTo);
}

/**
* The core `fillGradient` method.
* <p>
* Fills a rectangle with a gradient color from colorFrom to colorTo at the specified z-level using the given render
* type and coordinates as the boundaries.
*
* @param consumer the {@linkplain VertexConsumer} object for drawing the vertices on screen.
* @param x1 the x-coordinate of the first corner of the rectangle.
* @param y1 the y-coordinate of the first corner of the rectangle.
* @param x2 the x-coordinate of the second corner of the rectangle.
* @param y2 the y-coordinate of the second corner of the rectangle.
* @param z the z-level of the rectangle.
* @param colorFrom the starting color of the gradient.
* @param colorTo the ending color of the gradient.
*/
private static void fillHorizontalGradient(GuiGraphics graphics, VertexConsumer consumer, int x1, int y1, int x2,
int y2, int z, int colorFrom, int colorTo) {
float a1 = (float) FastColor.ARGB32.alpha(colorFrom) / 255.0F;
float r1 = (float) FastColor.ARGB32.red(colorFrom) / 255.0F;
float g1 = (float) FastColor.ARGB32.green(colorFrom) / 255.0F;
float b1 = (float) FastColor.ARGB32.blue(colorFrom) / 255.0F;
float a2 = (float) FastColor.ARGB32.alpha(colorTo) / 255.0F;
float r2 = (float) FastColor.ARGB32.red(colorTo) / 255.0F;
float g2 = (float) FastColor.ARGB32.green(colorTo) / 255.0F;
float b2 = (float) FastColor.ARGB32.blue(colorTo) / 255.0F;
Matrix4f matrix4f = graphics.pose().last().pose();
consumer.vertex(matrix4f, (float) x1, (float) y1, (float) z).color(r1, g1, b1, a1).endVertex();
consumer.vertex(matrix4f, (float) x1, (float) y2, (float) z).color(r1, g1, b1, a1).endVertex();
consumer.vertex(matrix4f, (float) x2, (float) y2, (float) z).color(r2, g2, b2, a2).endVertex();
consumer.vertex(matrix4f, (float) x2, (float) y1, (float) z).color(r2, g2, b2, a2).endVertex();
}

private ToolChargeBarRenderer() {}
}
66 changes: 66 additions & 0 deletions src/main/java/com/gregtechceu/gtceu/client/util/DrawUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.gregtechceu.gtceu.client.util;

import com.gregtechceu.gtceu.core.mixins.GuiGraphicsAccessor;

import com.lowdragmc.lowdraglib.utils.ColorUtils;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.RenderType;

import com.mojang.blaze3d.vertex.VertexConsumer;
import org.joml.Matrix4f;

public class DrawUtil {

/**
* Fills a rectangle with a gradient color from colorFrom to colorTo at the specified z-level using the given render
* type and coordinates as the boundaries.
*
* @param y2 the y-coordinate of the second corner of the rectangle.
* @param x2 the x-coordinate of the second corner of the rectangle.
* @param y1 the y-coordinate of the first corner of the rectangle.
* @param x1 the x-coordinate of the first corner of the rectangle.
* @param renderType the render type to use.
* @param z the z-level of the rectangle.
* @param colorTo the ending color of the gradient.
* @param colorFrom the starting color of the gradient.
*/
public static void fillHorizontalGradient(GuiGraphics graphics, RenderType renderType, int x1, int y1, int x2,
int y2, int colorFrom, int colorTo, int z) {
VertexConsumer vertexconsumer = graphics.bufferSource().getBuffer(renderType);
fillHorizontalGradient(graphics, vertexconsumer, x1, y1, x2, y2, z, colorFrom, colorTo);
((GuiGraphicsAccessor) graphics).callFlushIfUnmanaged();
}

/**
* The core `fillGradient` method.
* <p>
* Fills a rectangle with a gradient color from colorFrom to colorTo at the specified z-level using the given render
* type and coordinates as the boundaries.
*
* @param consumer the {@linkplain VertexConsumer} object for drawing the vertices on screen.
* @param x1 the x-coordinate of the first corner of the rectangle.
* @param y1 the y-coordinate of the first corner of the rectangle.
* @param x2 the x-coordinate of the second corner of the rectangle.
* @param y2 the y-coordinate of the second corner of the rectangle.
* @param z the z-level of the rectangle.
* @param colorFrom the starting color of the gradient.
* @param colorTo the ending color of the gradient.
*/
private static void fillHorizontalGradient(GuiGraphics graphics, VertexConsumer consumer, int x1, int y1, int x2,
int y2, int z, int colorFrom, int colorTo) {
float a1 = ColorUtils.alpha(colorFrom);
float r1 = ColorUtils.red(colorFrom);
float g1 = ColorUtils.green(colorFrom);
float b1 = ColorUtils.blue(colorFrom);
float a2 = ColorUtils.alpha(colorTo);
float r2 = ColorUtils.red(colorTo);
float g2 = ColorUtils.green(colorTo);
float b2 = ColorUtils.blue(colorTo);
Matrix4f matrix4f = graphics.pose().last().pose();
consumer.vertex(matrix4f, (float) x1, (float) y1, (float) z).color(r1, g1, b1, a1).endVertex();
consumer.vertex(matrix4f, (float) x1, (float) y2, (float) z).color(r1, g1, b1, a1).endVertex();
consumer.vertex(matrix4f, (float) x2, (float) y2, (float) z).color(r2, g2, b2, a2).endVertex();
consumer.vertex(matrix4f, (float) x2, (float) y1, (float) z).color(r2, g2, b2, a2).endVertex();
}
}
34 changes: 28 additions & 6 deletions src/main/java/com/gregtechceu/gtceu/common/block/LampBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.lowdragmc.lowdraglib.client.renderer.IBlockRendererProvider;
import com.lowdragmc.lowdraglib.client.renderer.IRenderer;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
Expand All @@ -30,6 +31,10 @@
import java.util.List;
import java.util.Map;

import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class LampBlock extends Block implements IBlockRendererProvider {

public static final BooleanProperty BLOOM = BooleanProperty.create("bloom");
Expand Down Expand Up @@ -68,18 +73,30 @@ public static boolean isLightActive(BlockState state) {
return state.getValue(INVERTED) != state.getValue(POWERED);
}

public boolean isInverted(BlockState state) {
public static boolean isInverted(BlockState state) {
return state.getValue(INVERTED);
}

public boolean isLightEnabled(BlockState state) {
public static boolean isLightEnabled(BlockState state) {
return state.getValue(LIGHT);
}

public boolean isBloomEnabled(BlockState state) {
public static boolean isBloomEnabled(BlockState state) {
return state.getValue(BLOOM);
}

public static boolean isInverted(CompoundTag tag) {
return tag.getBoolean(TAG_INVERTED);
}

public static boolean isLightEnabled(CompoundTag tag) {
return tag.getBoolean(TAG_LIT);
}

public static boolean isBloomEnabled(CompoundTag tag) {
return tag.getBoolean(TAG_BLOOM);
}

public CompoundTag getTagFromState(BlockState state) {
CompoundTag tag = new CompoundTag();
tag.putBoolean(TAG_BLOOM, state.getValue(BLOOM));
Expand Down Expand Up @@ -109,6 +126,7 @@ public int getLightEmission(BlockState state, BlockGetter level, BlockPos pos) {
}

@Override
@SuppressWarnings("deprecation")
public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean movedByPiston) {
if (!level.isClientSide) {
boolean powered = state.getValue(POWERED);
Expand All @@ -120,6 +138,7 @@ public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldS
}

@Override
@SuppressWarnings("deprecation")
public void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, BlockPos neighborPos,
boolean movedByPiston) {
if (!level.isClientSide) {
Expand All @@ -131,6 +150,7 @@ public void neighborChanged(BlockState state, Level level, BlockPos pos, Block n
}

@Override
@SuppressWarnings("deprecation")
public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
if (state.getValue(POWERED) && !level.hasNeighborSignal(pos)) {
level.setBlock(pos, state.cycle(POWERED), state.getValue(LIGHT) ? 2 | 8 : 2);
Expand All @@ -149,16 +169,18 @@ public ItemStack getCloneItemStack(BlockState state, HitResult target,
public void appendHoverText(ItemStack stack, @Nullable BlockGetter level, List<Component> tooltip,
TooltipFlag flag) {
if (stack.hasTag()) {
if (stack.getTag().getBoolean(TAG_INVERTED))
var tag = stack.getTag();
if (isInverted(tag))
tooltip.add(Component.translatable("block.gtceu.lamp.tooltip.inverted"));
if (!stack.getTag().getBoolean(TAG_BLOOM))
if (!isBloomEnabled(tag))
tooltip.add(Component.translatable("block.gtceu.lamp.tooltip.no_bloom"));
if (!stack.getTag().getBoolean(TAG_LIT))
if (!isLightEnabled(tag))
tooltip.add(Component.translatable("block.gtceu.lamp.tooltip.no_light"));
}
}

@Override
@SuppressWarnings("deprecation")
public List<ItemStack> getDrops(BlockState state, LootParams.Builder params) {
List<ItemStack> returnValue = super.getDrops(state, params);
for (ItemStack stack : returnValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ static GuiGraphics create(Minecraft client, PoseStack matrices,
MultiBufferSource.BufferSource vertexConsumerProvider) {
return null;
}

@Invoker
void callFlushIfUnmanaged();
}
Loading

0 comments on commit 1a4e0c9

Please sign in to comment.