Mercurial > games > semicongine
annotate examples/E04_input.nim @ 525:0285ff2281b1
add: keyboard/mouse inpute test
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 04 Feb 2023 02:24:41 +0700 |
parents | f2c97bdbb0b3 |
children | de433a47e635 |
rev | line source |
---|---|
525 | 1 import std/tables |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/strutils |
525 | 3 import std/typetraits |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/times |
525 | 5 import std/math |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 import semicongine |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 type |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 # define type of vertex |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 VertexDataA = object |
525 | 12 position: PositionAttribute[Vec3] |
13 color: ColorAttribute[Vec4] | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 transform: ModelTransformAttribute |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 Uniforms = object |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 projection: Descriptor[Mat44] |
525 | 17 |
18 const | |
19 arrow = @[ | |
20 Vec3([-1'f32, -1'f32, 0'f32]), | |
21 Vec3([1'f32, -1'f32, 0'f32]), | |
22 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | |
23 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | |
24 Vec3([-1'f32, 1'f32, 0'f32]), | |
25 Vec3([-1'f32, -1'f32, 0'f32]), | |
26 ] | |
27 arrow_colors = @[ | |
28 Vec4([1'f32, 0'f32, 0'f32, 1'f32]), | |
29 Vec4([1'f32, 0'f32, 0'f32, 1'f32]), | |
30 Vec4([1'f32, 0'f32, 0'f32, 1'f32]), | |
31 Vec4([0.8'f32, 0'f32, 0'f32, 1'f32]), | |
32 Vec4([0.8'f32, 0'f32, 0'f32, 1'f32]), | |
33 Vec4([0.8'f32, 0'f32, 0'f32, 1'f32]), | |
34 ] | |
35 # row width is 15, should sum up | |
36 keyrows = ( | |
37 [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 3.0], | |
38 [1.2, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.8], | |
39 [1.8, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2], | |
40 [2.1, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], | |
41 [3.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 3.0], | |
42 [1.5, 1.5, 1.5, 6, 1.5, 1.5, 1.5], | |
43 ) | |
44 rowWidth = 900 | |
45 keyHeight = 50'f32 | |
46 keyGap = 10'f32 | |
47 baseColor = Vec4([1'f32, 0'f32, 0'f32, 0'f32]) | |
48 activeColor = Vec4([1'f32, 1'f32, 1'f32, 0'f32]) | |
49 keyMap = { | |
50 Escape: 0, F1: 1, F2: 2, F3: 3, F4: 4, F5: 5, F6: 6, F7: 7, F8: 8, F9: 9, | |
51 F10: 10, F11: 11, F12: 12, | |
52 NumberRowExtra1: 13, `1`: 14, `2`: 15, `3`: 16, `4`: 17, `5`: 18, `6`: 19, | |
53 `7`: 20, `8`: 21, `9`: 22, `0`: 23, NumberRowExtra2: 24, | |
54 NumberRowExtra3: 25, Backspace: 26, | |
55 Tab: 27, Q: 28, W: 29, Key.E: 30, R: 31, T: 32, Key.Y: 33, U: 34, I: 35, O: 36, | |
56 P: 37, LetterRow1Extra1: 38, LetterRow1Extra2: 39, LetterRow1Extra3: 40, | |
57 CapsLock: 41, A: 42, S: 43, D: 44, F: 45, G: 46, H: 47, J: 48, K: 49, L: 50, | |
58 LetterRow2Extra1: 51, LetterRow2Extra2: 52, | |
59 ShiftL: 53, Key.Z: 54, Key.X: 55, C: 56, V: 57, B: 58, N: 59, M: 60, | |
60 LetterRow3Extra1: 61, LetterRow3Extra2: 62, LetterRow3Extra3: 63, | |
61 ShiftR: 64, | |
62 CtrlL: 65, SuperL: 66, AltL: 67, Space: 68, AltR: 69, SuperR: 70, CtrlR: 71, | |
63 }.toTable | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 var |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
66 pipeline: RenderPipeline[VertexDataA, Uniforms] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 uniforms: Uniforms |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 scene: Thing |
525 | 69 keyvertexpos: seq[Vec3] |
70 keyvertexcolor: seq[Vec4] | |
71 keyindices: seq[array[3, uint16]] | |
72 rowpos = Vec2([0'f32, 0'f32]) | |
73 i = 0'u16 | |
74 for row in keyrows.fields: | |
75 let | |
76 rowSpacePx = rowWidth - (row.len - 1) * keyGap | |
77 rowSpace = sum(row) | |
78 for key in row: | |
79 let keySpace = float32((key / rowSpace) * rowSpacePx) | |
80 keyvertexpos.add Vec3([rowpos[0], rowpos[1], 0'f32]) | |
81 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1], 0'f32]) | |
82 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] + keyHeight, 0'f32]) | |
83 keyvertexpos.add Vec3([rowpos[0], rowpos[1] + keyHeight, 0'f32]) | |
84 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] | |
85 keyindices.add [i, i + 1, i + 2] | |
86 keyindices.add [i + 2, i + 3, i] | |
87 rowpos[0] += keySpace + keyGap | |
88 i += 4 | |
89 rowpos[0] = 0 | |
90 rowpos[1] += keyHeight + keyGap | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 proc globalUpdate(engine: var Engine, dt: float32) = |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 uniforms.projection.value = ortho[float32]( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 0'f32, float32(engine.vulkan.frameSize.x), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 0'f32, float32(engine.vulkan.frameSize.y), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 0'f32, 1'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 engine.vulkan.device.updateUniformData(pipeline, uniforms) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 |
525 | 101 let |
102 mousePos = translate3d(engine.input.mousePos.x, engine.input.mousePos.y, 0'f32) | |
103 winsize = engine.window.size | |
104 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / | |
105 2'f32, 0.1'f32) | |
106 scene.firstWithName("cursor").transform = mousePos | |
107 scene.firstWithName("keyboard-center").transform = center | |
108 var mesh = Mesh[VertexDataA, uint16](scene.firstWithName("keyboard").parts[0]) | |
109 var hadUpdate = false | |
110 for key, index in keyMap.pairs: | |
111 if key in engine.input.keysPressed: | |
112 echo "Pressed ", key | |
113 let baseIndex = index * 4 | |
114 mesh.vertexData.color.data[baseIndex + 0] = activeColor | |
115 mesh.vertexData.color.data[baseIndex + 1] = activeColor | |
116 mesh.vertexData.color.data[baseIndex + 2] = activeColor | |
117 mesh.vertexData.color.data[baseIndex + 3] = activeColor | |
118 hadUpdate = true | |
119 if key in engine.input.keysReleased: | |
120 echo "Released ", key | |
121 let baseIndex = index * 4 | |
122 mesh.vertexData.color.data[baseIndex + 0] = baseColor | |
123 mesh.vertexData.color.data[baseIndex + 1] = baseColor | |
124 mesh.vertexData.color.data[baseIndex + 2] = baseColor | |
125 mesh.vertexData.color.data[baseIndex + 3] = baseColor | |
126 hadUpdate = true | |
127 if hadUpdate: | |
128 engine.vulkan.device.updateVertexData(mesh.vertexData.color) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
129 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
130 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
131 when isMainModule: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
132 var myengine = igniteEngine("Input") |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
133 |
525 | 134 var cursormesh = new Mesh[VertexDataA, uint16] |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
135 cursormesh.vertexData = VertexDataA( |
525 | 136 position: PositionAttribute[Vec3](data: arrow, useOnDeviceMemory: true), |
137 color: ColorAttribute[Vec4](data: arrow_colors), | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
138 transform: ModelTransformAttribute(data: @[Unit44]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
140 # transform the cursor a bit to make it look nice |
525 | 141 let cursorscale = ( |
142 scale2d(20'f32, 20'f32) * | |
143 translate2d(1'f32, 1'f32) * | |
144 rotate2d(-float32(PI) / 4'f32) * | |
145 scale2d(0.5'f32, 1'f32) * | |
146 rotate2d(float32(PI) / 4'f32) | |
147 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
148 for i in 0 ..< cursormesh.vertexData.position.data.len: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
149 let pos = Vec3([cursormesh.vertexData.position.data[i][0], |
525 | 150 cursormesh.vertexData.position.data[i][1], 0'f32]) |
151 cursormesh.vertexData.position.data[i] = (cursorscale * pos) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
152 |
525 | 153 var keyboardmesh = new Mesh[VertexDataA, uint16] |
154 keyboardmesh.indexed = true | |
155 keyboardmesh.vertexData = VertexDataA( | |
156 position: PositionAttribute[Vec3](data: keyvertexpos, | |
157 useOnDeviceMemory: true), | |
158 color: ColorAttribute[Vec4](data: keyvertexcolor), | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
159 transform: ModelTransformAttribute(data: @[Unit44]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
160 ) |
525 | 161 keyboardmesh.indices = keyindices |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
162 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
163 scene = newThing("scene") |
525 | 164 let keyboard = newThing("keyboard", keyboardmesh) |
165 keyboard.transform = translate3d(-float32(rowWidth) / 2'f32, -float32( | |
166 tupleLen(keyRows) * (keyHeight + keyGap) - keyGap) / 2'f32, 0'f32) | |
167 scene.add newThing("keyboard-center", keyboard) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
168 scene.add newThing("cursor", cursormesh) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
169 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
170 # upload data, prepare shaders, etc |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
171 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" |
525 | 172 out_position = uniforms.projection * transform * vec4(position, 1); |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
173 """) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
174 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
175 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
176 myengine, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
177 scene, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
178 vertexShader, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
179 fragmentShader |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
180 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
181 # show something |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
182 myengine.run(pipeline, globalUpdate) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
183 pipeline.trash() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
184 myengine.trash() |