sccd.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?xml version="1.1" ?>
  2. <diagram author="Joeri Exelmans+Raphael Mannadiar+Simon Van Mierlo+Arkadiusz Michał Ryś" name="BouncingBalls">
  3. <description>SVG canvas with bouncing balls in it.</description>
  4. <top>console.log('Bouncing balls demo started.');</top>
  5. <inport name="userInterface"/>
  6. <class name="MainApp" default="true">
  7. <relationships>
  8. <association name="fields" class="Field"/>
  9. </relationships>
  10. <constructor>
  11. <body>this.nrOfFields = 0; console.log('Constructor fired.');</body>
  12. </constructor>
  13. <scxml initial="running">
  14. <state id="running" initial="root">
  15. <parallel id="root">
  16. <state id="mainBehaviour" initial="initializing">
  17. <state id="initializing">
  18. <transition target="../running">
  19. <raise event="createField"/>
  20. <script>console.log('Raising event createField.');</script>
  21. </transition>
  22. </state>
  23. <state id="running">
  24. <transition target="." event="buttonPressed" cond="eventName == 'createNewField'">
  25. <parameter name="eventName" type="str"/>
  26. <raise event="createField"/>
  27. <script>console.log('Raising event createField with ' + eventName + '.');</script>
  28. </transition>
  29. </state>
  30. </state>
  31. <state id="cdBehaviour" initial="waiting">
  32. <state id="waiting">
  33. <transition event="createField" target="../creating">
  34. <raise scope="cd" event="createInstance">
  35. <parameter expr="'fields'"/>
  36. </raise>
  37. <script>console.log('Raising event createInstance.');</script>
  38. </transition>
  39. <transition event="deleteField" target="../checkNrOfFields">
  40. <parameter name="associationName" type="str"/>
  41. <raise scope="cd" event="deleteInstance">
  42. <parameter expr="associationName"/>
  43. </raise>
  44. <script>this.nrOfFields -= 1;</script>
  45. </transition>
  46. </state>
  47. <state id="creating">
  48. <transition event="instanceCreated" target="../waiting">
  49. <parameter name="associationName" type="string"/>
  50. <raise scope="cd" event="startInstance">
  51. <parameter expr="associationName"/>
  52. </raise>
  53. <raise scope="narrow" event="setAssociationName" target="associationName">
  54. <parameter expr="associationName"/>
  55. </raise>
  56. <script>this.nrOfFields += 1;</script>
  57. </transition>
  58. </state>
  59. <state id="checkNrOfFields">
  60. <transition target="../stopped" cond="this.nrOfFields == 0">
  61. <raise event="stop"/>
  62. </transition>
  63. <transition target="../waiting" cond="this.nrOfFields != 0"/>
  64. </state>
  65. <state id="stopped"/>
  66. </state>
  67. <transition target="../stopped" event="stop">
  68. <script>
  69. // NOTE: ideally, this should be userInterface.closeWindow(userInterface.mainWindow). we can't do this though
  70. // due to a security measure preventing scripts from closing pages they didn't
  71. // open themselves
  72. userInterface.println('Refresh to play again.');
  73. </script>
  74. </transition>
  75. </parallel>
  76. <state id="stopped"/>
  77. </state>
  78. </scxml>
  79. </class>
  80. <class name="Field">
  81. <attribute name="canvas"/>
  82. <attribute name="fieldWindow"/>
  83. <inport name="fieldUi"/>
  84. <relationships>
  85. <association name="balls" class="Ball"/>
  86. <association name="buttons" class="Button"/>
  87. <association name="parent" class="MainApp" min="1" max="1"/>
  88. </relationships>
  89. <constructor>
  90. <body>
  91. this.fieldWindow = userInterface.newWindow(500, 500);
  92. this.canvas = userInterface.appendCanvas(this.fieldWindow,400,400,{'background':'#bbb'});
  93. userInterface.bindEvent(this.fieldWindow, userInterface.EVENTS.WINDOW_CLOSE, this.controller, 'windowClose', this.inports['fieldUi']);
  94. userInterface.bindEvent(this.fieldWindow, userInterface.EVENTS.KEY_PRESS, this.controller, 'keyPress', this.inports['fieldUi']);
  95. userInterface.bindEvent(this.canvas.element, userInterface.EVENTS.MOUSE_RIGHT_CLICK, this.controller, 'rightClick', this.inports['fieldUi']);
  96. userInterface.bindEvent(this.canvas.element, userInterface.EVENTS.MOUSE_MOVE, this.controller, 'mouseMove');
  97. userInterface.bindEvent(this.canvas.element, userInterface.EVENTS.MOUSE_RELEASE, this.controller, 'mouseRelease');
  98. console.log('Canvas field created.');
  99. </body>
  100. </constructor>
  101. <destructor>
  102. <body>userInterface.closeWindow(this.fieldWindow); console.log('Canvas field destroyed.');</body>
  103. </destructor>
  104. <scxml initial="root">
  105. <state id="root" initial="waiting">
  106. <state id="waiting">
  107. <transition event="setAssociationName" target="../initializing">
  108. <parameter name="associationName" type="str"/>
  109. <script>this.associationName = associationName;</script>
  110. </transition>
  111. </state>
  112. <state id="initializing">
  113. <transition target="../creating">
  114. <raise scope="cd" event="createInstance">
  115. <parameter expr="'buttons'"/>
  116. <parameter expr="'Button'"/>
  117. <parameter expr="this"/>
  118. <parameter expr="'createNewField'"/>
  119. <parameter expr="'Spawn New Window'"/>
  120. </raise>
  121. </transition>
  122. </state>
  123. <state id="creating">
  124. <transition event="instanceCreated" target="../packing">
  125. <parameter name="associationName" type="string"/>
  126. <raise scope="cd" event="startInstance">
  127. <parameter expr="associationName"/>
  128. </raise>
  129. </transition>
  130. </state>
  131. <state id="packing">
  132. <transition event="buttonCreated" target="../running"/>
  133. </state>
  134. <parallel id="running">
  135. <transition port="fieldUi" event="windowClose" target="../deleting">
  136. <raise event="deleteInstance" scope="cd">
  137. <parameter expr="'buttons'"/>
  138. </raise>
  139. <raise event="deleteInstance" scope="cd">
  140. <parameter expr="'balls'"/>
  141. </raise>
  142. </transition>
  143. <state id="mainBehaviour" initial="running">
  144. <state id="running">
  145. <transition port="fieldUi" event="rightClick" target="../creating">
  146. <parameter name="x"/>
  147. <parameter name="y"/>
  148. <parameter name="button"/>
  149. <raise scope="cd" event="createInstance">
  150. <parameter expr="'balls'"/>
  151. <parameter expr="'Ball'"/>
  152. <parameter expr="this.canvas"/>
  153. <parameter expr="x"/>
  154. <parameter expr="y"/>
  155. </raise>
  156. </transition>
  157. </state>
  158. <state id="creating">
  159. <transition event="instanceCreated" target="../running">
  160. <parameter name="associationName" type="string"/>
  161. <raise scope="cd" event="startInstance">
  162. <parameter expr="associationName"/>
  163. </raise>
  164. <raise scope="narrow" event="setAssociationName" target="associationName">
  165. <parameter expr="associationName"/>
  166. </raise>
  167. </transition>
  168. </state>
  169. </state>
  170. <state id="deletingBehaviour" initial="running">
  171. <state id="running">
  172. <transition event="deleteBall" target=".">
  173. <parameter name="associationName" type="str"/>
  174. <raise scope="cd" event="deleteInstance">
  175. <parameter expr="associationName"/>
  176. </raise>
  177. </transition>
  178. </state>
  179. </state>
  180. <state id="childBehaviour" initial="listening">
  181. <state id="listening">
  182. <transition event="buttonPressed" target=".">
  183. <parameter name="eventName" type="str"/>
  184. <raise event="buttonPressed" scope="narrow" target="'parent'">
  185. <parameter expr="eventName"/>
  186. </raise>
  187. </transition>
  188. </state>
  189. </state>
  190. <state id="deletingBallsBehaviour" initial="listening">
  191. <state id="listening">
  192. <transition port="fieldUi" event="keyPress" target="." cond="key == userInterface.KEYCODES.DELETE">
  193. <parameter name="key"/>
  194. <raise event="deleteSelf" scope="narrow" target="'balls'"/>
  195. </transition>
  196. </state>
  197. </state>
  198. </parallel>
  199. <state id="deleting">
  200. <transition target="../deleted">
  201. <raise event="deleteField" scope="narrow" target="'parent'">
  202. <parameter expr="this.associationName"/>
  203. </raise>
  204. </transition>
  205. </state>
  206. <state id="deleted"/>
  207. </state>
  208. </scxml>
  209. </class>
  210. <class name="Button">
  211. <relationships>
  212. <association name="parent" class="Field" min="1" max="1"/>
  213. </relationships>
  214. <inport name="buttonUi"/>
  215. <constructor>
  216. <parameter name="parent" type="Field"/>
  217. <parameter name="eventName" type="str"/>
  218. <parameter name="buttonText" type="str"/>
  219. <body>
  220. this.eventName = eventName;
  221. var button = userInterface.appendButton(parent.fieldWindow, buttonText);
  222. userInterface.bindEvent(button.element, userInterface.EVENTS.MOUSE_CLICK, this.controller, 'mouseClick', this.inports["buttonUi"]);
  223. console.log('Button created.');
  224. </body>
  225. </constructor>
  226. <scxml initial="initializing">
  227. <state id="initializing">
  228. <transition target="../running">
  229. <raise event="buttonCreated" scope="narrow" target="'parent'">
  230. </raise>
  231. </transition>
  232. </state>
  233. <state id="running">
  234. <transition port="buttonUi" event="mouseClick" target="." cond="button == userInterface.MOUSE_BUTTONS.LEFT">
  235. <parameter name="x"/>
  236. <parameter name="y"/>
  237. <parameter name="button"/>
  238. <raise event="buttonPressed" scope="narrow" target="'parent'">
  239. <parameter expr="this.eventName"/>
  240. </raise>
  241. </transition>
  242. </state>
  243. </scxml>
  244. </class>
  245. <class name="Ball">
  246. <atrribute name="element"/>
  247. <attribute name="canvas"/>
  248. <inport name="ballUi"/>
  249. <relationships>
  250. <association name="parent" class="Field" min="1" max="1"/>
  251. </relationships>
  252. <constructor>
  253. <parameter name="canvas"/>
  254. <parameter name="x"/>
  255. <parameter name="y"/>
  256. <body>
  257. this.canvas = canvas;
  258. this.r = 20.0;
  259. this.vel = {'x': utils.random() * 3.0, 'y': utils.random() * 3.0};
  260. this.mousePos = {};
  261. this.smooth = 0.4;
  262. var circle = this.canvas.addCircle(x, y, this.r, {'fill':'#000'});
  263. userInterface.bindEvent(circle, userInterface.EVENTS.MOUSE_PRESS, this.controller, 'mousePress', this.inports["ballUi"]);
  264. this.element = circle;
  265. console.log('Ball created.');
  266. </body>
  267. </constructor>
  268. <destructor>
  269. <body>this.canvas.removeElement(this.element); console.log('Ball destroyed.');</body>
  270. </destructor>
  271. <scxml initial="mainBehaviour">
  272. <state id="mainBehaviour" initial="initializing">
  273. <state id="initializing">
  274. <transition event="setAssociationName" target="../bouncing">
  275. <parameter name="associationName" type="str"/>
  276. <script>this.associationName = associationName; console.log('We wanna bounce!');</script>
  277. </transition>
  278. </state>
  279. <state id="bouncing">
  280. <transition after="0.00694444" target=".">
  281. <script>
  282. <![CDATA[
  283. var pos = this.element.getPosition();
  284. if (pos.x - this.r <= 0 || pos.x + this.r >= this.canvas.width) {
  285. this.vel.x = -this.vel.x;
  286. }
  287. if (pos.y - this.r <= 0 || pos.y + this.r >= this.canvas.height) {
  288. this.vel.y = -this.vel.y;
  289. }
  290. this.element.move(this.vel.x, this.vel.y);
  291. ]]>
  292. </script>
  293. </transition>
  294. <transition port="ballUi" event="mousePress" target="../selected" cond="button == userInterface.MOUSE_BUTTONS.LEFT">
  295. <parameter name="x"/>
  296. <parameter name="y"/>
  297. <parameter name="button"/>
  298. <script>this.element.setColor("#ff0");</script>
  299. </transition>
  300. </state>
  301. <state id="dragging">
  302. <transition port="userInterface" event="mouseMove" target=".">
  303. <parameter name="x"/>
  304. <parameter name="y"/>
  305. <parameter name="button"/>
  306. <script>
  307. <![CDATA[
  308. var dx = x - this.mousePos.x;
  309. var dy = y - this.mousePos.y;
  310. this.element.move(dx, dy);
  311. // keep ball within boundaries
  312. var pos = this.element.getPosition();
  313. if (pos.x-this.r <= 0) {
  314. pos.x = this.r + 1;
  315. }
  316. else if (pos.x+this.r >= this.canvas.width) {
  317. pos.x = this.canvas.width-this.r-1;
  318. }
  319. if (pos.y-this.r <= 0) {
  320. pos.y = this.r + 1;
  321. } else if (pos.y+this.r >= this.canvas.height) {
  322. pos.y = this.canvas.height-this.r-1;
  323. }
  324. this.element.setPosition(pos.x, pos.y);
  325. this.mousePos = {'x':x, 'y':y};
  326. this.vel = {
  327. 'x': (1-this.smooth)*dx + this.smooth*this.vel.x,
  328. 'y': (1-this.smooth)*dy + this.smooth*this.vel.y
  329. };
  330. ]]>
  331. </script>
  332. </transition>
  333. <transition port="userInterface" event="mouseRelease" target="../bouncing">
  334. <parameter name="x"/>
  335. <parameter name="y"/>
  336. <script>this.element.setColor("#f00");</script>
  337. </transition>
  338. </state>
  339. <state id="selected">
  340. <transition port="ballUi" event="mousePress" target="../dragging" cond="button == userInterface.MOUSE_BUTTONS.LEFT">
  341. <parameter name="x"/>
  342. <parameter name="y"/>
  343. <parameter name="button"/>
  344. <script>this.mousePos = {'x':x, 'y':y};</script>
  345. </transition>
  346. <transition event="deleteSelf" target="../../deleted">
  347. <raise event="deleteBall" scope="narrow" target="'parent'">
  348. <parameter expr="this.associationName"/>
  349. </raise>
  350. </transition>
  351. </state>
  352. </state>
  353. <state id="deleted"/>
  354. </scxml>
  355. </class>
  356. </diagram>