Mercurial > games > semicongine
annotate semicongine/text.nim @ 897:fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sun, 11 Feb 2024 18:47:13 +0700 |
| parents | a0826956dc5c |
| children | 6924c2cf94e5 |
| 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 | |
| 16 MAX_TEXT_MATERIALS = 10 | |
| 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", |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 vertexAttributes: {TRANSFORM_ATTRIB: Mat4F32, POSITION_ATTRIB: Vec3F32, UV_ATTRIB: Vec2F32}.toTable, |
|
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
23 attributes: {"fontAtlas": TextureType, "color": Vec4F32}.toTable, |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 ) |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 TEXT_SHADER* = createShaderConfiguration( |
| 877 | 26 inputs = [ |
| 27 attr[Mat4](TRANSFORM_ATTRIB, memoryPerformanceHint = PreferFastWrite, perInstance = true), | |
| 28 attr[Vec3f](POSITION_ATTRIB, memoryPerformanceHint = PreferFastWrite), | |
| 29 attr[Vec2f](UV_ATTRIB, memoryPerformanceHint = PreferFastWrite), | |
|
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
30 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
|
31 ], |
|
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
32 intermediates = [ |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
33 attr[Vec2f]("uvFrag"), |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
34 attr[uint16]("materialIndexOut", noInterpolation = true) |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
35 ], |
| 877 | 36 outputs = [attr[Vec4f]("color")], |
|
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
37 uniforms = [attr[Vec4f]("color", arrayCount = MAX_TEXT_MATERIALS)], |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
38 samplers = [attr[Texture]("fontAtlas", arrayCount = MAX_TEXT_MATERIALS)], |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
39 vertexCode = &""" |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
40 gl_Position = vec4({POSITION_ATTRIB}, 1.0) * {TRANSFORM_ATTRIB}; |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
41 uvFrag = {UV_ATTRIB}; |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
42 materialIndexOut = {MATERIALINDEX_ATTRIBUTE}; |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
43 """, |
|
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
880
diff
changeset
|
44 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
|
45 ) |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 |
| 893 | 47 var instanceCounter = 0 |
| 48 | |
| 49 type | |
| 50 Text* = object | |
| 51 maxLen*: int | |
| 52 font*: Font | |
| 53 maxWidth: float32 = 0 | |
| 54 # properties: | |
| 55 text: seq[Rune] | |
| 56 position: Vec2f | |
| 57 horizontalAlignment: HorizontalAlignment = Center | |
| 58 verticalAlignment: VerticalAlignment = Center | |
| 59 scale: float32 | |
| 60 aspect_ratio: float32 | |
| 61 # management/internal: | |
| 62 dirty: bool # is true if any of the attributes changed | |
| 63 processedText: seq[Rune] # used to store processed (word-wrapper) text to preserve original | |
| 64 lastRenderedText: seq[Rune] # stores the last rendered text, to prevent unnecessary updates | |
| 65 mesh: Mesh | |
| 66 | |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
67 func `$`*(text: Text): string = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
68 "\"" & $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
|
69 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
70 proc refresh*(text: var Text) = |
| 884 | 71 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
|
72 return |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
73 |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 # pre-calculate text-width |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 var width = 0'f32 |
| 878 | 76 var lineWidths: seq[float32] |
|
886
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
77 for i in 0 ..< text.processedText.len: |
| 884 | 78 if text.processedText[i] == NEWLINE: |
| 878 | 79 lineWidths.add width |
|
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
868
diff
changeset
|
80 width = 0'f32 |
|
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
81 else: |
|
886
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
82 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
|
83 width += text.font.glyphs[text.processedText[i]].advance |
| 884 | 84 if i < text.processedText.len - 1: |
| 85 width += text.font.kerning[(text.processedText[i], text.processedText[i + 1])] | |
| 878 | 86 lineWidths.add width |
|
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
893
diff
changeset
|
87 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
|
88 if lineWidths[^1] == 0 and lineWidths.len > 1: |
|
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
89 height -= 1 |
| 878 | 90 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
91 let anchorY = (case text.verticalAlignment |
| 878 | 92 of Top: 0'f32 |
| 93 of Center: height / 2 | |
|
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
893
diff
changeset
|
94 of Bottom: height) - text.font.capHeight |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 |
| 878 | 96 var |
| 97 offsetX = 0'f32 | |
| 98 offsetY = 0'f32 | |
| 99 lineIndex = 0 | |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
100 anchorX = case text.horizontalAlignment |
| 878 | 101 of Left: 0'f32 |
| 102 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
|
103 of Right: lineWidths[lineIndex] |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
104 for i in 0 ..< text.maxLen: |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
105 let vertexOffset = i * 4 |
| 884 | 106 if i < text.processedText.len: |
| 107 if text.processedText[i] == Rune('\n'): | |
|
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
108 offsetX = 0 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
109 offsetY += text.font.lineAdvance |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
110 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = newVec3f() |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
111 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = newVec3f() |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
112 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = newVec3f() |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
113 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = newVec3f() |
| 878 | 114 inc lineIndex |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
115 anchorX = case text.horizontalAlignment |
| 878 | 116 of Left: 0'f32 |
| 117 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
|
118 of Right: lineWidths[lineIndex] |
|
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
119 else: |
|
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
120 let |
| 884 | 121 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
|
122 left = offsetX + glyph.leftOffset |
|
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
123 right = offsetX + glyph.leftOffset + glyph.dimension.x |
|
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
124 top = offsetY + glyph.topOffset |
|
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
125 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
|
126 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
127 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = newVec3f(left - anchorX, bottom - anchorY) |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
128 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = newVec3f(left - anchorX, top - anchorY) |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
129 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = newVec3f(right - anchorX, top - anchorY) |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
130 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
|
131 |
|
879
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 + 0] = glyph.uvs[0] |
|
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 + 1] = glyph.uvs[1] |
|
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 + 2] = glyph.uvs[2] |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
135 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
|
136 |
|
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
872
diff
changeset
|
137 offsetX += glyph.advance |
| 884 | 138 if i < text.processedText.len - 1: |
| 139 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
|
140 else: |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
141 text.mesh[POSITION_ATTRIB, vertexOffset + 0] = newVec3f() |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
142 text.mesh[POSITION_ATTRIB, vertexOffset + 1] = newVec3f() |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
143 text.mesh[POSITION_ATTRIB, vertexOffset + 2] = newVec3f() |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
144 text.mesh[POSITION_ATTRIB, vertexOffset + 3] = newVec3f() |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
145 text.mesh.transform = translate(text.position.x, text.position.y, 0) * scale(text.scale, text.scale * text.aspect_ratio) |
| 884 | 146 text.lastRenderedText = text.processedText |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
147 text.dirty = false |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
148 |
| 884 | 149 |
| 150 func width(text: seq[Rune], font: Font): float32 = | |
| 151 var currentWidth = 0'f32 | |
| 152 var lineWidths: seq[float32] | |
| 153 for i in 0 ..< text.len: | |
| 154 if text[i] == NEWLINE: | |
| 155 lineWidths.add currentWidth | |
| 156 currentWidth = 0'f32 | |
| 157 else: | |
| 158 if not (i == text.len - 1 and text[i].isWhiteSpace): | |
| 159 currentWidth += font.glyphs[text[i]].advance | |
| 160 if i < text.len - 1: | |
| 161 currentWidth += font.kerning[(text[i], text[i + 1])] | |
| 162 lineWidths.add currentWidth | |
| 163 return lineWidths.max | |
| 164 | |
| 165 func wordWrapped(text: seq[Rune], font: Font, maxWidth: float32): seq[Rune] = | |
| 166 var remaining: seq[seq[Rune]] = @[@[]] | |
| 167 for c in text: | |
| 168 if c == SPACE: | |
| 169 remaining.add newSeq[Rune]() | |
| 170 else: | |
| 171 remaining[^1].add c | |
| 172 remaining.reverse() | |
| 173 | |
| 174 var currentLine: seq[Rune] | |
| 175 | |
| 176 while remaining.len > 0: | |
| 177 var currentWord = remaining.pop() | |
| 178 assert not (SPACE in currentWord) | |
| 179 | |
| 180 if currentWord.len == 0: | |
| 181 currentLine.add SPACE | |
| 182 else: | |
| 183 assert currentWord[^1] != SPACE | |
| 184 # if this is the first word of the line and it is too long we need to | |
| 185 # split by character | |
| 186 if currentLine.len == 0 and (SPACE & currentWord).width(font) > maxWidth: | |
| 187 var subWord = @[currentWord[0]] | |
| 188 for c in currentWord[1 .. ^1]: | |
| 189 if (subWord & c).width(font) > maxWidth: | |
| 190 break | |
| 191 subWord.add c | |
| 192 result.add subWord & NEWLINE | |
| 193 remaining.add currentWord[subWord.len .. ^1] # process rest of the word in next iteration | |
| 194 else: | |
| 195 if (currentLine & SPACE & currentWord).width(font) <= maxWidth: | |
|
886
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
196 if currentLine.len == 0: |
|
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
197 currentLine = currentWord |
|
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
198 else: |
|
cf4000cc6286
fix: word-wrapping working correctly now
Sam <sam@basx.dev>
parents:
885
diff
changeset
|
199 currentLine = currentLine & SPACE & currentWord |
| 884 | 200 else: |
| 201 result.add currentLine & NEWLINE | |
| 202 remaining.add currentWord | |
| 203 currentLine = @[] | |
| 204 if currentLine.len > 0 and currentLine != @[SPACE]: | |
| 205 result.add currentLine | |
| 206 | |
| 207 return result | |
| 208 | |
| 209 | |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
210 func text*(text: Text): seq[Rune] = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
211 text.text |
| 884 | 212 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
213 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
|
214 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
|
215 |
| 884 | 216 text.processedText = text.text |
|
883
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
217 if text.maxWidth > 0: |
| 884 | 218 text.processedText = text.processedText.wordWrapped(text.font, text.maxWidth / text.scale) |
|
883
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
219 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
220 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
|
221 `text=`(text, newText.toRunes) |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
222 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
223 proc position*(text: Text): Vec2f = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
224 text.position |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
225 proc `position=`*(text: var Text, value: Vec2f) = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
226 if value != text.position: |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
227 text.position = value |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
228 text.dirty = true |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
229 |
| 890 | 230 proc color*(text: Text): Vec4f = |
| 231 text.mesh.material["color", 0, Vec4f] | |
| 232 proc `color=`*(text: var Text, value: Vec4f) = | |
| 233 if value != text.color: | |
| 234 text.mesh.material["color", 0] = value | |
| 235 | |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
236 proc horizontalAlignment*(text: Text): HorizontalAlignment = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
237 text.horizontalAlignment |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
238 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
|
239 if value != text.horizontalAlignment: |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
240 text.horizontalAlignment = value |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
241 text.dirty = true |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
242 |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
243 proc verticalAlignment*(text: Text): VerticalAlignment = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
244 text.verticalAlignment |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
245 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
|
246 if value != text.verticalAlignment: |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
247 text.verticalAlignment = value |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
248 text.dirty = true |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
249 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
250 proc scale*(text: Text): float32 = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
251 text.scale |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
252 proc `scale=`*(text: var Text, value: float32) = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
253 if value != text.scale: |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
254 text.scale = value |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
255 text.dirty = true |
| 878 | 256 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
257 proc aspect_ratio*(text: Text): float32 = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
258 text.aspect_ratio |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
259 proc `aspect_ratio=`*(text: var Text, value: float32) = |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
260 if value != text.aspect_ratio: |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
261 text.aspect_ratio = value |
|
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
262 text.dirty = true |
| 878 | 263 |
|
883
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
264 proc initText*(font: Font, text = "".toRunes, maxLen: int = text.len, color = newVec4f(0.07, 0.07, 0.07, 1), scale = 1'f32, position = newVec2f(), verticalAlignment = VerticalAlignment.Center, horizontalAlignment = HorizontalAlignment.Center, maxWidth = 0'f32): Text = |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
265 var |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
266 positions = newSeq[Vec3f](int(maxLen * 4)) |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
267 indices: seq[array[3, uint16]] |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
268 uvs = newSeq[Vec2f](int(maxLen * 4)) |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
269 for i in 0 ..< maxLen: |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
270 let offset = i * 4 |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
271 indices.add [ |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
272 [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
|
273 [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
|
274 ] |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
275 |
| 893 | 276 result = Text(maxLen: maxLen, font: font, dirty: true, scale: scale, position: position, aspect_ratio: 1, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, maxWidth: maxWidth) |
| 277 `text=`(result, text) | |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
278 result.mesh = newMesh(positions = positions, indices = indices, uvs = uvs, name = &"text-{instanceCounter}") |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
279 result.mesh[].renameAttribute("position", POSITION_ATTRIB) |
|
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
280 result.mesh[].renameAttribute("uv", UV_ATTRIB) |
|
868
252cbc10b25f
did: improve/refactor some of the material API
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
281 result.mesh.material = initMaterialData( |
| 877 | 282 theType = TEXT_MATERIAL_TYPE, |
| 283 name = font.name & " text", | |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
284 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
|
285 ) |
| 893 | 286 inc instanceCounter |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
287 |
|
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
288 result.refresh() |
|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
289 |
|
883
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
290 proc initText*(font: Font, text = "", maxLen: int = text.len, color = newVec4f(0.07, 0.07, 0.07, 1), scale = 1'f32, position = newVec2f(), verticalAlignment = VerticalAlignment.Center, horizontalAlignment = HorizontalAlignment.Center, maxWidth = 0'f32): Text = |
|
98b66663e07c
add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents:
882
diff
changeset
|
291 initText(font = font, text = text.toRunes, maxLen = maxLen, color = color, scale = scale, position = position, horizontalAlignment = horizontalAlignment, verticalAlignment = verticalAlignment, maxWidth = maxWidth) |
