From b1a3339b4f4d67c1d0f5764a965577ea6b1e7be0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Sat, 15 Feb 2014 00:58:31 +0100 Subject: [PATCH] improved tooltip wrapping a bit, basing it on the actual width of the rendered width (using the font renderer), and force wrapping long 'words', which allows actually wrapping Chinese text, e.g. --- src/main/java/li/cil/oc/util/Tooltip.scala | 50 +++++++++++++++------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/li/cil/oc/util/Tooltip.scala b/src/main/java/li/cil/oc/util/Tooltip.scala index 232803084..e49f1aeff 100644 --- a/src/main/java/li/cil/oc/util/Tooltip.scala +++ b/src/main/java/li/cil/oc/util/Tooltip.scala @@ -1,16 +1,20 @@ package li.cil.oc.util import li.cil.oc.Settings +import net.minecraft.client.Minecraft import net.minecraft.util.StatCollector import org.lwjgl.input.Keyboard import scala.collection.convert.WrapAsJava._ import scala.collection.mutable object Tooltip { + val maxWidth = 200 + def get(name: String, args: Any*): java.util.List[String] = { val tooltip = StatCollector.translateToLocal(Settings.namespace + "tooltip." + name).format(args.map(_.toString): _*) val isSubTooltip = name.contains(".") - val shouldShorten = (isSubTooltip || tooltip.length > 50) && + val font = Minecraft.getMinecraft.fontRenderer + val shouldShorten = (isSubTooltip || font.getStringWidth(tooltip) > maxWidth) && !Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && !Keyboard.isKeyDown(Keyboard.KEY_RSHIFT) if (shouldShorten) { @@ -22,23 +26,39 @@ object Tooltip { val lines = mutable.ArrayBuffer.empty[String] tooltip.split(nl).foreach(line => { val formatted = line.trim.stripLineEnd + var position = 0 var start = 0 - var end = 0 - var count = 0 - var formats = 0 - for (c <- formatted.trim) { + var lineEnd = 0 + var width = 0 + var lineWidth = 0 + val iterator = formatted.iterator + while (iterator.hasNext) { + val c = iterator.next() if (c == 'ยง') { - formats += 1 + iterator.next() } - else if (c == ' ') { - end = count - } - count += 1 - if (count - formats > 45 && end > 0) { - lines += formatted.substring(start, start + end) - count -= end + 1 - start += end + 1 - end = 0 + else { + if (c == ' ') { + lineEnd = position + lineWidth = width + } + else { + width += font.getCharWidth(c) + } + position += 1 + if (width > maxWidth) { + if (lineEnd > start) { + lines += formatted.substring(start, lineEnd) + start = lineEnd + 1 + width -= lineWidth + lineWidth = 0 + } + else { + lines += formatted.substring(start, position) + start = position + width = 0 + } + } } } if (start < formatted.length) {