annotate examples/E04_input.nim @ 143:8ce634aa6ea6

did: update example 04
author Sam <sam@basx.dev>
date Tue, 25 Apr 2023 18:24:18 +0700
parents 779607656b12
children a4e6e76128e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
1 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
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
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
9 const
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
10 arrow = @[
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
11 newVec3f(-1, -1),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
12 newVec3f(1, -1),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
13 newVec3f(-0.3, -0.3),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
14 newVec3f(-0.3, -0.3),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
15 newVec3f(-1, 1),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
16 newVec3f(-1, -1),
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
17 ]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
18 # 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
19 keyrows = (
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
20 [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, 1.0, 1.0, -0.1, 1.0, 1.0, 1.0],
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
21 [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, 1.0, 1.0, 1.0],
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
22 [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, 1.0, 1.0],
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
23 [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
24 [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
25 [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
26 )
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
27 keyDimension = 50'f32
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
28 keyGap = 10'f32
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
29 backgroundColor = newVec3f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
30 baseColor = newVec3f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118'f32)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
31 activeColor = newVec3f(0.6509803921568628'f32, 0.22745098039215686, 0.3137254901960784'f32)
69
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
32 arrow_colors = @[
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
33 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
34 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
35 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
36 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
37 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
38 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
39 ]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
40 keyIndices = [
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
41 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
42 ScrollLock, Pause,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
43
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
44 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
45
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
46 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
47 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
48 LetterRow1Extra2, Delete, End, PageDown,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
49
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
50 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
51 LetterRow2Extra3, Enter,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
52
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
53 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
54 LetterRow3Extra3, ShiftR, Up,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
55
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
56 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
57 ]
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
58
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
59 var
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
60 scene: Entity
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
61 keyvertexpos: seq[Vec3f]
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
62 keyvertexcolor: seq[Vec3f]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
63 keymeshindices: seq[array[3, uint16]]
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
64 rowpos = newVec2f(0, 0)
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
65 i = 0'u16
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
66 firstRow = true
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
67 rowWidth = 0'f32
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
68 for row in keyrows.fields:
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
69 for key in row:
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
70 let keySpace = float32(keyDimension * key)
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
71 if key > 0:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
72 if keyIndices[i div 4] == Enter:
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
73 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
74 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap)
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
75 else:
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
76 keyvertexpos.add newVec3f(rowpos[0], rowpos[1])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
77 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
78 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
79 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] + keyDimension)
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
80 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
81 keymeshindices.add [i, i + 1, i + 2]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
82 keymeshindices.add [i + 2, i + 3, i]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
83 rowpos[0] += keySpace + keyGap
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
84 i += 4
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
85 else:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
86 rowpos[0] += -keySpace + keyGap
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
87 if firstRow:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
88 rowWidth = rowpos[0]
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
89 rowpos[0] = 0
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32)
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
91 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
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
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
94
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
95 when isMainModule:
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
96 var myengine = initEngine("Input")
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
97
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
98 # 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
99 let cursorscale = (
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
100 scale2d(20'f32, 20'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
101 translate2d(1'f32, 1'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
102 rotate2d(-float32(PI) / 4'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
103 scale2d(0.5'f32, 1'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
104 rotate2d(float32(PI) / 4'f32)
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
105 )
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
106 var positions = arrow
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
107 for i in 0 ..< positions.len:
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
108 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
109 # cursor
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
110 var cursormesh = newMesh(
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
111 positions=positions,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
112 colors=arrow_colors,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
113 instanceCount=1,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
114 )
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
115
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
116 # keyboard
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
117 var keyboardmesh = newMesh(
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
118 positions=keyvertexpos,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
119 colors=keyvertexcolor,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
120 indices=keymeshindices
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
121 )
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
122
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
123 # background
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
124 var backgroundmesh = newMesh(
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
125 positions= @[
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
126 newVec3f(0'f32, 0'f32),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
127 newVec3f(1'f32, 0'f32),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
128 newVec3f(1'f32, 1'f32),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
129 newVec3f(0'f32, 1'f32),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
130 ],
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
131 colors= @[
67
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
132 backgroundColor,
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
133 backgroundColor,
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
134 backgroundColor,
31ccaeee09c9 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 66
diff changeset
135 backgroundColor,
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
136 ],
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
137 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
138 )
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
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
140 backgroundmesh.setInstanceData("transform", @[Unit4f32])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
141 keyboardmesh.setInstanceData("transform", @[Unit4f32])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
142 cursormesh.setInstanceData("transform", @[Unit4f32])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
144 scene = newEntity("scene")
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
145 scene.add newEntity("background", backgroundmesh)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
146 let keyboard = newEntity("keyboard", keyboardmesh)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
147 keyboard.transform = translate3d(
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
148 -float32(rowWidth) / 2'f32,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
149 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
150 0'f32
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
151 )
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
152 scene.add newEntity("keyboard-center", keyboard)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
153 scene.add newEntity("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
154
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
155 const
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
156 vertexInput = @[
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
157 attr[Vec3f]("position", memoryLocation=VRAM),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
158 attr[Vec3f]("color", memoryLocation=VRAMVisible),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
159 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
160 ]
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
161 vertexOutput = @[attr[Vec3f]("outcolor")]
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
162 uniforms = @[attr[Mat4]("projection")]
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
163 fragOutput = @[attr[Vec4f]("color")]
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
164 vertexCode = compileGlslShader(
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
165 stage=VK_SHADER_STAGE_VERTEX_BIT,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
166 inputs=vertexInput,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
167 uniforms=uniforms,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
168 outputs=vertexOutput,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
169 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);"""
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
170 )
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
171 fragmentCode = compileGlslShader(
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
172 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
173 inputs=vertexOutput,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
174 uniforms=uniforms,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
175 outputs=fragOutput,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
176 main="color = vec4(outcolor, 1);"
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
177 )
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
178 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5)))
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
179 myengine.addScene(scene, vertexInput, transformAttribute="transform")
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
180 var projection = initShaderGlobal("projection", Unit4f32)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
181 scene.add projection
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
182 while myengine.running and not myengine.keyWasPressed(Escape):
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
183 myengine.updateInputs()
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
184 setValue[Mat4](projection.value, ortho[float32](
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
185 0'f32, float32(myengine.getWindow().size[0]),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
186 0'f32, float32(myengine.getWindow().size[1]),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
187 0'f32, 1'f32,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
188 ))
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
189 let
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
190 mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
191 winsize = myengine.getWindow().size
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
192 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
193 scene.firstWithName("cursor").transform = mousePos
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
194 scene.firstWithName("keyboard-center").transform = center
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
195 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
196
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
197 var mesh = Mesh(scene.firstWithName("keyboard").components[0])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
198 for (index, key) in enumerate(keyIndices):
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
199 if myengine.keyWasPressed(key):
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
200 let baseIndex = uint32(index * 4)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
201 mesh.updateMeshData("color", baseIndex + 0, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
202 mesh.updateMeshData("color", baseIndex + 1, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
203 mesh.updateMeshData("color", baseIndex + 2, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
204 mesh.updateMeshData("color", baseIndex + 3, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
205 if myengine.keyWasReleased(key):
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
206 let baseIndex = uint32(index * 4)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
207 mesh.updateMeshData("color", baseIndex + 0, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
208 mesh.updateMeshData("color", baseIndex + 1, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
209 mesh.updateMeshData("color", baseIndex + 2, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
210 mesh.updateMeshData("color", baseIndex + 3, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
211
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
212 myengine.renderScene(scene)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
213
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
214 myengine.destroy()