forked from devcem/Playcanvas-UI-Components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomInput.js
61 lines (46 loc) · 1.47 KB
/
CustomInput.js
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
var CustomInput = pc.createScript('customInput');
CustomInput.attributes.add('trigger', { type : 'string' });
CustomInput.prototype.initialize = function() {
this.placeHolder = this.entity.element.text + '';
this.isActive = false;
this.keys = [];
this.excludedChars = [8, 13, 9, 16, 20, 17, 91];
this.cursor = false;
this.lastCursorTime = Date.now();
this.app.keyboard.on('keydown', this.onKeyDown, this);
};
CustomInput.prototype.onKeyDown = function(event) {
if(event.key == 13){
if(this.isActive){
this.entity.element.text = this.placeHolder;
if(this.trigger){
this.app.fire(this.trigger, this.keys.join(''));
}
this.keys = [];
}
this.isActive = !this.isActive;
}
if(!this.isActive){
return false;
}
if(
this.excludedChars.indexOf(event.key) === -1 &&
event.event.key
){
this.keys.push(event.event.key);
}
//special functions, backspace
if(event.key === 8){
this.keys.splice(this.keys.length - 1, 1);
}
};
CustomInput.prototype.update = function(dt) {
if(!this.isActive){
return false;
}
if(Date.now() - this.lastCursorTime > 200){
this.cursor = !this.cursor;
this.lastCursorTime = Date.now();
}
this.entity.element.text = this.keys.join('') + (this.cursor ? '_' : ' ');
};