Mercurial > games > semicongine
annotate semiconginev2/old/text.nim @ 1225:27cd1c21290e compiletime-tests
did: refactor resources
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 22:20:59 +0700 |
parents | 56781cc0fc7c |
children |
rev | line source |
---|---|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/tables |
884 | 2 import std/algorithm |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/unicode |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/strformat |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 import ./core |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 import ./mesh |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 import ./material |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 import ./vulkan/shader |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
11 const |
878 | 12 NEWLINE = Rune('\n') |
884 | 13 SPACE = Rune(' ') |
893 | 14 |
15 # font shader | |
1144
af18922b416d
did: reduce some limits, remove sqlite32 on windows for now
sam <sam@basx.dev>
parents:
1139
diff
changeset
|
16 MAX_TEXT_MATERIALS = 64 # need for every different font AND color |
893 | 17 SHADER_ATTRIB_PREFIX = "semicon_text_" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 POSITION_ATTRIB = SHADER_ATTRIB_PREFIX & "position" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 UV_ATTRIB = SHADER_ATTRIB_PREFIX & "uv" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 TEXT_MATERIAL_TYPE* = MaterialType( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 name: "default-text-material-type", |
999
b5be4ea07c3c
fix: errors that have now been discovered thanks to last commit :)
sam <sam@basx.dev>
parents:
998
diff
changeset
|
22 vertexAttributes: {POSITION_ATTRIB: Vec3F32, UV_ATTRIB: Vec2F32}.toTable, |
b5be4ea07c3c
fix: errors that have now been discovered thanks to last commit :)
sam <sam@basx.dev>
parents:
998
diff
changeset
|
23 instanceAttributes: {TRANSFORM_ATTRIB: Mat4F32, MATERIALINDEX_ATTRIBUTE: UInt16}.toTable, |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
24 attributes: {"fontAtlas": TextureType, "color": Vec4F32}.toTable, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 ) |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
26 TEXT_SHADER* = CreateShaderConfiguration( |
998
7e89c8fe57a8
did: add name for material types and fix shader-materialtype-compatability check
sam <sam@basx.dev>
parents:
920
diff
changeset
|
27 name = "font shader", |
877 | 28 inputs = [ |
1137 | 29 Attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true), |
30 Attr[Vec3f](POSITION_ATTRIB, memoryPerformanceHint = PreferFastWrite), | |
31 Attr[Vec2f](UV_ATTRIB, memoryPerformanceHint = PreferFastWrite), | |
32 Attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true), | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 ], |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
34 intermediates = [ |
1137 | 35 Attr[Vec2f]("uvFrag"), |
36 Attr[uint16]("materialIndexOut", noInterpolation = true) | |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
37 ], |
1137 | 38 outputs = [Attr[Vec4f]("color")], |
39 uniforms = [Attr[Vec4f]("color", arrayCount = MAX_TEXT_MATERIALS), Attr[float32](ASPECT_RATIO_ATTRIBUTE)], | |
40 samplers = [Attr[Texture]("fontAtlas", arrayCount = MAX_TEXT_MATERIALS)], | |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
41 vertexCode = &""" |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
903
diff
changeset
|
42 gl_Position = vec4({POSITION_ATTRIB}.x, {POSITION_ATTRIB}.y * Uniforms.{ASPECT_RATIO_ATTRIBUTE}, {POSITION_ATTRIB}.z, 1.0) * {TRANSFORM_ATTRIB}; |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
43 uvFrag = {UV_ATTRIB}; |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
44 materialIndexOut = {MATERIALINDEX_ATTRIBUTE}; |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
45 """, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
46 fragmentCode = &"""color = vec4(Uniforms.color[materialIndexOut].rgb, Uniforms.color[materialIndexOut].a * texture(fontAtlas[materialIndexOut], uvFrag).r);""" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 |
893 | 49 var instanceCounter = 0 |
50 | |
51 type | |
52 Text* = object | |
53 maxLen*: int | |
54 font*: Font | |
55 maxWidth: float32 = 0 | |
56 # properties: | |
57 text: seq[Rune] | |
58 horizontalAlignment: HorizontalAlignment = Center | |
59 verticalAlignment: VerticalAlignment = Center | |
60 # management/internal: | |
61 dirty: bool # is true if any of the attributes changed | |
62 processedText: seq[Rune] # used to store processed (word-wrapper) text to preserve original | |
63 lastRenderedText: seq[Rune] # stores the last rendered text, to prevent unnecessary updates | |
898 | 64 mesh*: Mesh |
893 | 65 |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
66 func `$`*(text: Text): string = |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
67 "\"" & $text.text[0 ..< min(text.text.len, 16)] & "\"" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 |
1138 | 69 proc Refresh*(text: var Text) = |
884 | 70 if not text.dirty and text.processedText == text.lastRenderedText: |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
71 return |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
72 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 # pre-calculate text-width |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 var width = 0'f32 |
878 | 75 var lineWidths: seq[float32] |
886
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
76 for i in 0 ..< text.processedText.len: |
884 | 77 if text.processedText[i] == NEWLINE: |
878 | 78 lineWidths.add width |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
79 width = 0'f32 |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
80 else: |
886
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
81 if not (i == text.processedText.len - 1 and text.processedText[i].isWhiteSpace): |
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
82 width += text.font.glyphs[text.processedText[i]].advance |
884 | 83 if i < text.processedText.len - 1: |
84 width += text.font.kerning[(text.processedText[i], text.processedText[i + 1])] | |
878 | 85 lineWidths.add width |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
893
diff
changeset
|
86 var height = float32(lineWidths.len - 1) * text.font.lineAdvance + text.font.capHeight |
886
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
87 if lineWidths[^1] == 0 and lineWidths.len > 1: |
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
88 height -= 1 |
878 | 89 |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
90 let anchorY = (case text.verticalAlignment |
878 | 91 of Top: 0'f32 |
92 of Center: height / 2 | |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
893
diff
changeset
|
93 of Bottom: height) - text.font.capHeight |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 |
878 | 95 var |
96 offsetX = 0'f32 | |
97 offsetY = 0'f32 | |
98 lineIndex = 0 | |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
99 anchorX = case text.horizontalAlignment |
878 | 100 of Left: 0'f32 |
101 of Center: lineWidths[lineIndex] / 2 | |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
102 of Right: lineWidths[lineIndex] |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
103 for i in 0 ..< text.maxLen: |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 let vertexOffset = i * 4 |
884 | 105 if i < text.processedText.len: |
106 if text.processedText[i] == Rune('\n'): | |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
107 offsetX = 0 |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
108 offsetY += text.font.lineAdvance |
1136 | 109 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = NewVec3f() |
110 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = NewVec3f() | |
111 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = NewVec3f() | |
112 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = NewVec3f() | |
878 | 113 inc lineIndex |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
114 anchorX = case text.horizontalAlignment |
878 | 115 of Left: 0'f32 |
116 of Center: lineWidths[lineIndex] / 2 | |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
117 of Right: lineWidths[lineIndex] |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
118 else: |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
119 let |
884 | 120 glyph = text.font.glyphs[text.processedText[i]] |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
121 left = offsetX + glyph.leftOffset |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
122 right = offsetX + glyph.leftOffset + glyph.dimension.x |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
123 top = offsetY + glyph.topOffset |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
124 bottom = offsetY + glyph.topOffset + glyph.dimension.y |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
125 |
1136 | 126 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = NewVec3f(left - anchorX, bottom - anchorY) |
127 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = NewVec3f(left - anchorX, top - anchorY) | |
128 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = NewVec3f(right - anchorX, top - anchorY) | |
129 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = NewVec3f(right - anchorX, bottom - anchorY) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
130 |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
131 text.mesh[UV_ATTRIB, vertexOffset + 0] = glyph.uvs[0] |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
132 text.mesh[UV_ATTRIB, vertexOffset + 1] = glyph.uvs[1] |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
133 text.mesh[UV_ATTRIB, vertexOffset + 2] = glyph.uvs[2] |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
134 text.mesh[UV_ATTRIB, vertexOffset + 3] = glyph.uvs[3] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
135 |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
136 offsetX += glyph.advance |
884 | 137 if i < text.processedText.len - 1: |
138 offsetX += text.font.kerning[(text.processedText[i], text.processedText[i + 1])] | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 else: |
1136 | 140 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = NewVec3f() |
141 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = NewVec3f() | |
142 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = NewVec3f() | |
143 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = NewVec3f() | |
884 | 144 text.lastRenderedText = text.processedText |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
145 text.dirty = false |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
146 |
884 | 147 |
148 func width(text: seq[Rune], font: Font): float32 = | |
149 var currentWidth = 0'f32 | |
150 var lineWidths: seq[float32] | |
151 for i in 0 ..< text.len: | |
152 if text[i] == NEWLINE: | |
153 lineWidths.add currentWidth | |
154 currentWidth = 0'f32 | |
155 else: | |
156 if not (i == text.len - 1 and text[i].isWhiteSpace): | |
157 currentWidth += font.glyphs[text[i]].advance | |
158 if i < text.len - 1: | |
159 currentWidth += font.kerning[(text[i], text[i + 1])] | |
160 lineWidths.add currentWidth | |
161 return lineWidths.max | |
162 | |
163 func wordWrapped(text: seq[Rune], font: Font, maxWidth: float32): seq[Rune] = | |
164 var remaining: seq[seq[Rune]] = @[@[]] | |
165 for c in text: | |
166 if c == SPACE: | |
167 remaining.add newSeq[Rune]() | |
168 else: | |
169 remaining[^1].add c | |
170 remaining.reverse() | |
171 | |
172 var currentLine: seq[Rune] | |
173 | |
174 while remaining.len > 0: | |
175 var currentWord = remaining.pop() | |
176 assert not (SPACE in currentWord) | |
177 | |
178 if currentWord.len == 0: | |
179 currentLine.add SPACE | |
180 else: | |
181 assert currentWord[^1] != SPACE | |
182 # if this is the first word of the line and it is too long we need to | |
183 # split by character | |
184 if currentLine.len == 0 and (SPACE & currentWord).width(font) > maxWidth: | |
185 var subWord = @[currentWord[0]] | |
186 for c in currentWord[1 .. ^1]: | |
187 if (subWord & c).width(font) > maxWidth: | |
188 break | |
189 subWord.add c | |
190 result.add subWord & NEWLINE | |
191 remaining.add currentWord[subWord.len .. ^1] # process rest of the word in next iteration | |
192 else: | |
193 if (currentLine & SPACE & currentWord).width(font) <= maxWidth: | |
886
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
194 if currentLine.len == 0: |
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
195 currentLine = currentWord |
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
196 else: |
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
197 currentLine = currentLine & SPACE & currentWord |
884 | 198 else: |
199 result.add currentLine & NEWLINE | |
200 remaining.add currentWord | |
201 currentLine = @[] | |
202 if currentLine.len > 0 and currentLine != @[SPACE]: | |
203 result.add currentLine | |
204 | |
205 return result | |
206 | |
207 | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
208 func text*(text: Text): seq[Rune] = |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
209 text.text |
884 | 210 |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
211 proc `text=`*(text: var Text, newText: seq[Rune]) = |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
212 text.text = newText[0 ..< min(newText.len, text.maxLen)] |
883
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
213 |
884 | 214 text.processedText = text.text |
883
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
215 if text.maxWidth > 0: |
1136 | 216 text.processedText = text.processedText.wordWrapped(text.font, text.maxWidth / text.mesh.transform.Scaling.x) |
883
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
217 |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
218 proc `text=`*(text: var Text, newText: string) = |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
219 `text=`(text, newText.toRunes) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
220 |
1138 | 221 proc Color*(text: Text): Vec4f = |
890 | 222 text.mesh.material["color", 0, Vec4f] |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
223 proc `Color=`*(text: var Text, value: Vec4f) = |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
224 if value != text.mesh.material["color", 0, Vec4f]: |
890 | 225 text.mesh.material["color", 0] = value |
226 | |
1138 | 227 proc HorizontalAlignment*(text: Text): HorizontalAlignment = |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
228 text.horizontalAlignment |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
229 proc `horizontalAlignment=`*(text: var Text, value: HorizontalAlignment) = |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
230 if value != text.horizontalAlignment: |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
231 text.horizontalAlignment = value |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
232 text.dirty = true |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
233 |
1138 | 234 proc VerticalAlignment*(text: Text): VerticalAlignment = |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
235 text.verticalAlignment |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
236 proc `verticalAlignment=`*(text: var Text, value: VerticalAlignment) = |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
237 if value != text.verticalAlignment: |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
238 text.verticalAlignment = value |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
239 text.dirty = true |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
240 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
241 proc InitText*(font: Font, text = "".toRunes, maxLen: int = text.len, color = NewVec4f(0.07, 0.07, 0.07, 1), verticalAlignment: VerticalAlignment = Center, horizontalAlignment: HorizontalAlignment = Center, maxWidth = 0'f32, transform = Unit4): Text = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
242 var |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
243 positions = newSeq[Vec3f](int(maxLen * 4)) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
244 indices: seq[array[3, uint16]] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
245 uvs = newSeq[Vec2f](int(maxLen * 4)) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
246 for i in 0 ..< maxLen: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
247 let offset = i * 4 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
248 indices.add [ |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
249 [uint16(offset + 0), uint16(offset + 1), uint16(offset + 2)], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
250 [uint16(offset + 2), uint16(offset + 3), uint16(offset + 0)], |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
251 ] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
252 |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
903
diff
changeset
|
253 result = Text(maxLen: maxLen, font: font, dirty: true, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, maxWidth: maxWidth) |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
254 result.mesh = NewMesh(positions = positions, indices = indices, uvs = uvs, name = &"text-{instanceCounter}") |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
255 result.mesh[].RenameAttribute("position", POSITION_ATTRIB) |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
256 result.mesh[].RenameAttribute("uv", UV_ATTRIB) |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
257 result.mesh.material = TEXT_MATERIAL_TYPE.InitMaterialData( |
877 | 258 name = font.name & " text", |
1137 | 259 attributes = {"fontAtlas": InitDataList(@[font.fontAtlas]), "color": InitDataList(@[color])}, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
260 ) |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
903
diff
changeset
|
261 result.mesh.transform = transform |
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
903
diff
changeset
|
262 `text=`(result, text) |
893 | 263 inc instanceCounter |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
264 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
265 result.Refresh() |