-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrobotnik.chai
More file actions
77 lines (71 loc) · 2.39 KB
/
robotnik.chai
File metadata and controls
77 lines (71 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Робот-жонглер позволяет печатать на клавиатуре заданную последовательность,
* делать паузы и вызывать колбеки.
*/
class Robotnik {
var queue
var busy
var release_at
var scancode
def Robotnik() {
this.queue = Vector()
this.busy = 0
this.release_at = -1
this.scancode = 0
}
/* Массив keys может содержать:
* строки - названия сканкодов из SDL_Keys
* целые числа - задержки в кадрах
* функции без аргументов - колбеки
*/
def types(keys) {
for (what : keys) {
this.queue.push_back(what)
}
}
/* Эта функция вызывается каждый кадр */
def onframe() {
if (this.busy == 0) {
if (!this.queue.empty()) {
var bu = this.queue[0]
this.queue.erase_at(0);
switch (bu.get_type_info().name()) {
case("string") {
this.release_at = 3
this.busy = 5
if (bu[0] == '\001') { // keydown
bu = bu.substr(1, bu.size())
this.release_at = -1
this.busy = 0
}
else if (bu[0] == '\002') { // keyup
bu = bu.substr(1, bu.size())
this.release_at = 0
this.busy = 0
}
this.scancode = scancode_from_name(bu)
keydown(this.scancode)
break
}
case ("int") {
this.busy = bu
break
}
case ("Function") {
bu()
this.busy = 5
break
}
}
}
}
if (this.busy == this.release_at) {
keyup(this.scancode)
this.release_at = -1
}
if (this.busy > 0) {
--this.busy
}
}
}
global keytyper = Robotnik()