Mercurial > games > semicongine
annotate tests/test_panel.nim @ 1096:9809bd9e2cdb
add: tests and did some simplification of code
| author | sam <sam@basx.dev> |
|---|---|
| date | Sun, 07 Apr 2024 01:17:40 +0700 |
| parents | f9d25bc331b3 |
| children | 73b572f82a1f |
| rev | line source |
|---|---|
| 430 | 1 import std/unicode |
| 2 | |
| 3 import semicongine | |
| 4 | |
| 434 | 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) = | |
| 444 | 15 panel.mesh.transform = panel.mesh.transform * scale(1.05, 1.05) |
| 16 panel.color = newVec4f(1, 0, 0, 0.3) | |
| 434 | 17 proc leave(panel: var Panel) = |
| 444 | 18 panel.mesh.transform = panel.mesh.transform * scale(1 / 1.05, 1 / 1.05) |
| 19 panel.color = newVec4f(1, 0, 0, 0.5) | |
| 430 | 20 |
| 21 proc main() = | |
| 22 # setup engine | |
| 23 var engine = initEngine("Test panels") | |
| 24 engine.initRenderer([]) | |
| 25 | |
| 434 | 26 const B = [0'u8, 0'u8, 0'u8, 255'u8] |
| 27 const T = [0'u8, 0'u8, 0'u8, 0'u8] | |
| 430 | 28 # build scene |
| 434 | 29 # |
| 433 | 30 var |
| 31 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32) | |
| 32 scene = Scene(name: "main") | |
| 434 | 33 origin = initPanel( |
| 444 | 34 transform = scale(0.005, 0.005), |
| 434 | 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 ) | |
| 444 | 38 button = initPanel( |
| 39 transform = translate(0.2, 0.1) * scale(0.3, 0.1), | |
| 434 | 40 color = newVec4f(1, 0, 0, 0.5), |
| 41 onMouseDown = click, | |
| 42 onMouseEnter = enter, | |
| 43 onMouseLeave = leave | |
| 44 ) | |
| 433 | 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 | |
|
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 | 57 Right click: Decrease counter""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = translate(-0.9, -0.9) * scale(0.0002, 0.0002)) |
| 430 | 58 |
| 444 | 59 counterText = font.initText(($counter).toRunes, maxLen = 99, transform = translate(0.2, 0.1) * scale(0.0004, 0.0004)) |
| 434 | 60 |
| 61 scene.add counterText | |
| 444 | 62 scene.add button |
| 433 | 63 scene.add help_text |
| 64 scene.add origin | |
| 430 | 65 engine.loadScene(scene) |
| 66 | |
| 67 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | |
| 434 | 68 if engine.keyWasPressed(F1): |
| 444 | 69 button.horizontalAlignment = Left |
| 434 | 70 counterText.horizontalAlignment = Left |
| 71 elif engine.keyWasPressed(F2): | |
| 444 | 72 button.horizontalAlignment = Center |
| 434 | 73 counterText.horizontalAlignment = Center |
| 74 elif engine.keyWasPressed(F3): | |
| 444 | 75 button.horizontalAlignment = Right |
| 434 | 76 counterText.horizontalAlignment = Right |
| 77 elif engine.keyWasPressed(F4): | |
| 444 | 78 button.verticalAlignment = Top |
| 434 | 79 counterText.verticalAlignment = Top |
| 80 elif engine.keyWasPressed(F5): | |
| 444 | 81 button.verticalAlignment = Center |
| 434 | 82 counterText.verticalAlignment = Center |
| 83 elif engine.keyWasPressed(F6): | |
| 444 | 84 button.verticalAlignment = Bottom |
| 434 | 85 counterText.verticalAlignment = Bottom |
| 86 | |
| 444 | 87 engine.processEvents(button) |
| 433 | 88 |
| 444 | 89 button.refresh() |
| 434 | 90 counterText.refresh() |
| 433 | 91 origin.refresh() |
| 92 help_text.refresh() | |
| 430 | 93 |
| 94 engine.renderScene(scene) | |
| 95 engine.destroy() | |
| 96 | |
| 97 | |
| 98 when isMainModule: | |
| 99 main() |
