wip: chat history

This commit is contained in:
Bixilon 2022-02-11 20:28:50 +01:00
parent 417408b2ea
commit c496f5e978
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -47,6 +47,8 @@ class ChatElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedElem
private val chatProfile = profile.chat
private val messages = TextFlowElement(guiRenderer, 20000).apply { parent = this@ChatElement }
private val input = TextInputElement(guiRenderer, maxLength = connection.version.maxChatMessageSize).apply { parent = this@ChatElement }
private val history: MutableList<String> = mutableListOf()
private var historyIndex = -1
private var active = false
set(value) {
field = value
@ -146,6 +148,11 @@ class ChatElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedElem
connection.util.sendChatMessage(value)
}
input.value = ""
if (history.lastOrNull() != value) {
// ToDo: Improve history
history += value
}
historyIndex = history.size
guiRenderer.gui.pop()
}
@ -171,6 +178,29 @@ class ChatElement(guiRenderer: GUIRenderer) : Element(guiRenderer), LayoutedElem
messages.scrollOffset--
return
}
KeyCodes.KEY_UP -> {
if (historyIndex <= 0) {
return
}
val size = history.size
if (historyIndex > size) {
historyIndex = size
}
historyIndex--
input.value = history[historyIndex]
}
KeyCodes.KEY_DOWN -> {
val size = history.size
historyIndex++
if (historyIndex > size) {
return
}
if (historyIndex == size) {
input.value = ""
return
}
input.value = history[historyIndex]
}
else -> {}
}
}