ScriptArea.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. MWF.xApplication.ScriptEditor.ScriptArea = new Class({
  2. initialize: function(editor){
  3. this.editor = editor;
  4. this.node = this.editor.scriptAreaNode;
  5. this.css = this.editor.css;
  6. this.statementNodes = [];
  7. this.statements = [];
  8. this.links = [];
  9. this.mortises = [];
  10. this.beginStatements = [];
  11. this.currentLink = null;
  12. this.load();
  13. },
  14. load: function(){
  15. this.titleNode = new Element("div", {"styles": this.css.scriptAreaTitleNode}).inject(this.node);
  16. this.blockArea = new Element("div", {"styles": this.css.scriptAreaBlockNode}).inject(this.node);
  17. this.scriptArea = new Element("div", {"styles": this.css.scriptAreaScriptNode}).inject(this.node);
  18. this.blockAreaContent = new Element("div").inject(this.blockArea);
  19. this.loadTitleNode();
  20. this.setAreaSizeFun = this.setAreaSize.bind(this);
  21. this.editor.app.addEvent("resize", this.setAreaSizeFun);
  22. this.setAreaSize();
  23. },
  24. setAreaSize: function(){
  25. var size = this.node.getSize();
  26. var titleSize = this.titleNode.getSize();
  27. var y = size.y-titleSize.y;
  28. this.blockArea.setStyle("height", ""+y+"px");
  29. this.scriptArea.setStyle("height", ""+y+"px");
  30. this.beginStatements.each(function(statement){
  31. var p = statement.node.getPosition(this.blockArea);
  32. var size = statement.node.getSize();
  33. var h = p.y+size.y;
  34. this.blockAreaContent.setStyle("height", ""+h+"px");
  35. }.bind(this));
  36. },
  37. loadTitleNode: function(){
  38. this.blockTabNode = new Element("div", {"styles": this.css.scriptAreaTitleBlockNode}).inject(this.titleNode);
  39. this.scriptTabNode = new Element("div", {"styles": this.css.scriptAreaTitleScriptNode}).inject(this.titleNode);
  40. this.blockTabNode.set("text", this.editor.app.lp.block);
  41. this.scriptTabNode.set("text", this.editor.app.lp.script);
  42. this.blockTabNode.setStyles(this.css.titleActionNode_current);
  43. this.blockTabNode.addEvent("click", function(){
  44. this.blockTabNode.setStyles(this.css.titleActionNode_current);
  45. this.blockArea.setStyle("display", "block");
  46. this.scriptTabNode.setStyles(this.css.scriptAreaTitleScriptNode);
  47. this.scriptArea.setStyle("display", "none");
  48. }.bind(this));
  49. this.scriptTabNode.addEvent("click", function(){
  50. this.blockTabNode.setStyles(this.css.scriptAreaTitleBlockNode);
  51. this.blockArea.setStyle("display", "none");
  52. this.scriptTabNode.setStyles(this.css.titleActionNode_current);
  53. this.scriptArea.setStyle("display", "block");
  54. }.bind(this));
  55. },
  56. createStatement: function(node, block){
  57. this.statementNodes.push(node);
  58. var classPath = ("statement."+block.data.class).split(".");
  59. var clazz = MWF.xApplication.ScriptEditor;
  60. classPath.each(function(p){ if (clazz){clazz = clazz[p];} }.bind(this));
  61. if (!clazz) clazz = this.createClazz(block);
  62. if (clazz){
  63. var statement = new clazz(node, block, this);
  64. return statement;
  65. }
  66. return null;
  67. },
  68. createClazz: function(block){
  69. var clazz = MWF.xApplication.ScriptEditor;
  70. var classPath = ("statement."+block.data.class).split(".");
  71. classPath.each(function(p, i){
  72. if (i===(classPath.length-1)){
  73. clazz[p] = new Class({
  74. Extends: MWF.xApplication.ScriptEditor.statement.$Statement[block.data.extend]
  75. });
  76. clazz = clazz[p];
  77. }else{
  78. clazz = clazz[p];
  79. if (!clazz) clazz = {};
  80. }
  81. }.bind(this));
  82. return clazz;
  83. },
  84. buildStatement: function(statement){
  85. statement.load();
  86. this.statements.push(statement);
  87. return statement;
  88. },
  89. // checkDownLinks: function(checkLink, p, max){
  90. // this.downLinks.each(function(link){
  91. // var v = Math.abs(link.position.x-p.x)+Math.abs(link.position.y-p.y);
  92. // if (checkLink.distance===null){
  93. // if (v<max){
  94. // checkLink.distance = v;
  95. // checkLink.link = link;
  96. // }
  97. // }else{
  98. // if (v<checkLink.distance){
  99. // checkLink.distance = v;
  100. // checkLink.link = link;
  101. // }
  102. // }
  103. // });
  104. // return checkLink;
  105. // },
  106. // checkUpLinks: function(checkLink, p, max){
  107. // this.upLinks.each(function(link){
  108. // var v = Math.abs(link.position.x-p.x)+Math.abs(link.position.y-p.y);
  109. // if (checkLink.distance===null){
  110. // if (v<max){
  111. // checkLink.distance = v;
  112. // checkLink.link = link;
  113. // }
  114. // }else{
  115. // if (v<checkLink.distance){
  116. // checkLink.distance = v;
  117. // checkLink.link = link;
  118. // }
  119. // }
  120. // });
  121. // return checkLink;
  122. // },
  123. checkLinks: function(node, statement, excludeLinks){
  124. var max = 200;
  125. var checkLink = {
  126. "distance": null,
  127. "link": null,
  128. "toLink": null,
  129. "linkType": ""
  130. };
  131. statement.normal();
  132. if (statement.topLink){
  133. var link = statement.topLink.rePosition();
  134. this.links.each(function(toLink){
  135. toLink.rePosition();
  136. var checkFlag = false;
  137. if (link.type!==toLink.type){
  138. if (excludeLinks.indexOf(toLink)===-1){
  139. var linkType = link.type+"-"+toLink.type;
  140. if (linkType==="up-down"){
  141. if (!link.statement.bottomLink && toLink.toLink){
  142. checkFlag = false;
  143. }else{
  144. checkFlag = true;
  145. }
  146. }
  147. if (linkType==="down-up"){
  148. if (!toLink.toLink && !link.toLink) checkFlag = true;
  149. }
  150. if (linkType==="up-middle") checkFlag = true;
  151. if (linkType==="middle-up"){
  152. if (!toLink.toLink && !link.toLink) checkFlag = true;
  153. }
  154. }
  155. }
  156. if (checkFlag){
  157. var v = Math.abs(toLink.position.x-link.position.x)+Math.abs(toLink.position.y-link.position.y);
  158. if (checkLink.distance===null){
  159. if (v<max){
  160. checkLink.distance = v;
  161. checkLink.link = link;
  162. checkLink.toLink = toLink;
  163. checkLink.linkType = linkType;
  164. }
  165. }else{
  166. if (v<checkLink.distance){
  167. checkLink.distance = v;
  168. checkLink.link = link;
  169. checkLink.toLink = toLink;
  170. checkLink.linkType = linkType;
  171. }
  172. }
  173. }
  174. }.bind(this));
  175. }
  176. this.clearCurrentLink();
  177. if (checkLink.link){
  178. if (this.currentLink){
  179. if (this.currentLink.link !== checkLink.link){
  180. this.currentLink.link.statement.notReadyLinkTo(this.currentLink);
  181. checkLink.link.statement.readyLinkTo(this.currentLink);
  182. }
  183. if (this.currentLink.toLink !== checkLink.toLink){
  184. this.currentLink.toLink.statement.notReadyLink(this.currentLink);
  185. checkLink.toLink.statement.readyLink(this.currentLink);
  186. }
  187. this.currentLink = checkLink;
  188. }else{
  189. this.currentLink = checkLink;
  190. this.currentLink.toLink.statement.readyLink(this.currentLink);
  191. this.currentLink.link.statement.readyLinkTo(this.currentLink);
  192. }
  193. }else{
  194. if (this.currentLink){
  195. this.currentLink.link.statement.notReadyLinkTo(this.currentLink);
  196. this.currentLink.toLink.statement.notReadyLink(this.currentLink);
  197. this.currentLink = null;
  198. }
  199. }
  200. },
  201. checkMortises: function(node, statement){
  202. this.currentMortise = null;
  203. if (statement.tenon){
  204. var p = statement.tenon.getPosition(this.blockArea);
  205. var size = statement.node.getSize();
  206. p.y = p.y+(size.y/2);
  207. for (var i=0; i<this.mortises.length; i++){
  208. var mortise = this.mortises[i];
  209. if (!mortise.tenonStatement){
  210. if (!mortise.types.length || (mortise.types.indexOf(statement.block.blockName)!==-1)){
  211. if (mortise.node.isPointIn(p.x,p.y,0,0,this.blockArea)){
  212. //mortise.node.setStyles(mortise.statement.css.mortise_current);
  213. mortise.shine();
  214. this.currentMortise = mortise;
  215. break;
  216. }else{
  217. mortise.unshine();
  218. //mortise.node.setStyles(mortise.statement.css.mortise);
  219. }
  220. }
  221. }
  222. }
  223. }
  224. // if (this.currentMortise){
  225. // this.currentMortise.setStyles(this.currentMortise.statement.css.)
  226. // }
  227. },
  228. checkBlockDrag: function(node, statement, excludeLinks){
  229. //var moveStatement = node.retrieve("statement");
  230. if (statement){
  231. this.checkLinks(node, statement, excludeLinks);
  232. this.checkMortises(node, statement);
  233. }
  234. },
  235. clearCurrentLink: function(){
  236. if (this.currentLink){
  237. this.currentLink.link.statement.notReadyLinkTo(this.currentLink);
  238. this.currentLink.toLink.statement.notReadyLink(this.currentLink);
  239. this.currentLink = null;
  240. }
  241. },
  242. getStatementGroup: function(statement){
  243. var statementGroup = [];
  244. var toLinkStatement = statement;
  245. while (toLinkStatement) {
  246. statementGroup.push(toLinkStatement);
  247. if (toLinkStatement.bottomLink){
  248. toLinkStatement = toLinkStatement.bottomLink.toLink;
  249. }else{
  250. toLinkStatement = null;
  251. }
  252. }
  253. return statementGroup;
  254. }
  255. });