Skip to content

Commit

Permalink
add tooltip for the red question mark
Browse files Browse the repository at this point in the history
  • Loading branch information
Glease committed Nov 12, 2023
1 parent 3b1be49 commit be973c9
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 60 deletions.
2 changes: 2 additions & 0 deletions src/main/java/net/glease/tc4tweak/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.glease.tc4tweak.asm.LoadingPlugin;
import net.glease.tc4tweak.modules.hudNotif.HUDNotification;
import net.glease.tc4tweak.modules.researchBrowser.BrowserPaging;
import net.glease.tc4tweak.modules.researchBrowser.DrawResearchCompletionCounter;
import net.glease.tc4tweak.modules.researchBrowser.ThaumonomiconIndexSearcher;
import net.glease.tc4tweak.network.NetworkedConfiguration;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -154,6 +155,7 @@ public void onResourceManagerReload(IResourceManager ignored) {
public void init(FMLInitializationEvent e) {
super.init(e);
ThaumonomiconIndexSearcher.init();
DrawResearchCompletionCounter.init();
HUDNotification.init();
BrowserPaging.init();
}
Expand Down
125 changes: 125 additions & 0 deletions src/main/java/net/glease/tc4tweak/ClientUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package net.glease.tc4tweak;

import java.awt.*;
import java.util.List;

import codechicken.lib.math.MathHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

public class ClientUtils {

Expand All @@ -16,4 +27,118 @@ public static void drawRectTextured(double xmin, double xmax, double ymin, doubl
tessellator.addVertexWithUV(xmin, ymin, zLevel, umin * f, vmin * f);
tessellator.draw();
}

// region GuiDraw lifted code
// code in this region is lifted from GuiDraw.java in CodeChickenLib to ensure this mod does not depend on
// any library of any particular version, except the latest thaumcraft 4.
// this region means the code between "region GuiDraw lifted code" and the nearest "endregion" or the end of file
// Copyright (C) 2014 ChickenBones
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
// USA

/**
* Append a string in the tooltip list with TOOLTIP_LINESPACE to have a small gap between it and the next line
*/
public static final String TOOLTIP_LINESPACE = "§h";

public static Dimension displaySize() {
Minecraft mc = Minecraft.getMinecraft();
ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
return new Dimension(res.getScaledWidth(), res.getScaledHeight());
}

public static void drawMultilineTip(FontRenderer font, int x, int y, List<String> list) {
if (list.isEmpty()) return;

GL11.glDisable(GL12.GL_RESCALE_NORMAL);
GL11.glDisable(GL11.GL_DEPTH_TEST);
RenderHelper.disableStandardItemLighting();

int w = 0;
int h = -2;
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
Dimension d = new Dimension(
font.getStringWidth(s),
list.get(i).endsWith(TOOLTIP_LINESPACE) && i + 1 < list.size() ? 12 : 10);
w = Math.max(w, d.width);
h += d.height;
}

if (x < 8) x = 8;
else if (x > displaySize().width - w - 8) {
x -= 24 + w; // flip side of cursor
if (x < 8) x = 8;
}
y = (int) MathHelper.clip(y, 8, displaySize().height - 8 - h);

gui.incZLevel(300);
drawTooltipBox(x - 4, y - 4, w + 7, h + 7);
for (String s : list) {
font.drawStringWithShadow(s, x, y, -1);
y += s.endsWith(TOOLTIP_LINESPACE) ? 12 : 10;
}

gui.incZLevel(-300);

GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL12.GL_RESCALE_NORMAL);
RenderHelper.enableGUIStandardItemLighting();
}

public static void drawTooltipBox(int x, int y, int w, int h) {
int bg = 0xf0100010;
drawGradientRect(x + 1, y, w - 1, 1, bg, bg);
drawGradientRect(x + 1, y + h, w - 1, 1, bg, bg);
drawGradientRect(x + 1, y + 1, w - 1, h - 1, bg, bg); // center
drawGradientRect(x, y + 1, 1, h - 1, bg, bg);
drawGradientRect(x + w, y + 1, 1, h - 1, bg, bg);
int grad1 = 0x505000ff;
int grad2 = 0x5028007F;
drawGradientRect(x + 1, y + 2, 1, h - 3, grad1, grad2);
drawGradientRect(x + w - 1, y + 2, 1, h - 3, grad1, grad2);

drawGradientRect(x + 1, y + 1, w - 1, 1, grad1, grad1);
drawGradientRect(x + 1, y + h - 1, w - 1, 1, grad2, grad2);
}

public static void drawGradientRect(int x, int y, int w, int h, int colour1, int colour2) {
gui.drawGradientRect(x, y, x + w, y + h, colour1, colour2);
}

private static final GuiHook gui = new GuiHook();

@SuppressWarnings("unused")
public static class GuiHook extends Gui {

public void setZLevel(float f) {
zLevel = f;
}

public float getZLevel() {
return zLevel;
}

public void incZLevel(float f) {
zLevel += f;
}

@Override
public void drawGradientRect(int par1, int par2, int par3, int par4, int par5, int par6) {
super.drawGradientRect(par1, par2, par3, par4, par5, par6);
}
}
// endregion
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
package net.glease.tc4tweak.modules.researchBrowser;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import net.glease.tc4tweak.ClientUtils;
import net.glease.tc4tweak.ConfigurationHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.resources.I18n;
import org.lwjgl.opengl.GL11;
import thaumcraft.api.research.ResearchCategories;
import thaumcraft.api.research.ResearchCategoryList;
import thaumcraft.api.research.ResearchItem;
import thaumcraft.client.gui.GuiResearchBrowser;

import static thaumcraft.client.gui.GuiResearchBrowser.completedResearch;

public class DrawResearchBrowserBorders {
// I'm actually amazed to see it's not the same...
public static final int BORDER_HEIGHT = 17, BORDER_WIDTH = 16, TEXTURE_WIDTH = 256, TEXTURE_HEIGHT = 230;
private static final double BACKGROUND_ZLEVEL = -100.0;
// each int[] denotes two vector. the dot product with (coord, border size) produce the right coord for a side on the 3x3 grid
private static final int[][] PARAMS = {{0, 0, 0, 1}, {0, 1, 1, -1}, {1, -1, 1, 0}};
static int guiX, guiY;

public static void drawResearchBrowserBorders(GuiResearchBrowser gui, int x, int y, int u, int v, int width, int height) {
// enable depth test to write into depth buffer to update depth value
Expand All @@ -42,52 +31,8 @@ public static void drawResearchBrowserBorders(GuiResearchBrowser gui, int x, int
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(oldDepthFunc);

drawCompletionCounter(gui, x, y);
}

private static boolean canUnlockResearch(ResearchItem res) {
String playerName = Minecraft.getMinecraft().thePlayer.getCommandSenderName();
if(res.parents != null) {
for(String pt : res.parents) {
ResearchItem parent = ResearchCategories.getResearch(pt);
if(parent != null && !completedResearch.get(playerName).contains(parent.key)) {
return false;
}
}
}

if(res.parentsHidden != null) {
for(String pt : res.parentsHidden) {
ResearchItem parent = ResearchCategories.getResearch(pt);
if(parent != null && !completedResearch.get(playerName).contains(parent.key)) {
return false;
}
}
}
return true;
}

private static void drawCompletionCounter(GuiResearchBrowser gui, int x, int y) {
ConfigurationHandler.CompletionCounterStyle style = ConfigurationHandler.INSTANCE.getResearchCounterStyle();
if (style == ConfigurationHandler.CompletionCounterStyle.None)
return;
// draw completion text progress text
ResearchCategoryList category = ResearchCategories.getResearchList(Utils.getActiveCategory());
// filter away stuff that are auto unlocked but never shown. probably should just filter away virtual research,
// but I'm not entirely sure how that field is actually used in practice, so let's be conservative for now
Map<String, ResearchItem> all = category.research.entrySet().stream().filter(e -> !(e.getValue().isAutoUnlock() && e.getValue().isVirtual())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
int total = all.size();
ArrayList<String> completedKeys = completedResearch.get(Minecraft.getMinecraft().thePlayer.getCommandSenderName());
long completed = completedKeys.stream().filter(all::containsKey).count();
long revealed = all.entrySet().stream().filter(e -> completedKeys.contains(e.getKey()) || completedKeys.contains("@" + e.getKey()) || !(e.getValue().isLost() || e.getValue().isHidden() && !completedKeys.contains("@" + e.getValue().key) || e.getValue().isConcealed() && !canUnlockResearch(e.getValue()))).count();
String tooltip;
if (style == ConfigurationHandler.CompletionCounterStyle.Current && revealed < total) {
tooltip = I18n.format("tc4tweaks.gui.progress.partial", completed, revealed);
} else {
tooltip = I18n.format("tc4tweaks.gui.progress", completed, total);
}
FontRenderer fontRenderer = gui.mc.fontRenderer;
fontRenderer.drawString(tooltip, x + BORDER_WIDTH + 2, y + (BORDER_HEIGHT - fontRenderer.FONT_HEIGHT) / 2, 0x777777, true);
guiX = x;
guiY = y;
}

public static void drawResearchBrowserBackground(GuiResearchBrowser gui, int x, int y, int u, int v, int width, int height) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package net.glease.tc4tweak.modules.researchBrowser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.glease.tc4tweak.ClientUtils;
import net.glease.tc4tweak.ConfigurationHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import thaumcraft.api.research.ResearchCategories;
import thaumcraft.api.research.ResearchCategoryList;
import thaumcraft.api.research.ResearchItem;
import thaumcraft.client.gui.GuiResearchBrowser;

import static net.glease.tc4tweak.modules.researchBrowser.DrawResearchBrowserBorders.BORDER_HEIGHT;
import static net.glease.tc4tweak.modules.researchBrowser.DrawResearchBrowserBorders.BORDER_WIDTH;
import static thaumcraft.client.gui.GuiResearchBrowser.completedResearch;

public class DrawResearchCompletionCounter {
public static void init() {
MinecraftForge.EVENT_BUS.register(EventHandler.INSTANCE);
}

private static boolean canUnlockResearch(ResearchItem res) {
String playerName = Minecraft.getMinecraft().thePlayer.getCommandSenderName();
if (res.parents != null) {
for (String pt : res.parents) {
ResearchItem parent = ResearchCategories.getResearch(pt);
if (parent != null && !completedResearch.get(playerName).contains(parent.key)) {
return false;
}
}
}

if (res.parentsHidden != null) {
for (String pt : res.parentsHidden) {
ResearchItem parent = ResearchCategories.getResearch(pt);
if (parent != null && !completedResearch.get(playerName).contains(parent.key)) {
return false;
}
}
}
return true;
}

static void drawCompletionCounter(GuiResearchBrowser gui, int x, int y, int mx, int my) {
ConfigurationHandler.CompletionCounterStyle style = ConfigurationHandler.INSTANCE.getResearchCounterStyle();
if (style == ConfigurationHandler.CompletionCounterStyle.None)
return;
// draw completion text progress text
ResearchCategoryList category = ResearchCategories.getResearchList(Utils.getActiveCategory());
// filter away stuff that are auto unlocked but never shown. probably should just filter away virtual research,
// but I'm not entirely sure how that field is actually used in practice, so let's be conservative for now
Map<String, ResearchItem> all = category.research.entrySet().stream().filter(e -> !(e.getValue().isAutoUnlock() && e.getValue().isVirtual())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
int total = all.size();
ArrayList<String> completedKeys = completedResearch.get(Minecraft.getMinecraft().thePlayer.getCommandSenderName());
long completed = completedKeys.stream().filter(all::containsKey).count();
long revealed = all.entrySet().stream().filter(e -> completedKeys.contains(e.getKey()) || completedKeys.contains("@" + e.getKey()) || !(e.getValue().isLost() || e.getValue().isHidden() && !completedKeys.contains("@" + e.getValue().key) || e.getValue().isConcealed() && !canUnlockResearch(e.getValue()))).count();
String tooltip;
if (style == ConfigurationHandler.CompletionCounterStyle.Current && revealed < total) {
tooltip = I18n.format("tc4tweaks.gui.progress.partial", completed, revealed);
} else {
tooltip = I18n.format("tc4tweaks.gui.progress", completed, total);
}

FontRenderer fontRenderer = gui.mc.fontRenderer;
int lblX = x + BORDER_WIDTH + 2;
int lblY = y + (BORDER_HEIGHT - fontRenderer.FONT_HEIGHT) / 2;
fontRenderer.drawString(tooltip, lblX, lblY, 0x777777, true);
if (mx >= lblX && mx <= lblX + fontRenderer.getStringWidth(tooltip) && my >= lblY && my <= lblY + fontRenderer.FONT_HEIGHT) {
String hover;
if (revealed < total) {
hover = I18n.format("tc4tweaks.gui.progress.partial.tooltip");
} else {
hover = I18n.format("tc4tweaks.gui.progress.tooltip");
}
ClientUtils.drawMultilineTip(fontRenderer, mx, my, Collections.singletonList(hover));
}
}

public enum EventHandler {
INSTANCE;

@SubscribeEvent
public void onPostGuiDraw(GuiScreenEvent.DrawScreenEvent.Post e) {
if (!(e.gui instanceof GuiResearchBrowser)) return;
drawCompletionCounter((GuiResearchBrowser) e.gui, DrawResearchBrowserBorders.guiX, DrawResearchBrowserBorders.guiY, e.mouseX, e.mouseY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import thaumcraft.api.research.ResearchCategoryList;
import thaumcraft.api.research.ResearchItem;
import thaumcraft.api.research.ResearchPage;
import thaumcraft.client.gui.GuiResearchBrowser;
import thaumcraft.client.gui.GuiResearchRecipe;
import thaumcraft.client.lib.UtilsFX;
import thaumcraft.common.config.ConfigItems;
Expand Down Expand Up @@ -202,6 +201,7 @@ public void onGuiPostDraw(GuiScreenEvent.DrawScreenEvent.Post event) {
tes.draw();
}
UtilsFX.bindTexture("textures/gui/guiresearchtable2.png");
GL11.glColor3f(1,1,1);
event.gui.drawTexturedModalRect(thaumSearchField.xPosition - 2, thaumSearchField.yPosition - 4, 94, 8, thaumSearchField.width + 8, thaumSearchField.height);
event.gui.drawTexturedModalRect(thaumSearchField.xPosition - 2, thaumSearchField.yPosition + thaumSearchField.height - 4, 138, 158, thaumSearchField.width + 8, 2);
event.gui.drawTexturedModalRect(thaumSearchField.xPosition + thaumSearchField.width + 6, thaumSearchField.yPosition - 4, 244, 136, 2, thaumSearchField.height + 2);
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/tc4tweak/lang/en_US.lang
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
tc4tweaks.gui.search=Search
tc4tweaks.gui.progress=Learnt %s out of %s
tc4tweaks.gui.progress.partial=Learnt %s out of %s..§4?§r
tc4tweaks.gui.progress.tooltip=No more hidden research
tc4tweaks.gui.progress.partial.tooltip=At least 1 research is still not visible
tc4tweaks.gui.clear_notification=Clear Notification
tc4tweaks.enabled_scrolling=Scrolling aspect list with mouse wheel at research table enabled
tc4tweaks.disable_vanilla=Arcane workbench will no longer work as an vanilla crafting table
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/tc4tweak/lang/ru_RU.lang
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
tc4tweaks.gui.search=Поиск
tc4tweaks.gui.progress=Изучено %s из %s
tc4tweaks.gui.progress.partial=Изучено %s из %s..§4?§r
tc4tweaks.gui.progress.tooltip=Больше никаких скрытых исследований
tc4tweaks.gui.progress.partial.tooltip=По крайней мере 1 исследование все еще не видно
tc4tweaks.enabled_scrolling=Прокрутка списка аспектов в столе исследований колёсиком мыши включена
tc4tweaks.disable_vanilla=Магический верстак больше не будет работать как обычный верстак
tc4tweaks.config.client=Настройки клиента
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.18-beta.2
1.5.18-beta.3

0 comments on commit be973c9

Please sign in to comment.