annotate tests/test_panel.nim @ 436:36b907544820

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