Skip to content
Merged
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
48 changes: 19 additions & 29 deletions src/main/java/betterquesting/api/utils/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import betterquesting.api2.client.gui.misc.GuiRectangle;
import betterquesting.api2.client.gui.misc.IGuiRect;
import betterquesting.api2.client.gui.misc.IRenderedStackProvider;
import betterquesting.api2.client.gui.resources.colors.GuiColorStatic;
import betterquesting.api2.client.gui.resources.colors.IGuiColor;
import betterquesting.api2.client.gui.themes.presets.PresetTexture;
Expand All @@ -22,6 +23,7 @@
import org.lwjgl.opengl.GL11;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -32,19 +34,19 @@
public class RenderUtils {
public static final String REGEX_NUMBER = "[^\\.0123456789-]"; // I keep screwing this up so now it's reusable

public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, String text) {
public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, @Nullable String text) {
RenderItemStack(mc, stack, x, y, text, Color.WHITE.getRGB());
}

public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, String text, Color color) {
public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, @Nullable String text, Color color) {
RenderItemStack(mc, stack, x, y, text, color.getRGB());
}

public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, String text, int color) {
public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, @Nullable String text, int color) {
RenderItemStack(mc, stack, x, y, 16F, text, color);
}

public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, float z, String text, int color) {
public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y, float z, @Nullable String text, int color) {
if (stack == null || stack.isEmpty()) {
return;
}
Expand All @@ -70,7 +72,9 @@ public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y,
try {
itemRender.renderItemAndEffectIntoGUI(stack, x, y);

if (stack.getCount() != 1 || text != null) {
// Custom ItemStack text
if (stack.getCount() != 1 || (text != null && !text.isEmpty())) {
String textToDraw = text == null ? String.valueOf(stack.getCount()) : text;
GlStateManager.pushMatrix();

int w = getStringWidth(text, font);
Expand All @@ -94,7 +98,7 @@ public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y,
GlStateManager.disableDepth();
GlStateManager.disableBlend();

font.drawString(text, 0, 0, 16777215, true);
font.drawStringWithShadow(textToDraw, 0, 0, 16777215);

GlStateManager.enableLighting();
GlStateManager.enableDepth();
Expand All @@ -103,7 +107,7 @@ public static void RenderItemStack(Minecraft mc, ItemStack stack, int x, int y,
GlStateManager.popMatrix();
}

itemRender.renderItemOverlayIntoGUI(font, stack, x, y, "");
itemRender.renderItemOverlayIntoGUI(font, stack, x, y, null); // Pass null to skip rendering default ItemStack text
} catch (Exception e) {
BetterQuesting.logger.warn("Unabled to render item " + stack, e);
}
Expand Down Expand Up @@ -663,6 +667,11 @@ public static void drawHoveringText(List<String> textLines, int mouseX, int mous
drawHoveringText(ItemStack.EMPTY, textLines, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, font);
}

public static void drawHoveringText(IRenderedStackProvider stackProvider, List<String> textLines, int mouseX, int mouseY, int screenWidth, int screenHeight, int maxTextWidth, FontRenderer font) {
drawHoveringText(stackProvider.getRenderedStack(), textLines, mouseX, mouseY, screenWidth, screenHeight, maxTextWidth, font);
stackProvider.resetRenderedStack();
}

/**
* Modified version of Forge's tooltip rendering that doesn't adjust Z depth
*/
Expand Down Expand Up @@ -766,29 +775,10 @@ public static void drawHoveringText(@Nonnull final ItemStack stack, List<String>
} else if (tooltipY + tooltipHeight + 4 > screenHeight) {
tooltipY = screenHeight - tooltipHeight - 4;
}

/*int backgroundColor = 0xF0100010;
int borderColorStart = 0x505000FF;
int borderColorEnd = (borderColorStart & 0xFEFEFE) >> 1 | borderColorStart & 0xFF000000;

RenderTooltipEvent.Color colorEvent = new RenderTooltipEvent.Color(stack, textLines, tooltipX, tooltipY, font, backgroundColor, borderColorStart, borderColorEnd);
MinecraftForge.EVENT_BUS.post(colorEvent);
backgroundColor = colorEvent.getBackground();
borderColorStart = colorEvent.getBorderStart();
borderColorEnd = colorEvent.getBorderEnd();

GuiUtils.drawGradientRect(0, tooltipX - 3, tooltipY - 4, tooltipX + tooltipTextWidth + 3, tooltipY - 3, backgroundColor, backgroundColor);
GuiUtils.drawGradientRect(0, tooltipX - 3, tooltipY + tooltipHeight + 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 4, backgroundColor, backgroundColor);
GuiUtils.drawGradientRect(0, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
GuiUtils.drawGradientRect(0, tooltipX - 4, tooltipY - 3, tooltipX - 3, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
GuiUtils.drawGradientRect(0, tooltipX + tooltipTextWidth + 3, tooltipY - 3, tooltipX + tooltipTextWidth + 4, tooltipY + tooltipHeight + 3, backgroundColor, backgroundColor);
GuiUtils.drawGradientRect(0, tooltipX - 3, tooltipY - 3 + 1, tooltipX - 3 + 1, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd);
GuiUtils.drawGradientRect(0, tooltipX + tooltipTextWidth + 2, tooltipY - 3 + 1, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3 - 1, borderColorStart, borderColorEnd);
GuiUtils.drawGradientRect(0, tooltipX - 3, tooltipY - 3, tooltipX + tooltipTextWidth + 3, tooltipY - 3 + 1, borderColorStart, borderColorStart);
GuiUtils.drawGradientRect(0, tooltipX - 3, tooltipY + tooltipHeight + 2, tooltipX + tooltipTextWidth + 3, tooltipY + tooltipHeight + 3, borderColorEnd, borderColorEnd);

MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostBackground(stack, textLines, tooltipX, tooltipY, font, tooltipTextWidth, tooltipHeight));*/

// Doesn't fire RenderTooltipEvent.Color as we draw background/border according to theme
PresetTexture.TOOLTIP_BG.getTexture().drawTexture(tooltipX - 4, tooltipY - 4, tooltipTextWidth + 8, tooltipHeight + 8, 0F, 1F);
MinecraftForge.EVENT_BUS.post(new RenderTooltipEvent.PostBackground(stack, textLines, tooltipX, tooltipY, font, tooltipTextWidth, tooltipHeight));
int tooltipTop = tooltipY;

GlStateManager.translate(0F, 0F, 0.1F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import java.util.concurrent.CopyOnWriteArrayList;

// This will probably be rewritten at a later date once I reimplement Minecraft's inventory controls natively into their own isolated canvas elements
public class GuiContainerCanvas extends GuiContainer implements IScene {
public class GuiContainerCanvas extends GuiContainer implements IScene, IRenderedStackProvider {
private final List<IGuiPanel> guiPanels = new CopyOnWriteArrayList<>();
private final GuiRectangle rootTransform = new GuiRectangle(0, 0, 0, 0, 0);
private final GuiTransform transform = new GuiTransform(GuiAlign.FULL_BOX, new GuiPadding(16, 16, 16, 16), 0);
private boolean enabled = true;
private boolean useMargins = true;
private boolean useDefaultBG = false;
private boolean isVolatile = false;
private ItemStack renderedStack = ItemStack.EMPTY;

public final GuiScreen parent;

Expand Down Expand Up @@ -396,7 +397,18 @@ protected void renderToolTip(ItemStack stack, int x, int y) {

@Override
protected void drawHoveringText(List<String> textLines, int x, int y, FontRenderer font) {
RenderUtils.drawHoveringText(textLines, x, y, width, height, -1, font);
RenderUtils.drawHoveringText(this, textLines, x, y, width, height, -1, font);
}

@Nonnull
@Override
public ItemStack getRenderedStack() {
return renderedStack;
}

@Override
public void setRenderedStack(@Nonnull ItemStack stack) {
renderedStack = stack;
}

public void confirmClose(int id) {
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/betterquesting/api2/client/gui/GuiScreenCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class GuiScreenCanvas extends GuiScreen implements IScene {
public class GuiScreenCanvas extends GuiScreen implements IScene, IRenderedStackProvider {
private final List<IGuiPanel> guiPanels = new CopyOnWriteArrayList<>();
private final GuiRectangle rootTransform = new GuiRectangle(0, 0, 0, 0, 0);
private final GuiTransform transform = new GuiTransform(GuiAlign.FULL_BOX, new GuiPadding(16, 16, 16, 16), 0);
private boolean enabled = true;
private boolean useMargins = true;
private boolean useDefaultBG = false;
private boolean isVolatile = false;
private ItemStack renderedStack = ItemStack.EMPTY;

public final GuiScreen parent;

Expand Down Expand Up @@ -404,7 +405,18 @@ protected void renderToolTip(ItemStack stack, int x, int y) {

@Override
protected void drawHoveringText(List<String> textLines, int x, int y, @Nonnull FontRenderer font) {
RenderUtils.drawHoveringText(textLines, x, y, width, height, -1, font);
RenderUtils.drawHoveringText(this, textLines, x, y, width, height, -1, font);
}

@Nonnull
@Override
public ItemStack getRenderedStack() {
return renderedStack;
}

@Override
public void setRenderedStack(@Nonnull ItemStack stack) {
renderedStack = stack;
}

private void confirmClose(int id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public static void setActiveScene(@Nullable IScene scene) {
public static void onGuiOpened(GuiOpenEvent event) {
if (event.getGui() instanceof IScene) {
// TODO: Review the following
// Does this need to be cleared if the GUI isn't compatible?
// Would this interfere with an overlay canvas?
curScene = (IScene) event.getGui();
} else {
curScene = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@

public class PanelButtonQuest extends PanelButtonStorage<DBEntry<IQuest>> {
public final GuiRectangle rect;
public final EntityPlayer player;
public final IGuiTexture txFrame;

public PanelButtonQuest(GuiRectangle rect, int id, String txt, DBEntry<IQuest> value) {
super(rect, id, txt, value);
this.rect = rect;

player = Minecraft.getMinecraft().player;
EntityPlayer player = Minecraft.getMinecraft().player;
EnumQuestState qState = value == null ? EnumQuestState.LOCKED : value.getValue().getState(player);
IGuiColor txIconCol = null;
IGuiColor txIconCol;
boolean lock = false;

if (value != null) {
Expand All @@ -67,7 +66,7 @@ public List<String> getTooltip(int mx, int my) {
if (!this.getTransform().contains(mx, my)) return null;

DBEntry<IQuest> value = this.getStoredValue();
return value == null ? Collections.emptyList() : getQuestTooltip(value.getValue(), player, value.getID());
return value == null ? Collections.emptyList() : getQuestTooltip(value.getValue(), Minecraft.getMinecraft().player, value.getID());
}

private List<String> getQuestTooltip(IQuest quest, EntityPlayer player, int qID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ public class PanelButtonStorage<T> extends PanelButton {
private T stored = null;
private ICallback<T> callback = null;

/**
* Creates a panel button that stores a value.
* <p>
* Note: If a subclass overrides {@link #setStoredValue(T)}, make sure to call that method during construction
* time after calling this super constructor!
* </p>
*/
public PanelButtonStorage(IGuiRect rect, int id, String txt, T value) {
super(rect, id, txt);
this.setStoredValue(value);
setStoredRaw(value);
Comment thread
jchung01 marked this conversation as resolved.
}

private void setStoredRaw(T value) {
stored = value;
}

public PanelButtonStorage<T> setStoredValue(T value) {
this.stored = value;
setStoredRaw(value);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package betterquesting.api2.client.gui.misc;

import javax.annotation.Nonnull;

import net.minecraft.item.ItemStack;

public interface IRenderedStackProvider {
@Nonnull
ItemStack getRenderedStack();

void setRenderedStack(@Nonnull ItemStack stack);

default void resetRenderedStack() {
setRenderedStack(ItemStack.EMPTY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public PanelFluidSlot(IGuiRect rect, int id, FluidStack value) {
}

public PanelFluidSlot(IGuiRect rect, int id, FluidStack value, boolean showCount) {
super(rect, id, "", value);
super(rect, id, "", null); // Value will be set by setStoredValue()
this.showCount = showCount;

this.setTextures(PresetTexture.ITEM_FRAME.getTexture(), PresetTexture.ITEM_FRAME.getTexture(), new LayeredTexture(PresetTexture.ITEM_FRAME.getTexture(), new ColorTexture(PresetColor.ITEM_HIGHLIGHT.getColor(), new GuiPadding(1, 1, 1, 1))));
this.setStoredValue(value); // Need to run this again because of the instatiation order of showCount
this.setStoredValue(value);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import betterquesting.api2.client.gui.controls.PanelButtonStorage;
import betterquesting.api2.client.gui.misc.GuiPadding;
import betterquesting.api2.client.gui.misc.IGuiRect;
import betterquesting.api2.client.gui.misc.IRenderedStackProvider;
import betterquesting.api2.client.gui.resources.textures.ColorTexture;
import betterquesting.api2.client.gui.resources.textures.ItemTexture;
import betterquesting.api2.client.gui.resources.textures.LayeredTexture;
Expand Down Expand Up @@ -42,25 +43,22 @@ public PanelItemSlot(IGuiRect rect, int id, BigItemStack value, boolean showCoun
}

public PanelItemSlot(IGuiRect rect, int id, BigItemStack value, boolean showCount, boolean oreDict) {
super(rect, id, "", value);
super(rect, id, "", null); // Value will be set by setStoredValue()
this.showCount = showCount;
this.oreDict = oreDict;

this.setTextures(PresetTexture.ITEM_FRAME.getTexture(), PresetTexture.ITEM_FRAME.getTexture(), new LayeredTexture(PresetTexture.ITEM_FRAME.getTexture(), new ColorTexture(PresetColor.ITEM_HIGHLIGHT.getColor(), new GuiPadding(1, 1, 1, 1))));
this.setStoredValue(value); // Need to run this again because of the instatiation order of showCount
this.setStoredValue(value);
}

@Override
public PanelItemSlot setStoredValue(BigItemStack value) {
super.setStoredValue(value);

if (value != null) {
Minecraft mc = Minecraft.getMinecraft();
this.setIcon(oreDict || value.getBaseStack().getItemDamage() == OreDictionary.WILDCARD_VALUE ? new OreDictTexture(1F, value, showCount, true) : new ItemTexture(value, showCount, true), 1);
this.setTooltip(value.getBaseStack().getTooltip(mc.player, mc.gameSettings.advancedItemTooltips ? TooltipFlags.ADVANCED : TooltipFlags.NORMAL));
} else {
this.setIcon(null);
this.setTooltip(null);
}

updateOreStacks();
Expand All @@ -78,6 +76,11 @@ public List<String> getTooltip(int mx, int my) {
}

Minecraft mc = Minecraft.getMinecraft();
if (mc.currentScreen instanceof IRenderedStackProvider renderedStackProvider) {
ItemStack representativeStack = ttStack.getBaseStack().copy();
representativeStack.setCount(ttStack.stackSize);
renderedStackProvider.setRenderedStack(representativeStack);
}
return ttStack.getBaseStack().getTooltip(mc.player, mc.gameSettings.advancedItemTooltips ? TooltipFlags.ADVANCED : TooltipFlags.NORMAL);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected boolean addResult(EntityEntry ee, int index, int cachedWidth) {
return false;
}

this.addPanel(new PanelButtonStorage<>(new GuiRectangle(0, index * 16, cachedWidth, 16, 0), btnId, ee.getName(), ee));
this.addPanelToBuffer(new PanelButtonStorage<>(new GuiRectangle(0, index * 16, cachedWidth, 16, 0), btnId, ee.getName(), ee));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected boolean addResult(FluidStack stack, int index, int cachedWidth) {
int x = (index % (cachedWidth / 18)) * 18;
int y = (index / (cachedWidth / 18)) * 18;

this.addPanel(new PanelFluidSlot(new GuiRectangle(x, y, 18, 18, 0), btnId, stack));
this.addPanelToBuffer(new PanelFluidSlot(new GuiRectangle(x, y, 18, 18, 0), btnId, stack));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public boolean addResult(ItemStack stack, int index, int cachedWidth) {
int x = (index % (cachedWidth / 18)) * 18;
int y = (index / (cachedWidth / 18)) * 18;

this.addPanel(new PanelItemSlot(new GuiRectangle(x, y, 18, 18, 0), btnId, new BigItemStack(stack)).setCallback(c -> {}));
this.addPanelToBuffer(new PanelItemSlot(new GuiRectangle(x, y, 18, 18, 0), btnId, new BigItemStack(stack)).setCallback(c -> {}));

return true;
}
Expand Down
Loading
Loading