aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/model/TextInput.kt
blob: f1a5001ad67cc454fb103bb6fe7a5dc692fdd6c1 (plain)
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
package at.hannibal2.skyhanni.data.model

import at.hannibal2.skyhanni.utils.KeyboardManager
import at.hannibal2.skyhanni.utils.KeyboardManager.isKeyClicked
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.CallbackInfoReturnable

class TextInput {

    var textBox: String = ""
    private var carriage: Int? = null

    fun editText() = textBox.let {
        with(carriage) {
            if (this == null) it
            else it.insert(this, '|')
        }
    }.replace("§", "&&")

    fun finalText() = textBox.replace("&&", "§")

    fun makeActive() = Companion.activate(this)
    fun disable() = Companion.disable()
    fun handle() = Companion.handleTextInput()
    fun clear() {
        textBox = ""
        carriage = null
    }

    companion object {
        private var activeInstance: TextInput? = null

        fun activate(instance: TextInput) {
            activeInstance = instance
            timeSinceKeyEvent = Keyboard.getEventNanoseconds()
        }

        fun disable() {
            activeInstance = null
        }

        fun onMinecraftInput(keyBinding: KeyBinding, cir: CallbackInfoReturnable<Boolean>) {
            if (activeInstance != null) {
                cir.returnValue = false
                return
            }
        }

        private var timeSinceKeyEvent = 0L

        private var carriage
            get() = activeInstance?.carriage
            set(value) {
                activeInstance?.carriage = value
            }

        private var textBox
            get() = activeInstance?.textBox ?: ""
            set(value) {
                activeInstance?.textBox = value
            }

        private fun handleTextInput() {
            if (KeyboardManager.isCopyingKeysDown()) {
                OSUtils.copyToClipboard(textBox)
                return
            }
            if (KeyboardManager.isPastingKeysDown()) {
                runBlocking {
                    textBox = OSUtils.readFromClipboard() ?: return@runBlocking
                }
                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)
                }
                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
                }
            }
        }

        private fun moveCarriageRight(carriage: Int) = carriage + 1

        private fun Int.moveCarriageLeft(): Int = when {
            this > 0 -> this - 1
            else -> 0
        }
    }
}