comparison old_tests/test_panel.nim @ 1227:4d97cfc4888b

merge
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 23:45:43 +0700
parents 6360c8d17ce0
children
comparison
equal deleted inserted replaced
1170:2addc5f6804f 1227:4d97cfc4888b
1 import std/unicode
2
3 import semicongine
4
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.mesh.transform = panel.mesh.transform * Scale(1.05, 1.05)
16 panel.color = NewVec4f(1, 0, 0, 0.3)
17 proc leave(panel: var Panel) =
18 panel.mesh.transform = panel.mesh.transform * Scale(1 / 1.05, 1 / 1.05)
19 panel.color = NewVec4f(1, 0, 0, 0.5)
20
21 proc main() =
22 # setup engine
23 var engine = InitEngine("Test panels")
24 engine.InitRenderer([])
25
26 const B = [0'u8, 0'u8, 0'u8, 255'u8]
27 const T = [0'u8, 0'u8, 0'u8, 0'u8]
28 # build scene
29 #
30 var
31 font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
32 scene = Scene(name: "main")
33 origin = InitPanel(
34 transform = Scale(0.005, 0.005),
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 )
38 button = InitPanel(
39 transform = Translate(0.2, 0.1) * Scale(0.3, 0.1),
40 color = NewVec4f(1, 0, 0, 0.5),
41 onMouseDown = click,
42 onMouseEnter = enter,
43 onMouseLeave = leave
44 )
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
54 F6: Bottom
55 Mouse:
56 Left click: Increase counter
57 Right click: Decrease counter""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = Translate(-0.9, -0.9) * Scale(0.0002, 0.0002))
58
59 counterText = font.InitText(($counter).toRunes, maxLen = 99, transform = Translate(0.2, 0.1) * Scale(0.0004, 0.0004))
60
61 scene.Add counterText
62 scene.Add button
63 scene.Add help_text
64 scene.Add origin
65 engine.LoadScene(scene)
66
67 while engine.UpdateInputs() and not KeyIsDown(Escape):
68 if KeyWasPressed(F1):
69 button.horizontalAlignment = Left
70 counterText.horizontalAlignment = Left
71 elif KeyWasPressed(F2):
72 button.horizontalAlignment = Center
73 counterText.horizontalAlignment = Center
74 elif KeyWasPressed(F3):
75 button.horizontalAlignment = Right
76 counterText.horizontalAlignment = Right
77 elif KeyWasPressed(F4):
78 button.verticalAlignment = Top
79 counterText.verticalAlignment = Top
80 elif KeyWasPressed(F5):
81 button.verticalAlignment = Center
82 counterText.verticalAlignment = Center
83 elif KeyWasPressed(F6):
84 button.verticalAlignment = Bottom
85 counterText.verticalAlignment = Bottom
86
87 engine.ProcessEvents(button)
88
89 button.Refresh()
90 counterText.Refresh()
91 origin.Refresh()
92 help_text.Refresh()
93
94 engine.RenderScene(scene)
95 engine.Destroy()
96
97
98 when isMainModule:
99 main()