Mercurial > games > semicongine
comparison tests/test_panel.nim @ 434:30cc1deda4c1
add: input-processing for panels
| author | Sam <sam@basx.dev> | 
|---|---|
| date | Sat, 10 Feb 2024 21:19:31 +0700 | 
| parents | 08b8facaf3b7 | 
| children | 36b907544820 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 433:08b8facaf3b7 | 434:30cc1deda4c1 | 
|---|---|
| 1 import std/unicode | 1 import std/unicode | 
| 2 | 2 | 
| 3 import semicongine | 3 import semicongine | 
| 4 | 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.size = newVec2f(0.22, 0.22) | |
| 16 proc leave(panel: var Panel) = | |
| 17 panel.size = newVec2f(0.2, 0.2) | |
| 5 | 18 | 
| 6 proc main() = | 19 proc main() = | 
| 7 # setup engine | 20 # setup engine | 
| 8 var engine = initEngine("Test panels") | 21 var engine = initEngine("Test panels") | 
| 9 engine.initRenderer([]) | 22 engine.initRenderer([]) | 
| 10 | 23 | 
| 24 const B = [0'u8, 0'u8, 0'u8, 255'u8] | |
| 25 const T = [0'u8, 0'u8, 0'u8, 0'u8] | |
| 11 # build scene | 26 # build scene | 
| 27 # | |
| 12 var | 28 var | 
| 13 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32) | 29 font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32) | 
| 14 scene = Scene(name: "main") | 30 scene = Scene(name: "main") | 
| 15 origin = initPanel(size = newVec2f(0.01, 0.01), color = newVec4f(0, 0, 0, 1)) | 31 origin = initPanel( | 
| 16 panel = initPanel(size = newVec2f(0.2, 0.2), color = newVec4f(1, 0, 0, 1)) | 32 size = newVec2f(0.02, 0.02), | 
| 33 color = newVec4f(1, 1, 1, 1), | |
| 34 texture = Texture(isGrayscale: false, colorImage: newImage[RGBAPixel](3, 3, [T, B, T, B, B, B, T, B, T]), sampler: NEAREST_SAMPLER), | |
| 35 ) | |
| 36 panel = initPanel( | |
| 37 size = newVec2f(0.2, 0.2), | |
| 38 color = newVec4f(1, 0, 0, 0.5), | |
| 39 onMouseDown = click, | |
| 40 onMouseEnter = enter, | |
| 41 onMouseLeave = leave | |
| 42 ) | |
| 17 help_text = font.initText("""Controls | 43 help_text = font.initText("""Controls | 
| 18 | 44 | 
| 19 Horizontal alignment: | 45 Horizontal alignment: | 
| 20 F1: Left | 46 F1: Left | 
| 21 F2: Center | 47 F2: Center | 
| 23 Vertical alignment: | 49 Vertical alignment: | 
| 24 F4: Top | 50 F4: Top | 
| 25 F5: Center | 51 F5: Center | 
| 26 F6: Bottom""", scale = 0.0002, position = newVec2f(-0.9, -0.9), horizontalAlignment = Left, verticalAlignment = Top) | 52 F6: Bottom""", scale = 0.0002, position = newVec2f(-0.9, -0.9), horizontalAlignment = Left, verticalAlignment = Top) | 
| 27 | 53 | 
| 54 counterText = font.initText($counter, maxLen = 99, scale = 0.0004) | |
| 55 | |
| 56 scene.add counterText | |
| 28 scene.add panel | 57 scene.add panel | 
| 29 scene.add help_text | 58 scene.add help_text | 
| 30 scene.add origin | 59 scene.add origin | 
| 31 engine.loadScene(scene) | 60 engine.loadScene(scene) | 
| 32 | 61 | 
| 33 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | 62 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | 
| 34 if engine.windowWasResized(): | 63 if engine.windowWasResized(): | 
| 35 var winSize = engine.getWindow().size | 64 var winSize = engine.getWindow().size | 
| 36 panel.aspect_ratio = winSize[0] / winSize[1] | 65 panel.aspect_ratio = winSize[0] / winSize[1] | 
| 66 counterText.aspect_ratio = winSize[0] / winSize[1] | |
| 37 origin.aspect_ratio = winSize[0] / winSize[1] | 67 origin.aspect_ratio = winSize[0] / winSize[1] | 
| 38 help_text.aspect_ratio = winSize[0] / winSize[1] | 68 help_text.aspect_ratio = winSize[0] / winSize[1] | 
| 39 | 69 | 
| 40 if engine.keyWasPressed(F1): panel.horizontalAlignment = Left | 70 if engine.keyWasPressed(F1): | 
| 41 elif engine.keyWasPressed(F2): panel.horizontalAlignment = Center | 71 panel.horizontalAlignment = Left | 
| 42 elif engine.keyWasPressed(F3): panel.horizontalAlignment = Right | 72 counterText.horizontalAlignment = Left | 
| 43 elif engine.keyWasPressed(F4): panel.verticalAlignment = Top | 73 elif engine.keyWasPressed(F2): | 
| 44 elif engine.keyWasPressed(F5): panel.verticalAlignment = Center | 74 panel.horizontalAlignment = Center | 
| 45 elif engine.keyWasPressed(F6): panel.verticalAlignment = Bottom | 75 counterText.horizontalAlignment = Center | 
| 76 elif engine.keyWasPressed(F3): | |
| 77 panel.horizontalAlignment = Right | |
| 78 counterText.horizontalAlignment = Right | |
| 79 elif engine.keyWasPressed(F4): | |
| 80 panel.verticalAlignment = Top | |
| 81 counterText.verticalAlignment = Top | |
| 82 elif engine.keyWasPressed(F5): | |
| 83 panel.verticalAlignment = Center | |
| 84 counterText.verticalAlignment = Center | |
| 85 elif engine.keyWasPressed(F6): | |
| 86 panel.verticalAlignment = Bottom | |
| 87 counterText.verticalAlignment = Bottom | |
| 88 | |
| 89 engine.processEventsFor(panel) | |
| 46 | 90 | 
| 47 panel.refresh() | 91 panel.refresh() | 
| 92 counterText.refresh() | |
| 48 origin.refresh() | 93 origin.refresh() | 
| 49 help_text.refresh() | 94 help_text.refresh() | 
| 50 | 95 | 
| 51 engine.renderScene(scene) | 96 engine.renderScene(scene) | 
| 52 engine.destroy() | 97 engine.destroy() | 
