Skip to content

Commit

Permalink
Use Forge tooltips where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Feb 10, 2017
1 parent 8654b9d commit e851fb5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 71 deletions.
21 changes: 21 additions & 0 deletions src/main/java/binnie/craftgui/core/Tooltip.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ public class Tooltip {
List<String> tooltip;
ITooltipType type;
public int maxWidth;
private ItemStack itemStack = ItemStack.EMPTY;

public Tooltip() {
this.tooltip = new ArrayList<>();
this.type = Type.Standard;
this.maxWidth = 256;
}

/**
* Sets the itemStack seen by tooltip event handlers.
*/
public void setItemStack(ItemStack itemStack) {
this.itemStack = itemStack;
}

/**
* Gets the itemStack seen by tooltip event handlers.
*/
public ItemStack getItemStack() {
return itemStack;
}

public void add(final String string) {
this.tooltip.add(string);
}
Expand Down Expand Up @@ -53,13 +68,19 @@ public ITooltipType getType() {
return this.type;
}

/**
* Add a tooltip that also displays an itemStack on the tooltip directly.
*/
public void add(final ItemStack item, final String string) {
final NBTTagCompound nbt = new NBTTagCompound();
item.writeToNBT(nbt);
nbt.setByte("nbt-type", (byte) 105);
this.add("~~~" + nbt.toString() + "~~~" + string);
}

/**
* Add a tooltip that also displays an fluidStack on the tooltip directly.
*/
public void add(final FluidStack item, final String string) {
final NBTTagCompound nbt = new NBTTagCompound();
item.writeToNBT(nbt);
Expand Down
133 changes: 72 additions & 61 deletions src/main/java/binnie/craftgui/minecraft/GuiCraftGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import net.minecraft.nbt.JsonToNBT;
import net.minecraft.nbt.NBTException;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.client.config.GuiUtils;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -115,10 +115,9 @@ public void renderTooltip(final IPoint mousePosition, final MinecraftTooltip too
final int mouseX = mousePosition.x();
final int mouseY = mousePosition.y();
final FontRenderer font = this.getFontRenderer();
GlStateManager.disableRescaleNormal();
RenderHelper.disableStandardItemLighting();
GlStateManager.disableLighting();
GlStateManager.disableDepth();

boolean containsItemRender = false;

int k = 0;
final List<String> strings = new ArrayList<>();
for (final String string : tooltip.getList()) {
Expand All @@ -127,6 +126,7 @@ public void renderTooltip(final IPoint mousePosition, final MinecraftTooltip too
strings.addAll(font.listFormattedStringToWidth(string, tooltip.maxWidth));
} else {
strings.add(string);
containsItemRender = true;
}
}
}
Expand All @@ -139,66 +139,77 @@ public void renderTooltip(final IPoint mousePosition, final MinecraftTooltip too
k = l;
}
}
int i1 = mouseX + 12;
int j1 = mouseY - 12;
int k2 = 8;
if (strings.size() > 1) {
k2 += 2 + (strings.size() - 1) * 10;
}
if (i1 + k > this.width) {
i1 -= 28 + k;
}
if (j1 + k2 + 6 > this.height) {
j1 = this.height - k2 - 6;
}
this.zLevel = 300.0f;
//GuiScreen.itemRender.zLevel = 300.0f;
final int l2 = -267386864;
final int j2;
final int i2 = j2 = 1342177280 + MinecraftTooltip.getOutline(tooltip.getType());
this.drawGradientRect(i1 - 3, j1 - 4, i1 + k + 3, j1 - 3, l2, l2);
this.drawGradientRect(i1 - 3, j1 + k2 + 3, i1 + k + 3, j1 + k2 + 4, l2, l2);
this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 + k2 + 3, l2, l2);
this.drawGradientRect(i1 - 4, j1 - 3, i1 - 3, j1 + k2 + 3, l2, l2);
this.drawGradientRect(i1 + k + 3, j1 - 3, i1 + k + 4, j1 + k2 + 3, l2, l2);
this.drawGradientRect(i1 - 3, j1 - 3 + 1, i1 - 3 + 1, j1 + k2 + 3 - 1, i2, j2);
this.drawGradientRect(i1 + k + 2, j1 - 3 + 1, i1 + k + 3, j1 + k2 + 3 - 1, i2, j2);
this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 - 3 + 1, i2, i2);
this.drawGradientRect(i1 - 3, j1 + k2 + 2, i1 + k + 3, j1 + k2 + 3, j2, j2);
for (int k3 = 0; k3 < strings.size(); ++k3) {
String s2 = strings.get(k3);
if (k3 == 0) {
s2 = MinecraftTooltip.getTitle(tooltip.getType()) + s2;
} else {
s2 = MinecraftTooltip.getBody(tooltip.getType()) + s2;

if (!containsItemRender) {
ItemStack itemStack = tooltip.getItemStack();
GuiUtils.drawHoveringText(itemStack, strings, mouseX, mouseY, width, height, tooltip.maxWidth, font);
} else {
GlStateManager.disableRescaleNormal();
RenderHelper.disableStandardItemLighting();
GlStateManager.disableLighting();
GlStateManager.disableDepth();

int i1 = mouseX + 12;
int j1 = mouseY - 12;
int k2 = 8;
if (strings.size() > 1) {
k2 += 2 + (strings.size() - 1) * 10;
}
if (s2.contains("~~~")) {
final String split = s2.split("~~~")[1];
try {
final NBTTagCompound nbt = JsonToNBT.getTagFromJson(split);
final ItemStack stack = new ItemStack(nbt);
GlStateManager.pushMatrix();
GlStateManager.translate(i1, j1 - 1.5f, 0.0f);
GlStateManager.scale(0.6f, 0.6f, 1.0f);
RenderUtil.drawItem(IPoint.ZERO, stack, false);
GlStateManager.popMatrix();
} catch (NBTException e) {
e.printStackTrace();
}
s2 = " " + s2.replaceAll("~~~(.*?)~~~", "");
if (i1 + k > this.width) {
i1 -= 28 + k;
}
font.drawStringWithShadow(s2, i1, j1, -1);
if (k3 == 0) {
j1 += 2;
if (j1 + k2 + 6 > this.height) {
j1 = this.height - k2 - 6;
}
j1 += 10;
this.zLevel = 300.0f;
//GuiScreen.itemRender.zLevel = 300.0f;
final int l2 = -267386864;
final int j2;
final int i2 = j2 = 1342177280 + MinecraftTooltip.getOutline(tooltip.getType());
this.drawGradientRect(i1 - 3, j1 - 4, i1 + k + 3, j1 - 3, l2, l2);
this.drawGradientRect(i1 - 3, j1 + k2 + 3, i1 + k + 3, j1 + k2 + 4, l2, l2);
this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 + k2 + 3, l2, l2);
this.drawGradientRect(i1 - 4, j1 - 3, i1 - 3, j1 + k2 + 3, l2, l2);
this.drawGradientRect(i1 + k + 3, j1 - 3, i1 + k + 4, j1 + k2 + 3, l2, l2);
this.drawGradientRect(i1 - 3, j1 - 3 + 1, i1 - 3 + 1, j1 + k2 + 3 - 1, i2, j2);
this.drawGradientRect(i1 + k + 2, j1 - 3 + 1, i1 + k + 3, j1 + k2 + 3 - 1, i2, j2);
this.drawGradientRect(i1 - 3, j1 - 3, i1 + k + 3, j1 - 3 + 1, i2, i2);
this.drawGradientRect(i1 - 3, j1 + k2 + 2, i1 + k + 3, j1 + k2 + 3, j2, j2);
for (int k3 = 0; k3 < strings.size(); ++k3) {
String s2 = strings.get(k3);
if (k3 == 0) {
s2 = MinecraftTooltip.getTitle(tooltip.getType()) + s2;
} else {
s2 = MinecraftTooltip.getBody(tooltip.getType()) + s2;
}
if (s2.contains("~~~")) {
final String split = s2.split("~~~")[1];
try {
final NBTTagCompound nbt = JsonToNBT.getTagFromJson(split);
final ItemStack stack = new ItemStack(nbt);
GlStateManager.pushMatrix();
GlStateManager.translate(i1, j1 - 1.5f, 0.0f);
GlStateManager.scale(0.6f, 0.6f, 1.0f);
RenderUtil.drawItem(IPoint.ZERO, stack, false);
GlStateManager.popMatrix();
} catch (NBTException e) {
e.printStackTrace();
}
s2 = " " + s2.replaceAll("~~~(.*?)~~~", "");
}
font.drawStringWithShadow(s2, i1, j1, -1);
if (k3 == 0) {
j1 += 2;
}
j1 += 10;
}
this.zLevel = 0.0f;
//GuiScreen.itemRender.zLevel = 0.0f;
GlStateManager.enableLighting();
GlStateManager.enableDepth();
RenderHelper.enableStandardItemLighting();
GlStateManager.enableRescaleNormal();
}
this.zLevel = 0.0f;
//GuiScreen.itemRender.zLevel = 0.0f;
GlStateManager.enableLighting();
GlStateManager.enableDepth();
RenderHelper.enableStandardItemLighting();
GlStateManager.enableRescaleNormal();
}

@Override
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/binnie/craftgui/minecraft/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ public void getTooltip(final Tooltip tooltip) {
final Deque<IWidget> queue = this.calculateMousedOverWidgets();
while (!queue.isEmpty()) {
final IWidget widget = queue.removeFirst();
if (widget.isEnabled() && widget.isVisible()) {
if (!widget.calculateIsMouseOver()) {
continue;
}
if (widget.isEnabled() && widget.isVisible() && widget.calculateIsMouseOver()) {
if (widget instanceof ITooltip) {
((ITooltip) widget).getTooltip(tooltip);
if (tooltip.exists()) {
Expand All @@ -126,7 +123,6 @@ public void getTooltip(final Tooltip tooltip) {
if (widget.hasAttribute(Attribute.BlockTooltip)) {
return;
}
continue;
}
}
}
Expand All @@ -135,10 +131,7 @@ public void getHelpTooltip(final MinecraftTooltip tooltip) {
final Deque<IWidget> queue = this.calculateMousedOverWidgets();
while (!queue.isEmpty()) {
final IWidget widget = queue.removeFirst();
if (widget.isEnabled() && widget.isVisible()) {
if (!widget.calculateIsMouseOver()) {
continue;
}
if (widget.isEnabled() && widget.isVisible() && widget.calculateIsMouseOver()) {
if (widget instanceof ITooltipHelp) {
((ITooltipHelp) widget).getHelpTooltip(tooltip);
if (tooltip.exists()) {
Expand All @@ -148,7 +141,6 @@ public void getHelpTooltip(final MinecraftTooltip tooltip) {
if (widget.hasAttribute(Attribute.BlockTooltip)) {
return;
}
continue;
}
}
}
Expand Down

0 comments on commit e851fb5

Please sign in to comment.