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