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