annotate tests/test_panel.nim @ 906:273f64d80f1f

fix: wrong comparison
author Sam <sam@basx.dev>
date Sat, 24 Feb 2024 17:08:58 +0700
parents a9d71d2c2461
children 73b572f82a1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/unicode
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
2
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
4
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
5 var counter = 0
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
6 var counterText: Text
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
7
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
8 proc click(panel: var Panel, buttons: set[MouseButton]) =
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
9 if buttons.contains(Mouse1):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
10 counter.inc
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
11 if buttons.contains(Mouse3):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
12 counter.dec
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
13 counterText.text = $counter
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
14 proc enter(panel: var Panel) =
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
15 panel.mesh.transform = panel.mesh.transform * scale(1.05, 1.05)
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
16 panel.color = newVec4f(1, 0, 0, 0.3)
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
17 proc leave(panel: var Panel) =
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
18 panel.mesh.transform = panel.mesh.transform * scale(1 / 1.05, 1 / 1.05)
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
19 panel.color = newVec4f(1, 0, 0, 0.5)
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
20
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
21 proc main() =
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
22 # setup engine
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
23 var engine = initEngine("Test panels")
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
24 engine.initRenderer([])
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
25
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
26 const B = [0'u8, 0'u8, 0'u8, 255'u8]
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
27 const T = [0'u8, 0'u8, 0'u8, 0'u8]
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
28 # build scene
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
29 #
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
30 var
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
31 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
32 scene = Scene(name: "main")
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
33 origin = initPanel(
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
34 transform = scale(0.005, 0.005),
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
35 color = newVec4f(1, 1, 1, 1),
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
36 texture = Texture(isGrayscale: false, colorImage: newImage[RGBAPixel](3, 3, [T, B, T, B, B, B, T, B, T]), sampler: NEAREST_SAMPLER),
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
37 )
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
38 button = initPanel(
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
39 transform = translate(0.2, 0.1) * scale(0.3, 0.1),
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
40 color = newVec4f(1, 0, 0, 0.5),
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
41 onMouseDown = click,
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
42 onMouseEnter = enter,
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
43 onMouseLeave = leave
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
44 )
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
45 help_text = font.initText("""Controls
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
46
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
47 Horizontal alignment:
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
48 F1: Left
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
49 F2: Center
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
50 F3: Right
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
51 Vertical alignment:
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
52 F4: Top
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
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
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
57 Right click: Decrease counter""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = translate(-0.9, -0.9) * scale(0.0002, 0.0002))
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
58
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
59 counterText = font.initText(($counter).toRunes, maxLen = 99, transform = translate(0.2, 0.1) * scale(0.0004, 0.0004))
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
60
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
61 scene.add counterText
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
62 scene.add button
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
63 scene.add help_text
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
64 scene.add origin
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
65 engine.loadScene(scene)
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
66
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
67 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
68 if engine.keyWasPressed(F1):
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
69 button.horizontalAlignment = Left
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
70 counterText.horizontalAlignment = Left
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
71 elif engine.keyWasPressed(F2):
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
72 button.horizontalAlignment = Center
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
73 counterText.horizontalAlignment = Center
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
74 elif engine.keyWasPressed(F3):
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
75 button.horizontalAlignment = Right
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
76 counterText.horizontalAlignment = Right
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
77 elif engine.keyWasPressed(F4):
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
78 button.verticalAlignment = Top
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
79 counterText.verticalAlignment = Top
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
80 elif engine.keyWasPressed(F5):
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
81 button.verticalAlignment = Center
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
82 counterText.verticalAlignment = Center
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
83 elif engine.keyWasPressed(F6):
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
84 button.verticalAlignment = Bottom
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
85 counterText.verticalAlignment = Bottom
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
86
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
87 engine.processEvents(button)
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
88
905
a9d71d2c2461 did: same for panels
Sam <sam@basx.dev>
parents: 897
diff changeset
89 button.refresh()
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
90 counterText.refresh()
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
91 origin.refresh()
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
92 help_text.refresh()
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
93
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
94 engine.renderScene(scene)
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
95 engine.destroy()
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
96
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
97
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
98 when isMainModule:
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
99 main()