ScriptArea.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. debugger;
  70. var clazz = MWF.xApplication.ScriptEditor;
  71. var classPath = ("statement."+block.data.class).split(".");
  72. classPath.each(function(p, i){
  73. if (i===(classPath.length-1)){
  74. clazz[p] = new Class({
  75. Extends: MWF.xApplication.ScriptEditor.statement.$Statement[block.data.extend]
  76. });
  77. clazz = clazz[p];
  78. }else{
  79. clazz = clazz[p];
  80. if (!clazz) clazz = {};
  81. }
  82. }.bind(this));
  83. return clazz;
  84. },
  85. buildStatement: function(statement){
  86. statement.load();
  87. this.statements.push(statement);
  88. return statement;
  89. },
  90. // checkDownLinks: function(checkLink, p, max){
  91. // this.downLinks.each(function(link){
  92. // var v = Math.abs(link.position.x-p.x)+Math.abs(link.position.y-p.y);
  93. // if (checkLink.distance===null){
  94. // if (v<max){
  95. // checkLink.distance = v;
  96. // checkLink.link = link;
  97. // }
  98. // }else{
  99. // if (v<checkLink.distance){
  100. // checkLink.distance = v;
  101. // checkLink.link = link;
  102. // }
  103. // }
  104. // });
  105. // return checkLink;
  106. // },
  107. // checkUpLinks: function(checkLink, p, max){
  108. // this.upLinks.each(function(link){
  109. // var v = Math.abs(link.position.x-p.x)+Math.abs(link.position.y-p.y);
  110. // if (checkLink.distance===null){
  111. // if (v<max){
  112. // checkLink.distance = v;
  113. // checkLink.link = link;
  114. // }
  115. // }else{
  116. // if (v<checkLink.distance){
  117. // checkLink.distance = v;
  118. // checkLink.link = link;
  119. // }
  120. // }
  121. // });
  122. // return checkLink;
  123. // },
  124. checkLinks: function(node, statement, excludeLinks){
  125. var max = 200;
  126. var checkLink = {
  127. "distance": null,
  128. "link": null,
  129. "toLink": null,
  130. "linkType": ""
  131. };
  132. statement.normal();
  133. if (statement.topLink){
  134. var link = statement.topLink.rePosition();
  135. this.links.each(function(toLink){
  136. toLink.rePosition();
  137. var checkFlag = false;
  138. if (link.type!==toLink.type){
  139. if (excludeLinks.indexOf(toLink)===-1){
  140. var linkType = link.type+"-"+toLink.type;
  141. if (linkType==="up-down"){
  142. if (!link.statement.bottomLink && toLink.toLink){
  143. checkFlag = false;
  144. }else{
  145. checkFlag = true;
  146. }
  147. }
  148. if (linkType==="down-up"){
  149. if (!toLink.toLink && !link.toLink) checkFlag = true;
  150. }
  151. if (linkType==="up-middle") checkFlag = true;
  152. if (linkType==="middle-up"){
  153. if (!toLink.toLink && !link.toLink) checkFlag = true;
  154. }
  155. }
  156. }
  157. if (checkFlag){
  158. var v = Math.abs(toLink.position.x-link.position.x)+Math.abs(toLink.position.y-link.position.y);
  159. if (checkLink.distance===null){
  160. if (v<max){
  161. checkLink.distance = v;
  162. checkLink.link = link;
  163. checkLink.toLink = toLink;
  164. checkLink.linkType = linkType;
  165. }
  166. }else{
  167. if (v<checkLink.distance){
  168. checkLink.distance = v;
  169. checkLink.link = link;
  170. checkLink.toLink = toLink;
  171. checkLink.linkType = linkType;
  172. }
  173. }
  174. }
  175. }.bind(this));
  176. }
  177. this.clearCurrentLink();
  178. if (checkLink.link){
  179. if (this.currentLink){
  180. if (this.currentLink.link !== checkLink.link){
  181. this.currentLink.link.statement.notReadyLinkTo(this.currentLink);
  182. checkLink.link.statement.readyLinkTo(this.currentLink);
  183. }
  184. if (this.currentLink.toLink !== checkLink.toLink){
  185. this.currentLink.toLink.statement.notReadyLink(this.currentLink);
  186. checkLink.toLink.statement.readyLink(this.currentLink);
  187. }
  188. this.currentLink = checkLink;
  189. }else{
  190. this.currentLink = checkLink;
  191. this.currentLink.toLink.statement.readyLink(this.currentLink);
  192. this.currentLink.link.statement.readyLinkTo(this.currentLink);
  193. }
  194. }else{
  195. if (this.currentLink){
  196. this.currentLink.link.statement.notReadyLinkTo(this.currentLink);
  197. this.currentLink.toLink.statement.notReadyLink(this.currentLink);
  198. this.currentLink = null;
  199. }
  200. }
  201. },
  202. checkMortises: function(node, statement){
  203. this.currentMortise = null;
  204. if (statement.tenon){
  205. var p = statement.tenon.getPosition(this.blockArea);
  206. var size = statement.node.getSize();
  207. p.y = p.y+(size.y/2);
  208. for (var i=0; i<this.mortises.length; i++){
  209. var mortise = this.mortises[i];
  210. if (!mortise.tenonStatement){
  211. if (!mortise.types.length || (mortise.types.indexOf(statement.block.blockName)!==-1)){
  212. if (mortise.node.isPointIn(p.x,p.y,0,0,this.blockArea)){
  213. //mortise.node.setStyles(mortise.statement.css.mortise_current);
  214. mortise.shine();
  215. this.currentMortise = mortise;
  216. break;
  217. }else{
  218. mortise.unshine();
  219. //mortise.node.setStyles(mortise.statement.css.mortise);
  220. }
  221. }
  222. }
  223. }
  224. }
  225. // if (this.currentMortise){
  226. // this.currentMortise.setStyles(this.currentMortise.statement.css.)
  227. // }
  228. },
  229. checkBlockDrag: function(node, statement, excludeLinks){
  230. //var moveStatement = node.retrieve("statement");
  231. if (statement){
  232. this.checkLinks(node, statement, excludeLinks);
  233. this.checkMortises(node, statement);
  234. }
  235. },
  236. clearCurrentLink: function(){
  237. if (this.currentLink){
  238. this.currentLink.link.statement.notReadyLinkTo(this.currentLink);
  239. this.currentLink.toLink.statement.notReadyLink(this.currentLink);
  240. this.currentLink = null;
  241. }
  242. },
  243. getStatementGroup: function(statement){
  244. var statementGroup = [];
  245. var toLinkStatement = statement;
  246. while (toLinkStatement) {
  247. statementGroup.push(toLinkStatement);
  248. if (toLinkStatement.bottomLink){
  249. toLinkStatement = toLinkStatement.bottomLink.toLink;
  250. }else{
  251. toLinkStatement = null;
  252. }
  253. }
  254. return statementGroup;
  255. }
  256. });