-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathUIMultilineTextInput.kt
185 lines (151 loc) · 6.67 KB
/
UIMultilineTextInput.kt
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
178
179
180
181
182
183
184
185
package gg.essential.elementa.components.input
import gg.essential.elementa.constraints.HeightConstraint
import gg.essential.elementa.dsl.coerceAtMost
import gg.essential.elementa.dsl.pixels
import gg.essential.elementa.dsl.width
import gg.essential.elementa.utils.splitStringToWidthTruncated
import gg.essential.universal.UKeyboard
import gg.essential.universal.UMatrixStack
import java.awt.Color
class UIMultilineTextInput @JvmOverloads constructor(
placeholder: String = "",
shadow: Boolean = true,
selectionBackgroundColor: Color = Color.WHITE,
selectionForegroundColor: Color = Color(64, 139, 229),
allowInactiveSelection: Boolean = false,
inactiveSelectionBackgroundColor: Color = Color(176, 176, 176),
inactiveSelectionForegroundColor: Color = Color.WHITE,
cursorColor: Color = Color.WHITE
) : AbstractTextInput(
placeholder,
shadow,
selectionBackgroundColor,
selectionForegroundColor,
allowInactiveSelection,
inactiveSelectionBackgroundColor,
inactiveSelectionForegroundColor,
cursorColor
) {
private var maxHeight: HeightConstraint? = null
fun setMaxHeight(maxHeight: HeightConstraint) = apply {
this.maxHeight = maxHeight
}
fun setMaxLines(maxLines: Int) = apply {
this.maxHeight = (lineHeight * maxLines).pixels()
}
override fun getText() = textualLines.joinToString("\n") { it.text }
override fun textToLines(text: String): List<String> {
return text.split('\n')
}
override fun scrollIntoView(pos: LinePosition) {
val visualPos = pos.toVisualPos()
val visualLineOffset = visualPos.line * -lineHeight
if (targetVerticalScrollingOffset < visualLineOffset) {
targetVerticalScrollingOffset = visualLineOffset
} else if (visualLineOffset - lineHeight < targetVerticalScrollingOffset - getHeight()) {
targetVerticalScrollingOffset += visualLineOffset - lineHeight - (targetVerticalScrollingOffset - getHeight())
}
}
override fun recalculateDimensions() {
if (maxHeight == null)
return
setHeight((lineHeight * visualLines.size).pixels().coerceAtMost(maxHeight!!))
}
override fun onEnterPressed() {
if (UKeyboard.isShiftKeyDown()) {
commitTextAddition("\n")
updateAction(getText())
} else {
activateAction(getText())
}
}
override fun draw(matrixStack: UMatrixStack) {
beforeDraw(matrixStack)
val textScale = getTextScale()
if (!active && !hasText()) {
val textToDraw = splitStringToWidthTruncated(placeholder, getWidth(), textScale, 1)[0]
drawUnselectedText(matrixStack, textToDraw, getLeft(), 0)
return super.draw(matrixStack)
}
if (hasSelection()) {
cursorComponent.hide(instantly = true)
} else if (active) {
cursorComponent.unhide()
val (cursorPosX, cursorPosY) = cursor.toScreenPos()
cursorComponent.setX((cursorPosX ).pixels())
cursorComponent.setY((cursorPosY ).pixels())
}
val (selectionStart, selectionEnd) = getSelection()
for ((i, visualLine) in visualLines.withIndex()) {
val topOffset = (lineHeight * i * getTextScale()) + verticalScrollingOffset
if (topOffset < -lineHeight * getTextScale() || topOffset > getHeight() + lineHeight * getTextScale())
continue
if (!hasSelection() || i < selectionStart.line || i > selectionEnd.line) {
drawUnselectedText(matrixStack, visualLine.text, getLeft(), i)
} else {
val startText = when {
i == selectionStart.line && selectionStart.column > 0 -> {
visualLine.text.substring(0, selectionStart.column)
}
else -> ""
}
val selectedText = when {
selectionStart.line == selectionEnd.line -> visualLine.text.substring(
selectionStart.column,
selectionEnd.column
)
i > selectionStart.line && i < selectionEnd.line -> visualLine.text
i == selectionStart.line -> visualLine.text.substring(selectionStart.column)
i == selectionEnd.line -> visualLine.text.substring(0, selectionEnd.column)
else -> ""
}
val endText = when {
i == selectionEnd.line && selectionEnd.column < visualLines[i].length -> {
visualLine.text.substring(selectionEnd.column)
}
else -> ""
}
val startTextWidth = startText.width(textScale)
val selectedTextWidth = selectedText.width(textScale)
val newlinePadding = if (i < selectionEnd.line) ' '.width(textScale) else 0f
if (startText.isNotEmpty())
drawUnselectedText(matrixStack, startText, getLeft(), i)
if (selectedText.isNotEmpty() || newlinePadding != 0f) {
drawSelectedText(
matrixStack,
selectedText,
getLeft() + startTextWidth,
getLeft() + startTextWidth + selectedTextWidth + newlinePadding,
i
)
}
if (endText.isNotEmpty())
drawUnselectedText(matrixStack, endText, getLeft() + startTextWidth + selectedTextWidth, i)
}
}
super.draw(matrixStack, )
}
override fun screenPosToVisualPos(x: Float, y: Float): LinePosition {
val realY = y - verticalScrollingOffset
if (realY <= 0)
return LinePosition(0, 0, isVisual = true)
val line = (realY / (lineHeight * getTextScale())).toInt()
if (line > visualLines.lastIndex)
return LinePosition(visualLines.lastIndex, visualLines.last().text.length, isVisual = true)
val text = visualLines[line].text
var column = 0
var currWidth = 0f
if (x <= 0)
return LinePosition(line, 0, isVisual = true)
if (x >= getWidth())
return LinePosition(line, visualLines[line].text.length, isVisual = true)
for (char in text.toCharArray()) {
val charWidth = char.width(getTextScale())
if (currWidth + (charWidth / 2) >= x)
return LinePosition(line, column, isVisual = true)
currWidth += charWidth
column++
}
return LinePosition(line, column, isVisual = true)
}
}