Rename cursorCharacter to cursorRow

This commit is contained in:
DBotThePony 2023-01-27 20:16:40 +07:00
parent 864d8b8df2
commit fd84c40c1e
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -79,7 +79,7 @@ open class TextInputPanel<out S : Screen>(
private inner class Snapshot { private inner class Snapshot {
private val lines = ArrayList(this@TextInputPanel.lines) // ultra fast copy private val lines = ArrayList(this@TextInputPanel.lines) // ultra fast copy
private val cursorLine = this@TextInputPanel.cursorLine private val cursorLine = this@TextInputPanel.cursorLine
private val cursorCharacter = this@TextInputPanel.cursorCharacter private val cursorCharacter = this@TextInputPanel.cursorRow
private val selections = Int2ObjectAVLTreeMap(this@TextInputPanel.selections) private val selections = Int2ObjectAVLTreeMap(this@TextInputPanel.selections)
private val multiLine = this@TextInputPanel.multiLine private val multiLine = this@TextInputPanel.multiLine
@ -96,7 +96,7 @@ open class TextInputPanel<out S : Screen>(
if (this@TextInputPanel.multiLine && multiLine) if (this@TextInputPanel.multiLine && multiLine)
this@TextInputPanel.selections.putAll(selections) this@TextInputPanel.selections.putAll(selections)
this@TextInputPanel.cursorCharacter = cursorCharacter this@TextInputPanel.cursorRow = cursorCharacter
this@TextInputPanel.cursorLine = cursorLine this@TextInputPanel.cursorLine = cursorLine
triggerChangeCallback() triggerChangeCallback()
} }
@ -139,7 +139,7 @@ open class TextInputPanel<out S : Screen>(
} }
var cursorLine = 0 var cursorLine = 0
var cursorCharacter = 0 var cursorRow = 0
open var textColor = RGBAColor.WHITE open var textColor = RGBAColor.WHITE
open var cursorColor = RGBAColor.GREEN open var cursorColor = RGBAColor.GREEN
open var backgroundColor = RGBAColor.BLACK open var backgroundColor = RGBAColor.BLACK
@ -315,7 +315,7 @@ open class TextInputPanel<out S : Screen>(
if (index != 0) if (index != 0)
cursorLine-- cursorLine--
cursorCharacter = lines.getOrNull(cursorLine)?.length ?: 0 cursorRow = lines.getOrNull(cursorLine)?.length ?: 0
} }
lines.removeAt(index) lines.removeAt(index)
@ -339,8 +339,8 @@ open class TextInputPanel<out S : Screen>(
@Suppress("name_shadowing") @Suppress("name_shadowing")
val moveBy = if (character + moveBy < 0) -character else moveBy val moveBy = if (character + moveBy < 0) -character else moveBy
if (cursorLine == line && cursorCharacter >= character) { if (cursorLine == line && cursorRow >= character) {
cursorCharacter = (cursorCharacter + moveBy).coerceIn(0, this[cursorLine]?.length ?: Int.MAX_VALUE) cursorRow = (cursorRow + moveBy).coerceIn(0, this[cursorLine]?.length ?: Int.MAX_VALUE)
} }
val selection = selections[line] val selection = selections[line]
@ -403,9 +403,9 @@ open class TextInputPanel<out S : Screen>(
if (lineNumber < cursorLine) { if (lineNumber < cursorLine) {
cursorLine = lineNumber cursorLine = lineNumber
cursorCharacter = selection.start cursorRow = selection.start
} else if (lineNumber == cursorLine && cursorCharacter > selection.start) { } else if (lineNumber == cursorLine && cursorRow > selection.start) {
cursorCharacter = selection.start cursorRow = selection.start
} }
} }
@ -437,7 +437,7 @@ open class TextInputPanel<out S : Screen>(
redo.clear() redo.clear()
undo.clear() undo.clear()
cursorLine = 0 cursorLine = 0
cursorCharacter = 0 cursorRow = 0
textCache = null textCache = null
if (multiLine) { if (multiLine) {
@ -464,36 +464,36 @@ open class TextInputPanel<out S : Screen>(
val line = this[cursorLine] val line = this[cursorLine]
var couldHaveChangedLine = false var couldHaveChangedLine = false
if (line != null && cursorCharacter > line.length) { if (line != null && cursorRow > line.length) {
cursorCharacter = line.length cursorRow = line.length
} else if (cursorCharacter < 0) { } else if (cursorRow < 0) {
cursorCharacter = 0 cursorRow = 0
} }
val oldChar = cursorCharacter val oldChar = cursorRow
if (cursorCharacter > 0) { if (cursorRow > 0) {
if (greedy && line != null) { if (greedy && line != null) {
cursorCharacter = greedyAdvanceLeft(line, cursorCharacter) cursorRow = greedyAdvanceLeft(line, cursorRow)
} else { } else {
cursorCharacter-- cursorRow--
} }
} else if (cursorLine > 0) { } else if (cursorLine > 0) {
cursorLine-- cursorLine--
cursorCharacter = 0 cursorRow = 0
couldHaveChangedLine = true couldHaveChangedLine = true
@Suppress("name_shadowing") @Suppress("name_shadowing")
val line = this[cursorLine] val line = this[cursorLine]
if (line != null) { if (line != null) {
cursorCharacter = line.length cursorRow = line.length
} }
} else { } else {
couldHaveChangedLine = true couldHaveChangedLine = true
} }
return CursorAdvanceResult(oldLine, cursorLine, oldChar, cursorCharacter, couldHaveChangedLine = couldHaveChangedLine) return CursorAdvanceResult(oldLine, cursorLine, oldChar, cursorRow, couldHaveChangedLine = couldHaveChangedLine)
} }
fun advanceCursorRight(greedy: Boolean = false): CursorAdvanceResult { fun advanceCursorRight(greedy: Boolean = false): CursorAdvanceResult {
@ -502,39 +502,39 @@ open class TextInputPanel<out S : Screen>(
val line = this[cursorLine] val line = this[cursorLine]
if (line != null && cursorCharacter > line.length) { if (line != null && cursorRow > line.length) {
cursorCharacter = line.length cursorRow = line.length
} else if (cursorCharacter < 0) { } else if (cursorRow < 0) {
cursorCharacter = 0 cursorRow = 0
} }
val oldChar = cursorCharacter val oldChar = cursorRow
if (greedy && line != null && cursorCharacter + 1 < line.length) { if (greedy && line != null && cursorRow + 1 < line.length) {
cursorCharacter = greedyAdvanceRight(line, cursorCharacter) cursorRow = greedyAdvanceRight(line, cursorRow)
} else { } else {
cursorCharacter++ cursorRow++
} }
if (line != null && cursorCharacter > line.length) { if (line != null && cursorRow > line.length) {
couldHaveChangedLine = true couldHaveChangedLine = true
if (lines.size <= cursorLine + 1) { if (lines.size <= cursorLine + 1) {
cursorCharacter = line.length cursorRow = line.length
} else { } else {
cursorLine++ cursorLine++
cursorCharacter = 0 cursorRow = 0
} }
} else if (line == null) { } else if (line == null) {
cursorCharacter = 0 cursorRow = 0
} }
return CursorAdvanceResult(oldLine, cursorLine, oldChar, cursorCharacter, couldHaveChangedLine = couldHaveChangedLine) return CursorAdvanceResult(oldLine, cursorLine, oldChar, cursorRow, couldHaveChangedLine = couldHaveChangedLine)
} }
private fun simulateSelectLeft(greedy: Boolean) { private fun simulateSelectLeft(greedy: Boolean) {
val line = this[cursorLine] ?: return val line = this[cursorLine] ?: return
val existing = selections[cursorLine] ?: TextSelection(cursorCharacter, 0) val existing = selections[cursorLine] ?: TextSelection(cursorRow, 0)
val result = advanceCursorLeft(greedy) val result = advanceCursorLeft(greedy)
if (result.couldHaveChangedLine) { if (result.couldHaveChangedLine) {
@ -557,7 +557,7 @@ open class TextInputPanel<out S : Screen>(
private fun simulateSelectRight(greedy: Boolean) { private fun simulateSelectRight(greedy: Boolean) {
val line = this[cursorLine] ?: return val line = this[cursorLine] ?: return
val existing = selections[cursorLine] ?: TextSelection(cursorCharacter, 0) val existing = selections[cursorLine] ?: TextSelection(cursorRow, 0)
val result = advanceCursorRight(greedy) val result = advanceCursorRight(greedy)
if (result.couldHaveChangedLine) { if (result.couldHaveChangedLine) {
@ -572,24 +572,24 @@ open class TextInputPanel<out S : Screen>(
if (cursorLine <= 0) if (cursorLine <= 0)
return false return false
val cursorCharacter = cursorCharacter val cursorCharacter = cursorRow
val cursorLine = cursorLine val cursorLine = cursorLine
while (this.cursorCharacter >= 0 && this.cursorLine == cursorLine) { while (this.cursorRow >= 0 && this.cursorLine == cursorLine) {
simulateSelectLeft(false) simulateSelectLeft(false)
} }
val line = this[this.cursorLine]!! val line = this[this.cursorLine]!!
if (cursorCharacter < line.length) { if (cursorCharacter < line.length) {
this.cursorCharacter = line.length this.cursorRow = line.length
while (this.cursorCharacter > cursorCharacter) { while (this.cursorRow > cursorCharacter) {
simulateSelectLeft(false) simulateSelectLeft(false)
} }
} }
this.cursorCharacter = cursorCharacter this.cursorRow = cursorCharacter
return true return true
} }
@ -597,19 +597,19 @@ open class TextInputPanel<out S : Screen>(
if (cursorLine >= lines.size - 1) if (cursorLine >= lines.size - 1)
return false return false
val cursorCharacter = cursorCharacter val cursorCharacter = cursorRow
val cursorLine = cursorLine val cursorLine = cursorLine
while (this.cursorCharacter >= cursorCharacter && this.cursorLine == cursorLine) { while (this.cursorRow >= cursorCharacter && this.cursorLine == cursorLine) {
simulateSelectRight(true) simulateSelectRight(true)
} }
val line = this[this.cursorLine]!! val line = this[this.cursorLine]!!
if (cursorCharacter < line.length) { if (cursorCharacter < line.length) {
this.cursorCharacter = 0 this.cursorRow = 0
while (this.cursorCharacter < cursorCharacter) { while (this.cursorRow < cursorCharacter) {
simulateSelectRight(false) simulateSelectRight(false)
} }
} else { } else {
@ -630,7 +630,7 @@ open class TextInputPanel<out S : Screen>(
} }
} }
this.cursorCharacter = cursorCharacter this.cursorRow = cursorCharacter
return true return true
} }
@ -648,25 +648,25 @@ open class TextInputPanel<out S : Screen>(
val line = this[cursorLine] val line = this[cursorLine]
if (line != null && !minecraft.window.isShiftDown) { if (line != null && !minecraft.window.isShiftDown) {
if (cursorCharacter <= 0) { if (cursorRow <= 0) {
recordHistory(true) recordHistory(true)
insertLine(cursorLine, "") insertLine(cursorLine, "")
if (!minecraft.window.isCtrlDown) { if (!minecraft.window.isCtrlDown) {
cursorLine++ cursorLine++
cursorCharacter = 0 cursorRow = 0
} }
} else if (cursorCharacter >= line.length) { } else if (cursorRow >= line.length) {
recordHistory(true) recordHistory(true)
insertLine(cursorLine + 1, "") insertLine(cursorLine + 1, "")
if (!minecraft.window.isCtrlDown) { if (!minecraft.window.isCtrlDown) {
cursorLine++ cursorLine++
cursorCharacter = 0 cursorRow = 0
} }
} else { } else {
val before = line.substring(0, cursorCharacter) val before = line.substring(0, cursorRow)
val after = line.substring(cursorCharacter) val after = line.substring(cursorRow)
recordHistory(true) recordHistory(true)
this[cursorLine] = before this[cursorLine] = before
@ -674,14 +674,14 @@ open class TextInputPanel<out S : Screen>(
if (!minecraft.window.isCtrlDown) { if (!minecraft.window.isCtrlDown) {
cursorLine++ cursorLine++
cursorCharacter = 0 cursorRow = 0
} }
} }
} else { } else {
recordHistory(true) recordHistory(true)
insertLine(cursorLine + 1) insertLine(cursorLine + 1)
cursorLine++ cursorLine++
cursorCharacter = 0 cursorRow = 0
} }
triggerChangeCallback() triggerChangeCallback()
@ -752,11 +752,11 @@ open class TextInputPanel<out S : Screen>(
val line = this[cursorLine] val line = this[cursorLine]
if (cursorLine <= 0 && cursorCharacter <= 0) { if (cursorLine <= 0 && cursorRow <= 0) {
return true return true
} else if (cursorCharacter <= 0 || line?.length == 0) { } else if (cursorRow <= 0 || line?.length == 0) {
if (line == null) { if (line == null) {
cursorCharacter = this[--cursorLine]?.length ?: 0 cursorRow = this[--cursorLine]?.length ?: 0
recordHistory() recordHistory()
} else { } else {
removeLine(cursorLine) removeLine(cursorLine)
@ -776,13 +776,13 @@ open class TextInputPanel<out S : Screen>(
pushbackSnapshotIfNoTimer() pushbackSnapshotIfNoTimer()
// remove from very end // remove from very end
if (cursorCharacter >= line.length) { if (cursorRow >= line.length) {
val newLine = line.substring(0, line.length - 1) val newLine = line.substring(0, line.length - 1)
moveCursors(cursorLine, cursorCharacter, -1) moveCursors(cursorLine, cursorRow, -1)
this[cursorLine] = newLine this[cursorLine] = newLine
} else { } else {
val newLine = line.substring(0, cursorCharacter - 1) + line.substring(cursorCharacter) val newLine = line.substring(0, cursorRow - 1) + line.substring(cursorRow)
moveCursors(cursorLine, cursorCharacter, -1) moveCursors(cursorLine, cursorRow, -1)
this[cursorLine] = newLine this[cursorLine] = newLine
} }
@ -815,7 +815,7 @@ open class TextInputPanel<out S : Screen>(
if (cursorLine >= lines.size) if (cursorLine >= lines.size)
cursorLine = lines.size cursorLine = lines.size
cursorCharacter = 0 cursorRow = 0
recordHistory(true) recordHistory(true)
triggerChangeCallback() triggerChangeCallback()
return true return true
@ -823,7 +823,7 @@ open class TextInputPanel<out S : Screen>(
val line = this[cursorLine]!! val line = this[cursorLine]!!
if (cursorCharacter >= line.length) { if (cursorRow >= line.length) {
if (cursorLine + 1 == lines.size) { if (cursorLine + 1 == lines.size) {
return true return true
} }
@ -831,18 +831,18 @@ open class TextInputPanel<out S : Screen>(
pushbackSnapshotIfNoTimer() pushbackSnapshotIfNoTimer()
val bottomLine = this[cursorLine + 1]!! val bottomLine = this[cursorLine + 1]!!
cursorCharacter = line.length cursorRow = line.length
this[cursorLine] = line + bottomLine this[cursorLine] = line + bottomLine
removeLine(cursorLine + 1) removeLine(cursorLine + 1)
} else { } else {
pushbackSnapshotIfNoTimer() pushbackSnapshotIfNoTimer()
val cursorCharacter = cursorCharacter val cursorCharacter = cursorRow
moveCursors(cursorLine, cursorCharacter, -1) moveCursors(cursorLine, cursorCharacter, -1)
this[cursorLine] = line.substring(0, cursorCharacter) + line.substring(cursorCharacter + 1) this[cursorLine] = line.substring(0, cursorCharacter) + line.substring(cursorCharacter + 1)
if (cursorCharacter != 0) if (cursorCharacter != 0)
this.cursorCharacter++ this.cursorRow++
} }
recordHistory() recordHistory()
@ -882,7 +882,7 @@ open class TextInputPanel<out S : Screen>(
} else if (line.isEmpty()) { } else if (line.isEmpty()) {
this[cursorLine] = insert[0] this[cursorLine] = insert[0]
} else { } else {
this[cursorLine] = line.substring(0, cursorCharacter.coerceAtMost(line.length - 1)) + insert[0] + line.substring(cursorCharacter.coerceAtMost(line.length)) this[cursorLine] = line.substring(0, cursorRow.coerceAtMost(line.length - 1)) + insert[0] + line.substring(cursorRow.coerceAtMost(line.length))
} }
for (i in 1 until insert.size - 1) { for (i in 1 until insert.size - 1) {
@ -907,7 +907,7 @@ open class TextInputPanel<out S : Screen>(
} else if (line.isEmpty()) { } else if (line.isEmpty()) {
this[cursorLine] = insert this[cursorLine] = insert
} else { } else {
this[cursorLine] = line.substring(0, cursorCharacter.coerceAtMost(line.length - 1)) + insert + line.substring(cursorCharacter.coerceAtMost(line.length)) this[cursorLine] = line.substring(0, cursorRow.coerceAtMost(line.length - 1)) + insert + line.substring(cursorRow.coerceAtMost(line.length))
} }
} }
@ -941,15 +941,15 @@ open class TextInputPanel<out S : Screen>(
} }
if (key == InputConstants.KEY_HOME) { if (key == InputConstants.KEY_HOME) {
if (minecraft.window.isShiftDown && cursorCharacter != 0) { if (minecraft.window.isShiftDown && cursorRow != 0) {
val thisLine = cursorLine val thisLine = cursorLine
while (cursorCharacter > 0 && thisLine == cursorLine) { while (cursorRow > 0 && thisLine == cursorLine) {
simulateSelectLeft(false) simulateSelectLeft(false)
} }
} else { } else {
if (!minecraft.window.isShiftDown) selections.clear() if (!minecraft.window.isShiftDown) selections.clear()
cursorCharacter = 0 cursorRow = 0
} }
return true return true
@ -958,15 +958,15 @@ open class TextInputPanel<out S : Screen>(
if (key == InputConstants.KEY_END) { if (key == InputConstants.KEY_END) {
if (minecraft.window.isShiftDown) { if (minecraft.window.isShiftDown) {
val line = this[cursorLine] ?: return true val line = this[cursorLine] ?: return true
if (cursorCharacter >= line.length) return true if (cursorRow >= line.length) return true
val thisLine = cursorLine val thisLine = cursorLine
while (cursorCharacter < line.length && thisLine == cursorLine) { while (cursorRow < line.length && thisLine == cursorLine) {
simulateSelectRight(false) simulateSelectRight(false)
} }
} else { } else {
if (!minecraft.window.isShiftDown) selections.clear() if (!minecraft.window.isShiftDown) selections.clear()
cursorCharacter = this[cursorLine]?.length ?: 0 cursorRow = this[cursorLine]?.length ?: 0
} }
return true return true
@ -1000,18 +1000,18 @@ open class TextInputPanel<out S : Screen>(
if (line == null) { if (line == null) {
set(cursorLine, "") set(cursorLine, "")
line = "" line = ""
cursorCharacter = 0 cursorRow = 0
} }
if (cursorCharacter >= line.length) if (cursorRow >= line.length)
line += codepoint line += codepoint
else else
line = line.substring(0, cursorCharacter) + codepoint + line.substring(cursorCharacter) line = line.substring(0, cursorRow) + codepoint + line.substring(cursorRow)
pushbackSnapshotIfNoTimer() pushbackSnapshotIfNoTimer()
set(cursorLine, line) set(cursorLine, line)
moveCursors(cursorLine, cursorCharacter, 1) moveCursors(cursorLine, cursorRow, 1)
recordHistory() recordHistory()
triggerChangeCallback() triggerChangeCallback()
@ -1082,7 +1082,7 @@ open class TextInputPanel<out S : Screen>(
if (isFocused && milliTime % 1000L > 500L) { if (isFocused && milliTime % 1000L > 500L) {
val activeLine = this[cursorLine] val activeLine = this[cursorLine]
if (activeLine == null || cursorCharacter >= activeLine.length) { if (activeLine == null || cursorRow >= activeLine.length) {
font.drawAligned( font.drawAligned(
poseStack = stack, poseStack = stack,
buffer = BUFFER, buffer = BUFFER,
@ -1098,7 +1098,7 @@ open class TextInputPanel<out S : Screen>(
buffer = BUFFER, buffer = BUFFER,
text = "|", text = "|",
align = TextAlign.TOP_LEFT, align = TextAlign.TOP_LEFT,
x = dockPadding.left + font.width(activeLine.substring(0, cursorCharacter)).toFloat() - 1f, x = dockPadding.left + font.width(activeLine.substring(0, cursorRow)).toFloat() - 1f,
y = dockPadding.top + cursorLine * (font.lineHeight + 2f), y = dockPadding.top + cursorLine * (font.lineHeight + 2f),
color = cursorColor color = cursorColor
) )
@ -1119,7 +1119,7 @@ open class TextInputPanel<out S : Screen>(
font.drawAligned( font.drawAligned(
poseStack = stack, poseStack = stack,
buffer = BUFFER, buffer = BUFFER,
text = cursorCharacter.toString(), text = cursorRow.toString(),
align = TextAlign.TOP_RIGHT, align = TextAlign.TOP_RIGHT,
x = width - dockPadding.right, x = width - dockPadding.right,
y = dockPadding.top + font.lineHeight + 2f, y = dockPadding.top + font.lineHeight + 2f,
@ -1157,7 +1157,7 @@ open class TextInputPanel<out S : Screen>(
val (lx, ly) = screenToLocal(x, y) val (lx, ly) = screenToLocal(x, y)
val pos = localToText(lx, ly) val pos = localToText(lx, ly)
cursorLine = pos.y cursorLine = pos.y
cursorCharacter = pos.x cursorRow = pos.x
} }
isSelecting = true isSelecting = true
@ -1231,15 +1231,15 @@ open class TextInputPanel<out S : Screen>(
while (cursorLine > this.cursorLine && simulateSelectionDown()) {} while (cursorLine > this.cursorLine && simulateSelectionDown()) {}
while (cursorLine < this.cursorLine && simulateSelectionUp()) {} while (cursorLine < this.cursorLine && simulateSelectionUp()) {}
var lastCursorCharacter = this.cursorCharacter + 1 var lastCursorCharacter = this.cursorRow + 1
while (cursorCharacter < this.cursorCharacter && lastCursorCharacter != this.cursorCharacter) { while (cursorCharacter < this.cursorRow && lastCursorCharacter != this.cursorRow) {
lastCursorCharacter = this.cursorCharacter lastCursorCharacter = this.cursorRow
simulateSelectLeft(false) simulateSelectLeft(false)
} }
while (cursorCharacter > this.cursorCharacter && lastCursorCharacter != this.cursorCharacter) { while (cursorCharacter > this.cursorRow && lastCursorCharacter != this.cursorRow) {
lastCursorCharacter = this.cursorCharacter lastCursorCharacter = this.cursorRow
simulateSelectRight(false) simulateSelectRight(false)
} }