btex: finally working <a> tag

This commit is contained in:
minjaesong
2024-05-18 18:15:27 +09:00
parent 1cb70c4620
commit 9e9ab5bfe0
4 changed files with 58 additions and 18 deletions

View File

@@ -41,42 +41,68 @@ class BTeXDocViewer(val doc: BTeXDocument) {
}
}
private var x1: Int = 0
private var x2: Int = 0
private var y: Int = 0
/**
* @param x top-centre
* @param y top-centre
*/
fun render(batch: SpriteBatch, x: Float, y: Float) {
val x1 = if (isTome)
x1 = if (isTome)
x.toInt() - pageGap/2 - doc.pageDimensionWidth
else
x.toInt() - doc.pageDimensionWidth / 2
val x2 = if (isTome)
x2 = if (isTome)
x.toInt() + pageGap/2
else
0
val y = y.toInt()
this.y = y.toInt()
if (doc.isFinalised || doc.fromArchive) {
if (isTome) {
batch.color = Color.WHITE
if (currentPage - 1 in doc.pageIndices)
doc.render(0f, batch, currentPage - 1, x1, y)
doc.render(0f, batch, currentPage - 1, x1, this.y)
if (currentPage in doc.pageIndices)
doc.render(0f, batch, currentPage, x2, y)
doc.render(0f, batch, currentPage, x2, this.y)
}
else {
batch.color = Color.WHITE
if (currentPage in doc.pageIndices)
doc.render(0f, batch, currentPage, x1, y)
doc.render(0f, batch, currentPage, x1, this.y)
}
}
}
private var clickLatched = false//true
fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int) {
if (!clickLatched) {
clickLatched = true
if (isTome) {
if (currentPage - 1 in doc.pageIndices)
doc.pages[currentPage - 1].touchDown(this, screenX - x1, screenY - y, pointer, button)
if (currentPage in doc.pageIndices)
doc.pages[currentPage].touchDown(this, screenX - x2, screenY - y, pointer, button)
}
else {
if (currentPage in doc.pageIndices)
doc.pages[currentPage].touchDown(this, screenX - x1, screenY - y, pointer, button)
}
}
}
fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int) {
clickLatched = false
}
fun prevPage() {
if (isTome) {
currentPage = (currentPage - 2).coerceAtLeast(0)

View File

@@ -398,24 +398,17 @@ class BTeXPage(
}
}
fun touchDown(viewer: BTeXDocViewer, screenX: Int, screenY: Int, pointer: Int, button: Int) {
val pageRelX = screenX - drawX
val pageRelY = screenY - drawY
fun touchDown(viewer: BTeXDocViewer, pageRelX: Int, pageRelY: Int, pointer: Int, button: Int) {
// filter clickable elements that are under the cursor
clickableElements.filter {
pageRelX in it.posX until it.posX+it.width &&
pageRelY in it.posY until it.posY+it.height
}.forEach { it.onClick(viewer) }
it.pointInHitbox(doc, pageRelX, pageRelY)
}.lastOrNull()?.let { it.onClick(viewer) }
}
fun isEmpty() = drawCalls.isEmpty()
fun isNotEmpty() = drawCalls.isNotEmpty()
private var drawX = 0
private var drawY = 0
fun renderToPixmap(pixmap: Pixmap, x: Int, y: Int, marginH: Int, marginV: Int) {
drawX = x; drawY = y
drawCalls.sortedBy { if (it.text != null) 16 else 0 }.let { drawCalls ->
// paint background
val backCol = back.cpy().also { it.a = 0.93f }
@@ -425,7 +418,8 @@ class BTeXPage(
// debug underlines on clickableElements
clickableElements.forEach {
pixmap.setColor(HREF_UNDERLINE)
it.debugDrawHitboxToPixmap(pixmap, doc)
// it.debugDrawHitboxToPixmap(pixmap, doc)
// TODO actually draw underlines
}
// print texts

View File

@@ -1929,7 +1929,10 @@ object BTeXParser {
val target = pageNumInt
val thePage = call.pageObject
thePage.appendClickable(BTeXClickable(boxx, boxy, boxw, boxh) { it.gotoPage(target) })
thePage.appendClickable(BTeXClickable(boxx, boxy, boxw, boxh) {
printdbg("Goto page p. ${target+1}")
it.gotoPage(target)
})
}
}

View File

@@ -3,6 +3,8 @@ package net.torvald.terrarum.tests
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.InputAdapter
import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration
import com.badlogic.gdx.graphics.Color
@@ -106,6 +108,21 @@ class BTeXTest : ApplicationAdapter() {
println("Time spent on loading [ms]: $it")
}
}
Gdx.input.inputProcessor = object : InputAdapter() {
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (::viewer.isInitialized)
viewer.touchDown(screenX, screenY, pointer, button)
return true
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (::viewer.isInitialized)
viewer.touchUp(screenX, screenY, pointer, button)
return true
}
}
}
var init = false