forked from WhatPumpkin/Sburb-Legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAction.js
135 lines (115 loc) · 3.82 KB
/
Action.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var Sburb = (function(Sburb){
///////////////////////////////////////////////
//Action Class
///////////////////////////////////////////////
//Constructor
Sburb.Action = function(command,info,name,sprite,followUp,noWait,noDelay,times,soft,silent){
this.sprite = sprite?sprite:null;
this.name = name?name:null;
this.command = command
this.info = info;
this.followUp = followUp?followUp:null;
this.noWait = noWait?noWait:false;
this.noDelay = noDelay?noDelay:false;
this.soft = soft?soft:false;
this.silent = silent?silent:false;
this.times = times?times:1;
}
//Make an exact copy
Sburb.Action.prototype.clone = function(){
return new Sburb.Action(this.command, this.info, this.name, this.sprite, this.followUp, this.noWait, this.noDelay, this.times, this.soft, this.silent);
}
//Serialize to XML (see serialization.js)
Sburb.Action.prototype.serialize = function(output){
output = output.concat("\n<action "+
"command='"+this.command+
(this.sprite?"' sprite='"+this.sprite:"")+
(this.name?"' name='"+this.name:"")+
(this.noWait?"' noWait='"+this.noWait:"")+
(this.noDelay?"' noDelay='"+this.noDelay:"")+
(this.soft?"' soft='"+this.soft:"")+
(this.silent?"' silent='"+this.silent:"")+
(this.times!=1?"' times='"+this.times:"")+
"'>");
output = output.concat('<args>' + escape(this.info.trim()) + '</args>' );
if(this.followUp){
output = this.followUp.serialize(output);
}
output = output.concat("</action>");
return output;
}
//////////////////////////////////////////////////
//Related utility functions
//////////////////////////////////////////////////
//Parse a serialized Action from an XML DOM node
Sburb.parseAction = function(node) {
var targSprite = null;
var firstAction = null;
var oldAction = null;
do{
var attributes = node.attributes;
if(attributes.getNamedItem("sprite") && attributes.getNamedItem("sprite").value!="null"){
targSprite = attributes.getNamedItem("sprite").value;
}
var times = attributes.getNamedItem("times") || attributes.getNamedItem("loops") || attributes.getNamedItem("for");
var newAction = new Sburb.Action(
attributes.getNamedItem("command").value,
node.firstChild?unescape(getNodeText(node)).trim():"",
attributes.getNamedItem("name")?attributes.getNamedItem("name").value:null,
targSprite,
null,
attributes.getNamedItem("noWait")?attributes.getNamedItem("noWait").value=="true":false,
attributes.getNamedItem("noDelay")?attributes.getNamedItem("noDelay").value=="true":false,
times?parseInt(times.value):1,
attributes.getNamedItem("soft")?attributes.getNamedItem("soft").value=="true":false,
attributes.getNamedItem("silent")?attributes.getNamedItem("silent").value=="true":false);
if(oldAction){
oldAction.followUp = newAction;
}
if(!firstAction){
firstAction = newAction;
}
oldAction = newAction;
var oldNode = node;
node = null;
for(var i=0;i<oldNode.childNodes.length;i++){
var child = oldNode.childNodes[i];
if(child.nodeName=="action"){
node = child;
break;
}
}
if(!node){
break;
}
}while(node);
return firstAction;
}
function getNodeText(xmlNode){
if(!xmlNode) return '';
for(var i=0;i<xmlNode.childNodes.length;i++){
var child = xmlNode.childNodes[i];
if(child.tagName=="args"){
for(var k=0;k<child.childNodes.length;k++){
if(child.childNodes[k].firstChild){
serializer = new XMLSerializer();
var output = "";
for(var j=0; j<child.childNodes.length; j++){
output += serializer.serializeToString(child.childNodes[j]);
}
return output;
}
}
if(typeof(child.textContent) != "undefined"){
return child.textContent;
}
return child.firstChild.nodeValue;
}
}
if(typeof(xmlNode.textContent) != "undefined"){
return xmlNode.textContent;
}
return xmlNode.firstChild.nodeValue;
}
return Sburb;
})(Sburb || {});