annotate tests/test_font.nim @ 883:98b66663e07c

add: text-wrapping, fix: incorrect descriptor poolsizes
author Sam <sam@basx.dev>
date Wed, 31 Jan 2024 00:29:40 +0700
parents 11a55a45aba2
children 1e3c242435af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
1 import std/unicode
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
2
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
4
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
5
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
6 proc main() =
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 805
diff changeset
7 # setup engine
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 805
diff changeset
8 var engine = initEngine("Test fonts")
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 805
diff changeset
9 engine.initRenderer([])
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 805
diff changeset
10
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 805
diff changeset
11 # build scene
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 805
diff changeset
12 var scene = Scene(name: "main")
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 832
diff changeset
13 # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes)
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
14 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
883
98b66663e07c add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 881
diff changeset
15 var main_text = font.initText("", maxLen = 255, color = newVec4f(1, 0.15, 0.15, 1), scale = 0.0005, maxWidth = 1.0)
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
16 var help_text = font.initText("""Controls
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
17
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
18 Horizontal alignment:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
19 F1: Left
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
20 F2: Center
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
21 F3: Right
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
22 Vertical alignment:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
23 F4: Top
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
24 F5: Center
881
11a55a45aba2 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 879
diff changeset
25 F6: Bottom""", scale = 0.0002, position = newVec2f(0, 0), horizontalAlignment = Left, verticalAlignment = Top)
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
26 scene.add main_text
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
27 scene.add help_text
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 805
diff changeset
28 engine.loadScene(scene)
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
29
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
30 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
31 if engine.windowWasResized():
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
32 var winSize = engine.getWindow().size
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
33 main_text.aspect_ratio = winSize[0] / winSize[1]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
34 help_text.aspect_ratio = winSize[0] / winSize[1]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
35 help_text.position = newVec2f(-0.99, -0.99)
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
36
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
37 # add character
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
38 if main_text.text.len < main_text.maxLen - 1:
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
39 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I,
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
40 Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S,
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
41 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]:
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
42 if engine.keyWasPressed(c):
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
43 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
44 main_text.text = main_text.text & ($c).toRunes
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
45 else:
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
46 main_text.text = main_text.text & ($c).toRunes[0].toLower()
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
47 if engine.keyWasPressed(Enter):
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
48 main_text.text = main_text.text & Rune('\n')
878
8ab77af322cc add: alignment for text boxes
Sam <sam@basx.dev>
parents: 877
diff changeset
49 if engine.keyWasPressed(Space):
879
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
50 main_text.text = main_text.text & Rune(' ')
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
51
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
52 # remove character
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
53 if engine.keyWasPressed(Backspace) and main_text.text.len > 0:
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
54 main_text.text = main_text.text[0 ..< ^1]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
55
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
56 # alignemtn with F-keys
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
57 if engine.keyWasPressed(F1): main_text.horizontalAlignment = Left
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
58 elif engine.keyWasPressed(F2): main_text.horizontalAlignment = Center
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
59 elif engine.keyWasPressed(F3): main_text.horizontalAlignment = Right
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
60 elif engine.keyWasPressed(F4): main_text.verticalAlignment = Top
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
61 elif engine.keyWasPressed(F5): main_text.verticalAlignment = Center
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
62 elif engine.keyWasPressed(F6): main_text.verticalAlignment = Bottom
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
63
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
64 main_text.text = main_text.text & Rune('_')
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
65 main_text.refresh()
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
66 main_text.text = main_text.text[0 ..< ^1]
7ecae083b670 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 878
diff changeset
67 help_text.refresh()
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
68 engine.renderScene(scene)
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
69 engine.destroy()
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
70
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
71
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
72 when isMainModule:
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
73 main()