annotate examples/E04_input.nim @ 601:7e54bbbcffba

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