comparison 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
comparison
equal deleted inserted replaced
524:37e42b3b120d 525:0285ff2281b1
1 import std/tables
1 import std/strutils 2 import std/strutils
3 import std/typetraits
2 import std/times 4 import std/times
5 import std/math
3 6
4 import semicongine 7 import semicongine
5 8
6 type 9 type
7 # define type of vertex 10 # define type of vertex
8 VertexDataA = object 11 VertexDataA = object
9 position: PositionAttribute[Vec2] 12 position: PositionAttribute[Vec3]
10 color: ColorAttribute[Vec3] 13 color: ColorAttribute[Vec4]
11 transform: ModelTransformAttribute 14 transform: ModelTransformAttribute
12 Uniforms = object 15 Uniforms = object
13 projection: Descriptor[Mat44] 16 projection: Descriptor[Mat44]
14 cursor: Descriptor[Vec2] 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
15 64
16 var 65 var
17 pipeline: RenderPipeline[VertexDataA, Uniforms] 66 pipeline: RenderPipeline[VertexDataA, Uniforms]
18 uniforms: Uniforms 67 uniforms: Uniforms
19 scene: Thing 68 scene: Thing
20 time: float 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
21 91
22 92
23 proc globalUpdate(engine: var Engine, dt: float32) = 93 proc globalUpdate(engine: var Engine, dt: float32) =
24 time += dt
25 uniforms.cursor.value = engine.input.mousePos
26 uniforms.projection.value = ortho[float32]( 94 uniforms.projection.value = ortho[float32](
27 0'f32, float32(engine.vulkan.frameSize.x), 95 0'f32, float32(engine.vulkan.frameSize.x),
28 0'f32, float32(engine.vulkan.frameSize.y), 96 0'f32, float32(engine.vulkan.frameSize.y),
29 0'f32, 1'f32, 97 0'f32, 1'f32,
30 ) 98 )
31 engine.vulkan.device.updateUniformData(pipeline, uniforms) 99 engine.vulkan.device.updateUniformData(pipeline, uniforms)
32 100
33 let cursor = firstPartWithName[Mesh[VertexDataA]](scene, "cursor") 101 let
34 if cursor != nil: 102 mousePos = translate3d(engine.input.mousePos.x, engine.input.mousePos.y, 0'f32)
35 for c in cursor.vertexData.color.data.mitems: 103 winsize = engine.window.size
36 c[1] = (sin(time * 8) * 0.5 + 0.5) * 0.2 104 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) /
37 c[2] = (sin(time * 8) * 0.5 + 0.5) * 0.2 105 2'f32, 0.1'f32)
38 engine.vulkan.device.updateVertexData(cursor.vertexData.color) 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)
39 129
40 var trans = translate3d(engine.input.mousePos.x, engine.input.mousePos.y, 0'f32)
41 # cursor.vertexData.transform.data = @[trans.transposed()]
42 engine.vulkan.device.updateVertexData(cursor.vertexData.transform)
43
44
45 const
46 shape = @[
47 Vec2([ - 1'f32, - 1'f32]),
48 Vec2([1'f32, - 1'f32]),
49 Vec2([-0.3'f32, -0.3'f32]),
50 Vec2([-0.3'f32, -0.3'f32]),
51 Vec2([ - 1'f32, 1'f32]),
52 Vec2([ - 1'f32, - 1'f32]),
53 ]
54 colors = @[
55 Vec3([1'f32, 0'f32, 0'f32]),
56 Vec3([1'f32, 0'f32, 0'f32]),
57 Vec3([1'f32, 0'f32, 0'f32]),
58 Vec3([0.8'f32, 0'f32, 0'f32]),
59 Vec3([0.8'f32, 0'f32, 0'f32]),
60 Vec3([0.8'f32, 0'f32, 0'f32]),
61 ]
62 130
63 when isMainModule: 131 when isMainModule:
64 var myengine = igniteEngine("Input") 132 var myengine = igniteEngine("Input")
65 133
66 var cursormesh = new Mesh[VertexDataA] 134 var cursormesh = new Mesh[VertexDataA, uint16]
67 cursormesh.vertexData = VertexDataA( 135 cursormesh.vertexData = VertexDataA(
68 position: PositionAttribute[Vec2](data: shape, useOnDeviceMemory: true), 136 position: PositionAttribute[Vec3](data: arrow, useOnDeviceMemory: true),
69 color: ColorAttribute[Vec3](data: colors), 137 color: ColorAttribute[Vec4](data: arrow_colors),
70 transform: ModelTransformAttribute(data: @[Unit44]), 138 transform: ModelTransformAttribute(data: @[Unit44]),
71 ) 139 )
72 # transform the cursor a bit to make it look nice 140 # transform the cursor a bit to make it look nice
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 )
73 for i in 0 ..< cursormesh.vertexData.position.data.len: 148 for i in 0 ..< cursormesh.vertexData.position.data.len:
74 let cursorscale = (
75 scale2d(20'f32, 20'f32) *
76 translate2d(1'f32, 1'f32) *
77 rotate2d(-float32(PI) / 4'f32) *
78 scale2d(0.5'f32, 1'f32) *
79 rotate2d(float32(PI) / 4'f32)
80 )
81 let pos = Vec3([cursormesh.vertexData.position.data[i][0], 149 let pos = Vec3([cursormesh.vertexData.position.data[i][0],
82 cursormesh.vertexData.position.data[i][1], 1'f32]) 150 cursormesh.vertexData.position.data[i][1], 0'f32])
83 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy 151 cursormesh.vertexData.position.data[i] = (cursorscale * pos)
84 152
85 var boxmesh = new Mesh[VertexDataA] 153 var keyboardmesh = new Mesh[VertexDataA, uint16]
86 boxmesh.vertexData = VertexDataA( 154 keyboardmesh.indexed = true
87 position: PositionAttribute[Vec2](data: shape), 155 keyboardmesh.vertexData = VertexDataA(
88 color: ColorAttribute[Vec3](data: colors), 156 position: PositionAttribute[Vec3](data: keyvertexpos,
157 useOnDeviceMemory: true),
158 color: ColorAttribute[Vec4](data: keyvertexcolor),
89 transform: ModelTransformAttribute(data: @[Unit44]), 159 transform: ModelTransformAttribute(data: @[Unit44]),
90 ) 160 )
91 for i in 0 ..< boxmesh.vertexData.position.data.len: 161 keyboardmesh.indices = keyindices
92 let boxscale = translate2d(100'f32, 100'f32) * scale2d(100'f32, 100'f32)
93 let pos = Vec3([boxmesh.vertexData.position.data[i][0],
94 boxmesh.vertexData.position.data[i][1], 1'f32])
95 boxmesh.vertexData.position.data[i] = (boxscale * pos).xy
96 162
97 scene = newThing("scene") 163 scene = newThing("scene")
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)
98 scene.add newThing("cursor", cursormesh) 168 scene.add newThing("cursor", cursormesh)
99 scene.add newThing("a box", boxmesh, newTransform(Unit44), newTransform(
100 translate3d(1'f32, 0'f32, 0'f32)))
101 scene.add newTransform(scale3d(1.5'f32, 1.5'f32, 1.5'f32))
102 169
103 # upload data, prepare shaders, etc 170 # upload data, prepare shaders, etc
104 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" 171 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
105 out_position = uniforms.projection * transform * vec4(position, 0, 1); 172 out_position = uniforms.projection * transform * vec4(position, 1);
106 """) 173 """)
107 const fragmentShader = generateFragmentShaderCode[VertexDataA]() 174 const fragmentShader = generateFragmentShaderCode[VertexDataA]()
108 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( 175 pipeline = setupPipeline[VertexDataA, Uniforms, uint16](
109 myengine, 176 myengine,
110 scene, 177 scene,