-
Notifications
You must be signed in to change notification settings - Fork 9
/
Template.js
83 lines (65 loc) · 2.38 KB
/
Template.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var Template = pc.createScript('template');
Template.prototype.initialize = function() {
this.app.on('Template:' + this.entity.name, this.onUpdate, this);
};
Template.prototype.render = function(data, text) {
var regex = /\{\{(.*?)\}\}/gm;
var str = text;
var m;
while ((m = regex.exec(text)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
var value = eval('data.' + m[1]);
if(value){
str = str.replace(m[0], value);
}else{
str = str.replace(m[0], '');
}
}
return str;
};
Template.prototype.getAsset = function(data, name) {
var key = data[name];
var asset = this.app.assets.find(key);
if(typeof key !== 'undefined' && asset){
return asset;
}
};
Template.prototype.onUpdate = function(data) {
var entities = this.entity.findByTag('Dynamic');
var i = entities.length;
while(i--){
var entity = entities[i];
if(entity && entity.enabled){
if(entity.element.type == pc.ELEMENTTYPE_TEXT){
var source = entity.element.text;
//clear
if(!entity.element.source){
entity.element.source = source;
}else{
entity.element.text = entity.element.source + '';
}
entity.element.text = this.render(data, entity.element.text);
}
if(entity.element.type == pc.ELEMENTTYPE_IMAGE){
var sourceImage = entity.element.textureAsset;
var asset = this.getAsset(data, entity.name);
entity.element.sourceImage = sourceImage;
if(!entity.element.sourceImage){
entity.element.sourceImage = sourceImage;
}else{
var sourceImageCopied = entity.element.sourceImage;
entity.element.textureAsset = sourceImageCopied;
}
if(asset){
entity.element.textureAsset = asset;
}
}
}
//aditional scripts
if(entity && entity.script && entity.script.visibility){
entity.script.visibility.trigger(data);
}
}
};