Mercurial > games > semicongine
annotate tests/test_font.nim @ 1021:73b572f82a1f
add: bases for a better input-system
author | sam <sam@basx.dev> |
---|---|
date | Thu, 09 May 2024 23:02:35 +0700 |
parents | 6406766a222d |
children | 74957cbf589b |
rev | line source |
---|---|
429 | 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 | 4 import semicongine |
5 | |
418
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
6 |
265 | 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 |
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
344
diff
changeset
|
9 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
|
10 engine.initRenderer([]) |
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") |
418
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
14 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32) |
979 | 15 var origin = initPanel(transform = scale(0.01, 0.01)) |
443
7629f85823a4
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
436
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)) |
418
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
17 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
|
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 |
443
7629f85823a4
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
436
diff
changeset
|
26 F6: Bottom""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = translate(-0.9, -0.9) * scale(0.0002, 0.0002)) |
436
36b907544820
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
432
diff
changeset
|
27 scene.add origin |
418
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
28 scene.add main_text |
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
29 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
|
30 engine.loadScene(scene) |
427
b1c9374c7080
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
426
diff
changeset
|
31 mixer[].loadSound("key", "key.ogg") |
429 | 32 mixer[].setLevel(0.5) |
272
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
265
diff
changeset
|
33 |
1021 | 34 while engine.updateInputs() and not keyIsDown(Escape): |
429 | 35 var t = cpuTime() |
36 main_text.color = newVec4f(sin(t) * 0.5 + 0.5, 0.15, 0.15, 1) | |
1021 | 37 if windowWasResized(): |
272
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
265
diff
changeset
|
38 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
|
39 |
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
40 # add character |
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
41 if main_text.text.len < main_text.maxLen - 1: |
417 | 42 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, |
43 Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, | |
44 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]: | |
1021 | 45 if keyWasPressed(c): |
427
b1c9374c7080
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
426
diff
changeset
|
46 discard mixer[].play("key") |
1021 | 47 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
|
48 main_text.text = main_text.text & ($c).toRunes |
417 | 49 else: |
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 & ($c).toRunes[0].toLower() |
1021 | 51 if keyWasPressed(Enter): |
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('\n') |
1021 | 54 if keyWasPressed(Space): |
427
b1c9374c7080
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
426
diff
changeset
|
55 discard mixer[].play("key") |
418
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
56 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
|
57 |
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
58 # remove character |
1021 | 59 if 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
|
60 discard mixer[].play("key") |
418
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
61 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
|
62 |
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
63 # alignemtn with F-keys |
1021 | 64 if keyWasPressed(F1): main_text.horizontalAlignment = Left |
65 elif keyWasPressed(F2): main_text.horizontalAlignment = Center | |
66 elif keyWasPressed(F3): main_text.horizontalAlignment = Right | |
67 elif keyWasPressed(F4): main_text.verticalAlignment = Top | |
68 elif keyWasPressed(F5): main_text.verticalAlignment = Center | |
69 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
|
70 |
436
36b907544820
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
432
diff
changeset
|
71 origin.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 & Rune('_') |
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
73 main_text.refresh() |
009d93d69170
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
417
diff
changeset
|
74 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
|
75 help_text.refresh() |
265 | 76 engine.renderScene(scene) |
77 engine.destroy() | |
78 | |
79 | |
80 when isMainModule: | |
81 main() |