mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 10:34:06 +09:00
console command loading from command list on the module directory
This commit is contained in:
@@ -41,55 +41,42 @@ object CommandDict {
|
|||||||
val packageConsole = "$packageRoot.console"
|
val packageConsole = "$packageRoot.console"
|
||||||
|
|
||||||
printdbg(this, "Loading console commands from '${packageConsole}'")
|
printdbg(this, "Loading console commands from '${packageConsole}'")
|
||||||
|
// printdbg(this, commandsList.joinToString())
|
||||||
|
|
||||||
|
commandsList.forEach { commandName ->
|
||||||
|
val canonicalName = "$packageConsole.$commandName"
|
||||||
|
val it = Class.forName(canonicalName)
|
||||||
|
|
||||||
// TODO load commands using commandsList
|
printdbg(this, "> Trying to instantiate ${it.canonicalName}")
|
||||||
|
|
||||||
|
try {
|
||||||
|
val instance = it.kotlin.objectInstance ?: it.kotlin.java.newInstance()
|
||||||
|
|
||||||
val stream = ClassLoader.getSystemClassLoader().getResourceAsStream(packageConsole.replace('.', '/'))
|
val aliases = instance.javaClass.getAnnotation(ConsoleAlias::class.java)?.aliasesCSV?.split(',')?.map { it.trim() }
|
||||||
|
val noexport = instance.javaClass.getAnnotation(ConsoleNoExport::class.java)
|
||||||
|
|
||||||
|
if (noexport == null) {
|
||||||
|
|
||||||
if (stream != null) { // not all modules have extra console commands
|
dict[instance.javaClass.simpleName.lowercase()] = instance as ConsoleCommand
|
||||||
|
aliases?.forEach {
|
||||||
val reader = BufferedReader(InputStreamReader(stream))
|
dict[it] = instance as ConsoleCommand
|
||||||
|
|
||||||
reader.lines()
|
|
||||||
.filter { it.endsWith(".class") && !it.contains('$') }
|
|
||||||
.map { Class.forName("$packageConsole.${it.substring(0, it.lastIndexOf('.'))}") }
|
|
||||||
.forEach {
|
|
||||||
|
|
||||||
printdbg(this, "> Trying to instantiate ${it.canonicalName}")
|
|
||||||
|
|
||||||
try {
|
|
||||||
val instance = it.kotlin.objectInstance ?: it.kotlin.java.newInstance()
|
|
||||||
|
|
||||||
val aliases = instance.javaClass.getAnnotation(ConsoleAlias::class.java)?.aliasesCSV?.split(',')?.map { it.trim() }
|
|
||||||
val noexport = instance.javaClass.getAnnotation(ConsoleNoExport::class.java)
|
|
||||||
|
|
||||||
if (noexport == null) {
|
|
||||||
|
|
||||||
dict[instance.javaClass.simpleName.lowercase()] = instance as ConsoleCommand
|
|
||||||
aliases?.forEach {
|
|
||||||
dict[it] = instance as ConsoleCommand
|
|
||||||
}
|
|
||||||
|
|
||||||
printdbg(this, "Class instantiated: ${instance.javaClass.simpleName}")
|
|
||||||
if (aliases != null)
|
|
||||||
printdbg(this, " Annotations: $aliases")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (e: ClassCastException) {
|
|
||||||
printdbgerr(this, "${it.canonicalName} is not a ConsoleCommand")
|
|
||||||
}
|
|
||||||
catch (e: InstantiationException) {
|
|
||||||
printdbgerr(this, "Could not instantiate ${it.canonicalName}")
|
|
||||||
e.printStackTrace(System.err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printdbg(this, "Class instantiated: ${instance.javaClass.simpleName}")
|
||||||
|
if (aliases != null)
|
||||||
|
printdbg(this, " Annotations: $aliases")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e: ClassCastException) {
|
||||||
|
printdbgerr(this, "${it.canonicalName} is not a ConsoleCommand")
|
||||||
|
}
|
||||||
|
catch (e: InstantiationException) {
|
||||||
|
printdbgerr(this, "Could not instantiate ${it.canonicalName}")
|
||||||
|
e.printStackTrace(System.err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,6 @@ internal object CommandInterpreter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (e: NullPointerException) {
|
catch (e: NullPointerException) {
|
||||||
e.printStackTrace()
|
|
||||||
echoUnknownCmd(single_command.name)
|
echoUnknownCmd(single_command.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,9 @@ class ConsoleWindow : UICanvas() {
|
|||||||
|
|
||||||
private var iMadeTheGameToPause = false
|
private var iMadeTheGameToPause = false
|
||||||
|
|
||||||
private val textinput = UIItemTextLineInput(this, 0, 0, this.width)
|
private val textinput = UIItemTextLineInput(this, 0, 0, this.width, keyFilter = { e ->
|
||||||
|
!e.keycodes.contains(Input.Keys.GRAVE)
|
||||||
|
})
|
||||||
|
|
||||||
private var clickLatched = false
|
private var clickLatched = false
|
||||||
|
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ class UIItemTextLineInput(
|
|||||||
val maxLen: InputLenCap = InputLenCap(1000, InputLenCap.CharLenUnit.CODEPOINTS),
|
val maxLen: InputLenCap = InputLenCap(1000, InputLenCap.CharLenUnit.CODEPOINTS),
|
||||||
// val enablePasteButton: Boolean = true,
|
// val enablePasteButton: Boolean = true,
|
||||||
// val enableIMEButton: Boolean = true
|
// val enableIMEButton: Boolean = true
|
||||||
|
var keyFilter: (TerrarumKeyboardEvent) -> Boolean = { true }
|
||||||
) : UIItem(parentUI, initialX, initialY) {
|
) : UIItem(parentUI, initialX, initialY) {
|
||||||
|
|
||||||
init {
|
init {
|
||||||
@@ -203,138 +204,140 @@ class UIItemTextLineInput(
|
|||||||
|
|
||||||
val (eventType, char, headkey, repeatCount, keycodes) = e
|
val (eventType, char, headkey, repeatCount, keycodes) = e
|
||||||
|
|
||||||
try {
|
if (keyFilter(e)) {
|
||||||
if (eventType == InputStrober.KEY_DOWN || eventType == InputStrober.KEY_CHANGE) {
|
try {
|
||||||
fboUpdateLatch = true
|
if (eventType == InputStrober.KEY_DOWN || eventType == InputStrober.KEY_CHANGE) {
|
||||||
forceLitCursor()
|
fboUpdateLatch = true
|
||||||
val ime = getIME()
|
forceLitCursor()
|
||||||
val lowLayer = IME.getLowLayerByName(App.getConfigString("basekeyboardlayout"))
|
val ime = getIME()
|
||||||
|
val lowLayer = IME.getLowLayerByName(App.getConfigString("basekeyboardlayout"))
|
||||||
|
|
||||||
if (keycodes.contains(Input.Keys.V) && keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT)) {
|
if (keycodes.contains(Input.Keys.V) && keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT)) {
|
||||||
endComposing()
|
endComposing()
|
||||||
paste(Clipboard.fetch().substringBefore('\n').substringBefore('\t').toCodePoints())
|
paste(Clipboard.fetch().substringBefore('\n').substringBefore('\t').toCodePoints())
|
||||||
}
|
}
|
||||||
else if (keycodes.contains(Input.Keys.C) && (keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT))) {
|
else if (keycodes.contains(Input.Keys.C) && (keycodes.containsSome(Input.Keys.CONTROL_LEFT, Input.Keys.CONTROL_RIGHT))) {
|
||||||
endComposing()
|
endComposing()
|
||||||
copyToClipboard()
|
copyToClipboard()
|
||||||
}
|
}
|
||||||
else if (keycodes.contains(Input.Keys.BACKSPACE) || (keycodes.contains(Input.Keys.CAPS_LOCK) && lowLayer.capsMode == TerrarumKeyCapsMode.BACK)) {
|
else if (keycodes.contains(Input.Keys.BACKSPACE) || (keycodes.contains(Input.Keys.CAPS_LOCK) && lowLayer.capsMode == TerrarumKeyCapsMode.BACK)) {
|
||||||
|
|
||||||
// printdbg(this, "BACKSPACE hit; ime.composing=${ime?.composing?.invoke()}; buflen=${textbuf.size}")
|
// printdbg(this, "BACKSPACE hit; ime.composing=${ime?.composing?.invoke()}; buflen=${textbuf.size}")
|
||||||
|
|
||||||
if (ime != null && ime.composing()) {
|
if (ime != null && ime.composing()) {
|
||||||
if (ime.config.mode == TerrarumIMEMode.CANDIDATES) {
|
if (ime.config.mode == TerrarumIMEMode.CANDIDATES) {
|
||||||
candidates = ime.backspace().map { CodepointSequence(it.toCodePoints()) }
|
candidates = ime.backspace().map { CodepointSequence(it.toCodePoints()) }
|
||||||
}
|
|
||||||
else if (ime.config.mode == TerrarumIMEMode.REWRITE) {
|
|
||||||
candidates = listOf()
|
|
||||||
val op = ime.backspace()
|
|
||||||
if (textbuf.isNotEmpty()) {
|
|
||||||
inputBackspaceOnce(1)
|
|
||||||
}
|
}
|
||||||
|
else if (ime.config.mode == TerrarumIMEMode.REWRITE) {
|
||||||
|
candidates = listOf()
|
||||||
|
val op = ime.backspace()
|
||||||
|
if (textbuf.isNotEmpty()) {
|
||||||
|
inputBackspaceOnce(1)
|
||||||
|
}
|
||||||
|
|
||||||
if (op.size > 0) {
|
if (op.size > 0) {
|
||||||
val codepoints = op[0].toCodePoints()
|
val codepoints = op[0].toCodePoints()
|
||||||
if (!maxLen.exceeds(textbuf, codepoints)) {
|
if (!maxLen.exceeds(textbuf, codepoints)) {
|
||||||
textbuf.addAll(cursorX, codepoints)
|
textbuf.addAll(cursorX, codepoints)
|
||||||
|
|
||||||
moveCursorToEnd(codepoints.size)
|
moveCursorToEnd(codepoints.size)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else if (cursorX <= 0) {
|
||||||
else if (cursorX <= 0) {
|
|
||||||
cursorX = 0
|
|
||||||
cursorDrawX = 0
|
|
||||||
cursorDrawScroll = 0
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
endComposing()
|
|
||||||
inputBackspaceOnce(2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (keycodes.contains(Input.Keys.LEFT)) {
|
|
||||||
endComposing()
|
|
||||||
|
|
||||||
if (cursorX > 0) {
|
|
||||||
cursorX -= 1
|
|
||||||
cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
|
|
||||||
tryCursorForward()
|
|
||||||
if (cursorX <= 0) {
|
|
||||||
cursorX = 0
|
cursorX = 0
|
||||||
cursorDrawX = 0
|
cursorDrawX = 0
|
||||||
cursorDrawScroll = 0
|
cursorDrawScroll = 0
|
||||||
}
|
}
|
||||||
}
|
else {
|
||||||
}
|
endComposing()
|
||||||
else if (keycodes.contains(Input.Keys.RIGHT)) {
|
inputBackspaceOnce(2)
|
||||||
endComposing()
|
|
||||||
|
|
||||||
if (cursorX < textbuf.size) {
|
|
||||||
cursorX += 1
|
|
||||||
cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
|
|
||||||
tryCursorBack()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (keycodes.containsSome(Input.Keys.ENTER, Input.Keys.NUMPAD_ENTER)) {
|
|
||||||
endComposing()
|
|
||||||
|
|
||||||
// println("END COMPOSING!!")
|
|
||||||
}
|
|
||||||
// accept:
|
|
||||||
// - literal "<"
|
|
||||||
// - keysymbol that does not start with "<" (not always has length of 1 because UTF-16)
|
|
||||||
else if (char != null && char.length > 0 && char[0].code >= 32 && (char == "<" || !char.startsWith("<"))) {
|
|
||||||
val shiftin = keycodes.containsSome(Input.Keys.SHIFT_LEFT, Input.Keys.SHIFT_RIGHT)
|
|
||||||
val altgrin = keycodes.contains(Input.Keys.ALT_RIGHT) || keycodes.containsAll(Input.Keys.ALT_LEFT, Input.Keys.CONTROL_LEFT)
|
|
||||||
|
|
||||||
val codepoints = if (ime != null) {
|
|
||||||
if (ime.config.mode == TerrarumIMEMode.CANDIDATES) {
|
|
||||||
val newStatus = ime.acceptChar(headkey, shiftin, altgrin, char)
|
|
||||||
candidates = newStatus.first.map { CodepointSequence(it.toCodePoints()) }
|
|
||||||
|
|
||||||
newStatus.second.toCodePoints()
|
|
||||||
}
|
}
|
||||||
else if (ime.config.mode == TerrarumIMEMode.REWRITE) {
|
}
|
||||||
candidates = listOf()
|
else if (keycodes.contains(Input.Keys.LEFT)) {
|
||||||
val op = ime.acceptChar(headkey, shiftin, altgrin, char)
|
endComposing()
|
||||||
|
|
||||||
// printdbg(this, "delcount: ${op.first[0].toInt()}, rewrite: '${op.second}'")
|
if (cursorX > 0) {
|
||||||
|
cursorX -= 1
|
||||||
repeat(op.first[0].toInt()) {
|
cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
|
||||||
if (textbuf.isNotEmpty()) {
|
tryCursorForward()
|
||||||
// printdbg(this, "<del 1>")
|
if (cursorX <= 0) {
|
||||||
inputBackspaceOnce(3)
|
cursorX = 0
|
||||||
}
|
cursorDrawX = 0
|
||||||
|
cursorDrawScroll = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
op.second.toCodePoints()
|
|
||||||
}
|
}
|
||||||
else throw IllegalArgumentException("Unknown IME Operation mode: ${ime.config.mode}")
|
|
||||||
}
|
}
|
||||||
else char.toCodePoints()
|
else if (keycodes.contains(Input.Keys.RIGHT)) {
|
||||||
|
endComposing()
|
||||||
|
|
||||||
// printdbg(this, "textinput codepoints: ${codepoints.map { it.toString(16) }.joinToString()}")
|
if (cursorX < textbuf.size) {
|
||||||
|
cursorX += 1
|
||||||
if (!maxLen.exceeds(textbuf, codepoints)) {
|
cursorDrawX = App.fontGame.getWidth(CodepointSequence(textbuf.subList(0, cursorX)))
|
||||||
textbuf.addAll(cursorX, codepoints)
|
tryCursorBack()
|
||||||
|
}
|
||||||
moveCursorToEnd(codepoints.size)
|
|
||||||
}
|
}
|
||||||
|
else if (keycodes.containsSome(Input.Keys.ENTER, Input.Keys.NUMPAD_ENTER)) {
|
||||||
|
endComposing()
|
||||||
|
|
||||||
|
// println("END COMPOSING!!")
|
||||||
|
}
|
||||||
|
// accept:
|
||||||
|
// - literal "<"
|
||||||
|
// - keysymbol that does not start with "<" (not always has length of 1 because UTF-16)
|
||||||
|
else if (char != null && char.length > 0 && char[0].code >= 32 && (char == "<" || !char.startsWith("<"))) {
|
||||||
|
val shiftin = keycodes.containsSome(Input.Keys.SHIFT_LEFT, Input.Keys.SHIFT_RIGHT)
|
||||||
|
val altgrin = keycodes.contains(Input.Keys.ALT_RIGHT) || keycodes.containsAll(Input.Keys.ALT_LEFT, Input.Keys.CONTROL_LEFT)
|
||||||
|
|
||||||
|
val codepoints = if (ime != null) {
|
||||||
|
if (ime.config.mode == TerrarumIMEMode.CANDIDATES) {
|
||||||
|
val newStatus = ime.acceptChar(headkey, shiftin, altgrin, char)
|
||||||
|
candidates = newStatus.first.map { CodepointSequence(it.toCodePoints()) }
|
||||||
|
|
||||||
|
newStatus.second.toCodePoints()
|
||||||
|
}
|
||||||
|
else if (ime.config.mode == TerrarumIMEMode.REWRITE) {
|
||||||
|
candidates = listOf()
|
||||||
|
val op = ime.acceptChar(headkey, shiftin, altgrin, char)
|
||||||
|
|
||||||
|
// printdbg(this, "delcount: ${op.first[0].toInt()}, rewrite: '${op.second}'")
|
||||||
|
|
||||||
|
repeat(op.first[0].toInt()) {
|
||||||
|
if (textbuf.isNotEmpty()) {
|
||||||
|
// printdbg(this, "<del 1>")
|
||||||
|
inputBackspaceOnce(3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
op.second.toCodePoints()
|
||||||
|
}
|
||||||
|
else throw IllegalArgumentException("Unknown IME Operation mode: ${ime.config.mode}")
|
||||||
|
}
|
||||||
|
else char.toCodePoints()
|
||||||
|
|
||||||
|
// printdbg(this, "textinput codepoints: ${codepoints.map { it.toString(16) }.joinToString()}")
|
||||||
|
|
||||||
|
if (!maxLen.exceeds(textbuf, codepoints)) {
|
||||||
|
textbuf.addAll(cursorX, codepoints)
|
||||||
|
|
||||||
|
moveCursorToEnd(codepoints.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// don't put innards of tryCursorBack/Forward here -- you absolutely don't want that behaviour
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't put innards of tryCursorBack/Forward here -- you absolutely don't want that behaviour
|
|
||||||
}
|
}
|
||||||
}
|
catch (e: IndexOutOfBoundsException) {
|
||||||
catch (e: IndexOutOfBoundsException) {
|
e.printStackTrace()
|
||||||
e.printStackTrace()
|
}
|
||||||
}
|
catch (e: NullPointerException) {
|
||||||
catch (e: NullPointerException) {
|
e.printStackTrace()
|
||||||
e.printStackTrace()
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (textbuf.size == 0) {
|
if (textbuf.size == 0) {
|
||||||
currentPlaceholderText = CodepointSequence(placeholder().toCodePoints())
|
currentPlaceholderText = CodepointSequence(placeholder().toCodePoints())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (oldActive) { // just became deactivated
|
else if (oldActive) { // just became deactivated
|
||||||
|
|||||||
Reference in New Issue
Block a user