Mercurial > games > semicongine
annotate tests/test_panel.nim @ 905:a9d71d2c2461
did: same for panels
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 24 Feb 2024 15:35:52 +0700 |
parents | fa54a8a1e3e4 |
children | 73b572f82a1f |
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) = | |
905 | 15 panel.mesh.transform = panel.mesh.transform * scale(1.05, 1.05) |
16 panel.color = newVec4f(1, 0, 0, 0.3) | |
895 | 17 proc leave(panel: var Panel) = |
905 | 18 panel.mesh.transform = panel.mesh.transform * scale(1 / 1.05, 1 / 1.05) |
19 panel.color = newVec4f(1, 0, 0, 0.5) | |
891 | 20 |
21 proc main() = | |
22 # setup engine | |
23 var engine = initEngine("Test panels") | |
24 engine.initRenderer([]) | |
25 | |
895 | 26 const B = [0'u8, 0'u8, 0'u8, 255'u8] |
27 const T = [0'u8, 0'u8, 0'u8, 0'u8] | |
891 | 28 # build scene |
895 | 29 # |
894 | 30 var |
31 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32) | |
32 scene = Scene(name: "main") | |
895 | 33 origin = initPanel( |
905 | 34 transform = scale(0.005, 0.005), |
895 | 35 color = newVec4f(1, 1, 1, 1), |
36 texture = Texture(isGrayscale: false, colorImage: newImage[RGBAPixel](3, 3, [T, B, T, B, B, B, T, B, T]), sampler: NEAREST_SAMPLER), | |
37 ) | |
905 | 38 button = initPanel( |
39 transform = translate(0.2, 0.1) * scale(0.3, 0.1), | |
895 | 40 color = newVec4f(1, 0, 0, 0.5), |
41 onMouseDown = click, | |
42 onMouseEnter = enter, | |
43 onMouseLeave = leave | |
44 ) | |
894 | 45 help_text = font.initText("""Controls |
46 | |
47 Horizontal alignment: | |
48 F1: Left | |
49 F2: Center | |
50 F3: Right | |
51 Vertical alignment: | |
52 F4: Top | |
53 F5: Center | |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
54 F6: Bottom |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
55 Mouse: |
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
895
diff
changeset
|
56 Left click: Increase counter |
905 | 57 Right click: Decrease counter""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = translate(-0.9, -0.9) * scale(0.0002, 0.0002)) |
891 | 58 |
905 | 59 counterText = font.initText(($counter).toRunes, maxLen = 99, transform = translate(0.2, 0.1) * scale(0.0004, 0.0004)) |
895 | 60 |
61 scene.add counterText | |
905 | 62 scene.add button |
894 | 63 scene.add help_text |
64 scene.add origin | |
891 | 65 engine.loadScene(scene) |
66 | |
67 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | |
895 | 68 if engine.keyWasPressed(F1): |
905 | 69 button.horizontalAlignment = Left |
895 | 70 counterText.horizontalAlignment = Left |
71 elif engine.keyWasPressed(F2): | |
905 | 72 button.horizontalAlignment = Center |
895 | 73 counterText.horizontalAlignment = Center |
74 elif engine.keyWasPressed(F3): | |
905 | 75 button.horizontalAlignment = Right |
895 | 76 counterText.horizontalAlignment = Right |
77 elif engine.keyWasPressed(F4): | |
905 | 78 button.verticalAlignment = Top |
895 | 79 counterText.verticalAlignment = Top |
80 elif engine.keyWasPressed(F5): | |
905 | 81 button.verticalAlignment = Center |
895 | 82 counterText.verticalAlignment = Center |
83 elif engine.keyWasPressed(F6): | |
905 | 84 button.verticalAlignment = Bottom |
895 | 85 counterText.verticalAlignment = Bottom |
86 | |
905 | 87 engine.processEvents(button) |
894 | 88 |
905 | 89 button.refresh() |
895 | 90 counterText.refresh() |
894 | 91 origin.refresh() |
92 help_text.refresh() | |
891 | 93 |
94 engine.renderScene(scene) | |
95 engine.destroy() | |
96 | |
97 | |
98 when isMainModule: | |
99 main() |