annotate examples/E04_input.nim @ 64:e766d138cc5d

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