mirror of
https://github.com/GTNewHorizons/waila.git
synced 2026-11-26 15:03:20 +00:00
Enormous simplification of the tooltip handling code.
Away with the Lines and overly complicated code, I welcome our new regexped Renderable overlord !
This commit is contained in:
1 parent
08db0ed7fd
commit
9f6e79c467
5 files changed
+98
-112
No files matched your search
@@ -1,5 +1,10 @@
|
||||
package mcp.mobius.waila.api;
|
||||
|
||||
import static mcp.mobius.waila.api.SpecialChars.RENDER;
|
||||
import static mcp.mobius.waila.api.SpecialChars.WailaStyle;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SpecialChars {
|
||||
|
||||
public static String MCStyle = "\u00A7";
|
||||
@@ -39,4 +44,11 @@ public class SpecialChars {
|
||||
public static String EHEART = WailaStyle + WailaIcon +"c";
|
||||
public static String RENDER = WailaStyle + WailaRenderer +"a";
|
||||
|
||||
public static final Pattern patternMinecraft = Pattern.compile("(?i)" + MCStyle + "[0-9A-FK-OR]");
|
||||
public static final Pattern patternWaila = Pattern.compile("(?i)" + WailaStyle + "..");
|
||||
public static final Pattern patternRender = Pattern.compile("(?i)(" + RENDER + "\\{(?<name>.+?)\\})");
|
||||
public static final Pattern patternTab = Pattern.compile("(?i)" + TAB);
|
||||
public static final Pattern patternRight = Pattern.compile("(?i)" + ALIGNRIGHT);
|
||||
public static final Pattern patternCenter = Pattern.compile("(?i)" + ALIGNCENTER);
|
||||
public static final Pattern patternIcon = Pattern.compile("(?i)(" + WailaStyle + WailaIcon + "(?<type>[0-9A-Z]))");
|
||||
}
|
||||
@@ -77,7 +77,7 @@ public class HUDHandlerBlocks implements IWailaDataProvider {
|
||||
|
||||
@Override
|
||||
public List<String> getWailaTail(ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) {
|
||||
currenttip.add(RENDER + "{Plop}");
|
||||
currenttip.add(RENDER + "{Plip}" + RENDER + "{Plop}");
|
||||
|
||||
String modName = ModIdentification.nameFromStack(itemStack);
|
||||
if (modName != null && !modName.equals("")){
|
||||
|
||||
@@ -3,10 +3,14 @@ package mcp.mobius.waila.overlay;
|
||||
import static mcp.mobius.waila.api.SpecialChars.*;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import mcp.mobius.waila.api.IWailaTooltipRenderer;
|
||||
import mcp.mobius.waila.api.impl.DataAccessorBlock;
|
||||
import mcp.mobius.waila.api.impl.ModuleRegistrar;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.ScaledResolution;
|
||||
@@ -16,17 +20,24 @@ import net.minecraft.util.EnumChatFormatting;
|
||||
|
||||
public class DisplayUtil {
|
||||
public static FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
|
||||
private static final Pattern formattingCodePattern = Pattern.compile("(?i)" + String.valueOf('\u00a7') + "[0-9A-FK-OR]");
|
||||
private static final Pattern formattingWailaPattern = Pattern.compile("(?i)" + WailaStyle + "..");
|
||||
|
||||
|
||||
public static int getStringWidth(String s) {
|
||||
public static int getDisplayWidth(String s) {
|
||||
if (s == null || s.equals(""))
|
||||
return 0;
|
||||
|
||||
String result = formattingCodePattern.matcher(s).replaceAll("");
|
||||
result = formattingWailaPattern.matcher(result).replaceAll("");
|
||||
return fontRenderer.getStringWidth(result);
|
||||
}
|
||||
int width = 0;
|
||||
|
||||
Matcher renderMatcher = patternRender.matcher(s);
|
||||
while (renderMatcher.find()){
|
||||
IWailaTooltipRenderer renderer = ModuleRegistrar.instance().getTooltipRenderer(renderMatcher.group("name"));
|
||||
if (renderer != null)
|
||||
width += renderer.getSize(DataAccessorBlock.instance).width;
|
||||
}
|
||||
|
||||
width += fontRenderer.getStringWidth(stripSymbols(s));
|
||||
return width;
|
||||
}
|
||||
|
||||
public static Dimension displaySize() {
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
@@ -34,6 +45,19 @@ public class DisplayUtil {
|
||||
return new Dimension(res.getScaledWidth(), res.getScaledHeight());
|
||||
}
|
||||
|
||||
public static String stripSymbols(String s){
|
||||
String result = patternRender.matcher(s).replaceAll("");
|
||||
result = patternMinecraft.matcher(result).replaceAll("");
|
||||
result = patternWaila.matcher(result).replaceAll("");
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String stripWailaSymbols(String s){
|
||||
String result = patternRender.matcher(s).replaceAll("");
|
||||
result = patternWaila.matcher(result).replaceAll("");
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
protected void drawGradientRect(int p_73733_1_, int p_73733_2_, int p_73733_3_, int p_73733_4_, int p_73733_5_, int p_73733_6_)
|
||||
{
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.regex.Pattern;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Point;
|
||||
|
||||
import scala.actors.threadpool.Arrays;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import mcp.mobius.waila.api.IWailaDataAccessor;
|
||||
@@ -21,7 +22,11 @@ public class Tooltip {
|
||||
public static int TabSpacing = 8;
|
||||
public static int IconSize = 8;
|
||||
|
||||
ArrayList<Line> lines = new ArrayList<Line>();
|
||||
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
|
||||
ArrayList<ArrayList<Integer>> sizes = new ArrayList<ArrayList<Integer>>();
|
||||
ArrayList<Integer> columns = new ArrayList<Integer>();
|
||||
ArrayList<Integer> columnsPos = new ArrayList<Integer>();
|
||||
|
||||
ArrayList<Renderable> elements = new ArrayList<Renderable>();
|
||||
int w,h,x,y,ty;
|
||||
int offsetX;
|
||||
@@ -32,78 +37,6 @@ public class Tooltip {
|
||||
|
||||
IWailaDataAccessor accessor = DataAccessorBlock.instance;
|
||||
|
||||
int[] columnsWidth;
|
||||
int columnsWidthMono;
|
||||
int[] columnsPos;
|
||||
int ncolumns = 0;
|
||||
|
||||
/////////////////////////////////////Line///////////////////////////////////////
|
||||
private class Line{
|
||||
String[] columnsRaw;
|
||||
ArrayList<ArrayList<String>> columns = new ArrayList<ArrayList<String>>();
|
||||
int ncolumns;
|
||||
int[] columnsWidth;
|
||||
int lineWidth;
|
||||
|
||||
public Line(String text){
|
||||
columnsRaw = text.split(TAB);
|
||||
ncolumns = columnsRaw.length;
|
||||
columnsWidth = new int[ncolumns];
|
||||
lineWidth = 0;
|
||||
|
||||
for (String s : columnsRaw)
|
||||
columns.add(parseString(s));
|
||||
|
||||
for (int i = 0; i < ncolumns; i++){
|
||||
for (String s : columns.get(i)){
|
||||
if (s.startsWith(WailaStyle + WailaIcon))
|
||||
columnsWidth[i] += IconSize;
|
||||
else if (s.startsWith(WailaStyle + WailaStyle))
|
||||
columnsWidth[i] += 0;
|
||||
else if (s.startsWith(RENDER)){
|
||||
IWailaTooltipRenderer renderer = ModuleRegistrar.instance().getTooltipRenderer(s.substring(3));
|
||||
if (renderer != null)
|
||||
columnsWidth[i] += renderer.getSize(DataAccessorBlock.instance).width;
|
||||
}
|
||||
else
|
||||
columnsWidth[i] += DisplayUtil.getStringWidth(s);
|
||||
}
|
||||
lineWidth += columnsWidth[i];
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<String> parseString(String s){
|
||||
ArrayList<String> retList = new ArrayList<String>();
|
||||
Pattern pattern = Pattern.compile(WailaStyle + "..");
|
||||
Matcher matcher = pattern.matcher(s);
|
||||
|
||||
int prevIndex = 0;
|
||||
while (matcher.find()){
|
||||
String substring = s.substring(prevIndex, matcher.start());
|
||||
|
||||
if (!substring.trim().equals(""))
|
||||
retList.add(substring);
|
||||
|
||||
String controlStr = s.substring(matcher.start(), matcher.end());
|
||||
|
||||
// If we don't have a render type block
|
||||
if (controlStr.startsWith(RENDER)){
|
||||
int endOfBlock = s.substring(prevIndex).indexOf("}");
|
||||
String renderStr = controlStr + s.substring(matcher.end() + 1, endOfBlock);
|
||||
retList.add(renderStr);
|
||||
prevIndex += renderStr.length() + 2;
|
||||
} else {
|
||||
retList.add(controlStr);
|
||||
prevIndex = matcher.end();
|
||||
}
|
||||
}
|
||||
retList.add(s.substring(prevIndex));
|
||||
|
||||
return retList;
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/////////////////////////////////////Renderable///////////////////////////////////////
|
||||
private class Renderable implements IWailaTooltipRenderer{
|
||||
final IWailaTooltipRenderer renderer;
|
||||
@@ -146,42 +79,58 @@ public class Tooltip {
|
||||
}
|
||||
|
||||
public Tooltip(List<String> textData, boolean hasIcon){
|
||||
for (String s : textData)
|
||||
lines.add(new Line(s));
|
||||
this.computeColumns();
|
||||
|
||||
columns.add(0); // Small init of the arrays to have at least one element
|
||||
columnsPos.add(0);
|
||||
|
||||
for (String s : textData){
|
||||
ArrayList<String> line = new ArrayList(Arrays.asList(patternTab.split(s)));
|
||||
ArrayList<Integer> size = new ArrayList<Integer>();
|
||||
for (String ss : line)
|
||||
size.add(DisplayUtil.getDisplayWidth(ss));
|
||||
|
||||
if (line.size() > 1){
|
||||
while (columns.size() < line.size()){
|
||||
columns.add(0);
|
||||
columnsPos.add(0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < line.size(); i++)
|
||||
columns.set(i, Math.max(columns.get(i), size.get(i)));
|
||||
}
|
||||
|
||||
maxStringW = Math.max(maxStringW, DisplayUtil.getDisplayWidth(s) + TabSpacing * (line.size() - 1));
|
||||
|
||||
lines.add(line);
|
||||
sizes.add(size);
|
||||
}
|
||||
|
||||
// We compute the position of the columns to be able to align the renderable later on
|
||||
for (int i = 1; i < columns.size(); i++)
|
||||
columnsPos.set(i, columns.get(i - 1) + columnsPos.get(i - 1) + TabSpacing);
|
||||
|
||||
this.computeRenderables();
|
||||
this.computePositionAndSize(hasIcon);
|
||||
}
|
||||
|
||||
private void computeColumns(){
|
||||
for (Line l : lines)
|
||||
ncolumns = Math.max(ncolumns, l.ncolumns);
|
||||
|
||||
columnsWidth = new int[ncolumns];
|
||||
columnsPos = new int[ncolumns];
|
||||
|
||||
for (Line l : lines){
|
||||
for (int i = 0; i < l.ncolumns; i++){
|
||||
if (l.ncolumns > 1 && ncolumns != 1)
|
||||
columnsWidth[i] = Math.max(columnsWidth[i], l.columnsWidth[i]);
|
||||
}
|
||||
columnsWidthMono = Math.max(columnsWidthMono, l.columnsWidth[0]);
|
||||
}
|
||||
|
||||
maxStringW = 0;
|
||||
for (int i = 0; i < ncolumns; i++)
|
||||
maxStringW += columnsWidth[i];
|
||||
maxStringW += ncolumns * TabSpacing;
|
||||
|
||||
maxStringW = Math.max(maxStringW, columnsWidthMono);
|
||||
|
||||
for (int i = 0; i < ncolumns - 1; i++)
|
||||
columnsPos[i + 1] = columnsWidth[i] + columnsPos[i];
|
||||
}
|
||||
|
||||
private void computeRenderables(){
|
||||
int offsetY = 0;
|
||||
for (int i = 0; i < lines.size(); i++){ // We check all the lines, one by one
|
||||
int maxHeight = 0; // Maximum height of this line
|
||||
for (int c = 0; c < lines.get(i).size(); c++){ // We check all the columns for this line
|
||||
String cs = lines.get(i).get(c);
|
||||
offsetX = columnsPos.get(c);
|
||||
|
||||
Renderable renderable = new Renderable(new TooltipRendererString(DisplayUtil.stripWailaSymbols(cs)), new Point(offsetX, offsetY));
|
||||
this.elements.add(renderable);
|
||||
offsetX += renderable.getSize(accessor).width;
|
||||
maxHeight = Math.max(maxHeight, renderable.getSize(accessor).height);
|
||||
}
|
||||
offsetY += maxHeight;
|
||||
}
|
||||
|
||||
/*
|
||||
int offsetY = 0;
|
||||
|
||||
for (int i = 0; i < lines.size(); i++){
|
||||
int maxHeight = 0;
|
||||
@@ -193,7 +142,7 @@ public class Tooltip {
|
||||
String s = lines.get(i).columns.get(c).get(is);
|
||||
|
||||
if (s.startsWith(ALIGNRIGHT))
|
||||
offsetX += columnsWidth[c] - DisplayUtil.getStringWidth(lines.get(i).columns.get(c).get(is + 1));
|
||||
offsetX += columnsWidth[c] - DisplayUtil.getDisplayWidth(lines.get(i).columns.get(c).get(is + 1));
|
||||
else{
|
||||
Renderable renderable = new Renderable(new TooltipRendererString(s), new Point(offsetX + columnsPos[c] + c*TabSpacing, offsetY));
|
||||
this.elements.add(renderable);
|
||||
@@ -204,6 +153,7 @@ public class Tooltip {
|
||||
}
|
||||
offsetY += maxHeight;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
private int getRenderableTotalHeight(){
|
||||
|
||||
@@ -13,7 +13,7 @@ public class TooltipRendererString implements IWailaTooltipRenderer{
|
||||
|
||||
public TooltipRendererString(String data){
|
||||
this.data = data;
|
||||
this.size = new Dimension(GuiDraw.getStringWidth(data), 10);
|
||||
this.size = new Dimension(DisplayUtil.getDisplayWidth(data), data.equals("") ? 0 : 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user