mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-19 04:06:43 -04:00
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:
parent
05858db34e
commit
b1a3339b4f
@ -1,16 +1,20 @@
|
|||||||
package li.cil.oc.util
|
package li.cil.oc.util
|
||||||
|
|
||||||
import li.cil.oc.Settings
|
import li.cil.oc.Settings
|
||||||
|
import net.minecraft.client.Minecraft
|
||||||
import net.minecraft.util.StatCollector
|
import net.minecraft.util.StatCollector
|
||||||
import org.lwjgl.input.Keyboard
|
import org.lwjgl.input.Keyboard
|
||||||
import scala.collection.convert.WrapAsJava._
|
import scala.collection.convert.WrapAsJava._
|
||||||
import scala.collection.mutable
|
import scala.collection.mutable
|
||||||
|
|
||||||
object Tooltip {
|
object Tooltip {
|
||||||
|
val maxWidth = 200
|
||||||
|
|
||||||
def get(name: String, args: Any*): java.util.List[String] = {
|
def get(name: String, args: Any*): java.util.List[String] = {
|
||||||
val tooltip = StatCollector.translateToLocal(Settings.namespace + "tooltip." + name).format(args.map(_.toString): _*)
|
val tooltip = StatCollector.translateToLocal(Settings.namespace + "tooltip." + name).format(args.map(_.toString): _*)
|
||||||
val isSubTooltip = name.contains(".")
|
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_LSHIFT) &&
|
||||||
!Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)
|
!Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)
|
||||||
if (shouldShorten) {
|
if (shouldShorten) {
|
||||||
@ -22,23 +26,39 @@ object Tooltip {
|
|||||||
val lines = mutable.ArrayBuffer.empty[String]
|
val lines = mutable.ArrayBuffer.empty[String]
|
||||||
tooltip.split(nl).foreach(line => {
|
tooltip.split(nl).foreach(line => {
|
||||||
val formatted = line.trim.stripLineEnd
|
val formatted = line.trim.stripLineEnd
|
||||||
|
var position = 0
|
||||||
var start = 0
|
var start = 0
|
||||||
var end = 0
|
var lineEnd = 0
|
||||||
var count = 0
|
var width = 0
|
||||||
var formats = 0
|
var lineWidth = 0
|
||||||
for (c <- formatted.trim) {
|
val iterator = formatted.iterator
|
||||||
|
while (iterator.hasNext) {
|
||||||
|
val c = iterator.next()
|
||||||
if (c == '§') {
|
if (c == '§') {
|
||||||
formats += 1
|
iterator.next()
|
||||||
}
|
}
|
||||||
else if (c == ' ') {
|
else {
|
||||||
end = count
|
if (c == ' ') {
|
||||||
}
|
lineEnd = position
|
||||||
count += 1
|
lineWidth = width
|
||||||
if (count - formats > 45 && end > 0) {
|
}
|
||||||
lines += formatted.substring(start, start + end)
|
else {
|
||||||
count -= end + 1
|
width += font.getCharWidth(c)
|
||||||
start += end + 1
|
}
|
||||||
end = 0
|
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) {
|
if (start < formatted.length) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user