comparison tests/test_font.nim @ 879:7ecae083b670

add: correct version of text-alignment, and a few improvments
author Sam <sam@basx.dev>
date Sun, 28 Jan 2024 21:26:39 +0700
parents 8ab77af322cc
children 91e018270832
comparison
equal deleted inserted replaced
878:8ab77af322cc 879:7ecae083b670
1 import std/unicode 1 import std/unicode
2 2
3 import semicongine 3 import semicongine
4
4 5
5 proc main() = 6 proc main() =
6 # setup engine 7 # setup engine
7 var engine = initEngine("Test fonts") 8 var engine = initEngine("Test fonts")
8 engine.initRenderer([]) 9 engine.initRenderer([])
9 10
10 # build scene 11 # build scene
11 var scene = Scene(name: "main") 12 var scene = Scene(name: "main")
12 # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes) 13 # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes)
13 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 250'f32) 14 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
14 var textbox = initText(32, font, "_", color = newVec4f(1, 0, 0, 1)) 15 var main_text = font.initText("", 32, color = newVec4f(1, 0.15, 0.15, 1), scale = 0.001)
15 let fontscale = 0.001 16 var help_text = font.initText("""Controls
16 scene.add textbox 17
17 textbox.mesh.transform = scale(fontscale, fontscale) 18 Horizontal alignment:
19 F1: Left
20 F2: Center
21 F3: Right
22 Vertical alignment:
23 F4: Top
24 F5: Center
25 F6: Bottom""", scale = 0.0001, position = newVec2f(0, 0), horizontalAlignment = Left, verticalAlignment = Top)
26 scene.add main_text
27 scene.add help_text
18 engine.loadScene(scene) 28 engine.loadScene(scene)
19 29
20 let cursor = Rune('_')
21 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 30 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
22 if engine.windowWasResized(): 31 if engine.windowWasResized():
23 var winSize = engine.getWindow().size 32 var winSize = engine.getWindow().size
24 textbox.mesh.transform = scale(fontscale * (winSize[1] / winSize[0]), fontscale) 33 main_text.aspect_ratio = winSize[0] / winSize[1]
25 if textbox.text.len < textbox.maxLen - 1: 34 help_text.aspect_ratio = winSize[0] / winSize[1]
35 help_text.position = newVec2f(-0.99, -0.99)
36
37 # add character
38 if main_text.text.len < main_text.maxLen - 1:
26 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, 39 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, 40 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]: 41 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]:
29 if engine.keyWasPressed(c): 42 if engine.keyWasPressed(c):
30 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): 43 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
31 textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes & cursor 44 main_text.text = main_text.text & ($c).toRunes
32 else: 45 else:
33 textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes[0].toLower() & cursor 46 main_text.text = main_text.text & ($c).toRunes[0].toLower()
34 if engine.keyWasPressed(Enter): 47 if engine.keyWasPressed(Enter):
35 textbox.text = textbox.text[0 ..< ^1] & Rune('\n') & cursor 48 main_text.text = main_text.text & Rune('\n')
36 if engine.keyWasPressed(Space): 49 if engine.keyWasPressed(Space):
37 textbox.text = textbox.text[0 ..< ^1] & Rune(' ') & cursor 50 main_text.text = main_text.text & Rune(' ')
38 if engine.keyWasPressed(Backspace) and textbox.text.len > 1: 51
39 textbox.text = textbox.text[0 ..< ^2] & cursor 52 # remove character
53 if engine.keyWasPressed(Backspace) and main_text.text.len > 0:
54 main_text.text = main_text.text[0 ..< ^1]
55
56 # alignemtn with F-keys
57 if engine.keyWasPressed(F1): main_text.horizontalAlignment = Left
58 elif engine.keyWasPressed(F2): main_text.horizontalAlignment = Center
59 elif engine.keyWasPressed(F3): main_text.horizontalAlignment = Right
60 elif engine.keyWasPressed(F4): main_text.verticalAlignment = Top
61 elif engine.keyWasPressed(F5): main_text.verticalAlignment = Center
62 elif engine.keyWasPressed(F6): main_text.verticalAlignment = Bottom
63
64 main_text.text = main_text.text & Rune('_')
65 main_text.refresh()
66 main_text.text = main_text.text[0 ..< ^1]
67 help_text.refresh()
40 engine.renderScene(scene) 68 engine.renderScene(scene)
41 engine.destroy() 69 engine.destroy()
42 70
43 71
44 when isMainModule: 72 when isMainModule: