summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt b/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt
index d7e6ff8e1..6ae2342b6 100644
--- a/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt
+++ b/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt
@@ -8,6 +8,7 @@ import at.hannibal2.skyhanni.utils.StringUtils.insert
import kotlinx.coroutines.runBlocking
import net.minecraft.client.settings.KeyBinding
import org.lwjgl.input.Keyboard
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
class TextInput {
@@ -22,16 +23,35 @@ class TextInput {
}
}.replace("(?<!§.\\|)§(?!.\\|§.)".toRegex(), "&&")
+ fun editTextWithAlwaysCarriage() = textBox.let {
+ with(carriage) {
+ if (this == null) it.plus('|')
+ else it.insert(this, '|')
+ }
+ }.replace("§", "&&")
+
fun finalText() = textBox.replace("&&", "§")
- fun makeActive() = Companion.activate(this)
- fun disable() = Companion.disable()
+ fun makeActive() = if (!isActive) Companion.activate(this) else Unit
+ fun disable() = if (isActive) Companion.disable() else Unit
fun handle() = Companion.handleTextInput()
fun clear() {
textBox = ""
carriage = null
}
+ val isActive get() = Companion.activeInstance == this
+
+ private val updateEvents = mutableMapOf<Int, (TextInput) -> Unit>()
+
+ fun registerToEvent(key: Int, event: (TextInput) -> Unit) {
+ updateEvents[key] = event
+ }
+
+ fun removeFromEvent(key: Int) {
+ updateEvents.remove(key)
+ }
+
companion object {
private var activeInstance: TextInput? = null
@@ -51,6 +71,13 @@ class TextInput {
}
}
+ fun onGuiInput(ci: CallbackInfo) {
+ if (activeInstance != null) {
+ ci.cancel()
+ return
+ }
+ }
+
private var timeSinceKeyEvent = 0L
private var carriage
@@ -65,6 +92,13 @@ class TextInput {
activeInstance?.textBox = value
}
+ private fun updated() {
+ with(activeInstance) {
+ if (this == null) return
+ this.updateEvents.forEach { (_, it) -> it(this) }
+ }
+ }
+
private fun handleTextInput() {
if (KeyboardManager.isCopyingKeysDown()) {
OSUtils.copyToClipboard(textBox)
@@ -73,6 +107,7 @@ class TextInput {
if (KeyboardManager.isPastingKeysDown()) {
runBlocking {
textBox = OSUtils.readFromClipboard() ?: return@runBlocking
+ updated()
}
return
}
@@ -96,6 +131,7 @@ class TextInput {
} else {
textBox.dropLast(1)
}
+ updated()
return
}
@@ -122,6 +158,7 @@ class TextInput {
textBox + char
}
}
+ updated()
}
private fun moveCarriageRight(carriage: Int) = carriage + 1