mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 12:21:52 +09:00
more btex tests
This commit is contained in:
@@ -15,10 +15,18 @@ class BTeXDocument {
|
|||||||
var inner = "standard"
|
var inner = "standard"
|
||||||
var papersize = "standard"
|
var papersize = "standard"
|
||||||
|
|
||||||
var pageWidth = 420
|
var textWidth = 450
|
||||||
var lineHeightInPx = 24
|
var lineHeightInPx = 24
|
||||||
var pageLines = 25
|
var pageLines = 24
|
||||||
var pageHeight = pageLines * lineHeightInPx
|
var textHeight = pageLines * lineHeightInPx
|
||||||
|
|
||||||
|
val pageMarginH = 15
|
||||||
|
val pageMarginV = 12
|
||||||
|
|
||||||
|
val pageWidth: Int
|
||||||
|
get() = 2 * pageMarginH + textWidth
|
||||||
|
val pageHeight: Int
|
||||||
|
get() = 2 * pageMarginV + textHeight
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val DEFAULT_PAGE_BACK = Color(0xe1e1d7ff.toInt())
|
val DEFAULT_PAGE_BACK = Color(0xe1e1d7ff.toInt())
|
||||||
@@ -30,8 +38,10 @@ class BTeXDocument {
|
|||||||
val currentPage: Int
|
val currentPage: Int
|
||||||
get() = pages.size - 1
|
get() = pages.size - 1
|
||||||
|
|
||||||
|
val pageIndices: IntRange
|
||||||
|
get() = pages.indices
|
||||||
|
|
||||||
var currentLine: Int = 0
|
var currentLine: Int = 0
|
||||||
private set
|
|
||||||
|
|
||||||
fun addNewPage(back: Color = DEFAULT_PAGE_BACK) {
|
fun addNewPage(back: Color = DEFAULT_PAGE_BACK) {
|
||||||
pages.add(BTeXPage(back, pageWidth, pageHeight))
|
pages.add(BTeXPage(back, pageWidth, pageHeight))
|
||||||
@@ -50,7 +60,7 @@ class BTeXDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun render(frameDelta: Float, batch: SpriteBatch, page: Int, x: Int, y: Int) {
|
fun render(frameDelta: Float, batch: SpriteBatch, page: Int, x: Int, y: Int) {
|
||||||
pages[page].render(frameDelta, batch, x, y)
|
pages[page].render(frameDelta, batch, x, y, pageMarginH, pageMarginV)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,11 +75,11 @@ class BTeXPage(
|
|||||||
drawCalls.add(drawCall)
|
drawCalls.add(drawCall)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun render(frameDelta: Float, batch: SpriteBatch, x: Int, y: Int) {
|
fun render(frameDelta: Float, batch: SpriteBatch, x: Int, y: Int, marginH: Int, marginV: Int) {
|
||||||
batch.color = back
|
batch.color = back
|
||||||
Toolkit.fillArea(batch, x, y, width, height)
|
Toolkit.fillArea(batch, x, y, width, height)
|
||||||
drawCalls.forEach {
|
drawCalls.forEach {
|
||||||
it.draw(batch, x, y)
|
it.draw(batch, x + marginH, y + marginV)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ object BTeXParser {
|
|||||||
|
|
||||||
private var btexOpened = false
|
private var btexOpened = false
|
||||||
|
|
||||||
private var pageWidth = doc.pageWidth
|
private var pageWidth = doc.textWidth
|
||||||
private var pageLines = doc.pageLines
|
private var pageLines = doc.pageLines
|
||||||
private var pageHeight = doc.pageHeight
|
private var pageHeight = doc.textHeight
|
||||||
|
|
||||||
private val blockLut = HashMap<String, ItemID>()
|
private val blockLut = HashMap<String, ItemID>()
|
||||||
|
|
||||||
@@ -396,10 +396,10 @@ object BTeXParser {
|
|||||||
)
|
)
|
||||||
|
|
||||||
private val pageWidthMap = hashMapOf(
|
private val pageWidthMap = hashMapOf(
|
||||||
"standard" to 420
|
"standard" to 450
|
||||||
)
|
)
|
||||||
private val pageHeightMap = hashMapOf(
|
private val pageHeightMap = hashMapOf(
|
||||||
"standard" to 25
|
"standard" to 24
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -421,8 +421,8 @@ object BTeXParser {
|
|||||||
handler.pageLines = pageHeightMap[papersize]!!
|
handler.pageLines = pageHeightMap[papersize]!!
|
||||||
handler.pageHeight = pageLines * LINE_HEIGHT
|
handler.pageHeight = pageLines * LINE_HEIGHT
|
||||||
|
|
||||||
doc.pageWidth = pageWidth
|
doc.textWidth = pageWidth
|
||||||
doc.pageHeight = pageHeight
|
doc.textHeight = pageHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
handler.btexOpened = true
|
handler.btexOpened = true
|
||||||
@@ -460,6 +460,7 @@ object BTeXParser {
|
|||||||
@OpenTag // reflective access is impossible with 'private'
|
@OpenTag // reflective access is impossible with 'private'
|
||||||
fun processElemCOVER(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
fun processElemCOVER(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
||||||
doc.addNewPage(Color(0x6f4a45ff))
|
doc.addNewPage(Color(0x6f4a45ff))
|
||||||
|
handler.spanColour = "white"
|
||||||
}
|
}
|
||||||
|
|
||||||
@OpenTag // reflective access is impossible with 'private'
|
@OpenTag // reflective access is impossible with 'private'
|
||||||
@@ -478,38 +479,62 @@ object BTeXParser {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@OpenTag // reflective access is impossible with 'private'
|
@OpenTag // reflective access is impossible with 'private'
|
||||||
fun processElemBR(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
fun processElemBR(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
||||||
handler.paragraphBuffer.append("\n")
|
handler.paragraphBuffer.append("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@OpenTag // reflective access is impossible with 'private'
|
@OpenTag // reflective access is impossible with 'private'
|
||||||
fun processElemNEWPAGE(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
fun processElemNEWPAGE(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
||||||
|
doc.addNewPage()
|
||||||
|
}
|
||||||
|
|
||||||
|
@CloseTag // reflective access is impossible with 'private'
|
||||||
|
fun closeElemFULLPAGEBOX(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) {
|
||||||
|
doc.addNewPage()
|
||||||
}
|
}
|
||||||
|
|
||||||
@OpenTag // reflective access is impossible with 'private'
|
@OpenTag // reflective access is impossible with 'private'
|
||||||
fun processElemP(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
fun processElemP(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String, attribs: HashMap<String, String>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@CloseTag
|
||||||
|
fun closeElemCOVER(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) {
|
||||||
|
handler.spanColour = null
|
||||||
|
}
|
||||||
|
|
||||||
|
@CloseTag // reflective access is impossible with 'private'
|
||||||
|
fun closeElemTITLE(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri)
|
||||||
|
@CloseTag // reflective access is impossible with 'private'
|
||||||
|
fun closeElemAUTHOR(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri)
|
||||||
|
@CloseTag // reflective access is impossible with 'private'
|
||||||
|
fun closeElemEDITION(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri)
|
||||||
|
@CloseTag // reflective access is impossible with 'private'
|
||||||
|
fun closeElemCHAPTER(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri)
|
||||||
|
@CloseTag // reflective access is impossible with 'private'
|
||||||
|
fun closeElemSECTION(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) = closeElemP(handler, doc, theTag, uri)
|
||||||
|
|
||||||
@CloseTag // reflective access is impossible with 'private'
|
@CloseTag // reflective access is impossible with 'private'
|
||||||
fun closeElemP(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) {
|
fun closeElemP(handler: BTeXHandler, doc: BTeXDocument, theTag: String, uri: String) {
|
||||||
printdbg("Par: ${handler.paragraphBuffer}")
|
val thePar = handler.paragraphBuffer.toString().trim() + "\n"
|
||||||
|
printdbg("Par: '$thePar'")
|
||||||
|
|
||||||
val font = getFont()
|
val font = getFont()
|
||||||
val slugs = MovableType(font, handler.paragraphBuffer.toString(), doc.pageWidth)
|
val slugs = MovableType(font, thePar, doc.textWidth)
|
||||||
|
|
||||||
|
|
||||||
var remainder = doc.pageLines - doc.currentLine
|
var remainder = doc.pageLines - doc.currentLine
|
||||||
var slugHeight = slugs.height
|
var slugHeight = slugs.height
|
||||||
var linesOut = 0
|
var linesOut = 0
|
||||||
|
|
||||||
|
printdbg("Page: ${doc.currentPage+1}, Line: ${doc.currentLine}")
|
||||||
|
|
||||||
if (slugHeight > remainder) {
|
if (slugHeight > remainder) {
|
||||||
val subset = linesOut to linesOut + remainder
|
val subset = linesOut to linesOut + remainder
|
||||||
|
|
||||||
val drawCall = BTeXDrawCall(
|
val drawCall = BTeXDrawCall(
|
||||||
0,
|
0,
|
||||||
remainder * doc.pageLines,
|
doc.currentLine * doc.lineHeightInPx,
|
||||||
handler.currentTheme,
|
handler.currentTheme,
|
||||||
handler.getSpanColour(),
|
handler.getSpanColour(),
|
||||||
MovableTypeDrawCall(slugs, subset.first, subset.second)
|
MovableTypeDrawCall(slugs, subset.first, subset.second)
|
||||||
@@ -530,7 +555,7 @@ object BTeXParser {
|
|||||||
|
|
||||||
val drawCall = BTeXDrawCall(
|
val drawCall = BTeXDrawCall(
|
||||||
0,
|
0,
|
||||||
0,
|
doc.currentLine * doc.lineHeightInPx,
|
||||||
handler.currentTheme,
|
handler.currentTheme,
|
||||||
handler.getSpanColour(),
|
handler.getSpanColour(),
|
||||||
MovableTypeDrawCall(slugs, subset.first, subset.second)
|
MovableTypeDrawCall(slugs, subset.first, subset.second)
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
package net.torvald.terrarum.tests
|
package net.torvald.terrarum.tests
|
||||||
|
|
||||||
import com.badlogic.gdx.ApplicationAdapter
|
import com.badlogic.gdx.ApplicationAdapter
|
||||||
|
import com.badlogic.gdx.Gdx
|
||||||
|
import com.badlogic.gdx.Input
|
||||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application
|
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application
|
||||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration
|
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration
|
||||||
|
import com.badlogic.gdx.graphics.OrthographicCamera
|
||||||
import com.badlogic.gdx.graphics.glutils.ShaderProgram
|
import com.badlogic.gdx.graphics.glutils.ShaderProgram
|
||||||
import net.torvald.btex.BTeXParser
|
import net.torvald.btex.BTeXParser
|
||||||
|
import net.torvald.terrarum.FlippingSpriteBatch
|
||||||
import net.torvald.terrarum.btex.BTeXDocument
|
import net.torvald.terrarum.btex.BTeXDocument
|
||||||
|
import net.torvald.terrarum.ceilToInt
|
||||||
|
import net.torvald.terrarum.gdxClearAndEnableBlend
|
||||||
|
import net.torvald.terrarum.inUse
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,11 +48,11 @@ class BTeXTest : ApplicationAdapter() {
|
|||||||
|
|
||||||
<newpage />
|
<newpage />
|
||||||
|
|
||||||
<fullpagebox>
|
<!--<fullpagebox>
|
||||||
<p><span colour="grey">
|
<p><span colour="grey">
|
||||||
this page is intentionally left blank
|
this page is intentionally left blank
|
||||||
</span></p>
|
</span></p>
|
||||||
</fullpagebox>
|
</fullpagebox>-->
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -90,11 +97,38 @@ class BTeXTest : ApplicationAdapter() {
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
private lateinit var document: BTeXDocument
|
private lateinit var document: BTeXDocument
|
||||||
|
private lateinit var batch: FlippingSpriteBatch
|
||||||
|
private lateinit var camera: OrthographicCamera
|
||||||
|
|
||||||
override fun create() {
|
override fun create() {
|
||||||
|
batch = FlippingSpriteBatch(1000)
|
||||||
|
camera = OrthographicCamera(1280f, 720f)
|
||||||
|
camera.setToOrtho(true) // some elements are pre-flipped, while some are not. The statement itself is absolutely necessary to make edge of the screen as the origin
|
||||||
|
camera.update()
|
||||||
|
batch.projectionMatrix = camera.combined
|
||||||
|
|
||||||
document = BTeXParser.invoke(tex)
|
document = BTeXParser.invoke(tex)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var scroll = 0
|
||||||
|
|
||||||
|
|
||||||
|
override fun render() {
|
||||||
|
gdxClearAndEnableBlend(.063f, .070f, .086f, 1f)
|
||||||
|
|
||||||
|
batch.inUse {
|
||||||
|
if (scroll - 1 in document.pageIndices)
|
||||||
|
document.render(0f, batch, scroll - 1, 12, 12)
|
||||||
|
if (scroll in document.pageIndices)
|
||||||
|
document.render(0f, batch, scroll, 12 + (6 + document.pageWidth), 12)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (Gdx.input.isKeyJustPressed(Input.Keys.LEFT))
|
||||||
|
scroll = (scroll - 2).coerceAtLeast(0)
|
||||||
|
else if (Gdx.input.isKeyJustPressed(Input.Keys.RIGHT))
|
||||||
|
scroll = (scroll + 2).coerceAtMost(document.pageIndices.endInclusive.toFloat().div(2f).ceilToInt().times(2))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user