annotate tests/test_font.nim @ 427:b1c9374c7080

add: nicer key-stroke test, cleanup test-resource files
author Sam <sam@basx.dev>
date Sat, 03 Feb 2024 23:03:34 +0700
parents 1e3c242435af
children 740d2f0d1264
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
1 import std/unicode
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
2
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
4
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
5
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
6 proc main() =
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
7 # setup engine
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
8 var engine = initEngine("Test fonts")
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
9 engine.initRenderer([])
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
10
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
11 # build scene
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
12 var scene = Scene(name: "main")
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
13 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
422
a03b6db25282 add: text-wrapping, fix: incorrect descriptor poolsizes
Sam <sam@basx.dev>
parents: 420
diff changeset
14 var main_text = font.initText("", maxLen = 255, color = newVec4f(1, 0.15, 0.15, 1), scale = 0.0005, maxWidth = 1.0)
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
15 var help_text = font.initText("""Controls
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
16
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
17 Horizontal alignment:
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
18 F1: Left
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
19 F2: Center
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
20 F3: Right
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
21 Vertical alignment:
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
22 F4: Top
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
23 F5: Center
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 418
diff changeset
24 F6: Bottom""", scale = 0.0002, position = newVec2f(0, 0), horizontalAlignment = Left, verticalAlignment = Top)
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
25 scene.add main_text
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
26 scene.add help_text
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
27 engine.loadScene(scene)
427
b1c9374c7080 add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents: 426
diff changeset
28 mixer[].loadSound("key", "key.ogg")
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
29
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
30 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
31 if engine.windowWasResized():
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
32 var winSize = engine.getWindow().size
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
33 main_text.aspect_ratio = winSize[0] / winSize[1]
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
34 help_text.aspect_ratio = winSize[0] / winSize[1]
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
35 help_text.position = newVec2f(-0.99, -0.99)
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
36
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
37 # add character
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
38 if main_text.text.len < main_text.maxLen - 1:
417
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
39 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
40 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
41 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]:
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
42 if engine.keyWasPressed(c):
427
b1c9374c7080 add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents: 426
diff changeset
43 discard mixer[].play("key")
417
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
44 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
45 main_text.text = main_text.text & ($c).toRunes
417
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
46 else:
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
47 main_text.text = main_text.text & ($c).toRunes[0].toLower()
417
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
48 if engine.keyWasPressed(Enter):
427
b1c9374c7080 add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents: 426
diff changeset
49 discard mixer[].play("key")
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
50 main_text.text = main_text.text & Rune('\n')
417
b032768df631 add: alignment for text boxes
Sam <sam@basx.dev>
parents: 416
diff changeset
51 if engine.keyWasPressed(Space):
427
b1c9374c7080 add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents: 426
diff changeset
52 discard mixer[].play("key")
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
53 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
54
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
55 # remove character
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
56 if engine.keyWasPressed(Backspace) and main_text.text.len > 0:
427
b1c9374c7080 add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents: 426
diff changeset
57 discard mixer[].play("key")
418
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
58 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
59
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
60 # alignemtn with F-keys
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
61 if engine.keyWasPressed(F1): main_text.horizontalAlignment = Left
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
62 elif engine.keyWasPressed(F2): main_text.horizontalAlignment = Center
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
63 elif engine.keyWasPressed(F3): main_text.horizontalAlignment = Right
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
64 elif engine.keyWasPressed(F4): main_text.verticalAlignment = Top
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
65 elif engine.keyWasPressed(F5): main_text.verticalAlignment = Center
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
66 elif engine.keyWasPressed(F6): main_text.verticalAlignment = Bottom
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
67
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
68 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
69 main_text.refresh()
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[0 ..< ^1]
009d93d69170 add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents: 417
diff changeset
71 help_text.refresh()
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
72 engine.renderScene(scene)
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
73 engine.destroy()
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
74
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
75
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
76 when isMainModule:
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
77 main()