Mercurial > games > semicongine
annotate examples/E04_input.nim @ 528:3ec1be39e1ad
add: nicer keyboard demo
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 05 Feb 2023 00:20:07 +0700 |
parents | de433a47e635 |
children | cfa230e1f3b9 |
rev | line source |
---|---|
525 | 1 import std/tables |
527 | 2 import std/enumerate |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/strutils |
525 | 4 import std/typetraits |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/times |
525 | 6 import std/math |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 import semicongine |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 type |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 # define type of vertex |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 VertexDataA = object |
525 | 13 position: PositionAttribute[Vec3] |
14 color: ColorAttribute[Vec4] | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 transform: ModelTransformAttribute |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 Uniforms = object |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 projection: Descriptor[Mat44] |
525 | 18 |
19 const | |
20 arrow = @[ | |
21 Vec3([-1'f32, -1'f32, 0'f32]), | |
22 Vec3([1'f32, -1'f32, 0'f32]), | |
23 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | |
24 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | |
25 Vec3([-1'f32, 1'f32, 0'f32]), | |
26 Vec3([-1'f32, -1'f32, 0'f32]), | |
27 ] | |
28 arrow_colors = @[ | |
29 Vec4([1'f32, 0'f32, 0'f32, 1'f32]), | |
30 Vec4([1'f32, 0'f32, 0'f32, 1'f32]), | |
31 Vec4([1'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 Vec4([0.8'f32, 0'f32, 0'f32, 1'f32]), | |
35 ] | |
527 | 36 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces |
525 | 37 keyrows = ( |
527 | 38 [1.0, -0.6, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, |
39 1.0, 1.0, -0.1, 1.0, 1.0, 1.0], | |
40 [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, -0.1, | |
41 1.0, 1.0, 1.0], | |
42 [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.5, 1.0, | |
43 1.0, 1.0], | |
44 [2.0, 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.0], | |
45 [2.6, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.8, -1.3, 1.0], | |
46 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0], | |
525 | 47 ) |
527 | 48 keyDimension = 50'f32 |
525 | 49 keyGap = 10'f32 |
528 | 50 backgroundColor = Vec4([1'f32, 0.3'f32, 0.3'f32, 0'f32]) |
525 | 51 baseColor = Vec4([1'f32, 0'f32, 0'f32, 0'f32]) |
52 activeColor = Vec4([1'f32, 1'f32, 1'f32, 0'f32]) | |
527 | 53 keyIndices = [ |
54 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen, | |
55 ScrollLock, Pause, | |
56 | |
57 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`, | |
58 | |
59 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp, | |
60 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1, | |
61 LetterRow1Extra2, Delete, End, PageDown, | |
62 | |
63 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2, | |
64 LetterRow2Extra3, Enter, | |
65 | |
66 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2, | |
67 LetterRow3Extra3, ShiftR, Up, | |
68 | |
69 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right | |
70 ] | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 var |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
73 pipeline: RenderPipeline[VertexDataA, Uniforms] |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 uniforms: Uniforms |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 scene: Thing |
525 | 76 keyvertexpos: seq[Vec3] |
77 keyvertexcolor: seq[Vec4] | |
527 | 78 keymeshindices: seq[array[3, uint16]] |
525 | 79 rowpos = Vec2([0'f32, 0'f32]) |
80 i = 0'u16 | |
527 | 81 firstRow = true |
82 rowWidth = 0'f32 | |
525 | 83 for row in keyrows.fields: |
84 for key in row: | |
527 | 85 let keySpace = float32(keyDimension * key) |
86 if key > 0: | |
87 if keyIndices[i div 4] == Enter: | |
88 keyvertexpos.add Vec3([rowpos[0], rowpos[1] - keyDimension - keyGap, 0'f32]) | |
89 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] - keyDimension - | |
90 keyGap, 0'f32]) | |
91 else: | |
92 keyvertexpos.add Vec3([rowpos[0], rowpos[1], 0'f32]) | |
93 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1], 0'f32]) | |
94 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] + keyDimension, 0'f32]) | |
95 keyvertexpos.add Vec3([rowpos[0], rowpos[1] + keyDimension, 0'f32]) | |
96 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] | |
97 keymeshindices.add [i, i + 1, i + 2] | |
98 keymeshindices.add [i + 2, i + 3, i] | |
99 rowpos[0] += keySpace + keyGap | |
100 i += 4 | |
101 else: | |
102 rowpos[0] += -keySpace + keyGap | |
103 if firstRow: | |
104 rowWidth = rowpos[0] | |
525 | 105 rowpos[0] = 0 |
527 | 106 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
107 firstRow = false | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 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
|
111 uniforms.projection.value = ortho[float32]( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 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
|
113 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
|
114 0'f32, 1'f32, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 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
|
117 |
525 | 118 let |
119 mousePos = translate3d(engine.input.mousePos.x, engine.input.mousePos.y, 0'f32) | |
120 winsize = engine.window.size | |
121 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / | |
122 2'f32, 0.1'f32) | |
123 scene.firstWithName("cursor").transform = mousePos | |
124 scene.firstWithName("keyboard-center").transform = center | |
527 | 125 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), |
126 float32(winsize[1]), 0'f32) | |
525 | 127 var mesh = Mesh[VertexDataA, uint16](scene.firstWithName("keyboard").parts[0]) |
128 var hadUpdate = false | |
527 | 129 for (index, key) in enumerate(keyIndices): |
525 | 130 if key in engine.input.keysPressed: |
131 let baseIndex = index * 4 | |
132 mesh.vertexData.color.data[baseIndex + 0] = activeColor | |
133 mesh.vertexData.color.data[baseIndex + 1] = activeColor | |
134 mesh.vertexData.color.data[baseIndex + 2] = activeColor | |
135 mesh.vertexData.color.data[baseIndex + 3] = activeColor | |
136 hadUpdate = true | |
137 if key in engine.input.keysReleased: | |
138 let baseIndex = index * 4 | |
139 mesh.vertexData.color.data[baseIndex + 0] = baseColor | |
140 mesh.vertexData.color.data[baseIndex + 1] = baseColor | |
141 mesh.vertexData.color.data[baseIndex + 2] = baseColor | |
142 mesh.vertexData.color.data[baseIndex + 3] = baseColor | |
143 hadUpdate = true | |
144 if hadUpdate: | |
145 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
|
146 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
147 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
148 when isMainModule: |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
149 var myengine = igniteEngine("Input") |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
150 |
527 | 151 # cursor |
525 | 152 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
|
153 cursormesh.vertexData = VertexDataA( |
525 | 154 position: PositionAttribute[Vec3](data: arrow, useOnDeviceMemory: true), |
155 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
|
156 transform: ModelTransformAttribute(data: @[Unit44]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
157 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
158 # transform the cursor a bit to make it look nice |
525 | 159 let cursorscale = ( |
160 scale2d(20'f32, 20'f32) * | |
161 translate2d(1'f32, 1'f32) * | |
162 rotate2d(-float32(PI) / 4'f32) * | |
163 scale2d(0.5'f32, 1'f32) * | |
164 rotate2d(float32(PI) / 4'f32) | |
165 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
166 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
|
167 let pos = Vec3([cursormesh.vertexData.position.data[i][0], |
525 | 168 cursormesh.vertexData.position.data[i][1], 0'f32]) |
169 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
|
170 |
527 | 171 # keyboard |
525 | 172 var keyboardmesh = new Mesh[VertexDataA, uint16] |
173 keyboardmesh.indexed = true | |
174 keyboardmesh.vertexData = VertexDataA( | |
175 position: PositionAttribute[Vec3](data: keyvertexpos, | |
176 useOnDeviceMemory: true), | |
177 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
|
178 transform: ModelTransformAttribute(data: @[Unit44]), |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
179 ) |
527 | 180 keyboardmesh.indices = keymeshindices |
181 | |
182 # background | |
183 var backgroundmesh = new Mesh[VertexDataA, uint16] | |
184 backgroundmesh.indexed = true | |
185 backgroundmesh.indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]] | |
186 backgroundmesh.vertexData = VertexDataA( | |
187 position: PositionAttribute[Vec3](data: @[ | |
188 Vec3([0'f32, 0'f32, 0'f32]), | |
189 Vec3([1'f32, 0'f32, 0'f32]), | |
190 Vec3([1'f32, 1'f32, 0'f32]), | |
191 Vec3([0'f32, 1'f32, 0'f32]), | |
192 ], useOnDeviceMemory: true), | |
193 color: ColorAttribute[Vec4](data: @[ | |
528 | 194 backgroundColor, |
195 backgroundColor, | |
196 backgroundColor, | |
197 backgroundColor, | |
527 | 198 ]), |
199 transform: ModelTransformAttribute(data: @[Unit44]), | |
200 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
201 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
202 scene = newThing("scene") |
527 | 203 scene.add newThing("background", backgroundmesh) |
525 | 204 let keyboard = newThing("keyboard", keyboardmesh) |
205 keyboard.transform = translate3d(-float32(rowWidth) / 2'f32, -float32( | |
527 | 206 tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, 0'f32) |
525 | 207 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
|
208 scene.add newThing("cursor", cursormesh) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
209 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
210 # upload data, prepare shaders, etc |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
211 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" |
525 | 212 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
|
213 """) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
214 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
215 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
216 myengine, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
217 scene, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
218 vertexShader, |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
219 fragmentShader |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
220 ) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
221 # show something |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
222 myengine.run(pipeline, globalUpdate) |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
223 pipeline.trash() |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
224 myengine.trash() |