annotate old_tests/test_font.nim @ 1364:8f774d9c2518 default tip

fix: change implementation to allow for easier reload of whole engine
author sam <sam@basx.dev>
date Tue, 05 Nov 2024 23:28:25 +0700
parents 6360c8d17ce0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
429
740d2f0d1264 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 427
diff changeset
1 import std/times
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
2 import std/unicode
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
3
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
5
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
6
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
7 proc main() =
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
8 # setup engine
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
9 var engine = InitEngine("Test fonts")
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
10 engine.InitRenderer([])
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
11
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
12 # build scene
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
13 var scene = Scene(name: "main")
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
14 var font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
15 var origin = InitPanel(transform = Scale(0.01, 0.01))
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
16 var main_text = font.InitText("".toRunes, maxLen = 255, color = NewVec4f(1, 0.15, 0.15, 1), maxWidth = 1.0, transform = Scale(0.0005, 0.0005))
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
17 var help_text = font.InitText("""Controls
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
18
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
19 Horizontal alignment:
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
20 F1: Left
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
21 F2: Center
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
22 F3: Right
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
23 Vertical alignment:
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
24 F4: Top
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
25 F5: Center
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
26 F6: Bottom""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = Translate(-0.9, -0.9) * Scale(0.0002, 0.0002))
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
27 scene.Add origin
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
28 scene.Add main_text
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
29 scene.Add help_text
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
30 engine.LoadScene(scene)
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
31 mixer[].LoadSound("key", "key.ogg")
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
32 mixer[].SetLevel(0.5)
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
33
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
34 while engine.UpdateInputs() and not KeyIsDown(Escape):
429
740d2f0d1264 did: small fix with materials & cleanup
Sam <sam@basx.dev>
parents: 427
diff changeset
35 var t = cpuTime()
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
36 main_text.Color = NewVec4f(sin(t) * 0.5 + 0.5, 0.15, 0.15, 1)
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
37
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
38 # add character
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
39 if main_text.text.len < main_text.maxLen - 1:
417
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
40 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I,
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
41 Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S,
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
42 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]:
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
43 if KeyWasPressed(c):
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
44 discard mixer[].Play("key")
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
45 if KeyIsDown(ShiftL) or KeyIsDown(ShiftR):
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
46 main_text.text = main_text.text & ($c).toRunes
417
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
47 else:
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
48 main_text.text = main_text.text & ($c).toRunes[0].toLower()
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
49 if KeyWasPressed(Enter):
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
50 discard mixer[].Play("key")
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
51 main_text.text = main_text.text & Rune('\n')
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
52 if KeyWasPressed(Space):
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
53 discard mixer[].Play("key")
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
54 main_text.text = main_text.text & Rune(' ')
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
55
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
56 # remove character
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
57 if KeyWasPressed(Backspace) and main_text.text.len > 0:
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
58 discard mixer[].Play("key")
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
59 main_text.text = main_text.text[0 ..< ^1]
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
60
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
61 # alignemtn with F-keys
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
62 if KeyWasPressed(F1): main_text.horizontalAlignment = Left
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
63 elif KeyWasPressed(F2): main_text.horizontalAlignment = Center
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
64 elif KeyWasPressed(F3): main_text.horizontalAlignment = Right
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
65 elif KeyWasPressed(F4): main_text.verticalAlignment = Top
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
66 elif KeyWasPressed(F5): main_text.verticalAlignment = Center
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
67 elif KeyWasPressed(F6): main_text.verticalAlignment = Bottom
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
68
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
69 origin.Refresh()
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
70 main_text.text = main_text.text & Rune('_')
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
71 main_text.Refresh()
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
72 main_text.text = main_text.text[0 ..< ^1]
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
73 help_text.Refresh()
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
74 engine.RenderScene(scene)
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
75 engine.Destroy()
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
76
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
77
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
78 when isMainModule:
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
79 main()