Mercurial > games > semicongine
annotate tests/test_font.nim @ 417:b032768df631
add: alignment for text boxes
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 28 Jan 2024 00:41:11 +0700 |
parents | 73cca428e27a |
children | 009d93d69170 |
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 | 3 import semicongine |
4 | |
5 proc main() = | |
371
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
344
diff
changeset
|
6 # setup engine |
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
344
diff
changeset
|
7 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
|
8 engine.initRenderer([]) |
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
344
diff
changeset
|
9 |
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
344
diff
changeset
|
10 # build scene |
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
344
diff
changeset
|
11 var scene = Scene(name: "main") |
411
e50c57423567
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
371
diff
changeset
|
12 # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes) |
416 | 13 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 250'f32) |
14 var textbox = initText(32, font, "_", color = newVec4f(1, 0, 0, 1)) | |
15 let fontscale = 0.001 | |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
16 scene.add textbox |
411
e50c57423567
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
371
diff
changeset
|
17 textbox.mesh.transform = scale(fontscale, fontscale) |
371
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
344
diff
changeset
|
18 engine.loadScene(scene) |
272
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
265
diff
changeset
|
19 |
416 | 20 let cursor = Rune('_') |
265 | 21 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
|
22 if engine.windowWasResized(): |
bfcb41211c5b
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
265
diff
changeset
|
23 var winSize = engine.getWindow().size |
411
e50c57423567
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
371
diff
changeset
|
24 textbox.mesh.transform = scale(fontscale * (winSize[1] / winSize[0]), fontscale) |
417 | 25 if textbox.text.len < textbox.maxLen - 1: |
26 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, | |
27 Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, | |
28 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]: | |
29 if engine.keyWasPressed(c): | |
30 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): | |
31 textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes & cursor | |
32 else: | |
33 textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes[0].toLower() & cursor | |
34 if engine.keyWasPressed(Enter): | |
35 textbox.text = textbox.text[0 ..< ^1] & Rune('\n') & cursor | |
36 if engine.keyWasPressed(Space): | |
37 textbox.text = textbox.text[0 ..< ^1] & Rune(' ') & cursor | |
416 | 38 if engine.keyWasPressed(Backspace) and textbox.text.len > 1: |
39 textbox.text = textbox.text[0 ..< ^2] & cursor | |
265 | 40 engine.renderScene(scene) |
41 engine.destroy() | |
42 | |
43 | |
44 when isMainModule: | |
45 main() |