| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- <?xml version="1.1" ?>
- <diagram author="Joeri Exelmans+Raphael Mannadiar+Simon Van Mierlo+Arkadiusz Michał Ryś" name="BouncingBalls">
- <description>SVG canvas with bouncing balls in it.</description>
- <top>console.log('Bouncing balls demo started.');</top>
- <inport name="userInterface"/>
- <class name="MainApp" default="true">
- <relationships>
- <association name="fields" class="Field"/>
- </relationships>
- <constructor>
- <body>this.nrOfFields = 0; console.log('Constructor fired.');</body>
- </constructor>
- <scxml initial="running">
- <state id="running" initial="root">
- <parallel id="root">
- <state id="mainBehaviour" initial="initializing">
- <state id="initializing">
- <transition target="../running">
- <raise event="createField"/>
- <script>console.log('Raising event createField.');</script>
- </transition>
- </state>
- <state id="running">
- <transition target="." event="buttonPressed" cond="eventName == 'createNewField'">
- <parameter name="eventName" type="str"/>
- <raise event="createField"/>
- <script>console.log('Raising event createField with ' + eventName + '.');</script>
- </transition>
- </state>
- </state>
- <state id="cdBehaviour" initial="waiting">
- <state id="waiting">
- <transition event="createField" target="../creating">
- <raise scope="cd" event="createInstance">
- <parameter expr="'fields'"/>
- </raise>
- <script>console.log('Raising event createInstance.');</script>
- </transition>
- <transition event="deleteField" target="../checkNrOfFields">
- <parameter name="associationName" type="str"/>
- <raise scope="cd" event="deleteInstance">
- <parameter expr="associationName"/>
- </raise>
- <script>this.nrOfFields -= 1;</script>
- </transition>
- </state>
- <state id="creating">
- <transition event="instanceCreated" target="../waiting">
- <parameter name="associationName" type="string"/>
- <raise scope="cd" event="startInstance">
- <parameter expr="associationName"/>
- </raise>
- <raise scope="narrow" event="setAssociationName" target="associationName">
- <parameter expr="associationName"/>
- </raise>
- <script>this.nrOfFields += 1;</script>
- </transition>
- </state>
- <state id="checkNrOfFields">
- <transition target="../stopped" cond="this.nrOfFields == 0">
- <raise event="stop"/>
- </transition>
- <transition target="../waiting" cond="this.nrOfFields != 0"/>
- </state>
- <state id="stopped"/>
- </state>
- <transition target="../stopped" event="stop">
- <script>
- // NOTE: ideally, this should be userInterface.closeWindow(userInterface.mainWindow). we can't do this though
- // due to a security measure preventing scripts from closing pages they didn't
- // open themselves
- userInterface.println('Refresh to play again.');
- </script>
- </transition>
- </parallel>
- <state id="stopped"/>
- </state>
- </scxml>
- </class>
- <class name="Field">
- <attribute name="canvas"/>
- <attribute name="fieldWindow"/>
- <inport name="fieldUi"/>
- <relationships>
- <association name="balls" class="Ball"/>
- <association name="buttons" class="Button"/>
- <association name="parent" class="MainApp" min="1" max="1"/>
- </relationships>
- <constructor>
- <body>
- this.fieldWindow = userInterface.newWindow(500, 500);
- this.canvas = userInterface.appendCanvas(this.fieldWindow,400,400,{'background':'#bbb'});
- userInterface.bindEvent(this.fieldWindow, userInterface.EVENTS.WINDOW_CLOSE, this.controller, 'windowClose', this.inports['fieldUi']);
- userInterface.bindEvent(this.fieldWindow, userInterface.EVENTS.KEY_PRESS, this.controller, 'keyPress', this.inports['fieldUi']);
- userInterface.bindEvent(this.canvas.element, userInterface.EVENTS.MOUSE_RIGHT_CLICK, this.controller, 'rightClick', this.inports['fieldUi']);
- userInterface.bindEvent(this.canvas.element, userInterface.EVENTS.MOUSE_MOVE, this.controller, 'mouseMove');
- userInterface.bindEvent(this.canvas.element, userInterface.EVENTS.MOUSE_RELEASE, this.controller, 'mouseRelease');
- console.log('Canvas field created.');
- </body>
- </constructor>
- <destructor>
- <body>userInterface.closeWindow(this.fieldWindow); console.log('Canvas field destroyed.');</body>
- </destructor>
- <scxml initial="root">
- <state id="root" initial="waiting">
- <state id="waiting">
- <transition event="setAssociationName" target="../initializing">
- <parameter name="associationName" type="str"/>
- <script>this.associationName = associationName;</script>
- </transition>
- </state>
- <state id="initializing">
- <transition target="../creating">
- <raise scope="cd" event="createInstance">
- <parameter expr="'buttons'"/>
- <parameter expr="'Button'"/>
- <parameter expr="this"/>
- <parameter expr="'createNewField'"/>
- <parameter expr="'Spawn New Window'"/>
- </raise>
- </transition>
- </state>
- <state id="creating">
- <transition event="instanceCreated" target="../packing">
- <parameter name="associationName" type="string"/>
- <raise scope="cd" event="startInstance">
- <parameter expr="associationName"/>
- </raise>
- </transition>
- </state>
- <state id="packing">
- <transition event="buttonCreated" target="../running"/>
- </state>
- <parallel id="running">
- <transition port="fieldUi" event="windowClose" target="../deleting">
- <raise event="deleteInstance" scope="cd">
- <parameter expr="'buttons'"/>
- </raise>
- <raise event="deleteInstance" scope="cd">
- <parameter expr="'balls'"/>
- </raise>
- </transition>
- <state id="mainBehaviour" initial="running">
- <state id="running">
- <transition port="fieldUi" event="rightClick" target="../creating">
- <parameter name="x"/>
- <parameter name="y"/>
- <parameter name="button"/>
- <raise scope="cd" event="createInstance">
- <parameter expr="'balls'"/>
- <parameter expr="'Ball'"/>
- <parameter expr="this.canvas"/>
- <parameter expr="x"/>
- <parameter expr="y"/>
- </raise>
- </transition>
- </state>
- <state id="creating">
- <transition event="instanceCreated" target="../running">
- <parameter name="associationName" type="string"/>
- <raise scope="cd" event="startInstance">
- <parameter expr="associationName"/>
- </raise>
- <raise scope="narrow" event="setAssociationName" target="associationName">
- <parameter expr="associationName"/>
- </raise>
- </transition>
- </state>
- </state>
- <state id="deletingBehaviour" initial="running">
- <state id="running">
- <transition event="deleteBall" target=".">
- <parameter name="associationName" type="str"/>
- <raise scope="cd" event="deleteInstance">
- <parameter expr="associationName"/>
- </raise>
- </transition>
- </state>
- </state>
- <state id="childBehaviour" initial="listening">
- <state id="listening">
- <transition event="buttonPressed" target=".">
- <parameter name="eventName" type="str"/>
- <raise event="buttonPressed" scope="narrow" target="'parent'">
- <parameter expr="eventName"/>
- </raise>
- </transition>
- </state>
- </state>
- <state id="deletingBallsBehaviour" initial="listening">
- <state id="listening">
- <transition port="fieldUi" event="keyPress" target="." cond="key == userInterface.KEYCODES.DELETE">
- <parameter name="key"/>
- <raise event="deleteSelf" scope="narrow" target="'balls'"/>
- </transition>
- </state>
- </state>
- </parallel>
- <state id="deleting">
- <transition target="../deleted">
- <raise event="deleteField" scope="narrow" target="'parent'">
- <parameter expr="this.associationName"/>
- </raise>
- </transition>
- </state>
- <state id="deleted"/>
- </state>
- </scxml>
- </class>
- <class name="Button">
- <relationships>
- <association name="parent" class="Field" min="1" max="1"/>
- </relationships>
- <inport name="buttonUi"/>
- <constructor>
- <parameter name="parent" type="Field"/>
- <parameter name="eventName" type="str"/>
- <parameter name="buttonText" type="str"/>
- <body>
- this.eventName = eventName;
- var button = userInterface.appendButton(parent.fieldWindow, buttonText);
- userInterface.bindEvent(button.element, userInterface.EVENTS.MOUSE_CLICK, this.controller, 'mouseClick', this.inports["buttonUi"]);
- console.log('Button created.');
- </body>
- </constructor>
- <scxml initial="initializing">
- <state id="initializing">
- <transition target="../running">
- <raise event="buttonCreated" scope="narrow" target="'parent'">
- </raise>
- </transition>
- </state>
- <state id="running">
- <transition port="buttonUi" event="mouseClick" target="." cond="button == userInterface.MOUSE_BUTTONS.LEFT">
- <parameter name="x"/>
- <parameter name="y"/>
- <parameter name="button"/>
- <raise event="buttonPressed" scope="narrow" target="'parent'">
- <parameter expr="this.eventName"/>
- </raise>
- </transition>
- </state>
- </scxml>
- </class>
- <class name="Ball">
- <atrribute name="element"/>
- <attribute name="canvas"/>
- <inport name="ballUi"/>
- <relationships>
- <association name="parent" class="Field" min="1" max="1"/>
- </relationships>
- <constructor>
- <parameter name="canvas"/>
- <parameter name="x"/>
- <parameter name="y"/>
- <body>
- this.canvas = canvas;
- this.r = 20.0;
- this.vel = {'x': utils.random() * 3.0, 'y': utils.random() * 3.0};
- this.mousePos = {};
- this.smooth = 0.4;
- var circle = this.canvas.addCircle(x, y, this.r, {'fill':'#000'});
- userInterface.bindEvent(circle, userInterface.EVENTS.MOUSE_PRESS, this.controller, 'mousePress', this.inports["ballUi"]);
- this.element = circle;
- console.log('Ball created.');
- </body>
- </constructor>
- <destructor>
- <body>this.canvas.removeElement(this.element); console.log('Ball destroyed.');</body>
- </destructor>
- <scxml initial="mainBehaviour">
- <state id="mainBehaviour" initial="initializing">
- <state id="initializing">
- <transition event="setAssociationName" target="../bouncing">
- <parameter name="associationName" type="str"/>
- <script>this.associationName = associationName; console.log('We wanna bounce!');</script>
- </transition>
- </state>
- <state id="bouncing">
- <transition after="0.00694444" target=".">
- <script>
- <![CDATA[
- var pos = this.element.getPosition();
- if (pos.x - this.r <= 0 || pos.x + this.r >= this.canvas.width) {
- this.vel.x = -this.vel.x;
- }
- if (pos.y - this.r <= 0 || pos.y + this.r >= this.canvas.height) {
- this.vel.y = -this.vel.y;
- }
- this.element.move(this.vel.x, this.vel.y);
- ]]>
- </script>
- </transition>
- <transition port="ballUi" event="mousePress" target="../selected" cond="button == userInterface.MOUSE_BUTTONS.LEFT">
- <parameter name="x"/>
- <parameter name="y"/>
- <parameter name="button"/>
- <script>this.element.setColor("#ff0");</script>
- </transition>
- </state>
- <state id="dragging">
- <transition port="userInterface" event="mouseMove" target=".">
- <parameter name="x"/>
- <parameter name="y"/>
- <parameter name="button"/>
- <script>
- <![CDATA[
- var dx = x - this.mousePos.x;
- var dy = y - this.mousePos.y;
- this.element.move(dx, dy);
- // keep ball within boundaries
- var pos = this.element.getPosition();
- if (pos.x-this.r <= 0) {
- pos.x = this.r + 1;
- }
- else if (pos.x+this.r >= this.canvas.width) {
- pos.x = this.canvas.width-this.r-1;
- }
- if (pos.y-this.r <= 0) {
- pos.y = this.r + 1;
- } else if (pos.y+this.r >= this.canvas.height) {
- pos.y = this.canvas.height-this.r-1;
- }
- this.element.setPosition(pos.x, pos.y);
- this.mousePos = {'x':x, 'y':y};
- this.vel = {
- 'x': (1-this.smooth)*dx + this.smooth*this.vel.x,
- 'y': (1-this.smooth)*dy + this.smooth*this.vel.y
- };
- ]]>
- </script>
- </transition>
- <transition port="userInterface" event="mouseRelease" target="../bouncing">
- <parameter name="x"/>
- <parameter name="y"/>
- <script>this.element.setColor("#f00");</script>
- </transition>
- </state>
- <state id="selected">
- <transition port="ballUi" event="mousePress" target="../dragging" cond="button == userInterface.MOUSE_BUTTONS.LEFT">
- <parameter name="x"/>
- <parameter name="y"/>
- <parameter name="button"/>
- <script>this.mousePos = {'x':x, 'y':y};</script>
- </transition>
- <transition event="deleteSelf" target="../../deleted">
- <raise event="deleteBall" scope="narrow" target="'parent'">
- <parameter expr="this.associationName"/>
- </raise>
- </transition>
- </state>
- </state>
- <state id="deleted"/>
- </scxml>
- </class>
- </diagram>
|