annotate examples/E04_input.nim @ 69:d830a1c0f86e

fix: mouse coord, colors
author Sam <sam@basx.dev>
date Sun, 05 Feb 2023 00:46:40 +0700
parents 31ccaeee09c9
children 53ccd6090116
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
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
2 import std/enumerate
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
3 import std/strutils
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
4 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
5 import std/times
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
6 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
7
56
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
8 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
9
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 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
11 # 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
12 VertexDataA = object
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
13 position: PositionAttribute[Vec3]
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
14 color: ColorAttribute[Vec4]
61
0f04ba283558 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents: 60
diff changeset
15 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
16 Uniforms = object
59
d7d9420ba675 did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents: 58
diff changeset
17 projection: Descriptor[Mat44]
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
18
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
19 const
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
20 arrow = @[
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([1'f32, -1'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([-0.3'f32, -0.3'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 Vec3([-1'f32, -1'f32, 0'f32]),
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
27 ]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
28 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
29 keyrows = (
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
30 [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,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
31 1.0, 1.0, -0.1, 1.0, 1.0, 1.0],
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
32 [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,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
33 1.0, 1.0, 1.0],
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
34 [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,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
35 1.0, 1.0],
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
36 [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],
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
37 [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],
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
38 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0],
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
39 )
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
40 keyDimension = 50'f32
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
41 keyGap = 10'f32
69
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
42 backgroundColor = Vec4([0.6705882352941176'f32, 0.6078431372549019'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
43 0.5882352941176471'f32, 0'f32])
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
44 baseColor = Vec4([0.9411764705882353'f32, 0.9058823529411765'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
45 0.8470588235294118'f32, 0'f32])
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
46 activeColor = Vec4([0.6509803921568628'f32, 0.22745098039215686'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
47 0.3137254901960784'f32, 0'f32])
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
48
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
49 arrow_colors = @[
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
50 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
51 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
52 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
53 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
54 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
55 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
56 ]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
57 keyIndices = [
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
58 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
59 ScrollLock, Pause,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
60
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
61 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
62
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
63 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
64 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
65 LetterRow1Extra2, Delete, End, PageDown,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
67 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
68 LetterRow2Extra3, Enter,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
69
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
70 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
71 LetterRow3Extra3, ShiftR, Up,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
72
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
73 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
74 ]
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
75
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
76 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
77 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
78 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
79 scene: Thing
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
80 keyvertexpos: seq[Vec3]
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
81 keyvertexcolor: seq[Vec4]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
82 keymeshindices: seq[array[3, uint16]]
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
83 rowpos = Vec2([0'f32, 0'f32])
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
84 i = 0'u16
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
85 firstRow = true
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
86 rowWidth = 0'f32
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
87 for row in keyrows.fields:
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
88 for key in row:
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
89 let keySpace = float32(keyDimension * key)
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
90 if key > 0:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
91 if keyIndices[i div 4] == Enter:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
92 keyvertexpos.add Vec3([rowpos[0], rowpos[1] - keyDimension - keyGap, 0'f32])
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
93 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] - keyDimension -
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
94 keyGap, 0'f32])
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
95 else:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
96 keyvertexpos.add Vec3([rowpos[0], rowpos[1], 0'f32])
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
97 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1], 0'f32])
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
98 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] + keyDimension, 0'f32])
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
99 keyvertexpos.add Vec3([rowpos[0], rowpos[1] + keyDimension, 0'f32])
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
100 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
101 keymeshindices.add [i, i + 1, i + 2]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
102 keymeshindices.add [i + 2, i + 3, i]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
103 rowpos[0] += keySpace + keyGap
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
104 i += 4
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
105 else:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
106 rowpos[0] += -keySpace + keyGap
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
107 if firstRow:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
108 rowWidth = rowpos[0]
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
109 rowpos[0] = 0
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
110 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32)
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
111 firstRow = false
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
112
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
113
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
114 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
115 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
116 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
117 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
118 0'f32, 1'f32,
94d7eed3f118 did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents: 40
diff changeset
119 )
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
120 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
121
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
122 let
69
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
123 mousePos = translate3d(engine.input.mousePos.x + 20,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
124 engine.input.mousePos.y + 20, 0'f32)
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
125 winsize = engine.window.size
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
126 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) /
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
127 2'f32, 0.1'f32)
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
128 scene.firstWithName("cursor").transform = mousePos
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
129 scene.firstWithName("keyboard-center").transform = center
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
130 scene.firstWithName("background").transform = scale3d(float32(winsize[0]),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
131 float32(winsize[1]), 0'f32)
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
132 var mesh = Mesh[VertexDataA, uint16](scene.firstWithName("keyboard").parts[0])
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
133 var hadUpdate = false
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
134 for (index, key) in enumerate(keyIndices):
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
135 if key in engine.input.keysPressed:
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
136 let baseIndex = index * 4
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
137 mesh.vertexData.color.data[baseIndex + 0] = activeColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
138 mesh.vertexData.color.data[baseIndex + 1] = activeColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
139 mesh.vertexData.color.data[baseIndex + 2] = activeColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
140 mesh.vertexData.color.data[baseIndex + 3] = activeColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
141 hadUpdate = true
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
142 if key in engine.input.keysReleased:
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
143 let baseIndex = index * 4
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
144 mesh.vertexData.color.data[baseIndex + 0] = baseColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
145 mesh.vertexData.color.data[baseIndex + 1] = baseColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
146 mesh.vertexData.color.data[baseIndex + 2] = baseColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
147 mesh.vertexData.color.data[baseIndex + 3] = baseColor
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
148 hadUpdate = true
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
149 if hadUpdate:
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
150 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
151
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
152
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
153 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
154 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
155
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
156 # cursor
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
157 var cursormesh = new Mesh[VertexDataA, uint16]
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
158 cursormesh.vertexData = VertexDataA(
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
159 position: PositionAttribute[Vec3](data: arrow, useOnDeviceMemory: true),
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
160 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
161 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
162 )
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
163 # 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
164 let cursorscale = (
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
165 scale2d(20'f32, 20'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
166 translate2d(1'f32, 1'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
167 rotate2d(-float32(PI) / 4'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
168 scale2d(0.5'f32, 1'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
169 rotate2d(float32(PI) / 4'f32)
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
170 )
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
171 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
172 let pos = Vec3([cursormesh.vertexData.position.data[i][0],
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
173 cursormesh.vertexData.position.data[i][1], 0'f32])
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
174 cursormesh.vertexData.position.data[i] = (cursorscale * pos)
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
175
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
176 # keyboard
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
177 var keyboardmesh = new Mesh[VertexDataA, uint16]
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
178 keyboardmesh.indexed = true
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
179 keyboardmesh.vertexData = VertexDataA(
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
180 position: PositionAttribute[Vec3](data: keyvertexpos,
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
181 useOnDeviceMemory: true),
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
182 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
183 transform: ModelTransformAttribute(data: @[Unit44]),
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
184 )
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
185 keyboardmesh.indices = keymeshindices
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
186
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
187 # background
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
188 var backgroundmesh = new Mesh[VertexDataA, uint16]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
189 backgroundmesh.indexed = true
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
190 backgroundmesh.indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
191 backgroundmesh.vertexData = VertexDataA(
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
192 position: PositionAttribute[Vec3](data: @[
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
193 Vec3([0'f32, 0'f32, 0'f32]),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
194 Vec3([1'f32, 0'f32, 0'f32]),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
195 Vec3([1'f32, 1'f32, 0'f32]),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
196 Vec3([0'f32, 1'f32, 0'f32]),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
197 ], useOnDeviceMemory: true),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
198 color: ColorAttribute[Vec4](data: @[
67
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
199 backgroundColor,
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
200 backgroundColor,
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
201 backgroundColor,
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
202 backgroundColor,
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
203 ]),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
204 transform: ModelTransformAttribute(data: @[Unit44]),
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
205 )
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
206
60
c57285d292b6 did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents: 59
diff changeset
207 scene = newThing("scene")
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
208 scene.add newThing("background", backgroundmesh)
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
209 let keyboard = newThing("keyboard", keyboardmesh)
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
210 keyboard.transform = translate3d(-float32(rowWidth) / 2'f32, -float32(
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
211 tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, 0'f32)
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
212 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
213 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
214
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
215 # 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
216 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]("""
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
217 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
218 """)
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
219 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
220 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
221 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
222 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
223 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
224 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
225 )
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
226 # 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
227 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
228 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
229 myengine.trash()