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