annotate tests/test_panel.nim @ 895:10267da04fba

add: input-processing for panels
author Sam <sam@basx.dev>
date Sat, 10 Feb 2024 21:19:31 +0700
parents 2aa26c23cc60
children 36b907544820
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) =
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
15 panel.size = newVec2f(0.22, 0.22)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
16 proc leave(panel: var Panel) =
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
17 panel.size = newVec2f(0.2, 0.2)
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
18
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
19 proc main() =
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
20 # setup engine
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
21 var engine = initEngine("Test panels")
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
22 engine.initRenderer([])
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
23
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
24 const B = [0'u8, 0'u8, 0'u8, 255'u8]
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
25 const T = [0'u8, 0'u8, 0'u8, 0'u8]
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
26 # build scene
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
27 #
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
28 var
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
29 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
30 scene = Scene(name: "main")
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
31 origin = initPanel(
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
32 size = newVec2f(0.02, 0.02),
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
33 color = newVec4f(1, 1, 1, 1),
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
34 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
35 )
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
36 panel = initPanel(
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
37 size = newVec2f(0.2, 0.2),
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
38 color = newVec4f(1, 0, 0, 0.5),
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
39 onMouseDown = click,
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
40 onMouseEnter = enter,
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
41 onMouseLeave = leave
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
42 )
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
43 help_text = font.initText("""Controls
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
44
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
45 Horizontal alignment:
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
46 F1: Left
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
47 F2: Center
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
48 F3: Right
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
49 Vertical alignment:
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
50 F4: Top
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
51 F5: Center
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
52 F6: Bottom""", scale = 0.0002, position = newVec2f(-0.9, -0.9), horizontalAlignment = Left, verticalAlignment = Top)
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
53
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
54 counterText = font.initText($counter, maxLen = 99, scale = 0.0004)
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
55
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
56 scene.add counterText
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
57 scene.add panel
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
58 scene.add help_text
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
59 scene.add origin
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
60 engine.loadScene(scene)
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
61
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
62 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
63 if engine.windowWasResized():
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
64 var winSize = engine.getWindow().size
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
65 panel.aspect_ratio = winSize[0] / winSize[1]
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
66 counterText.aspect_ratio = winSize[0] / winSize[1]
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
67 origin.aspect_ratio = winSize[0] / winSize[1]
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
68 help_text.aspect_ratio = winSize[0] / winSize[1]
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
69
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
70 if engine.keyWasPressed(F1):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
71 panel.horizontalAlignment = Left
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
72 counterText.horizontalAlignment = Left
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
73 elif engine.keyWasPressed(F2):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
74 panel.horizontalAlignment = Center
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
75 counterText.horizontalAlignment = Center
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
76 elif engine.keyWasPressed(F3):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
77 panel.horizontalAlignment = Right
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
78 counterText.horizontalAlignment = Right
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
79 elif engine.keyWasPressed(F4):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
80 panel.verticalAlignment = Top
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
81 counterText.verticalAlignment = Top
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
82 elif engine.keyWasPressed(F5):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
83 panel.verticalAlignment = Center
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
84 counterText.verticalAlignment = Center
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
85 elif engine.keyWasPressed(F6):
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
86 panel.verticalAlignment = Bottom
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
87 counterText.verticalAlignment = Bottom
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
88
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
89 engine.processEventsFor(panel)
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
90
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
91 panel.refresh()
895
10267da04fba add: input-processing for panels
Sam <sam@basx.dev>
parents: 894
diff changeset
92 counterText.refresh()
894
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
93 origin.refresh()
2aa26c23cc60 add: initial implementation of "panels"
Sam <sam@basx.dev>
parents: 892
diff changeset
94 help_text.refresh()
891
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
95
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
96 engine.renderScene(scene)
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
97 engine.destroy()
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
98
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
99
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
100 when isMainModule:
edf591800357 add: panels
Sam <sam@basx.dev>
parents:
diff changeset
101 main()