mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-22 13:09:42 +00:00
129 lines
3.3 KiB
Java
129 lines
3.3 KiB
Java
/**
|
|
* This class was created by <Vazkii>. It's distributed as
|
|
* part of the Botania Mod. Get the Source Code in github:
|
|
* https://github.com/Vazkii/Botania
|
|
*
|
|
* Botania is Open Source and distributed under the
|
|
* Botania License: http://botaniamod.net/license.php
|
|
*
|
|
* File Created @ [Jan 14, 2014, 6:45:33 PM (GMT)]
|
|
*/
|
|
package vazkii.botania.common.lexicon.page;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.client.gui.FontRenderer;
|
|
import net.minecraft.util.StatCollector;
|
|
import vazkii.botania.api.internal.IGuiLexiconEntry;
|
|
import vazkii.botania.api.lexicon.LexiconPage;
|
|
import vazkii.botania.common.core.handler.ConfigHandler;
|
|
|
|
import com.google.common.base.Joiner;
|
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
|
|
|
public class PageText extends LexiconPage {
|
|
|
|
public PageText(String unlocalizedName) {
|
|
super(unlocalizedName);
|
|
}
|
|
|
|
@Override
|
|
@SideOnly(Side.CLIENT)
|
|
public void renderScreen(IGuiLexiconEntry gui, int mx, int my) {
|
|
int width = gui.getWidth() - 30;
|
|
int x = gui.getLeft() + 16;
|
|
int y = gui.getTop() + 2;
|
|
|
|
renderText(x, y, width, gui.getHeight(), getUnlocalizedName());
|
|
}
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
public static void renderText(int x, int y, int width, int height, String unlocalizedText) {
|
|
x += 2;
|
|
y += 10;
|
|
width -= 4;
|
|
|
|
FontRenderer font = Minecraft.getMinecraft().fontRenderer;
|
|
boolean unicode = font.getUnicodeFlag();
|
|
font.setUnicodeFlag(true);
|
|
String text = StatCollector.translateToLocal(unlocalizedText).replaceAll("&", "\u00a7");
|
|
String[] textEntries = text.split("<br>");
|
|
|
|
List<List<String>> lines = new ArrayList();
|
|
|
|
String controlCodes = "";
|
|
for(String s : textEntries) {
|
|
List<String> words = new ArrayList();
|
|
String lineStr = "";
|
|
String[] tokens = s.split(" ");
|
|
for(String token : tokens) {
|
|
String prev = lineStr;
|
|
String spaced = token + " ";
|
|
lineStr += spaced;
|
|
|
|
controlCodes = toControlCodes(getControlCodes(prev));
|
|
if(font.getStringWidth(lineStr) > width) {
|
|
lines.add(words);
|
|
lineStr = controlCodes + spaced;
|
|
words = new ArrayList();
|
|
}
|
|
|
|
words.add(controlCodes + token);
|
|
}
|
|
|
|
if(!lineStr.isEmpty())
|
|
lines.add(words);
|
|
lines.add(new ArrayList());
|
|
}
|
|
|
|
int i = 0;
|
|
for(List<String> words : lines) {
|
|
words.size();
|
|
int xi = x;
|
|
int spacing = 4;
|
|
int wcount = words.size();
|
|
int compensationSpaces = 0;
|
|
boolean justify = ConfigHandler.lexiconJustifiedText && wcount > 0 && lines.size() > i && !lines.get(i + 1).isEmpty();
|
|
|
|
if(justify) {
|
|
String s = Joiner.on("").join(words);
|
|
int swidth = font.getStringWidth(s);
|
|
int space = width - swidth;
|
|
|
|
spacing = wcount == 1 ? 0 : space / (wcount - 1);
|
|
compensationSpaces = wcount == 1 ? 0 : space % (wcount - 1);
|
|
}
|
|
|
|
for(String s : words) {
|
|
int extra = 0;
|
|
if(compensationSpaces > 0) {
|
|
compensationSpaces--;
|
|
extra++;
|
|
}
|
|
font.drawString(s, xi, y, 0);
|
|
xi += font.getStringWidth(s) + spacing + extra;
|
|
}
|
|
|
|
y += 10;
|
|
i++;
|
|
}
|
|
|
|
font.setUnicodeFlag(unicode);
|
|
}
|
|
|
|
public static String getControlCodes(String s) {
|
|
String controls = s.replaceAll("(?<!\u00a7)(.)", "");
|
|
String wiped = controls.replaceAll(".*r", "r");
|
|
return wiped;
|
|
}
|
|
|
|
public static String toControlCodes(String s) {
|
|
return s.replaceAll(".", "\u00a7$0");
|
|
}
|
|
|
|
}
|