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 26, 2024
1 parent 33d22b5 commit 044530c
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/aeronicamc/mods/mxtune/MXTune.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import aeronicamc.mods.mxtune.managers.PlayManager;
import aeronicamc.mods.mxtune.network.MultiPacketStringManager;
import aeronicamc.mods.mxtune.network.PacketDispatcher;
import aeronicamc.mods.mxtune.render.RenderHelper;
import aeronicamc.mods.mxtune.render.blockentity.MusicBlockEntityRenderer;
import aeronicamc.mods.mxtune.render.entity.InfoRenderer;
import aeronicamc.mods.mxtune.render.entity.MusicVenueInfoRenderer;
Expand Down Expand Up @@ -123,6 +124,7 @@ private void clientSetup(final FMLClientSetupEvent event)
RenderTypeLookup.setRenderLayer(ModBlocks.MUSIC_BLOCK.get(), RenderType.cutoutMipped());
ClientRegistry.bindTileEntityRenderer(ModBlockEntities.INV_MUSIC_BLOCK.get(), MusicBlockEntityRenderer::new);
new InfoRenderer(Minecraft.getInstance().getTextureManager());
RenderHelper.init();
}

private void modLoadingComplete(FMLLoadCompleteEvent event)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/aeronicamc/mods/mxtune/events/ClientEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import aeronicamc.mods.mxtune.managers.GroupClient;
import aeronicamc.mods.mxtune.network.PacketDispatcher;
import aeronicamc.mods.mxtune.network.messages.SyncRequestMessage;
import aeronicamc.mods.mxtune.render.RenderHelper;
import aeronicamc.mods.mxtune.render.entity.InfoRenderer;
import aeronicamc.mods.mxtune.sound.ClientAudio;
import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -20,12 +21,14 @@ private ClientEvents() { /* NOOP */ }
public static void event(ClientPlayerNetworkEvent.LoggedInEvent event)
{
GroupClient.clear();
RenderHelper.getOverlayItem().clear();
PacketDispatcher.sendToServer(new SyncRequestMessage());
}

@SubscribeEvent
public static void event(ClientPlayerNetworkEvent.LoggedOutEvent event)
{
RenderHelper.getOverlayItem().clear();
InfoRenderer.getInstance().clearInfoRendererInstances();
}

Expand All @@ -34,6 +37,7 @@ public static void event(ClientPlayerNetworkEvent.RespawnEvent event)
{
GroupClient.clear();
ClientAudio.stopAll();
RenderHelper.getOverlayItem().clear();
PacketDispatcher.sendToServer(new SyncRequestMessage());
}
}
4 changes: 3 additions & 1 deletion src/main/java/aeronicamc/mods/mxtune/gui/TestScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import aeronicamc.mods.mxtune.Reference;
import aeronicamc.mods.mxtune.gui.mml.GuiFileImporter;
import aeronicamc.mods.mxtune.gui.mml.GuiMXT;
import aeronicamc.mods.mxtune.gui.toasts.MXOverlay;
import aeronicamc.mods.mxtune.gui.toasts.MXToast;
import aeronicamc.mods.mxtune.gui.widget.MXButton;
import aeronicamc.mods.mxtune.gui.widget.MXLabel;
import aeronicamc.mods.mxtune.gui.widget.MXTextFieldWidget;
import aeronicamc.mods.mxtune.gui.widget.list.SoundFontList;
import aeronicamc.mods.mxtune.init.ModItems;
import aeronicamc.mods.mxtune.render.RenderHelper;
import aeronicamc.mods.mxtune.util.Misc;
import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.gui.screen.Screen;
Expand Down Expand Up @@ -88,7 +90,7 @@ public void init()

public void onButtonOpen()
{
LOGGER.debug("Nothing to do :P");
RenderHelper.getOverlayItem().addOverlay(new MXOverlay());
}

public void onButtonFile()
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/aeronicamc/mods/mxtune/gui/toasts/MXOverlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package aeronicamc.mods.mxtune.gui.toasts;

import aeronicamc.mods.mxtune.init.ModItems;
import aeronicamc.mods.mxtune.render.IOverlayItem;
import aeronicamc.mods.mxtune.render.OverlayItemGui;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;

public class MXOverlay implements IOverlayItem {
private static final ITextComponent TITLE_TEXT = new TranslationTextComponent("itemGroup.mxtune").withStyle(TextFormatting.YELLOW);
private final ItemStack itemStack = ModItems.getMultiInst(27);
private long lastChanged;
private boolean changed;

@SuppressWarnings("deprecation")
@Override
public Visibility render(MatrixStack pPoseStack, OverlayItemGui pOverlayComponent, long delta) {
if (this.changed) {
this.lastChanged = delta;
this.changed = false;
}
pOverlayComponent.getMinecraft().getTextureManager().bind(TEXTURE);
RenderSystem.color3f(1.0F, 1.0F, 1.0F);
pOverlayComponent.blit(pPoseStack, 0, 0, 0, 0, this.width(), this.height());
pOverlayComponent.getMinecraft().font.draw(pPoseStack, TITLE_TEXT, 30.0F, 7.0F, -11534256);

pOverlayComponent.getMinecraft().getItemRenderer().renderAndDecorateItem(itemStack, 8, 8);
return delta - this.lastChanged >= 5000L ? IOverlayItem.Visibility.HIDE : IOverlayItem.Visibility.SHOW;
}
}
42 changes: 42 additions & 0 deletions src/main/java/aeronicamc/mods/mxtune/render/IOverlayItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package aeronicamc.mods.mxtune.render;

import com.mojang.blaze3d.matrix.MatrixStack;
import net.minecraft.client.audio.SimpleSound;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.SoundEvents;

public interface IOverlayItem {
ResourceLocation TEXTURE = new ResourceLocation("textures/gui/toasts.png");
Object NO_TOKEN = new Object();

IOverlayItem.Visibility render(MatrixStack pPoseStack, OverlayItemGui pOverlayComponent, long delta);

default Object getToken() {
return NO_TOKEN;
}

default int width() {
return 160;
}

default int height() {
return 32;
}

enum Visibility {
SHOW(SoundEvents.UI_TOAST_IN),
HIDE(SoundEvents.UI_TOAST_OUT);

private final SoundEvent soundEvent;

private Visibility(SoundEvent pSoundEvent) {
this.soundEvent = pSoundEvent;
}

public void playSound(SoundHandler pHandler) {
pHandler.play(SimpleSound.forUI(this.soundEvent, 1.0F, 1.0F));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import aeronicamc.mods.mxtune.util.SheetMusicHelper;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.toasts.IToast;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
Expand All @@ -30,7 +31,7 @@ public static void render(ItemStack itemStack, RenderGameOverlayEvent.Post event

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);
mc.getTextureManager().bind(IToast.TEXTURE);

RenderSystem.color3f(1.0F, 1.0F, 1.0F);
RenderHelper.blit(pPoseStack, 0, 0, 0, 0, RenderHelper.WIDTH, RenderHelper.HEIGHT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.toasts.IToast;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
Expand Down Expand Up @@ -56,7 +57,7 @@ else if (raytraceresult instanceof EntityRayTraceResult)
else
blockName = new StringTextComponent("---").withStyle(TextFormatting.AQUA);

mc.getTextureManager().bind(RenderHelper.TEXTURE);
mc.getTextureManager().bind(IToast.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);
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/aeronicamc/mods/mxtune/render/OverlayItemGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package aeronicamc.mods.mxtune.render;

import com.google.common.collect.Queues;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.util.Util;
import net.minecraft.util.math.MathHelper;

import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Deque;

;

public class OverlayItemGui extends AbstractGui {
private final Minecraft minecraft;
private final OverlayInstance<?>[] visible = new OverlayInstance[5];
private final Deque<IOverlayItem> queued = Queues.newArrayDeque();

public OverlayItemGui(Minecraft pMinecraft) {
this.minecraft = pMinecraft;
}

public void render(MatrixStack pPoseStack) {
if (!this.minecraft.options.hideGui) {
for(int i = 0; i < this.visible.length; ++i) {
OverlayInstance<?> overlayInstance = this.visible[i];
if (overlayInstance != null && overlayInstance.render(this.minecraft.getWindow().getGuiScaledWidth(), i, pPoseStack)) {
this.visible[i] = null;
}

if (this.visible[i] == null && !this.queued.isEmpty()) {
this.visible[i] = new OverlayInstance<>(this.queued.removeFirst());
}
}

}
}

@SuppressWarnings("unchecked")
@Nullable
public <T extends IOverlayItem> T getOverlay(Class<? extends T> pIOverlayItem, Object pToken) {
for(OverlayInstance<?> overlayInstance : this.visible) {
if (overlayInstance != null && pIOverlayItem.isAssignableFrom(overlayInstance.getOverlayItem().getClass()) && overlayInstance.getOverlayItem().getToken().equals(pToken)) {
return (T)overlayInstance.getOverlayItem();
}
}

for(IOverlayItem overlayItem : this.queued) {
if (pIOverlayItem.isAssignableFrom(overlayItem.getClass()) && overlayItem.getToken().equals(pToken)) {
return (T)overlayItem;
}
}

return (T)null;
}

public void clear() {
Arrays.fill(this.visible, (Object)null);
this.queued.clear();
}

public void addOverlay(IOverlayItem overlayItem) {
this.queued.add(overlayItem);
}

public Minecraft getMinecraft() {
return this.minecraft;
}

class OverlayInstance<T extends IOverlayItem> {
private final T overlayItem;
private long animationTime = -1L;
private long visibleTime = -1L;
private IOverlayItem.Visibility visibility = IOverlayItem.Visibility.SHOW;

private OverlayInstance(T pOverlayItem) {
this.overlayItem = pOverlayItem;
}

public T getOverlayItem() {
return this.overlayItem;
}

private float getVisibility(long p_193686_1_) {
float f = MathHelper.clamp((float)(p_193686_1_ - this.animationTime) / 600.0F, 0.0F, 1.0F);
f = f * f;
return this.visibility == IOverlayItem.Visibility.HIDE ? 1.0F - f : f;
}

@SuppressWarnings("deprecation")
public boolean render(int x, int y, MatrixStack pPoseStack) {
long i = Util.getMillis();
if (this.animationTime == -1L) {
this.animationTime = i;
this.visibility.playSound(OverlayItemGui.this.minecraft.getSoundManager());
}

if (this.visibility == IOverlayItem.Visibility.SHOW && i - this.animationTime <= 600L) {
this.visibleTime = i;
}

RenderSystem.pushMatrix();
RenderSystem.translatef((float)x - (float)this.overlayItem.width() * this.getVisibility(i), (float)(y * this.overlayItem.height()), (float)(800 + y));
IOverlayItem.Visibility itoast$visibility = this.overlayItem.render(pPoseStack, OverlayItemGui.this, i - this.visibleTime);
RenderSystem.popMatrix();
if (itoast$visibility != this.visibility) {
this.animationTime = i - (long)((int)((1.0F - this.getVisibility(i)) * 600.0F));
this.visibility = itoast$visibility;
this.visibility.playSound(OverlayItemGui.this.minecraft.getSoundManager());
}

return this.visibility == IOverlayItem.Visibility.HIDE && i - this.animationTime > 600L;
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/aeronicamc/mods/mxtune/render/RenderEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.DrawHighlightEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
Expand Down Expand Up @@ -139,6 +140,13 @@ public static void event(RenderGameOverlayEvent.Post event)
MusicVenueToolOverlayRenderer.render(getPlayer(), itemStack, event);
}

@SubscribeEvent
public static void event(TickEvent.RenderTickEvent event) {
if (TickEvent.Phase.END.equals(event.phase)) {
RenderHelper.getOverlayItem().render(new MatrixStack());
}
}

static void renderGroupStatusPlacard(MatrixStack pMatrixStack, IRenderTypeBuffer.Impl pBuffer, ActiveRenderInfo pActiveRenderInfo, float pPartialTicks, ClippingHelper pClippingHelper)
{
if (GroupClient.hasGroups())
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/aeronicamc/mods/mxtune/render/RenderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.debug.DebugRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.shapes.ISelectionContext;
Expand All @@ -28,10 +27,18 @@
@SuppressWarnings("unused")
public class RenderHelper
{
static final ResourceLocation TEXTURE = new ResourceLocation("textures/gui/toasts.png");
static final int WIDTH = 160;
static final int HEIGHT = 32;
static final Minecraft mc = Minecraft.getInstance();
private static OverlayItemGui overlayItem;

public static void init() {
overlayItem = new OverlayItemGui(mc);
}

public static OverlayItemGui getOverlayItem() {
return overlayItem;
}

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

0 comments on commit 044530c

Please sign in to comment.