Skip to content

Commit

Permalink
WIP: Improve Instrument and MusicVenueTool overlays.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeronica committed Feb 25, 2024
1 parent c38bdc3 commit 33d22b5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aeronicamc.mods.mxtune.render;

import aeronicamc.mods.mxtune.MXTune;
import aeronicamc.mods.mxtune.init.ModItems;
import aeronicamc.mods.mxtune.sound.ClientAudio;
import aeronicamc.mods.mxtune.util.IInstrument;
import aeronicamc.mods.mxtune.util.SheetMusicHelper;
Expand All @@ -12,9 +12,11 @@
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.client.event.RenderGameOverlayEvent;

import static aeronicamc.mods.mxtune.render.RenderHelper.getPlayer;
import static aeronicamc.mods.mxtune.render.RenderHelper.mc;

public class InstrumentOverlayRenderer {
private static final ItemStack PLACARD_ITEM = new ItemStack(ModItems.PLACARD_ITEM.get());

public static void render(ItemStack itemStack, RenderGameOverlayEvent.Post event) {
// Display basic info about the instrument and tune. Optionally, displays some debug info depending on MXTune.isDevEnv flag.
Expand All @@ -26,25 +28,30 @@ public static void render(ItemStack itemStack, RenderGameOverlayEvent.Post event
final ITextComponent infoText = new StringTextComponent("").append(SheetMusicHelper.getFormattedMusicDuration(sheetMusic))
.append(String.format(" %s %s", mc.getSoundManager().getDebugString(), ClientAudio.getDebugString())).withStyle(TextFormatting.WHITE);

final int offset = Math.max(Math.max(mc.font.width(titleText), mc.font.width(MXTune.isDevEnv() ? infoText : extraText)) + 40, RenderHelper.WIDTH);
final int offset = Math.max(Math.max(mc.font.width(titleText), mc.font.width(isPlacardInHotBar() ? infoText : extraText)) + 40, RenderHelper.WIDTH);
final MatrixStack pPoseStack = event.getMatrixStack();

mc.getTextureManager().bind(RenderHelper.TEXTURE);

RenderSystem.color3f(1.0F, 1.0F, 1.0F);
RenderHelper.blit(pPoseStack, 0, 0, 0, 0, RenderHelper.WIDTH, RenderHelper.HEIGHT);
RenderHelper.blit(pPoseStack, ((offset - RenderHelper.WIDTH)/2) + 5, 0, 10, 0, RenderHelper.WIDTH -10, RenderHelper.HEIGHT);
RenderHelper.blit(pPoseStack, offset - RenderHelper.WIDTH + 10, 0, 10, 0, RenderHelper.WIDTH, RenderHelper.HEIGHT);
mc.getItemRenderer().renderAndDecorateItem(itemStack, 8, 8);

mc.font.draw(pPoseStack, titleText, 30.0F, 7.0F, -11534256);
mc.font.draw(pPoseStack, MXTune.isDevEnv() ? infoText : extraText, 30.0F, 17.0F, -11534256);
mc.getItemRenderer().renderAndDecorateItem(itemStack, 8, 8);
mc.font.draw(pPoseStack, isPlacardInHotBar() ? infoText : extraText, 30.0F, 17.0F, -11534256);

if (MXTune.isDevEnv()) {
final int[] posY = new int[1];
posY[0] = 25;
if (isPlacardInHotBar()) {
final int[] posY = { 0 };
posY[0] = (25);
ClientAudio.getAudioData()
.forEach(audioData -> mc.font.drawShadow(pPoseStack, audioData.getInfo(), 5, posY[0] += 10, -11534256));
}
}
}

private static boolean isPlacardInHotBar() {
int slot = getPlayer().inventory.findSlotMatchingItem(PLACARD_ITEM);
return slot >= 0 && slot < 9;
}
}
29 changes: 13 additions & 16 deletions src/main/java/aeronicamc/mods/mxtune/render/RenderEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.List;

import static aeronicamc.mods.mxtune.render.ModRenderType.*;
import static aeronicamc.mods.mxtune.render.RenderHelper.getPlayer;
import static aeronicamc.mods.mxtune.render.RenderHelper.mc;

@SuppressWarnings("deprecation")
Expand All @@ -52,30 +53,28 @@ public static void event(ItemTooltipEvent event)
{
if (event.getItemStack().getItem().equals(ModBlocks.MUSIC_BLOCK.get().asItem()))
event.getToolTip().add(new TranslationTextComponent("tooltip.mxtune.block_music.help").withStyle(TextFormatting.YELLOW));
else if (event.getItemStack().getItem().equals(ModItems.MUSIC_PAPER.get()))
if (event.getItemStack().getItem().equals(ModItems.MUSIC_PAPER.get()))
event.getToolTip().add(new TranslationTextComponent("tooltip.mxtune.music_paper.help").withStyle(TextFormatting.YELLOW));
}

@SuppressWarnings("deprecation")
@SubscribeEvent
public static void event(DrawHighlightEvent.HighlightBlock event)
{
if (mc.player == null )
return;
if (mc.options.renderDebug) return;
final BlockRayTraceResult blockRayTraceResult = event.getTarget();
final IRenderTypeBuffer renderTypeBuffer = event.getBuffers();
final ActiveRenderInfo activeRenderInfo = event.getInfo();
final MatrixStack matrixStack = event.getMatrix();

final Vector3d camera = activeRenderInfo.getPosition();
final World level = mc.player.level;
final World level = getPlayer().level;

final BlockPos blockPos = blockRayTraceResult.getBlockPos();
final BlockState blockState = level.getBlockState(blockRayTraceResult.getBlockPos());

// Highlight blocks for tool use
if (mc.player.inventory.getSelected().getItem() instanceof MusicVenueToolItem)
if (getPlayer().inventory.getSelected().getItem() instanceof MusicVenueToolItem)
{
if (event.isCancelable()) event.setCanceled(true);

Expand All @@ -87,18 +86,18 @@ public static void event(DrawHighlightEvent.HighlightBlock event)
}

// Show the pre-placement info panel outline.
if (mc.player.inventory.getSelected().getItem() instanceof MusicVenueInfoItem)
if (getPlayer().inventory.getSelected().getItem() instanceof MusicVenueInfoItem)
{
if (event.isCancelable()) event.setCanceled(true);

if (!blockState.isAir(level, blockPos) && level.getWorldBorder().isWithinBounds(blockPos))
{
final ItemUseContext useContext = new ItemUseContext(mc.player, Hand.MAIN_HAND, blockRayTraceResult);
final ItemUseContext useContext = new ItemUseContext(getPlayer(), Hand.MAIN_HAND, blockRayTraceResult);
final BlockPos clickedPos = useContext.getClickedPos();
final Direction facing = useContext.getClickedFace();
final BlockPos placementPos = clickedPos.relative(facing);

if (MusicVenueInfoItem.mayPlace(mc.player, facing, useContext.getItemInHand(), placementPos))
if (MusicVenueInfoItem.mayPlace(getPlayer(), facing, useContext.getItemInHand(), placementPos))
{
final HangingEntity infoEntity = new MusicVenueInfoEntity(useContext.getLevel(), placementPos, facing);
if (infoEntity.survives())
Expand All @@ -116,10 +115,8 @@ public static void event(DrawHighlightEvent.HighlightBlock event)
@SubscribeEvent
public static void event(DrawHighlightEvent.HighlightEntity event)
{
if (mc.player == null )
return;
if (mc.options.renderDebug) return;
if (!(mc.player.inventory.getSelected().getItem() instanceof MusicVenueToolItem)) return;
if (!(getPlayer().inventory.getSelected().getItem() instanceof MusicVenueToolItem)) return;
if (event.isCancelable()) event.setCanceled(true);

final EntityRayTraceResult entityRayTraceResult = event.getTarget();
Expand All @@ -136,19 +133,19 @@ public static void event(DrawHighlightEvent.HighlightEntity event)
@SubscribeEvent
public static void event(RenderGameOverlayEvent.Post event)
{
if (mc.player == null || mc.options.renderDebug ) return;
final ItemStack itemStack = mc.player.inventory.getSelected();
if (mc.options.renderDebug ) return;
final ItemStack itemStack = getPlayer().inventory.getSelected();
InstrumentOverlayRenderer.render(itemStack, event);
MusicVenueToolOverlayRenderer.render(mc.player, itemStack, event);
MusicVenueToolOverlayRenderer.render(getPlayer(), itemStack, event);
}

static void renderGroupStatusPlacard(MatrixStack pMatrixStack, IRenderTypeBuffer.Impl pBuffer, ActiveRenderInfo pActiveRenderInfo, float pPartialTicks, ClippingHelper pClippingHelper)
{
if (mc.player != null && mc.level != null && GroupClient.hasGroups())
if (GroupClient.hasGroups())
{
ItemStack placardStack = new ItemStack(ModItems.PLACARD_ITEM.get());
Vector3d cam = pActiveRenderInfo.getPosition();
List<Entity> nearLivingEntities = mc.level.getEntities(null, mc.player.getBoundingBox().inflate(48));
List<Entity> nearLivingEntities = getPlayer().level.getEntities(null, getPlayer().getBoundingBox().inflate(48));
if (!nearLivingEntities.isEmpty()) {
nearLivingEntities.stream().filter(p -> pClippingHelper.isVisible(p.getBoundingBoxForCulling())).forEach(livingEntity -> {

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/aeronicamc/mods/mxtune/render/RenderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.blaze3d.vertex.IVertexBuilder;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.ActiveRenderInfo;
Expand All @@ -22,6 +23,8 @@
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;

import java.util.Objects;

@SuppressWarnings("unused")
public class RenderHelper
{
Expand All @@ -30,6 +33,10 @@ public class RenderHelper
static final int HEIGHT = 32;
static final Minecraft mc = Minecraft.getInstance();

static ClientPlayerEntity getPlayer() {
return Objects.requireNonNull(mc.player);
}

private RenderHelper() { /* NOP */ }

/**
Expand Down

0 comments on commit 33d22b5

Please sign in to comment.