1234
|
1 type
|
|
2 Textbox* = object
|
|
3 font*: Font
|
|
4 maxLen*: int # maximum amount of characters that will be rendered
|
|
5 maxWidth: float32 = 0 # if set, will cause automatic word breaks at maxWidth
|
|
6 # properties:
|
|
7 text: seq[Rune]
|
|
8 horizontalAlignment: HorizontalAlignment = Center
|
|
9 verticalAlignment: VerticalAlignment = Center
|
|
10 # management/internal:
|
|
11 dirtyGeometry: bool # is true if any of the attributes changed
|
|
12 dirtyShaderdata: bool # is true if any of the attributes changed
|
|
13 processedText: seq[Rune] # used to store processed (word-wrapper) text to preserve original
|
|
14 lastRenderedText: seq[Rune] # stores the last rendered text, to prevent unnecessary updates
|
|
15
|
|
16 # rendering data
|
1236
|
17 position*: GPUArray[Vec3f, VertexBuffer]
|
|
18 uv*: GPUArray[Vec2f, VertexBuffer]
|
|
19 indices*: GPUArray[uint16, IndexBuffer]
|
|
20 shaderdata*: DescriptorSet[TextboxDescriptorSet]
|
1234
|
21
|
1236
|
22 func `$`*(textbox: Textbox): string =
|
|
23 "\"" & $textbox.text[0 ..< min(textbox.text.len, 16)] & "\""
|
1234
|
24
|
1236
|
25 proc RefreshShaderdata(textbox: Textbox) =
|
|
26 textbox.shaderdata.data.textbox.UpdateGPUBuffer()
|
1234
|
27
|
1236
|
28 proc RefreshGeometry(textbox: var Textbox) =
|
1234
|
29 # pre-calculate text-width
|
|
30 var width = 0'f32
|
|
31 var lineWidths: seq[float32]
|
1236
|
32 for i in 0 ..< textbox.processedText.len:
|
|
33 if textbox.processedText[i] == NEWLINE:
|
1234
|
34 lineWidths.add width
|
|
35 width = 0'f32
|
|
36 else:
|
1236
|
37 if not (i == textbox.processedText.len - 1 and textbox.processedText[i].isWhiteSpace):
|
|
38 width += textbox.font.glyphs[textbox.processedText[i]].advance
|
|
39 if i < textbox.processedText.len - 1:
|
|
40 width += textbox.font.kerning[(textbox.processedText[i], textbox.processedText[i + 1])]
|
1234
|
41 lineWidths.add width
|
1236
|
42 var height = float32(lineWidths.len - 1) * textbox.font.lineAdvance + textbox.font.capHeight
|
1234
|
43 if lineWidths[^1] == 0 and lineWidths.len > 1:
|
|
44 height -= 1
|
|
45
|
1236
|
46 let anchorY = (case textbox.verticalAlignment
|
1234
|
47 of Top: 0'f32
|
|
48 of Center: height / 2
|
1236
|
49 of Bottom: height) - textbox.font.capHeight
|
1234
|
50
|
|
51 var
|
|
52 offsetX = 0'f32
|
|
53 offsetY = 0'f32
|
|
54 lineIndex = 0
|
1236
|
55 anchorX = case textbox.horizontalAlignment
|
1234
|
56 of Left: 0'f32
|
|
57 of Center: lineWidths[lineIndex] / 2
|
|
58 of Right: lineWidths[lineIndex]
|
1236
|
59 for i in 0 ..< textbox.maxLen:
|
1234
|
60 let vertexOffset = i * 4
|
1236
|
61 if i < textbox.processedText.len:
|
|
62 if textbox.processedText[i] == Rune('\n'):
|
1234
|
63 offsetX = 0
|
1236
|
64 offsetY += textbox.font.lineAdvance
|
|
65 textbox.position.data[vertexOffset + 0] = NewVec3f()
|
|
66 textbox.position.data[vertexOffset + 1] = NewVec3f()
|
|
67 textbox.position.data[vertexOffset + 2] = NewVec3f()
|
|
68 textbox.position.data[vertexOffset + 3] = NewVec3f()
|
1234
|
69 inc lineIndex
|
1236
|
70 anchorX = case textbox.horizontalAlignment
|
1234
|
71 of Left: 0'f32
|
|
72 of Center: lineWidths[lineIndex] / 2
|
|
73 of Right: lineWidths[lineIndex]
|
|
74 else:
|
|
75 let
|
1236
|
76 glyph = textbox.font.glyphs[textbox.processedText[i]]
|
1234
|
77 left = offsetX + glyph.leftOffset
|
|
78 right = offsetX + glyph.leftOffset + glyph.dimension.x
|
|
79 top = offsetY + glyph.topOffset
|
|
80 bottom = offsetY + glyph.topOffset + glyph.dimension.y
|
|
81
|
1236
|
82 textbox.position.data[vertexOffset + 1] = NewVec3f(left - anchorX, bottom - anchorY)
|
|
83 textbox.position.data[vertexOffset + 0] = NewVec3f(left - anchorX, top - anchorY)
|
|
84 textbox.position.data[vertexOffset + 3] = NewVec3f(right - anchorX, top - anchorY)
|
|
85 textbox.position.data[vertexOffset + 2] = NewVec3f(right - anchorX, bottom - anchorY)
|
1234
|
86
|
1236
|
87 textbox.uv.data[vertexOffset + 0] = glyph.uvs[0]
|
|
88 textbox.uv.data[vertexOffset + 1] = glyph.uvs[1]
|
|
89 textbox.uv.data[vertexOffset + 2] = glyph.uvs[2]
|
|
90 textbox.uv.data[vertexOffset + 3] = glyph.uvs[3]
|
1234
|
91
|
|
92 offsetX += glyph.advance
|
1236
|
93 if i < textbox.processedText.len - 1:
|
|
94 offsetX += textbox.font.kerning[(textbox.processedText[i], textbox.processedText[i + 1])]
|
1234
|
95 else:
|
1236
|
96 textbox.position.data[vertexOffset + 0] = NewVec3f()
|
|
97 textbox.position.data[vertexOffset + 1] = NewVec3f()
|
|
98 textbox.position.data[vertexOffset + 2] = NewVec3f()
|
|
99 textbox.position.data[vertexOffset + 3] = NewVec3f()
|
|
100 textbox.lastRenderedText = textbox.processedText
|
|
101
|
|
102 func text*(textbox: Textbox): seq[Rune] =
|
|
103 textbox.text
|
1234
|
104
|
1236
|
105 proc `text=`*(textbox: var Textbox, newText: seq[Rune]) =
|
|
106 if newText[0 ..< min(newText.len, textbox.maxLen)] == textbox.text:
|
|
107 return
|
1234
|
108
|
1236
|
109 textbox.text = newText[0 ..< min(newText.len, textbox.maxLen)]
|
1234
|
110
|
1236
|
111 textbox.processedText = textbox.text
|
|
112 if textbox.maxWidth > 0:
|
|
113 textbox.processedText = WordWrapped(
|
|
114 textbox.processedText,
|
|
115 textbox.font[],
|
|
116 textbox.maxWidth / textbox.shaderdata.data.textbox.data.scale,
|
1234
|
117 )
|
|
118
|
1236
|
119 proc `text=`*(textbox: var Textbox, newText: string) =
|
|
120 `text=`(textbox, newText.toRunes)
|
|
121
|
|
122 proc Color*(textbox: Textbox): Vec4f =
|
|
123 textbox.shaderdata.data.textbox.data.color
|
1234
|
124
|
1236
|
125 proc `Color=`*(textbox: var Textbox, value: Vec4f) =
|
|
126 if textbox.shaderdata.data.textbox.data.color != value:
|
|
127 textbox.dirtyShaderdata = true
|
|
128 textbox.shaderdata.data.textbox.data.color = value
|
1234
|
129
|
1236
|
130 proc Scale*(textbox: Textbox): float32 =
|
|
131 textbox.shaderdata.data.textbox.data.scale
|
1234
|
132
|
1236
|
133 proc `Scale=`*(textbox: var Textbox, value: float32) =
|
|
134 if textbox.shaderdata.data.textbox.data.scale != value:
|
|
135 textbox.dirtyShaderdata = true
|
|
136 textbox.shaderdata.data.textbox.data.scale = value
|
|
137
|
|
138 proc AspectRatio*(textbox: Textbox): float32 =
|
|
139 textbox.shaderdata.data.textbox.data.aspectratio
|
1234
|
140
|
1236
|
141 proc `AspectRatio=`*(textbox: var Textbox, value: float32) =
|
|
142 if textbox.shaderdata.data.textbox.data.aspectratio != value:
|
|
143 textbox.dirtyShaderdata = true
|
|
144 textbox.shaderdata.data.textbox.data.aspectratio = value
|
1234
|
145
|
1236
|
146 proc Position*(textbox: Textbox): Vec3f =
|
|
147 textbox.shaderdata.data.textbox.data.position
|
1234
|
148
|
1236
|
149 proc `Position=`*(textbox: var Textbox, value: Vec3f) =
|
|
150 if textbox.shaderdata.data.textbox.data.position != value:
|
|
151 textbox.dirtyShaderdata = true
|
|
152 textbox.shaderdata.data.textbox.data.position = value
|
1234
|
153
|
1236
|
154 proc horizontalAlignment*(textbox: Textbox): HorizontalAlignment =
|
|
155 textbox.horizontalAlignment
|
|
156 proc `horizontalAlignment=`*(textbox: var Textbox, value: HorizontalAlignment) =
|
|
157 if value != textbox.horizontalAlignment:
|
|
158 textbox.horizontalAlignment = value
|
|
159 textbox.dirtyGeometry = true
|
1234
|
160
|
1236
|
161 proc verticalAlignment*(textbox: Textbox): VerticalAlignment =
|
|
162 textbox.verticalAlignment
|
|
163 proc `verticalAlignment=`*(textbox: var Textbox, value: VerticalAlignment) =
|
|
164 if value != textbox.verticalAlignment:
|
|
165 textbox.verticalAlignment = value
|
|
166 textbox.dirtyGeometry = true
|
|
167
|
|
168 proc Refresh*(textbox: var Textbox, aspectratio: float32) =
|
|
169 `AspectRatio=`(textbox, aspectratio)
|
1234
|
170
|
1236
|
171 if textbox.dirtyShaderdata:
|
|
172 textbox.RefreshShaderdata()
|
|
173 textbox.dirtyShaderdata = false
|
|
174
|
|
175 if textbox.dirtyGeometry or textbox.processedText != textbox.lastRenderedText:
|
|
176 textbox.RefreshGeometry()
|
|
177 textbox.dirtyGeometry = false
|
|
178
|
|
179 proc Render*(textbox: Textbox, commandbuffer: VkCommandBuffer, pipeline: Pipeline, currentFiF: int) =
|
1234
|
180 WithBind(commandbuffer, (textbox.shaderdata, ), pipeline, currentFiF):
|
1236
|
181 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = textbox)
|
1234
|
182
|
1236
|
183 proc InitTextbox*[T: string | seq[Rune]](
|
1234
|
184 renderdata: var RenderData,
|
|
185 descriptorSetLayout: VkDescriptorSetLayout,
|
|
186 font: Font,
|
1236
|
187 text: T = default(T),
|
1234
|
188 scale: float32 = 1,
|
|
189 position: Vec3f = NewVec3f(),
|
|
190 color: Vec4f = NewVec4f(0, 0, 0, 1),
|
|
191 maxLen: int = text.len,
|
|
192 verticalAlignment: VerticalAlignment = Center,
|
|
193 horizontalAlignment: HorizontalAlignment = Center,
|
|
194 maxWidth = 0'f32
|
|
195 ): Textbox =
|
|
196
|
|
197 result = Textbox(
|
|
198 maxLen: maxLen,
|
|
199 font: font,
|
|
200 dirtyGeometry: true,
|
1236
|
201 dirtyShaderdata: true,
|
1234
|
202 horizontalAlignment: horizontalAlignment,
|
|
203 verticalAlignment: verticalAlignment,
|
|
204 maxWidth: maxWidth,
|
|
205 position: asGPUArray(newSeq[Vec3f](int(maxLen * 4)), VertexBuffer),
|
|
206 uv: asGPUArray(newSeq[Vec2f](int(maxLen * 4)), VertexBuffer),
|
|
207 indices: asGPUArray(newSeq[uint16](int(maxLen * 6)), IndexBuffer),
|
|
208 shaderdata: asDescriptorSet(
|
|
209 TextboxDescriptorSet(
|
|
210 textbox: asGPUValue(TextboxData(
|
|
211 scale: scale,
|
|
212 position: position,
|
|
213 color: color,
|
1236
|
214 aspectratio: 1,
|
1234
|
215 ), UniformBufferMapped),
|
|
216 fontAtlas: font.fontAtlas
|
|
217 )
|
|
218 )
|
|
219 )
|
|
220
|
|
221 for i in 0 ..< maxLen:
|
|
222 let vertexIndex = i.uint16 * 4'u16
|
|
223 result.indices.data[i * 6 + 0] = vertexIndex + 0
|
|
224 result.indices.data[i * 6 + 1] = vertexIndex + 1
|
|
225 result.indices.data[i * 6 + 2] = vertexIndex + 2
|
|
226 result.indices.data[i * 6 + 3] = vertexIndex + 2
|
|
227 result.indices.data[i * 6 + 4] = vertexIndex + 3
|
|
228 result.indices.data[i * 6 + 5] = vertexIndex + 0
|
|
229
|
1236
|
230 when T is string:
|
|
231 `text=`(result, text.toRunes())
|
|
232 else:
|
|
233 `text=`(result, text)
|
1234
|
234
|
1236
|
235 AssignBuffers(renderdata, result, uploadData = false)
|
1234
|
236 UploadImages(renderdata, result.shaderdata)
|
|
237 InitDescriptorSet(renderdata, descriptorSetLayout, result.shaderdata)
|
|
238
|
1236
|
239 result.Refresh(1)
|
|
240 UpdateAllGPUBuffers(result, flush = true)
|
|
241 UpdateAllGPUBuffers(result.shaderdata.data, flush = true)
|