mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-15 10:21:45 -04:00
Rendering multiline list elements indented (checking if line starts with -
or *
).
This commit is contained in:
parent
0f1946f4c4
commit
e8f4e79d04
@ -35,6 +35,9 @@ isn't*.
|
|||||||
|
|
||||||
# not a header
|
# not a header
|
||||||
|
|
||||||
|
* this is a list item and the text that will be wrapped will be indented appropriately
|
||||||
|
- this should also `work for code rendered text, if it doesn't i` will be a sad person
|
||||||
|
|
||||||
asdasd  qweqwe
|
asdasd  qweqwe
|
||||||
|
|
||||||
And finally, [this is a link!](https://avatars1.githubusercontent.com/u/514903).
|
And finally, [this is a link!](https://avatars1.githubusercontent.com/u/514903).
|
||||||
@ -46,3 +49,6 @@ And finally, [this is a link!](https://avatars1.githubusercontent.com/u/514903).
|
|||||||
wrap testing
|
wrap testing
|
||||||
12345678901234567890.1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
|
12345678901234567890.1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||||
`123456789012345678901234567890.12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890`
|
`123456789012345678901234567890.12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890`
|
||||||
|
|
||||||
|
* 12345678901234567890.1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||||
|
- `123456789012345678901234567890.12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890`
|
@ -6,16 +6,19 @@ import net.minecraft.client.gui.FontRenderer
|
|||||||
import org.lwjgl.opengl.GL11
|
import org.lwjgl.opengl.GL11
|
||||||
|
|
||||||
private[markdown] class CodeSegment(protected val parent: Segment, val text: String) extends Segment {
|
private[markdown] class CodeSegment(protected val parent: Segment, val text: String) extends Segment {
|
||||||
final val breaks = Set(' ', '.', ',', ':', ';', '!', '?', '_', '=', '-', '+', '*', '/', '\\', ')', '\'', '"')
|
private final val breaks = Set(' ', '.', ',', ':', ';', '!', '?', '_', '=', '-', '+', '*', '/', '\\', ')', '\'', '"')
|
||||||
|
private final val lists = Set("- ", "* ")
|
||||||
|
private lazy val rootPrefix = root.asInstanceOf[TextSegment].text.take(2)
|
||||||
|
|
||||||
override def height(indent: Int, maxWidth: Int, renderer: FontRenderer): Int = {
|
override def height(indent: Int, maxWidth: Int, renderer: FontRenderer): Int = {
|
||||||
var lines = 0
|
var lines = 0
|
||||||
var chars = text
|
var chars = text
|
||||||
var lineChars = maxChars(chars, maxWidth - indent, maxWidth)
|
val wrapIndent = computeWrapIndent(renderer)
|
||||||
while (chars.length > lineChars) {
|
var numChars = maxChars(chars, maxWidth - indent, maxWidth - wrapIndent)
|
||||||
|
while (chars.length > numChars) {
|
||||||
lines += 1
|
lines += 1
|
||||||
chars = chars.drop(lineChars).dropWhile(_.isWhitespace)
|
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
||||||
lineChars = maxChars(chars, maxWidth, maxWidth)
|
numChars = maxChars(chars, maxWidth - wrapIndent, maxWidth - wrapIndent)
|
||||||
}
|
}
|
||||||
lines * Document.lineHeight(renderer)
|
lines * Document.lineHeight(renderer)
|
||||||
}
|
}
|
||||||
@ -23,11 +26,12 @@ private[markdown] class CodeSegment(protected val parent: Segment, val text: Str
|
|||||||
override def width(indent: Int, maxWidth: Int, renderer: FontRenderer): Int = {
|
override def width(indent: Int, maxWidth: Int, renderer: FontRenderer): Int = {
|
||||||
var currentX = indent
|
var currentX = indent
|
||||||
var chars = text
|
var chars = text
|
||||||
var lineChars = maxChars(chars, maxWidth - indent, maxWidth)
|
val wrapIndent = computeWrapIndent(renderer)
|
||||||
while (chars.length > lineChars) {
|
var numChars = maxChars(chars, maxWidth - indent, maxWidth - wrapIndent)
|
||||||
chars = chars.drop(lineChars).dropWhile(_.isWhitespace)
|
while (chars.length > numChars) {
|
||||||
lineChars = maxChars(chars, maxWidth, maxWidth)
|
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
||||||
currentX = 1
|
numChars = maxChars(chars, maxWidth - wrapIndent, maxWidth - wrapIndent)
|
||||||
|
currentX = wrapIndent + 1
|
||||||
}
|
}
|
||||||
currentX + stringWidth(chars)
|
currentX + stringWidth(chars)
|
||||||
}
|
}
|
||||||
@ -38,15 +42,16 @@ private[markdown] class CodeSegment(protected val parent: Segment, val text: Str
|
|||||||
var currentX = x + indent
|
var currentX = x + indent
|
||||||
var currentY = y
|
var currentY = y
|
||||||
var chars = text
|
var chars = text
|
||||||
var numChars = maxChars(chars, maxWidth - indent, maxWidth)
|
val wrapIndent = computeWrapIndent(renderer)
|
||||||
|
var numChars = maxChars(chars, maxWidth - indent, maxWidth - wrapIndent)
|
||||||
while (chars.length > 0 && (currentY - y) < maxY) {
|
while (chars.length > 0 && (currentY - y) < maxY) {
|
||||||
val part = chars.take(numChars)
|
val part = chars.take(numChars)
|
||||||
GL11.glColor4f(0.75f, 0.8f, 1, 1)
|
GL11.glColor4f(0.75f, 0.8f, 1, 1)
|
||||||
TextBufferRenderCache.renderer.drawString(part, currentX, currentY)
|
TextBufferRenderCache.renderer.drawString(part, currentX, currentY)
|
||||||
currentX = x
|
currentX = x + wrapIndent
|
||||||
currentY += Document.lineHeight(renderer)
|
currentY += Document.lineHeight(renderer)
|
||||||
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
||||||
numChars = maxChars(chars, maxWidth, maxWidth)
|
numChars = maxChars(chars, maxWidth - wrapIndent, maxWidth - wrapIndent)
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
@ -69,5 +74,7 @@ private[markdown] class CodeSegment(protected val parent: Segment, val text: Str
|
|||||||
pos
|
pos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private def computeWrapIndent(renderer: FontRenderer) = if (lists.contains(rootPrefix)) renderer.getStringWidth(rootPrefix) else 0
|
||||||
|
|
||||||
override def toString: String = s"{CodeSegment: text = $text}"
|
override def toString: String = s"{CodeSegment: text = $text}"
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package li.cil.oc.client.renderer.markdown.segment
|
|||||||
|
|
||||||
import net.minecraft.client.gui.FontRenderer
|
import net.minecraft.client.gui.FontRenderer
|
||||||
|
|
||||||
|
import scala.annotation.tailrec
|
||||||
import scala.util.matching.Regex
|
import scala.util.matching.Regex
|
||||||
|
|
||||||
trait Segment {
|
trait Segment {
|
||||||
@ -26,6 +27,8 @@ trait Segment {
|
|||||||
// Used when rendering, to compute the style of a nested segment.
|
// Used when rendering, to compute the style of a nested segment.
|
||||||
protected def parent: Segment
|
protected def parent: Segment
|
||||||
|
|
||||||
|
@tailrec protected final def root: Segment = if (parent == null) this else parent.root
|
||||||
|
|
||||||
// Used during construction, checks a segment for inner segments.
|
// Used during construction, checks a segment for inner segments.
|
||||||
private[markdown] def refine(pattern: Regex, factory: (Segment, Regex.Match) => Segment): Iterable[Segment] = Iterable(this)
|
private[markdown] def refine(pattern: Regex, factory: (Segment, Regex.Match) => Segment): Iterable[Segment] = Iterable(this)
|
||||||
|
|
||||||
|
@ -9,7 +9,9 @@ import scala.collection.mutable
|
|||||||
import scala.util.matching.Regex
|
import scala.util.matching.Regex
|
||||||
|
|
||||||
private[markdown] class TextSegment(protected val parent: Segment, val text: String) extends Segment {
|
private[markdown] class TextSegment(protected val parent: Segment, val text: String) extends Segment {
|
||||||
final val breaks = Set(' ', '.', ',', ':', ';', '!', '?', '_', '=', '-', '+', '*', '/', '\\', ')', '\'', '"')
|
private final val breaks = Set(' ', '.', ',', ':', ';', '!', '?', '_', '=', '-', '+', '*', '/', '\\', ')', '\'', '"')
|
||||||
|
private final val lists = Set("- ", "* ")
|
||||||
|
private lazy val rootPrefix = root.asInstanceOf[TextSegment].text.take(2)
|
||||||
|
|
||||||
override def refine(pattern: Regex, factory: (Segment, Regex.Match) => Segment): Iterable[Segment] = {
|
override def refine(pattern: Regex, factory: (Segment, Regex.Match) => Segment): Iterable[Segment] = {
|
||||||
val result = mutable.Buffer.empty[Segment]
|
val result = mutable.Buffer.empty[Segment]
|
||||||
@ -41,11 +43,12 @@ private[markdown] class TextSegment(protected val parent: Segment, val text: Str
|
|||||||
var lines = 0
|
var lines = 0
|
||||||
var chars = text
|
var chars = text
|
||||||
if (indent == 0) chars = chars.dropWhile(_.isWhitespace)
|
if (indent == 0) chars = chars.dropWhile(_.isWhitespace)
|
||||||
var lineChars = maxChars(chars, maxWidth - indent, maxWidth, renderer)
|
val wrapIndent = computeWrapIndent(renderer)
|
||||||
while (chars.length > lineChars) {
|
var numChars = maxChars(chars, maxWidth - indent, maxWidth - wrapIndent, renderer)
|
||||||
|
while (chars.length > numChars) {
|
||||||
lines += 1
|
lines += 1
|
||||||
chars = chars.drop(lineChars).dropWhile(_.isWhitespace)
|
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
||||||
lineChars = maxChars(chars, maxWidth, maxWidth, renderer)
|
numChars = maxChars(chars, maxWidth - wrapIndent, maxWidth - wrapIndent, renderer)
|
||||||
}
|
}
|
||||||
(lines * Document.lineHeight(renderer) * resolvedScale).toInt
|
(lines * Document.lineHeight(renderer) * resolvedScale).toInt
|
||||||
}
|
}
|
||||||
@ -54,11 +57,12 @@ private[markdown] class TextSegment(protected val parent: Segment, val text: Str
|
|||||||
var currentX = indent
|
var currentX = indent
|
||||||
var chars = text
|
var chars = text
|
||||||
if (indent == 0) chars = chars.dropWhile(_.isWhitespace)
|
if (indent == 0) chars = chars.dropWhile(_.isWhitespace)
|
||||||
var lineChars = maxChars(chars, maxWidth - indent, maxWidth, renderer)
|
val wrapIndent = computeWrapIndent(renderer)
|
||||||
while (chars.length > lineChars) {
|
var numChars = maxChars(chars, maxWidth - indent, maxWidth - wrapIndent, renderer)
|
||||||
chars = chars.drop(lineChars).dropWhile(_.isWhitespace)
|
while (chars.length > numChars) {
|
||||||
lineChars = maxChars(chars, maxWidth, maxWidth, renderer)
|
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
||||||
currentX = 0
|
numChars = maxChars(chars, maxWidth - wrapIndent, maxWidth - wrapIndent, renderer)
|
||||||
|
currentX = wrapIndent
|
||||||
}
|
}
|
||||||
currentX + (stringWidth(chars, renderer) * resolvedScale).toInt
|
currentX + (stringWidth(chars, renderer) * resolvedScale).toInt
|
||||||
}
|
}
|
||||||
@ -69,7 +73,8 @@ private[markdown] class TextSegment(protected val parent: Segment, val text: Str
|
|||||||
var currentY = y
|
var currentY = y
|
||||||
var chars = text
|
var chars = text
|
||||||
if (indent == 0) chars = chars.dropWhile(_.isWhitespace)
|
if (indent == 0) chars = chars.dropWhile(_.isWhitespace)
|
||||||
var numChars = maxChars(chars, maxWidth - indent, maxWidth, renderer)
|
val wrapIndent = computeWrapIndent(renderer)
|
||||||
|
var numChars = maxChars(chars, maxWidth - indent, maxWidth - wrapIndent, renderer)
|
||||||
val interactive = findInteractive()
|
val interactive = findInteractive()
|
||||||
var hovered: Option[InteractiveSegment] = None
|
var hovered: Option[InteractiveSegment] = None
|
||||||
while (chars.length > 0 && (currentY - y) < maxY) {
|
while (chars.length > 0 && (currentY - y) < maxY) {
|
||||||
@ -81,10 +86,10 @@ private[markdown] class TextSegment(protected val parent: Segment, val text: Str
|
|||||||
GL11.glTranslatef(-currentX, -currentY, 0)
|
GL11.glTranslatef(-currentX, -currentY, 0)
|
||||||
renderer.drawString(resolvedFormat + part, currentX, currentY, resolvedColor)
|
renderer.drawString(resolvedFormat + part, currentX, currentY, resolvedColor)
|
||||||
GL11.glPopMatrix()
|
GL11.glPopMatrix()
|
||||||
currentX = x
|
currentX = x + wrapIndent
|
||||||
currentY += (Document.lineHeight(renderer) * fontScale).toInt
|
currentY += (Document.lineHeight(renderer) * fontScale).toInt
|
||||||
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
|
||||||
numChars = maxChars(chars, maxWidth, maxWidth, renderer)
|
numChars = maxChars(chars, maxWidth - wrapIndent, maxWidth - wrapIndent, renderer)
|
||||||
}
|
}
|
||||||
|
|
||||||
hovered
|
hovered
|
||||||
@ -137,5 +142,7 @@ private[markdown] class TextSegment(protected val parent: Segment, val text: Str
|
|||||||
pos
|
pos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private def computeWrapIndent(renderer: FontRenderer) = if (lists.contains(rootPrefix)) renderer.getStringWidth(rootPrefix) else 0
|
||||||
|
|
||||||
override def toString: String = s"{TextSegment: text = $text}"
|
override def toString: String = s"{TextSegment: text = $text}"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user