Mercurial > games > semicongine
annotate semiconginev2/text/textbox.nim @ 1238:03634915bbdb
add: tests for updating texts, does not pass validation atm
author | sam <sam@basx.dev> |
---|---|
date | Sun, 21 Jul 2024 11:31:11 +0700 |
parents | 176383220123 |
children | 69489a678141 |
rev | line source |
---|---|
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() | |
1238
03634915bbdb
add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents:
1236
diff
changeset
|
100 UpdateGPUBuffer(textbox.position, flush = true) |
03634915bbdb
add: tests for updating texts, does not pass validation atm
sam <sam@basx.dev>
parents:
1236
diff
changeset
|
101 UpdateGPUBuffer(textbox.uv, flush = true) |
1236 | 102 textbox.lastRenderedText = textbox.processedText |
103 | |
104 func text*(textbox: Textbox): seq[Rune] = | |
105 textbox.text | |
1234 | 106 |
1236 | 107 proc `text=`*(textbox: var Textbox, newText: seq[Rune]) = |
108 if newText[0 ..< min(newText.len, textbox.maxLen)] == textbox.text: | |
109 return | |
1234 | 110 |
1236 | 111 textbox.text = newText[0 ..< min(newText.len, textbox.maxLen)] |
1234 | 112 |
1236 | 113 textbox.processedText = textbox.text |
114 if textbox.maxWidth > 0: | |
115 textbox.processedText = WordWrapped( | |
116 textbox.processedText, | |
117 textbox.font[], | |
118 textbox.maxWidth / textbox.shaderdata.data.textbox.data.scale, | |
1234 | 119 ) |
120 | |
1236 | 121 proc `text=`*(textbox: var Textbox, newText: string) = |
122 `text=`(textbox, newText.toRunes) | |
123 | |
124 proc Color*(textbox: Textbox): Vec4f = | |
125 textbox.shaderdata.data.textbox.data.color | |
1234 | 126 |
1236 | 127 proc `Color=`*(textbox: var Textbox, value: Vec4f) = |
128 if textbox.shaderdata.data.textbox.data.color != value: | |
129 textbox.dirtyShaderdata = true | |
130 textbox.shaderdata.data.textbox.data.color = value | |
1234 | 131 |
1236 | 132 proc Scale*(textbox: Textbox): float32 = |
133 textbox.shaderdata.data.textbox.data.scale | |
1234 | 134 |
1236 | 135 proc `Scale=`*(textbox: var Textbox, value: float32) = |
136 if textbox.shaderdata.data.textbox.data.scale != value: | |
137 textbox.dirtyShaderdata = true | |
138 textbox.shaderdata.data.textbox.data.scale = value | |
139 | |
140 proc AspectRatio*(textbox: Textbox): float32 = | |
141 textbox.shaderdata.data.textbox.data.aspectratio | |
1234 | 142 |
1236 | 143 proc `AspectRatio=`*(textbox: var Textbox, value: float32) = |
144 if textbox.shaderdata.data.textbox.data.aspectratio != value: | |
145 textbox.dirtyShaderdata = true | |
146 textbox.shaderdata.data.textbox.data.aspectratio = value | |
1234 | 147 |
1236 | 148 proc Position*(textbox: Textbox): Vec3f = |
149 textbox.shaderdata.data.textbox.data.position | |
1234 | 150 |
1236 | 151 proc `Position=`*(textbox: var Textbox, value: Vec3f) = |
152 if textbox.shaderdata.data.textbox.data.position != value: | |
153 textbox.dirtyShaderdata = true | |
154 textbox.shaderdata.data.textbox.data.position = value | |
1234 | 155 |
1236 | 156 proc horizontalAlignment*(textbox: Textbox): HorizontalAlignment = |
157 textbox.horizontalAlignment | |
158 proc `horizontalAlignment=`*(textbox: var Textbox, value: HorizontalAlignment) = | |
159 if value != textbox.horizontalAlignment: | |
160 textbox.horizontalAlignment = value | |
161 textbox.dirtyGeometry = true | |
1234 | 162 |
1236 | 163 proc verticalAlignment*(textbox: Textbox): VerticalAlignment = |
164 textbox.verticalAlignment | |
165 proc `verticalAlignment=`*(textbox: var Textbox, value: VerticalAlignment) = | |
166 if value != textbox.verticalAlignment: | |
167 textbox.verticalAlignment = value | |
168 textbox.dirtyGeometry = true | |
169 | |
170 proc Refresh*(textbox: var Textbox, aspectratio: float32) = | |
171 `AspectRatio=`(textbox, aspectratio) | |
1234 | 172 |
1236 | 173 if textbox.dirtyShaderdata: |
174 textbox.RefreshShaderdata() | |
175 textbox.dirtyShaderdata = false | |
176 | |
177 if textbox.dirtyGeometry or textbox.processedText != textbox.lastRenderedText: | |
178 textbox.RefreshGeometry() | |
179 textbox.dirtyGeometry = false | |
180 | |
181 proc Render*(textbox: Textbox, commandbuffer: VkCommandBuffer, pipeline: Pipeline, currentFiF: int) = | |
1234 | 182 WithBind(commandbuffer, (textbox.shaderdata, ), pipeline, currentFiF): |
1236 | 183 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = textbox) |
1234 | 184 |
1236 | 185 proc InitTextbox*[T: string | seq[Rune]]( |
1234 | 186 renderdata: var RenderData, |
187 descriptorSetLayout: VkDescriptorSetLayout, | |
188 font: Font, | |
1236 | 189 text: T = default(T), |
1234 | 190 scale: float32 = 1, |
191 position: Vec3f = NewVec3f(), | |
192 color: Vec4f = NewVec4f(0, 0, 0, 1), | |
193 maxLen: int = text.len, | |
194 verticalAlignment: VerticalAlignment = Center, | |
195 horizontalAlignment: HorizontalAlignment = Center, | |
196 maxWidth = 0'f32 | |
197 ): Textbox = | |
198 | |
199 result = Textbox( | |
200 maxLen: maxLen, | |
201 font: font, | |
202 dirtyGeometry: true, | |
1236 | 203 dirtyShaderdata: true, |
1234 | 204 horizontalAlignment: horizontalAlignment, |
205 verticalAlignment: verticalAlignment, | |
206 maxWidth: maxWidth, | |
207 position: asGPUArray(newSeq[Vec3f](int(maxLen * 4)), VertexBuffer), | |
208 uv: asGPUArray(newSeq[Vec2f](int(maxLen * 4)), VertexBuffer), | |
209 indices: asGPUArray(newSeq[uint16](int(maxLen * 6)), IndexBuffer), | |
210 shaderdata: asDescriptorSet( | |
211 TextboxDescriptorSet( | |
212 textbox: asGPUValue(TextboxData( | |
213 scale: scale, | |
214 position: position, | |
215 color: color, | |
1236 | 216 aspectratio: 1, |
1234 | 217 ), UniformBufferMapped), |
218 fontAtlas: font.fontAtlas | |
219 ) | |
220 ) | |
221 ) | |
222 | |
223 for i in 0 ..< maxLen: | |
224 let vertexIndex = i.uint16 * 4'u16 | |
225 result.indices.data[i * 6 + 0] = vertexIndex + 0 | |
226 result.indices.data[i * 6 + 1] = vertexIndex + 1 | |
227 result.indices.data[i * 6 + 2] = vertexIndex + 2 | |
228 result.indices.data[i * 6 + 3] = vertexIndex + 2 | |
229 result.indices.data[i * 6 + 4] = vertexIndex + 3 | |
230 result.indices.data[i * 6 + 5] = vertexIndex + 0 | |
231 | |
1236 | 232 when T is string: |
233 `text=`(result, text.toRunes()) | |
234 else: | |
235 `text=`(result, text) | |
1234 | 236 |
1236 | 237 AssignBuffers(renderdata, result, uploadData = false) |
1234 | 238 UploadImages(renderdata, result.shaderdata) |
239 InitDescriptorSet(renderdata, descriptorSetLayout, result.shaderdata) | |
240 | |
1236 | 241 result.Refresh(1) |
242 UpdateAllGPUBuffers(result, flush = true) | |
243 UpdateAllGPUBuffers(result.shaderdata.data, flush = true) |