Mercurial > games > semicongine
annotate semiconginev2/text/textbox.nim @ 1254:b0f4c8ccd49a
did: stuff to test gltf importer
author | sam <sam@basx.dev> |
---|---|
date | Sat, 27 Jul 2024 20:47:54 +0700 |
parents | 42eeb59f3a43 |
children |
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) = |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
26 textbox.shaderdata.data.textbox.UpdateGPUBuffer(flush = true) |
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 |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
48 of Center: -height / 2 |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
49 of Bottom: -height |
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1239
diff
changeset
|
50 ) + textbox.font.capHeight |
1234 | 51 |
52 var | |
53 offsetX = 0'f32 | |
54 offsetY = 0'f32 | |
55 lineIndex = 0 | |
1236 | 56 anchorX = case textbox.horizontalAlignment |
1234 | 57 of Left: 0'f32 |
58 of Center: lineWidths[lineIndex] / 2 | |
59 of Right: lineWidths[lineIndex] | |
1236 | 60 for i in 0 ..< textbox.maxLen: |
1234 | 61 let vertexOffset = i * 4 |
1236 | 62 if i < textbox.processedText.len: |
63 if textbox.processedText[i] == Rune('\n'): | |
1234 | 64 offsetX = 0 |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
65 offsetY -= textbox.font.lineAdvance |
1236 | 66 textbox.position.data[vertexOffset + 0] = NewVec3f() |
67 textbox.position.data[vertexOffset + 1] = NewVec3f() | |
68 textbox.position.data[vertexOffset + 2] = NewVec3f() | |
69 textbox.position.data[vertexOffset + 3] = NewVec3f() | |
1234 | 70 inc lineIndex |
1236 | 71 anchorX = case textbox.horizontalAlignment |
1234 | 72 of Left: 0'f32 |
73 of Center: lineWidths[lineIndex] / 2 | |
74 of Right: lineWidths[lineIndex] | |
75 else: | |
76 let | |
1236 | 77 glyph = textbox.font.glyphs[textbox.processedText[i]] |
1234 | 78 left = offsetX + glyph.leftOffset |
79 right = offsetX + glyph.leftOffset + glyph.dimension.x | |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
80 top = offsetY - glyph.topOffset |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
81 bottom = offsetY - glyph.topOffset - glyph.dimension.y |
1234 | 82 |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
83 textbox.position.data[vertexOffset + 0] = NewVec3f(left - anchorX, bottom - anchorY) |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
84 textbox.position.data[vertexOffset + 1] = NewVec3f(left - anchorX, top - anchorY) |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
85 textbox.position.data[vertexOffset + 2] = NewVec3f(right - anchorX, top - anchorY) |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
86 textbox.position.data[vertexOffset + 3] = NewVec3f(right - anchorX, bottom - anchorY) |
1234 | 87 |
1236 | 88 textbox.uv.data[vertexOffset + 0] = glyph.uvs[0] |
89 textbox.uv.data[vertexOffset + 1] = glyph.uvs[1] | |
90 textbox.uv.data[vertexOffset + 2] = glyph.uvs[2] | |
91 textbox.uv.data[vertexOffset + 3] = glyph.uvs[3] | |
1234 | 92 |
93 offsetX += glyph.advance | |
1236 | 94 if i < textbox.processedText.len - 1: |
95 offsetX += textbox.font.kerning[(textbox.processedText[i], textbox.processedText[i + 1])] | |
1234 | 96 else: |
1236 | 97 textbox.position.data[vertexOffset + 0] = NewVec3f() |
98 textbox.position.data[vertexOffset + 1] = NewVec3f() | |
99 textbox.position.data[vertexOffset + 2] = NewVec3f() | |
100 textbox.position.data[vertexOffset + 3] = NewVec3f() | |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
101 UpdateGPUBuffer(textbox.position) |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
102 UpdateGPUBuffer(textbox.uv) |
1236 | 103 textbox.lastRenderedText = textbox.processedText |
104 | |
105 func text*(textbox: Textbox): seq[Rune] = | |
106 textbox.text | |
1234 | 107 |
1236 | 108 proc `text=`*(textbox: var Textbox, newText: seq[Rune]) = |
109 if newText[0 ..< min(newText.len, textbox.maxLen)] == textbox.text: | |
110 return | |
1234 | 111 |
1236 | 112 textbox.text = newText[0 ..< min(newText.len, textbox.maxLen)] |
1234 | 113 |
1236 | 114 textbox.processedText = textbox.text |
115 if textbox.maxWidth > 0: | |
116 textbox.processedText = WordWrapped( | |
117 textbox.processedText, | |
118 textbox.font[], | |
119 textbox.maxWidth / textbox.shaderdata.data.textbox.data.scale, | |
1234 | 120 ) |
121 | |
1236 | 122 proc `text=`*(textbox: var Textbox, newText: string) = |
123 `text=`(textbox, newText.toRunes) | |
124 | |
125 proc Color*(textbox: Textbox): Vec4f = | |
126 textbox.shaderdata.data.textbox.data.color | |
1234 | 127 |
1236 | 128 proc `Color=`*(textbox: var Textbox, value: Vec4f) = |
129 if textbox.shaderdata.data.textbox.data.color != value: | |
130 textbox.dirtyShaderdata = true | |
131 textbox.shaderdata.data.textbox.data.color = value | |
1234 | 132 |
1236 | 133 proc Scale*(textbox: Textbox): float32 = |
134 textbox.shaderdata.data.textbox.data.scale | |
1234 | 135 |
1236 | 136 proc `Scale=`*(textbox: var Textbox, value: float32) = |
137 if textbox.shaderdata.data.textbox.data.scale != value: | |
138 textbox.dirtyShaderdata = true | |
139 textbox.shaderdata.data.textbox.data.scale = value | |
140 | |
141 proc Position*(textbox: Textbox): Vec3f = | |
142 textbox.shaderdata.data.textbox.data.position | |
1234 | 143 |
1236 | 144 proc `Position=`*(textbox: var Textbox, value: Vec3f) = |
145 if textbox.shaderdata.data.textbox.data.position != value: | |
146 textbox.dirtyShaderdata = true | |
147 textbox.shaderdata.data.textbox.data.position = value | |
1234 | 148 |
1236 | 149 proc horizontalAlignment*(textbox: Textbox): HorizontalAlignment = |
150 textbox.horizontalAlignment | |
151 proc `horizontalAlignment=`*(textbox: var Textbox, value: HorizontalAlignment) = | |
152 if value != textbox.horizontalAlignment: | |
153 textbox.horizontalAlignment = value | |
154 textbox.dirtyGeometry = true | |
1234 | 155 |
1236 | 156 proc verticalAlignment*(textbox: Textbox): VerticalAlignment = |
157 textbox.verticalAlignment | |
158 proc `verticalAlignment=`*(textbox: var Textbox, value: VerticalAlignment) = | |
159 if value != textbox.verticalAlignment: | |
160 textbox.verticalAlignment = value | |
161 textbox.dirtyGeometry = true | |
162 | |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
163 proc Refresh*(textbox: var Textbox) = |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
164 if textbox.shaderdata.data.textbox.data.aspectratio != GetAspectRatio(): |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
165 textbox.dirtyShaderdata = true |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
166 textbox.shaderdata.data.textbox.data.aspectratio = GetAspectRatio() |
1234 | 167 |
1236 | 168 if textbox.dirtyShaderdata: |
169 textbox.RefreshShaderdata() | |
170 textbox.dirtyShaderdata = false | |
171 | |
172 if textbox.dirtyGeometry or textbox.processedText != textbox.lastRenderedText: | |
173 textbox.RefreshGeometry() | |
174 textbox.dirtyGeometry = false | |
175 | |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
176 proc Render*(textbox: Textbox, commandbuffer: VkCommandBuffer, pipeline: Pipeline) = |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
177 WithBind(commandbuffer, (textbox.shaderdata, ), pipeline): |
1236 | 178 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = textbox) |
1234 | 179 |
1236 | 180 proc InitTextbox*[T: string | seq[Rune]]( |
1234 | 181 renderdata: var RenderData, |
182 descriptorSetLayout: VkDescriptorSetLayout, | |
183 font: Font, | |
1236 | 184 text: T = default(T), |
1234 | 185 scale: float32 = 1, |
186 position: Vec3f = NewVec3f(), | |
187 color: Vec4f = NewVec4f(0, 0, 0, 1), | |
188 maxLen: int = text.len, | |
189 verticalAlignment: VerticalAlignment = Center, | |
190 horizontalAlignment: HorizontalAlignment = Center, | |
191 maxWidth = 0'f32 | |
192 ): Textbox = | |
193 | |
194 result = Textbox( | |
195 maxLen: maxLen, | |
196 font: font, | |
197 dirtyGeometry: true, | |
1236 | 198 dirtyShaderdata: true, |
1234 | 199 horizontalAlignment: horizontalAlignment, |
200 verticalAlignment: verticalAlignment, | |
201 maxWidth: maxWidth, | |
202 position: asGPUArray(newSeq[Vec3f](int(maxLen * 4)), VertexBuffer), | |
203 uv: asGPUArray(newSeq[Vec2f](int(maxLen * 4)), VertexBuffer), | |
204 indices: asGPUArray(newSeq[uint16](int(maxLen * 6)), IndexBuffer), | |
205 shaderdata: asDescriptorSet( | |
206 TextboxDescriptorSet( | |
207 textbox: asGPUValue(TextboxData( | |
208 scale: scale, | |
209 position: position, | |
210 color: color, | |
1236 | 211 aspectratio: 1, |
1234 | 212 ), UniformBufferMapped), |
213 fontAtlas: font.fontAtlas | |
214 ) | |
215 ) | |
216 ) | |
217 | |
218 for i in 0 ..< maxLen: | |
219 let vertexIndex = i.uint16 * 4'u16 | |
220 result.indices.data[i * 6 + 0] = vertexIndex + 0 | |
221 result.indices.data[i * 6 + 1] = vertexIndex + 1 | |
222 result.indices.data[i * 6 + 2] = vertexIndex + 2 | |
223 result.indices.data[i * 6 + 3] = vertexIndex + 2 | |
224 result.indices.data[i * 6 + 4] = vertexIndex + 3 | |
225 result.indices.data[i * 6 + 5] = vertexIndex + 0 | |
226 | |
1236 | 227 when T is string: |
228 `text=`(result, text.toRunes()) | |
229 else: | |
230 `text=`(result, text) | |
1234 | 231 |
1236 | 232 AssignBuffers(renderdata, result, uploadData = false) |
1234 | 233 UploadImages(renderdata, result.shaderdata) |
234 InitDescriptorSet(renderdata, descriptorSetLayout, result.shaderdata) | |
235 | |
1239
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
236 result.Refresh() |
69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
sam <sam@basx.dev>
parents:
1238
diff
changeset
|
237 UpdateAllGPUBuffers(result, flush = true, allFrames = true) |
1236 | 238 UpdateAllGPUBuffers(result.shaderdata.data, flush = true) |