annotate semiconginev2/text/textbox.nim @ 1247:c15770761865

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