Mercurial > games > semicongine
annotate tests/test_panel.nim @ 897:fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 11 Feb 2024 18:47:13 +0700 |
parents | 10267da04fba |
children | f9d25bc331b3 |
rev | line source |
---|---|
891 | 1 import std/unicode |
2 | |
3 import semicongine | |
4 | |
895 | 5 var counter = 0 |
6 var counterText: Text | |
7 | |
8 proc click(panel: var Panel, buttons: set[MouseButton]) = | |
9 if buttons.contains(Mouse1): | |
10 counter.inc | |
11 if buttons.contains(Mouse3): | |
12 counter.dec | |
13 counterText.text = $counter | |
14 proc enter(panel: var Panel) = | |
15 panel.size = newVec2f(0.22, 0.22) | |
16 proc leave(panel: var Panel) = | |
17 panel.size = newVec2f(0.2, 0.2) | |
891 | 18 |
19 proc main() = | |
20 # setup engine | |
21 var engine = initEngine("Test panels") | |
22 engine.initRenderer([]) | |
23 | |
895 | 24 const B = [0'u8, 0'u8, 0'u8, 255'u8] |
25 const T = [0'u8, 0'u8, 0'u8, 0'u8] | |
891 | 26 # build scene |
895 | 27 # |
894 | 28 var |
29 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32) | |
30 scene = Scene(name: "main") | |
895 | 31 origin = initPanel( |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
32 size = newVec2f(0.005, 0.005), |
895 | 33 color = newVec4f(1, 1, 1, 1), |
34 texture = Texture(isGrayscale: false, colorImage: newImage[RGBAPixel](3, 3, [T, B, T, B, B, B, T, B, T]), sampler: NEAREST_SAMPLER), | |
35 ) | |
36 panel = initPanel( | |
37 size = newVec2f(0.2, 0.2), | |
38 color = newVec4f(1, 0, 0, 0.5), | |
39 onMouseDown = click, | |
40 onMouseEnter = enter, | |
41 onMouseLeave = leave | |
42 ) | |
894 | 43 help_text = font.initText("""Controls |
44 | |
45 Horizontal alignment: | |
46 F1: Left | |
47 F2: Center | |
48 F3: Right | |
49 Vertical alignment: | |
50 F4: Top | |
51 F5: Center | |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
52 F6: Bottom |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
53 Mouse: |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
54 Left click: Increase counter |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
55 Right click: Decrease counter""", scale = 0.0002, position = newVec2f(-0.9, -0.9), horizontalAlignment = Left, verticalAlignment = Top) |
891 | 56 |
895 | 57 counterText = font.initText($counter, maxLen = 99, scale = 0.0004) |
58 | |
59 scene.add counterText | |
891 | 60 scene.add panel |
894 | 61 scene.add help_text |
62 scene.add origin | |
891 | 63 engine.loadScene(scene) |
64 | |
65 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | |
66 if engine.windowWasResized(): | |
67 var winSize = engine.getWindow().size | |
68 panel.aspect_ratio = winSize[0] / winSize[1] | |
895 | 69 counterText.aspect_ratio = winSize[0] / winSize[1] |
894 | 70 origin.aspect_ratio = winSize[0] / winSize[1] |
71 help_text.aspect_ratio = winSize[0] / winSize[1] | |
72 | |
895 | 73 if engine.keyWasPressed(F1): |
74 panel.horizontalAlignment = Left | |
75 counterText.horizontalAlignment = Left | |
76 elif engine.keyWasPressed(F2): | |
77 panel.horizontalAlignment = Center | |
78 counterText.horizontalAlignment = Center | |
79 elif engine.keyWasPressed(F3): | |
80 panel.horizontalAlignment = Right | |
81 counterText.horizontalAlignment = Right | |
82 elif engine.keyWasPressed(F4): | |
83 panel.verticalAlignment = Top | |
84 counterText.verticalAlignment = Top | |
85 elif engine.keyWasPressed(F5): | |
86 panel.verticalAlignment = Center | |
87 counterText.verticalAlignment = Center | |
88 elif engine.keyWasPressed(F6): | |
89 panel.verticalAlignment = Bottom | |
90 counterText.verticalAlignment = Bottom | |
91 | |
92 engine.processEventsFor(panel) | |
894 | 93 |
94 panel.refresh() | |
895 | 95 counterText.refresh() |
894 | 96 origin.refresh() |
97 help_text.refresh() | |
891 | 98 |
99 engine.renderScene(scene) | |
100 engine.destroy() | |
101 | |
102 | |
103 when isMainModule: | |
104 main() |