Selecting text with mouse

This commit is contained in:
DBotThePony 2023-01-26 16:06:20 +07:00
parent b6d5cc4024
commit 518b7e67fe
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -515,6 +515,64 @@ open class TextInputPanel<out S : Screen>(
if (result.couldHaveChangedLine) Int.MAX_VALUE else result.newCharacter)) if (result.couldHaveChangedLine) Int.MAX_VALUE else result.newCharacter))
} }
private fun simulateSelectionUp(): Boolean {
if (cursorLine <= 0)
return false
val cursorCharacter = cursorCharacter
val cursorLine = cursorLine
while (this.cursorCharacter >= 0 && this.cursorLine == cursorLine) {
simulateSelectLeft(false)
}
val line = this[this.cursorLine]!!
if (cursorCharacter < line.length) {
this.cursorCharacter = line.length
while (this.cursorCharacter > cursorCharacter) {
simulateSelectLeft(false)
}
}
this.cursorCharacter = cursorCharacter
return true
}
private fun simulateSelectionDown(): Boolean {
if (cursorLine >= lines.size - 1)
return false
val cursorCharacter = cursorCharacter
val cursorLine = cursorLine
while (this.cursorCharacter >= cursorCharacter && this.cursorLine == cursorLine) {
simulateSelectRight(true)
}
val line = this[this.cursorLine]!!
if (cursorCharacter < line.length) {
this.cursorCharacter = 0
while (this.cursorCharacter < cursorCharacter) {
simulateSelectRight(false)
}
} else {
val store = this.cursorLine
for (i in 0 .. line.length) {
simulateSelectRight(false)
}
this.cursorLine = store
}
this.cursorCharacter = cursorCharacter
return true
}
override fun keyPressedInternal(key: Int, scancode: Int, mods: Int): Boolean { override fun keyPressedInternal(key: Int, scancode: Int, mods: Int): Boolean {
if (key == InputConstants.KEY_ESCAPE) { if (key == InputConstants.KEY_ESCAPE) {
killFocus() killFocus()
@ -598,26 +656,7 @@ open class TextInputPanel<out S : Screen>(
return true return true
} else if (key == InputConstants.KEY_UP) { } else if (key == InputConstants.KEY_UP) {
if (minecraft.window.isShiftDown) { if (minecraft.window.isShiftDown) {
if (cursorLine > 0) { simulateSelectionUp()
val cursorCharacter = cursorCharacter
val cursorLine = cursorLine
while (this.cursorCharacter >= 0 && this.cursorLine == cursorLine) {
simulateSelectLeft(false)
}
val line = this[this.cursorLine]!!
if (cursorCharacter < line.length) {
this.cursorCharacter = line.length
while (this.cursorCharacter > cursorCharacter) {
simulateSelectLeft(false)
}
}
this.cursorCharacter = cursorCharacter
}
} else { } else {
if (cursorLine > 0) { if (cursorLine > 0) {
cursorLine-- cursorLine--
@ -629,34 +668,7 @@ open class TextInputPanel<out S : Screen>(
return true return true
} else if (key == InputConstants.KEY_DOWN) { } else if (key == InputConstants.KEY_DOWN) {
if (minecraft.window.isShiftDown) { if (minecraft.window.isShiftDown) {
if (cursorLine < lines.size - 1) { simulateSelectionDown()
val cursorCharacter = cursorCharacter
val cursorLine = cursorLine
while (this.cursorCharacter >= cursorCharacter && this.cursorLine == cursorLine) {
simulateSelectRight(true)
}
val line = this[this.cursorLine]!!
if (cursorCharacter < line.length) {
this.cursorCharacter = 0
while (this.cursorCharacter < cursorCharacter) {
simulateSelectRight(false)
}
} else {
val store = this.cursorLine
for (i in 0 .. line.length) {
simulateSelectRight(false)
}
this.cursorLine = store
}
this.cursorCharacter = cursorCharacter
}
} else { } else {
if (cursorLine < lines.size - 1) { if (cursorLine < lines.size - 1) {
cursorLine++ cursorLine++
@ -930,15 +942,22 @@ open class TextInputPanel<out S : Screen>(
BUFFER.endBatch() BUFFER.endBatch()
} }
var isSelecting = false
protected set
override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean { override fun mouseClickedInner(x: Double, y: Double, button: Int): Boolean {
selections.clear() selections.clear()
requestFocus() requestFocus()
if (button == InputConstants.MOUSE_BUTTON_LEFT) {
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 cursorCharacter = pos.x
isSelecting = true
}
return true return true
} }
@ -996,6 +1015,40 @@ open class TextInputPanel<out S : Screen>(
return Vector2i(sLine.length, line) return Vector2i(sLine.length, line)
} }
override fun mouseReleasedInner(x: Double, y: Double, button: Int): Boolean {
if (button == InputConstants.MOUSE_BUTTON_LEFT)
isSelecting = false
return true
}
override fun mouseDraggedInner(x: Double, y: Double, button: Int, xDelta: Double, yDelta: Double): Boolean {
if (button != InputConstants.MOUSE_BUTTON_LEFT || !isSelecting)
return true
val (lx, ly) = screenToLocal(x, y)
val pos = localToText(lx, ly)
val cursorLine = pos.y
val cursorCharacter = pos.x
while (cursorLine > this.cursorLine && simulateSelectionDown()) {}
while (cursorLine < this.cursorLine && simulateSelectionUp()) {}
var lastCursorCharacter = this.cursorCharacter + 1
while (cursorCharacter < this.cursorCharacter && lastCursorCharacter != this.cursorCharacter) {
lastCursorCharacter = this.cursorCharacter
simulateSelectLeft(false)
}
while (cursorCharacter > this.cursorCharacter && lastCursorCharacter != this.cursorCharacter) {
lastCursorCharacter = this.cursorCharacter
simulateSelectRight(false)
}
return true
}
private enum class CharType { private enum class CharType {
SPACES { SPACES {
override fun contains(input: Char): Boolean { override fun contains(input: Char): Boolean {