mirror of
https://github.com/GTNewHorizons/waila.git
synced 2026-11-21 20:52:40 +00:00
First working version of Tooltip rendering using custom renderers (for strings)
Moved some CBCore methods to DisplayUtil
This commit is contained in:
1 parent
adcc7783a8
commit
08db0ed7fd
5 files changed
+214
-28
No files matched your search
@@ -1,9 +1,10 @@
|
||||
package mcp.mobius.waila.api;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Point;
|
||||
|
||||
|
||||
public interface IWailaTooltipRenderer {
|
||||
Point getSize(IWailaDataAccessor accessor);
|
||||
void draw (IWailaDataAccessor accessor);
|
||||
Dimension getSize(IWailaDataAccessor accessor);
|
||||
void draw (IWailaDataAccessor accessor, int x, int y);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package mcp.mobius.waila.overlay;
|
||||
|
||||
import static mcp.mobius.waila.api.SpecialChars.*;
|
||||
|
||||
import java.awt.Dimension;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.ScaledResolution;
|
||||
import net.minecraft.client.renderer.OpenGlHelper;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
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) {
|
||||
if (s == null || s.equals(""))
|
||||
return 0;
|
||||
|
||||
String result = formattingCodePattern.matcher(s).replaceAll("");
|
||||
result = formattingWailaPattern.matcher(result).replaceAll("");
|
||||
return fontRenderer.getStringWidth(result);
|
||||
}
|
||||
|
||||
public static Dimension displaySize() {
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
ScaledResolution res = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);
|
||||
return new Dimension(res.getScaledWidth(), res.getScaledHeight());
|
||||
}
|
||||
|
||||
/*
|
||||
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_)
|
||||
{
|
||||
float f = (float)(p_73733_5_ >> 24 & 255) / 255.0F;
|
||||
float f1 = (float)(p_73733_5_ >> 16 & 255) / 255.0F;
|
||||
float f2 = (float)(p_73733_5_ >> 8 & 255) / 255.0F;
|
||||
float f3 = (float)(p_73733_5_ & 255) / 255.0F;
|
||||
float f4 = (float)(p_73733_6_ >> 24 & 255) / 255.0F;
|
||||
float f5 = (float)(p_73733_6_ >> 16 & 255) / 255.0F;
|
||||
float f6 = (float)(p_73733_6_ >> 8 & 255) / 255.0F;
|
||||
float f7 = (float)(p_73733_6_ & 255) / 255.0F;
|
||||
GL11.glDisable(GL11.GL_TEXTURE_2D);
|
||||
GL11.glEnable(GL11.GL_BLEND);
|
||||
GL11.glDisable(GL11.GL_ALPHA_TEST);
|
||||
OpenGlHelper.glBlendFunc(770, 771, 1, 0);
|
||||
GL11.glShadeModel(GL11.GL_SMOOTH);
|
||||
Tessellator tessellator = Tessellator.instance;
|
||||
tessellator.startDrawingQuads();
|
||||
tessellator.setColorRGBA_F(f1, f2, f3, f);
|
||||
tessellator.addVertex((double)p_73733_3_, (double)p_73733_2_, (double)this.zLevel);
|
||||
tessellator.addVertex((double)p_73733_1_, (double)p_73733_2_, (double)this.zLevel);
|
||||
tessellator.setColorRGBA_F(f5, f6, f7, f4);
|
||||
tessellator.addVertex((double)p_73733_1_, (double)p_73733_4_, (double)this.zLevel);
|
||||
tessellator.addVertex((double)p_73733_3_, (double)p_73733_4_, (double)this.zLevel);
|
||||
tessellator.draw();
|
||||
GL11.glShadeModel(GL11.GL_FLAT);
|
||||
GL11.glDisable(GL11.GL_BLEND);
|
||||
GL11.glEnable(GL11.GL_ALPHA_TEST);
|
||||
GL11.glEnable(GL11.GL_TEXTURE_2D);
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -7,9 +7,9 @@ import java.util.regex.Pattern;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Point;
|
||||
|
||||
import codechicken.lib.gui.GuiDraw;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import mcp.mobius.waila.api.IWailaDataAccessor;
|
||||
import mcp.mobius.waila.api.IWailaTooltipRenderer;
|
||||
import mcp.mobius.waila.api.impl.ConfigHandler;
|
||||
import mcp.mobius.waila.api.impl.DataAccessorBlock;
|
||||
@@ -21,7 +21,8 @@ public class Tooltip {
|
||||
public static int TabSpacing = 8;
|
||||
public static int IconSize = 8;
|
||||
|
||||
ArrayList<Line> lines = new ArrayList<Line>();
|
||||
ArrayList<Line> lines = new ArrayList<Line>();
|
||||
ArrayList<Renderable> elements = new ArrayList<Renderable>();
|
||||
int w,h,x,y,ty;
|
||||
int offsetX;
|
||||
int maxStringW;
|
||||
@@ -29,11 +30,14 @@ public class Tooltip {
|
||||
boolean hasIcon = false;
|
||||
ItemStack stack;
|
||||
|
||||
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>>();
|
||||
@@ -59,10 +63,10 @@ public class Tooltip {
|
||||
else if (s.startsWith(RENDER)){
|
||||
IWailaTooltipRenderer renderer = ModuleRegistrar.instance().getTooltipRenderer(s.substring(3));
|
||||
if (renderer != null)
|
||||
columnsWidth[i] += renderer.getSize(DataAccessorBlock.instance).x;
|
||||
columnsWidth[i] += renderer.getSize(DataAccessorBlock.instance).width;
|
||||
}
|
||||
else
|
||||
columnsWidth[i] += GuiDraw.getStringWidth(s);
|
||||
columnsWidth[i] += DisplayUtil.getStringWidth(s);
|
||||
}
|
||||
lineWidth += columnsWidth[i];
|
||||
}
|
||||
@@ -77,7 +81,7 @@ public class Tooltip {
|
||||
while (matcher.find()){
|
||||
String substring = s.substring(prevIndex, matcher.start());
|
||||
|
||||
if (!substring.equals(""))
|
||||
if (!substring.trim().equals(""))
|
||||
retList.add(substring);
|
||||
|
||||
String controlStr = s.substring(matcher.start(), matcher.end());
|
||||
@@ -98,6 +102,43 @@ public class Tooltip {
|
||||
return retList;
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/////////////////////////////////////Renderable///////////////////////////////////////
|
||||
private class Renderable implements IWailaTooltipRenderer{
|
||||
final IWailaTooltipRenderer renderer;
|
||||
final Point pos;
|
||||
|
||||
public Renderable(IWailaTooltipRenderer renderer, Point pos){
|
||||
this.renderer = renderer;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
//public void setPos(int x, int y){
|
||||
// this.pos = new Point(x, y);
|
||||
//}
|
||||
|
||||
public Point getPos(){
|
||||
return this.pos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getSize(IWailaDataAccessor accessor) {
|
||||
return this.renderer.getSize(accessor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(IWailaDataAccessor accessor, int x, int y) {
|
||||
this.renderer.draw(accessor, x + this.pos.x, y + this.pos.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return String.format("Renderable@[%d,%d] | %s", pos.x, pos.y, renderer);
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
public Tooltip(List<String> textData, ItemStack stack){
|
||||
this(textData, true);
|
||||
@@ -106,8 +147,14 @@ public class Tooltip {
|
||||
|
||||
public Tooltip(List<String> textData, boolean hasIcon){
|
||||
for (String s : textData)
|
||||
lines.add(new Line(s));
|
||||
|
||||
lines.add(new Line(s));
|
||||
this.computeColumns();
|
||||
|
||||
this.computeRenderables();
|
||||
this.computePositionAndSize(hasIcon);
|
||||
}
|
||||
|
||||
private void computeColumns(){
|
||||
for (Line l : lines)
|
||||
ncolumns = Math.max(ncolumns, l.ncolumns);
|
||||
|
||||
@@ -119,9 +166,7 @@ public class Tooltip {
|
||||
if (l.ncolumns > 1 && ncolumns != 1)
|
||||
columnsWidth[i] = Math.max(columnsWidth[i], l.columnsWidth[i]);
|
||||
}
|
||||
|
||||
columnsWidthMono = Math.max(columnsWidthMono, l.columnsWidth[0]);
|
||||
|
||||
}
|
||||
|
||||
maxStringW = 0;
|
||||
@@ -132,28 +177,67 @@ public class Tooltip {
|
||||
maxStringW = Math.max(maxStringW, columnsWidthMono);
|
||||
|
||||
for (int i = 0; i < ncolumns - 1; i++)
|
||||
columnsPos[i + 1] = columnsWidth[i] + columnsPos[i];
|
||||
columnsPos[i + 1] = columnsWidth[i] + columnsPos[i];
|
||||
}
|
||||
|
||||
private void computeRenderables(){
|
||||
int offsetY = 0;
|
||||
|
||||
for (int i = 0; i < lines.size(); i++){
|
||||
int maxHeight = 0;
|
||||
|
||||
for (int c = 0; c < lines.get(i).ncolumns; c++){
|
||||
int offsetX = 0;
|
||||
|
||||
for (int is = 0; is < lines.get(i).columns.get(c).size(); is++){
|
||||
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));
|
||||
else{
|
||||
Renderable renderable = new Renderable(new TooltipRendererString(s), new Point(offsetX + columnsPos[c] + c*TabSpacing, offsetY));
|
||||
this.elements.add(renderable);
|
||||
offsetX += renderable.getSize(accessor).width;
|
||||
maxHeight = Math.max(maxHeight, renderable.getSize(accessor).height);
|
||||
}
|
||||
}
|
||||
}
|
||||
offsetY += maxHeight;
|
||||
}
|
||||
}
|
||||
|
||||
private int getRenderableTotalHeight(){
|
||||
int result = 0;
|
||||
for (Renderable r : this.elements)
|
||||
result = Math.max(r.getPos().y + r.getSize(accessor).height, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private void computePositionAndSize(boolean hasIcon){
|
||||
this.pos = new Point(ConfigHandler.instance().getConfig(Configuration.CATEGORY_GENERAL, Constants.CFG_WAILA_POSX,0),
|
||||
ConfigHandler.instance().getConfig(Configuration.CATEGORY_GENERAL, Constants.CFG_WAILA_POSY,0));
|
||||
ConfigHandler.instance().getConfig(Configuration.CATEGORY_GENERAL, Constants.CFG_WAILA_POSY,0));
|
||||
this.hasIcon = hasIcon;
|
||||
|
||||
|
||||
int paddingW = hasIcon ? 29 : 13;
|
||||
int paddingH = hasIcon ? 24 : 0;
|
||||
offsetX = hasIcon ? 24 : 6;
|
||||
|
||||
w = maxStringW + paddingW;
|
||||
|
||||
w = maxStringW + paddingW;
|
||||
|
||||
h = Math.max(paddingH, 10 + 10*lines.size());
|
||||
|
||||
Dimension size = GuiDraw.displaySize();
|
||||
x = ((int)(size.width / OverlayConfig.scale)-w-1)*pos.x/10000;
|
||||
y = ((int)(size.height / OverlayConfig.scale)-h-1)*pos.y/10000;
|
||||
|
||||
ty = (h-10*lines.size())/2;
|
||||
h = Math.max(paddingH, this.getRenderableTotalHeight() + 10);
|
||||
|
||||
Dimension size = DisplayUtil.displaySize();
|
||||
x = ((int)(size.width / OverlayConfig.scale)-w-1)*pos.x/10000;
|
||||
y = ((int)(size.height / OverlayConfig.scale)-h-1)*pos.y/10000;
|
||||
|
||||
ty = (h - this.getRenderableTotalHeight())/2;
|
||||
}
|
||||
|
||||
public void drawStrings(){
|
||||
for (Renderable r : this.elements)
|
||||
r.draw(accessor, x + offsetX, y + ty);
|
||||
|
||||
/*
|
||||
for (int i = 0; i < lines.size(); i++){
|
||||
for (int c = 0; c < lines.get(i).ncolumns; c++){
|
||||
int offX = 0;
|
||||
@@ -171,12 +255,13 @@ public class Tooltip {
|
||||
offX += GuiDraw.getStringWidth(s);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public void drawIcons(){
|
||||
/*
|
||||
for (int i = 0; i < lines.size(); i++){
|
||||
for (int c = 0; c < lines.get(i).ncolumns; c++){
|
||||
int offX = 0;
|
||||
@@ -186,13 +271,16 @@ public class Tooltip {
|
||||
offX += IconSize;
|
||||
} else if (s.startsWith(RENDER)){
|
||||
IWailaTooltipRenderer renderer = ModuleRegistrar.instance().getTooltipRenderer(s.substring(3));
|
||||
if (renderer != null)
|
||||
renderer.draw(DataAccessorBlock.instance);
|
||||
if (renderer != null){
|
||||
renderer.draw(DataAccessorBlock.instance, x + offsetX + columnsPos[c] + c*TabSpacing + offX, y + ty + 10*i);
|
||||
offX += renderer.getSize(DataAccessorBlock.instance).getWidth();
|
||||
}
|
||||
}else{
|
||||
offX += GuiDraw.getStringWidth(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package mcp.mobius.waila.overlay;
|
||||
|
||||
import java.awt.Dimension;
|
||||
|
||||
import codechicken.lib.gui.GuiDraw;
|
||||
import mcp.mobius.waila.api.IWailaDataAccessor;
|
||||
import mcp.mobius.waila.api.IWailaTooltipRenderer;
|
||||
|
||||
public class TooltipRendererString implements IWailaTooltipRenderer{
|
||||
|
||||
final String data;
|
||||
final Dimension size;
|
||||
|
||||
public TooltipRendererString(String data){
|
||||
this.data = data;
|
||||
this.size = new Dimension(GuiDraw.getStringWidth(data), 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getSize(IWailaDataAccessor accessor) {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(IWailaDataAccessor accessor, int x, int y) {
|
||||
GuiDraw.drawString(data, x, y, OverlayConfig.fontcolor, true);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user