annotate old_tests/test_panel.nim @ 1327:373a4888f6ac

did: rework font-rendering
author sam <sam@basx.dev>
date Sat, 17 Aug 2024 13:54:22 +0700
parents 6360c8d17ce0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/unicode
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
2
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
4
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
5 var counter = 0
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
6 var counterText: Text
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
7
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
8 proc click(panel: var Panel, buttons: set[MouseButton]) =
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
9 if buttons.contains(Mouse1):
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
10 counter.inc
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
11 if buttons.contains(Mouse3):
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
12 counter.dec
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
13 counterText.text = $counter
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
14 proc enter(panel: var Panel) =
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
15 panel.mesh.transform = panel.mesh.transform * Scale(1.05, 1.05)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
16 panel.color = NewVec4f(1, 0, 0, 0.3)
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
17 proc leave(panel: var Panel) =
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
18 panel.mesh.transform = panel.mesh.transform * Scale(1 / 1.05, 1 / 1.05)
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
19 panel.color = NewVec4f(1, 0, 0, 0.5)
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
20
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
21 proc main() =
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
22 # setup engine
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
23 var engine = InitEngine("Test panels")
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
24 engine.InitRenderer([])
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
25
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
26 const B = [0'u8, 0'u8, 0'u8, 255'u8]
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
27 const T = [0'u8, 0'u8, 0'u8, 0'u8]
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
28 # build scene
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
29 #
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
30 var
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
31 font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
32 scene = Scene(name: "main")
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
33 origin = InitPanel(
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
34 transform = Scale(0.005, 0.005),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
35 color = NewVec4f(1, 1, 1, 1),
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
36 texture = Texture(isGrayscale: false, colorImage: NewImage[RGBAPixel](3, 3, [T, B, T, B, B, B, T, B, T]), sampler: NEAREST_SAMPLER),
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
37 )
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
38 button = InitPanel(
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
39 transform = Translate(0.2, 0.1) * Scale(0.3, 0.1),
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
40 color = NewVec4f(1, 0, 0, 0.5),
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
41 onMouseDown = click,
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
42 onMouseEnter = enter,
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
43 onMouseLeave = leave
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
44 )
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
45 help_text = font.InitText("""Controls
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
46
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
47 Horizontal alignment:
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
48 F1: Left
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
49 F2: Center
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
50 F3: Right
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
51 Vertical alignment:
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
52 F4: Top
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
53 F5: Center
436
36b907544820 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 434
diff changeset
54 F6: Bottom
36b907544820 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 434
diff changeset
55 Mouse:
36b907544820 fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents: 434
diff changeset
56 Left click: Increase counter
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
57 Right click: Decrease counter""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = Translate(-0.9, -0.9) * Scale(0.0002, 0.0002))
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
58
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
59 counterText = font.InitText(($counter).toRunes, maxLen = 99, transform = Translate(0.2, 0.1) * Scale(0.0004, 0.0004))
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
60
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
61 scene.Add counterText
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
62 scene.Add button
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
63 scene.Add help_text
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
64 scene.Add origin
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
65 engine.LoadScene(scene)
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
66
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
67 while engine.UpdateInputs() and not KeyIsDown(Escape):
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
68 if KeyWasPressed(F1):
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
69 button.horizontalAlignment = Left
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
70 counterText.horizontalAlignment = Left
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
71 elif KeyWasPressed(F2):
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
72 button.horizontalAlignment = Center
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
73 counterText.horizontalAlignment = Center
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
74 elif KeyWasPressed(F3):
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
75 button.horizontalAlignment = Right
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
76 counterText.horizontalAlignment = Right
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
77 elif KeyWasPressed(F4):
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
78 button.verticalAlignment = Top
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
79 counterText.verticalAlignment = Top
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
80 elif KeyWasPressed(F5):
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
81 button.verticalAlignment = Center
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
82 counterText.verticalAlignment = Center
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
83 elif KeyWasPressed(F6):
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
84 button.verticalAlignment = Bottom
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
85 counterText.verticalAlignment = Bottom
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
86
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
87 engine.ProcessEvents(button)
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
88
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
89 button.Refresh()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
90 counterText.Refresh()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
91 origin.Refresh()
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
92 help_text.Refresh()
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
93
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
94 engine.RenderScene(scene)
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
95 engine.Destroy()
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
96
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
97
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
98 when isMainModule:
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
99 main()