annotate tests/test_panel.nim @ 1021:73b572f82a1f

add: bases for a better input-system
author sam <sam@basx.dev>
date Thu, 09 May 2024 23:02:35 +0700
parents f9d25bc331b3
children 74957cbf589b
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) =
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
15 panel.mesh.transform = panel.mesh.transform * scale(1.05, 1.05)
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
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) =
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
18 panel.mesh.transform = panel.mesh.transform * scale(1 / 1.05, 1 / 1.05)
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
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
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
23 var engine = initEngine("Test panels")
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
24 engine.initRenderer([])
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
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
31 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
32 scene = Scene(name: "main")
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
33 origin = initPanel(
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
34 transform = scale(0.005, 0.005),
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
35 color = newVec4f(1, 1, 1, 1),
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
36 texture = Texture(isGrayscale: false, colorImage: newImage[RGBAPixel](3, 3, [T, B, T, B, B, B, T, B, T]), sampler: NEAREST_SAMPLER),
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
37 )
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
38 button = initPanel(
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
39 transform = translate(0.2, 0.1) * scale(0.3, 0.1),
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
40 color = newVec4f(1, 0, 0, 0.5),
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 )
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
45 help_text = font.initText("""Controls
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
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
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
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
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
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
61 scene.add counterText
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
62 scene.add button
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
63 scene.add help_text
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
64 scene.add origin
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
65 engine.loadScene(scene)
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
66
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 444
diff changeset
67 while engine.updateInputs() and not keyIsDown(Escape):
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 444
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
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 444
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
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 444
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
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 444
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
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 444
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
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 444
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
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
87 engine.processEvents(button)
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
88
444
f9d25bc331b3 did: same for panels
Sam <sam@basx.dev>
parents: 436
diff changeset
89 button.refresh()
434
30cc1deda4c1 add: input-processing for panels
Sam <sam@basx.dev>
parents: 433
diff changeset
90 counterText.refresh()
433
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
91 origin.refresh()
08b8facaf3b7 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 431
diff changeset
92 help_text.refresh()
430
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
93
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
94 engine.renderScene(scene)
c5999345f42c add: panels
Sam <sam@basx.dev>
parents:
diff changeset
95 engine.destroy()
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()