1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
package at.hannibal2.skyhanni.data.model
import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyClicked
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyHeld
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.OSUtils
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 {
var textBox: String = ""
private var carriage: Int? = null
fun editText(textColor: LorenzColor = LorenzColor.WHITE, carriageColor: LorenzColor = LorenzColor.GREEN) = textBox.let {
with(carriage) {
if (this == null) it
else it.insert(this, "${carriageColor.getChatColor()}|${textColor.getChatColor()}")
}
}.replace("(?<!§.\\|)§(?!.\\|§.)".toRegex(), "&&")
fun editTextWithAlwaysCarriage() = textBox.let {
with(carriage) {
if (this == null) it.plus('|')
else it.insert(this, '|')
}
}.replace("§", "&&")
fun finalText() = textBox.replace("&&", "§")
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
fun activate(instance: TextInput) {
activeInstance = instance
timeSinceKeyEvent = Keyboard.getEventNanoseconds()
}
fun disable() {
activeInstance = null
}
@Suppress("UnusedParameter")
fun onMinecraftInput(keyBinding: KeyBinding, cir: CallbackInfoReturnable<Boolean>) {
if (activeInstance != null) {
cir.returnValue = false
return
}
}
fun onGuiInput(ci: CallbackInfo) {
if (activeInstance != null) {
if (Keyboard.KEY_ESCAPE.isKeyHeld()) {
disable()
} else {
ci.cancel()
}
return
}
}
private var timeSinceKeyEvent = 0L
private var carriage
get() = activeInstance?.carriage
set(value) {
activeInstance?.carriage = value
}
private var textBox
get() = activeInstance?.textBox.orEmpty()
set(value) {
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)
return
}
if (KeyboardManager.isPastingKeysDown()) {
runBlocking {
textBox = OSUtils.readFromClipboard()?.take(2024) ?: return@runBlocking
updated()
}
return
}
val carriage = carriage
if (Keyboard.KEY_LEFT.isKeyClicked()) {
this.carriage = carriage?.moveCarriageLeft() ?: (textBox.length - 1)
return
}
if (Keyboard.KEY_RIGHT.isKeyClicked()) {
this.carriage = when {
carriage == null -> null
(carriage >= textBox.length - 1) -> null
else -> moveCarriageRight(carriage)
}
return
}
if (Keyboard.KEY_DELETE.isKeyClicked()) { // Does not work for some reason
if (carriage != null) {
textBox.removeRange(carriage, carriage + 1)
} else {
textBox.dropLast(1)
}
updated()
return
}
if (timeSinceKeyEvent == Keyboard.getEventNanoseconds()) return
timeSinceKeyEvent = Keyboard.getEventNanoseconds()
val char = Keyboard.getEventCharacter()
textBox = when (char) {
Char(0) -> return
'\b' -> if (carriage != null) {
if (carriage == 0) {
textBox.substring(1)
} else {
this.carriage = carriage.minus(1)
textBox.removeRange(carriage - 1, carriage)
}
} else {
textBox.dropLast(1)
}
else -> if (carriage != null) {
this.carriage = carriage + 1
textBox.insert(carriage, char)
} else {
textBox + char
}
}
updated()
}
private fun moveCarriageRight(carriage: Int) = carriage + 1
private fun Int.moveCarriageLeft(): Int = when {
this > 0 -> this - 1
else -> 0
}
}
}
|