annotate semiconginev2/text/textbox.nim @ 1236:176383220123

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