-
Notifications
You must be signed in to change notification settings - Fork 30
/
UIWrappedText.kt
211 lines (179 loc) · 7.88 KB
/
UIWrappedText.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package gg.essential.elementa.components
import gg.essential.elementa.UIComponent
import gg.essential.elementa.UIConstraints
import gg.essential.elementa.constraints.CenterConstraint
import gg.essential.elementa.dsl.basicHeightConstraint
import gg.essential.elementa.dsl.width
import gg.essential.elementa.state.BasicState
import gg.essential.elementa.state.MappedState
import gg.essential.elementa.state.State
import gg.essential.elementa.state.pixels
import gg.essential.elementa.utils.getStringSplitToWidth
import gg.essential.elementa.utils.splitStringToWidthTruncated
import gg.essential.universal.UGraphics
import gg.essential.universal.UMatrixStack
import java.awt.Color
/**
* Simple text component that draws its given `text` at the scale determined by
* this component's width & height constrains.
*/
open class UIWrappedText @JvmOverloads constructor(
text: State<String>,
shadow: State<Boolean> = BasicState(true),
shadowColor: State<Color?> = BasicState(null),
private var centered: Boolean = false,
/**
* Keeps the rendered text within the bounds of the component,
* inserting an ellipsis ("...") if text is trimmed
*/
private val trimText: Boolean = false,
private val lineSpacing: Float = 9f,
private val trimmedTextSuffix: String = "..."
) : UIComponent() {
@JvmOverloads constructor(
text: String = "",
shadow: Boolean = true,
shadowColor: Color? = null,
centered: Boolean = false,
/**
* Keeps the rendered text within the bounds of the component,
* inserting an ellipsis ("...") if text is trimmed
*/
trimText: Boolean = false,
lineSpacing: Float = 9f,
trimmedTextSuffix: String = "..."
) : this(BasicState(text), BasicState(shadow), BasicState(shadowColor), centered, trimText, lineSpacing, trimmedTextSuffix)
private val textState: MappedState<String, String> = text.map { it } // extra map so we can easily rebind it
private val shadowState: MappedState<Boolean, Boolean> = shadow.map { it }
private val shadowColorState: MappedState<Color?, Color?> = shadowColor.map { it }
private val textScaleState = constraints.asState { getTextScale() }
private val fontProviderState = constraints.asState { fontProvider }
private var textWidthState = textState.zip(textScaleState.zip(fontProviderState)).map { (text, opts) ->
val (textScale, fontProvider) = opts
text.width(textScale, fontProvider) / textScale
}
private val charWidth = UGraphics.getCharWidth('x')
/** Guess on whether we should be trying to center or top-align this component. See [BELOW_LINE_HEIGHT]. */
private val verticallyCenteredState = constraints.asState { y is CenterConstraint }
private fun <T> UIConstraints.asState(selector: UIConstraints.() -> T) = BasicState(selector(constraints)).also {
constraints.addObserver { _, _ -> it.set(selector(constraints)) }
}
/**
* Balances out space required below the line by adding empty space above the first one.
* Also, if there are no shadows, the last line can be shorter so it looks more centered overall.
*/
private val extraHeightState = fontProviderState.zip(verticallyCenteredState).zip(shadowState).map { (opts, shadow) ->
val (fontProvider, verticallyCentered) = opts
(if (verticallyCentered) fontProvider.getBelowLineHeight() else 0f) + (if (shadow) 0f else - fontProvider.getShadowHeight())
}
init {
setWidth(textWidthState.pixels())
setHeight(basicHeightConstraint {
val fontProvider = super.getFontProvider()
val lines = getStringSplitToWidth(
getText(),
getWidth(),
getTextScale(),
ensureSpaceAtEndOfLines = false,
fontProvider = fontProvider,
)
if (lines.isEmpty()) {
return@basicHeightConstraint 0f
}
// The height of the last line of text should be equal the size of that text
// independent of the lineSpacing property. Otherwise, when lineSpacing is greater
// than the text height the component's size will be larger than the area the text
// is rendered
((lines.size - 1) * lineSpacing + extraHeightState.get() // All lines but last
+ (fontProvider.getBaseLineHeight() + fontProvider.getBelowLineHeight() + fontProvider.getShadowHeight()) // Last line
) * getTextScale()
})
}
fun bindText(newTextState: State<String>) = apply {
textState.rebind(newTextState)
}
fun bindShadow(newShadowState: State<Boolean>) = apply {
shadowState.rebind(newShadowState)
}
fun bindShadowColor(newShadowColorState: State<Color?>) = apply {
shadowColorState.rebind(newShadowColorState)
}
fun getText() = textState.get()
fun setText(text: String) = apply { textState.set(text) }
fun getShadow() = shadowState.get()
fun setShadow(shadow: Boolean) = apply { shadowState.set(shadow) }
@Deprecated("Wrong return type", level = DeprecationLevel.HIDDEN)
@JvmName("getShadowColor")
fun getShadowColorState(): State<Color?> = shadowColorState
fun getShadowColor(): Color? = shadowColorState.get()
fun setShadowColor(shadowColor: Color?) = apply { shadowColorState.set(shadowColor) }
/**
* Returns the text width if no scale is applied to the text
*/
fun getTextWidth() = textWidthState.get()
override fun draw(matrixStack: UMatrixStack) {
beforeDrawCompat(matrixStack)
val textScale = getTextScale()
val x = getLeft()
val y = getTop() + (if (verticallyCenteredState.get()) fontProviderState.get().getBelowLineHeight() * textScale else 0f)
val width = getWidth()
val color = getColor()
// We aren't visible, don't draw
if (color.alpha <= 10) {
return super.draw(matrixStack)
}
if (width / textScale <= charWidth) {
// If we are smaller than a char, we can't physically split this string into
// "width" strings, so we'll prefer a no-op to an error.
return super.draw(matrixStack)
}
UGraphics.enableBlend()
val lines = if (trimText) {
splitStringToWidthTruncated(
textState.get(),
width,
textScale,
getMaxLines(),
ensureSpaceAtEndOfLines = false,
fontProvider = getFontProvider(),
trimmedTextSuffix = trimmedTextSuffix
)
} else {
getStringSplitToWidth(
textState.get(),
width,
textScale,
ensureSpaceAtEndOfLines = false,
fontProvider = getFontProvider()
)
}.map { it.trimEnd() }
val shadow = shadowState.get()
val shadowColor = shadowColorState.get()
lines.forEachIndexed { i, line ->
val xOffset = if (centered) {
(width - line.width(textScale, getFontProvider())) / 2f
} else 0f
getFontProvider().drawString(
matrixStack,
line,
color,
x + xOffset,
y + i * lineSpacing * textScale,
10f,
textScale,
shadow,
if (shadow) shadowColor else null,
)
}
super.draw(matrixStack)
}
private fun getMaxLines(): Int {
val fontProvider = getFontProvider()
val height = getHeight() / getTextScale() - extraHeightState.get()
val baseLineHeight = fontProvider.getBaseLineHeight() + fontProvider.getBelowLineHeight() + fontProvider.getShadowHeight()
if (height < baseLineHeight) {
return 0
}
return 1 + ((height - baseLineHeight) / lineSpacing).toInt()
}
}