|
@@ -0,0 +1,369 @@
|
|
|
+<?xml version="1.0" ?>
|
|
|
+<diagram author="Simon Van Mierlo" name="Bouncing Balls - Tkinter Version ">
|
|
|
+ <description>
|
|
|
+ Tkinter frame with bouncing balls in it.
|
|
|
+ </description>
|
|
|
+ <top>
|
|
|
+ import random
|
|
|
+ import Tkinter as tk
|
|
|
+ from widget import Widget
|
|
|
+ </top>
|
|
|
+ <inport name="input"/>
|
|
|
+ <class name="MainApp" default="true">
|
|
|
+ <relationships>
|
|
|
+ <association name="windows" class="Window" />
|
|
|
+ </relationships>
|
|
|
+ <constructor>
|
|
|
+ <parameter name="root"/>
|
|
|
+ <body>
|
|
|
+ self.nr_of_windows = 0
|
|
|
+ self.root = root
|
|
|
+ </body>
|
|
|
+ </constructor>
|
|
|
+ <scxml initial="main">
|
|
|
+ <parallel id="main">
|
|
|
+ <state id="main_behaviour" initial="initializing">
|
|
|
+ <state id="initializing">
|
|
|
+ <onentry>
|
|
|
+ <raise event="create_window" scope="local" />
|
|
|
+ </onentry>
|
|
|
+ <transition target="../running" />
|
|
|
+ </state>
|
|
|
+ <state id="running">
|
|
|
+ <transition event="window_created" target=".">
|
|
|
+ <script>
|
|
|
+ self.nr_of_windows += 1
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ <transition event="window_deleted" target="." cond="self.nr_of_windows > 1">
|
|
|
+ <script>
|
|
|
+ self.nr_of_windows -= 1
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ <transition event="window_deleted" target="." cond="self.nr_of_windows == 1">
|
|
|
+ <raise event="stop" scope="local" />
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ </state>
|
|
|
+ <state id="creating_behaviour" initial="waiting">
|
|
|
+ <state id="waiting">
|
|
|
+ <transition event="create_window" target="../creating">
|
|
|
+ <raise event="create_instance" scope="cd">
|
|
|
+ <parameter expr="'windows'" />
|
|
|
+ <paremeter expr="'Window'" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="creating">
|
|
|
+ <transition event="instance_created" target="../waiting">
|
|
|
+ <parameter name="association_name" />
|
|
|
+ <raise event="start_instance" scope="cd">
|
|
|
+ <parameter expr="association_name" />
|
|
|
+ </raise>
|
|
|
+ <raise event="set_association_name" scope="narrow" target="association_name">
|
|
|
+ <parameter expr="association_name" />
|
|
|
+ </raise>
|
|
|
+ <raise event="window_created" scope="local" />
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ </state>
|
|
|
+ <state id="deleting_behaviour" initial="waiting">
|
|
|
+ <state id="waiting">
|
|
|
+ <transition event="delete_window" target="../deleting">
|
|
|
+ <parameter name="association_name" />
|
|
|
+ <raise event="delete_instance" scope="cd">
|
|
|
+ <parameter expr="association_name" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="deleting">
|
|
|
+ <transition event="instance_deleted" target="../waiting">
|
|
|
+ <raise event="window_deleted" scope="local" />
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ </state>
|
|
|
+ <transition event="stop" target="../stopped" />
|
|
|
+ </parallel>
|
|
|
+ <state id="stopped">
|
|
|
+ <onentry>
|
|
|
+ <script>
|
|
|
+ self.root.quit()
|
|
|
+ </script>
|
|
|
+ </onentry>
|
|
|
+ </state>
|
|
|
+ </scxml>
|
|
|
+ </class>
|
|
|
+ <class name="Window">
|
|
|
+ <relationships>
|
|
|
+ <association name="parent" class="MainApp" min="1" max="1" />
|
|
|
+ <association name="buttons" class="Button" />
|
|
|
+ <association name="balls" class="Ball" />
|
|
|
+ <inheritance class="tk.Toplevel" priority="1" />
|
|
|
+ <inheritance class="Widget" priority="0" />
|
|
|
+ </relationships>
|
|
|
+ <constructor>
|
|
|
+ <super class="tk.Toplevel" />
|
|
|
+ <super class="Widget">
|
|
|
+ <parameter expr="True" />
|
|
|
+ </super>
|
|
|
+ <body>
|
|
|
+ <![CDATA[
|
|
|
+ self.title('BouncingBalls')
|
|
|
+
|
|
|
+ CANVAS_SIZE_TUPLE = (0, 0, self.winfo_screenwidth() * 2, self.winfo_screenheight() * 2)
|
|
|
+ self.c = tk.Canvas(self, relief=tk.RIDGE, scrollregion=CANVAS_SIZE_TUPLE)
|
|
|
+
|
|
|
+ self.set_bindable_and_tagorid(self.c)
|
|
|
+ ]]>
|
|
|
+ </body>
|
|
|
+ </constructor>
|
|
|
+ <destructor>
|
|
|
+ <body>
|
|
|
+ self.destroy()
|
|
|
+ </body>
|
|
|
+ </destructor>
|
|
|
+ <scxml initial="main">
|
|
|
+ <parallel id="main">
|
|
|
+ <state id="main_behaviour" initial="initializing">
|
|
|
+ <state id="initializing">
|
|
|
+ <transition event="set_association_name" target="../creating_button">
|
|
|
+ <parameter name="association_name" />
|
|
|
+ <script>
|
|
|
+ self.association_name = association_name
|
|
|
+ </script>
|
|
|
+ <raise scope="cd" event="create_instance">
|
|
|
+ <parameter expr="'buttons'" />
|
|
|
+ <parameter expr="'Button'" />
|
|
|
+ <parameter expr="self" />
|
|
|
+ <parameter expr="'create_window'" />
|
|
|
+ <parameter expr="'Create Window'" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="creating_button">
|
|
|
+ <transition event="instance_created" target="../packing_button">
|
|
|
+ <parameter name="association_name" type="string"/>
|
|
|
+ <raise scope="cd" event="start_instance">
|
|
|
+ <parameter expr="association_name" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="packing_button">
|
|
|
+ <transition event="button_created" target="../running">
|
|
|
+ <parameter name="button" type="Button"/>
|
|
|
+ <script>
|
|
|
+ button.pack(expand=False, fill=tk.X, side=tk.TOP)
|
|
|
+ self.c.focus_force()
|
|
|
+ self.c.pack(expand=True, fill=tk.BOTH)
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="running">
|
|
|
+ <transition event="window-close" port="input" target="." cond="tagorid == id(self)">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ <raise event="stop" scope="local" />
|
|
|
+ </transition>
|
|
|
+ <transition event="button_pressed" target="." cond="event_name == 'create_window'">
|
|
|
+ <parameter name="event_name" type="string" />
|
|
|
+ <raise event="create_window" scope="narrow" target="'parent'" />
|
|
|
+ </transition>
|
|
|
+ <transition event="right-click" port="input" target="../creating_ball" cond="tagorid == id(self)">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ <raise scope="cd" event="create_instance">
|
|
|
+ <parameter expr='"balls"' />
|
|
|
+ <parameter expr='"Ball"' />
|
|
|
+ <parameter expr="self.c" />
|
|
|
+ <parameter expr="self.last_x" />
|
|
|
+ <parameter expr="self.last_y" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ <transition event="delete_ball" target=".">
|
|
|
+ <parameter name="association_name" type="string" />
|
|
|
+ <raise event="delete_instance" scope="cd">
|
|
|
+ <parameter expr="association_name" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="creating_ball">
|
|
|
+ <transition event="instance_created" target="../running">
|
|
|
+ <parameter name="association_name" type="string"/>
|
|
|
+ <raise event="start_instance" scope="cd">
|
|
|
+ <parameter expr="association_name" />
|
|
|
+ </raise>
|
|
|
+ <raise event="set_association_name" scope="narrow" target="association_name">
|
|
|
+ <parameter expr="association_name" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ </state>
|
|
|
+ <transition event="stop" target="../stopped">
|
|
|
+ <raise event="delete_instance" scope="cd">
|
|
|
+ <parameter expr="'buttons'" />
|
|
|
+ </raise>
|
|
|
+ <raise event="delete_instance" scope="cd">
|
|
|
+ <parameter expr="'balls'" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </parallel>
|
|
|
+ <state id="stopped">
|
|
|
+ <onentry>
|
|
|
+ <raise event="delete_window" scope="narrow" target="'parent'">
|
|
|
+ <parameter expr="self.association_name" />
|
|
|
+ </raise>
|
|
|
+ </onentry>
|
|
|
+ </state>
|
|
|
+ </scxml>
|
|
|
+ </class>
|
|
|
+ <class name="Button">
|
|
|
+ <relationships>
|
|
|
+ <association name="parent" class="Field" min="1" max="1" />
|
|
|
+ <inheritance class="tk.Button" priority="1" />
|
|
|
+ <inheritance class="Widget" priority="0" />
|
|
|
+ </relationships>
|
|
|
+ <constructor>
|
|
|
+ <parameter name="parent" type="Field" />
|
|
|
+ <parameter name="event_name" type="str" />
|
|
|
+ <parameter name="button_text" type="str" />
|
|
|
+ <super class="tk.Button">
|
|
|
+ <parameter expr="parent" />
|
|
|
+ <parameter expr="**{'text': button_text}" />
|
|
|
+ </super>
|
|
|
+ <body>
|
|
|
+ self.event_name = event_name
|
|
|
+ </body>
|
|
|
+ </constructor>
|
|
|
+ <destructor>
|
|
|
+ <body>
|
|
|
+ self.destroy()
|
|
|
+ </body>
|
|
|
+ </destructor>
|
|
|
+ <scxml initial="initializing">
|
|
|
+ <state id="initializing">
|
|
|
+ <onentry>
|
|
|
+ <raise event="button_created" scope="narrow" target="'parent'">
|
|
|
+ <parameter expr="self" />
|
|
|
+ </raise>
|
|
|
+ </onentry>
|
|
|
+ <transition target="../running" />
|
|
|
+ </state>
|
|
|
+ <state id="running">
|
|
|
+ <transition event="left-click" port="input" target="." cond="tagorid == id(self)">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ <raise event="button_pressed" scope="narrow" target="'parent'">
|
|
|
+ <parameter expr="self.event_name" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ </scxml>
|
|
|
+ </class>
|
|
|
+ <class name="Ball">
|
|
|
+ <relationships>
|
|
|
+ <association name="parent" class="Window" min="1" max="1" />
|
|
|
+ <inheritance class="Widget" priority="0" />
|
|
|
+ </relationships>
|
|
|
+ <attribute name="canvas" />
|
|
|
+ <constructor>
|
|
|
+ <parameter name="canvas" />
|
|
|
+ <parameter name="x" />
|
|
|
+ <parameter name="y" />
|
|
|
+ <super class="Widget">
|
|
|
+ <parameter expr="True" />
|
|
|
+ </super>
|
|
|
+ <body>
|
|
|
+ self.canvas = canvas
|
|
|
+ self.r = 20.0;
|
|
|
+ self.vel = {'x': random.uniform(-5.0, 5.0), 'y': random.uniform(-5.0, 5.0)};
|
|
|
+ self.smooth = 0.4 # value between 0 and 1
|
|
|
+ self.id = self.canvas.create_oval(x, y, x + (self.r * 2), y + (self.r * 2), fill="black")
|
|
|
+ self.set_bindable_and_tagorid(self.canvas, self.id)
|
|
|
+ </body>
|
|
|
+ </constructor>
|
|
|
+ <destructor>
|
|
|
+ <body>
|
|
|
+ self.canvas.delete(self.id)
|
|
|
+ </body>
|
|
|
+ </destructor>
|
|
|
+ <scxml initial="initializing">
|
|
|
+ <state id="initializing">
|
|
|
+ <transition event="set_association_name" target="../bouncing">
|
|
|
+ <parameter name="association_name" type="str" />
|
|
|
+ <script>
|
|
|
+ self.association_name = association_name
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="bouncing">
|
|
|
+ <transition after="(20 - self.getSimulatedTime() % 20) / 1000.0" target=".">
|
|
|
+ <script>
|
|
|
+ <![CDATA[
|
|
|
+ pos = self.canvas.coords(self.id)
|
|
|
+ x = self.canvas.canvasx(pos[0])
|
|
|
+ y = self.canvas.canvasy(pos[1])
|
|
|
+ if x <= 0 or x + (self.r * 2) >= self.canvas.canvasx(self.canvas.winfo_width()):
|
|
|
+ self.vel['x'] = -self.vel['x']
|
|
|
+ if y <= 0 or y + (self.r * 2) >= self.canvas.canvasy(self.canvas.winfo_height()):
|
|
|
+ self.vel['y'] = -self.vel['y']
|
|
|
+ self.canvas.move(self.id, self.vel['x'], self.vel['y']);
|
|
|
+ ]]>
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ <transition port="input" event="left-click" target="../selected" cond="tagorid == id(self)">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ <script>
|
|
|
+ self.canvas.itemconfig(self.id, fill="yellow")
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="dragging">
|
|
|
+ <transition port="input" event="motion" target=".">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ <script>
|
|
|
+ <![CDATA[
|
|
|
+ coords = self.canvas.coords(self.id)
|
|
|
+ dx = self.canvas.canvasx(self.last_x) - self.canvas.canvasx(coords[0])
|
|
|
+ dy = self.canvas.canvasx(self.last_y) - self.canvas.canvasy(coords[1])
|
|
|
+
|
|
|
+ self.canvas.move(self.id, dx, dy);
|
|
|
+
|
|
|
+ # keep ball within boundaries
|
|
|
+ coords = self.canvas.coords(self.id)
|
|
|
+ x = self.canvas.canvasx(coords[0])
|
|
|
+ y = self.canvas.canvasy(coords[1])
|
|
|
+ if x - self.r <= 0:
|
|
|
+ x = 1;
|
|
|
+ elif x + self.r >= self.canvas.winfo_width():
|
|
|
+ x = self.canvas.winfo_width() - (2 * self.r) - 1
|
|
|
+ if y - self.r <= 0:
|
|
|
+ y = 1
|
|
|
+ elif y + self.r >= self.canvas.winfo_height():
|
|
|
+ y = self.canvas.winfo_height() - (2 * self.r) - 1;
|
|
|
+ self.canvas.coords(self.id, x, y, x + (self.r * 2), y + (self.r * 2));
|
|
|
+ self.vel = {
|
|
|
+ 'x': (1 - self.smooth) * dx + self.smooth * self.vel['x'],
|
|
|
+ 'y': (1 - self.smooth) * dy + self.smooth * self.vel['y']
|
|
|
+ }
|
|
|
+ ]]>
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ <transition port="input" event="left-release" target="../bouncing">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ <script>
|
|
|
+ self.canvas.itemconfig(self.id, fill="red")
|
|
|
+ </script>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="selected">
|
|
|
+ <transition port="input" event="left-click" target="../dragging" cond="tagorid == id(self)">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ </transition>
|
|
|
+ <transition port="input" event="delete" target="../deleted">
|
|
|
+ <parameter name="tagorid" type="int" default="None" />
|
|
|
+ <raise event="delete_ball" scope="narrow" target="'parent'">
|
|
|
+ <parameter expr="self.association_name" />
|
|
|
+ </raise>
|
|
|
+ </transition>
|
|
|
+ </state>
|
|
|
+ <state id="deleted" />
|
|
|
+ </scxml>
|
|
|
+ </class>
|
|
|
+</diagram>
|