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.

This commit is contained in:
Florian Nücke 2014-02-15 00:58:31 +01:00
parent 05858db34e
commit b1a3339b4f

View File

@ -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) {