|
|
@@ -15,6 +15,9 @@ from sccd.runtime.libs import ui_v2 as ui
|
|
|
from sccd.runtime.libs.utils import utils
|
|
|
import random
|
|
|
|
|
|
+CANVAS_WIDTH = 800
|
|
|
+CANVAS_HEIGHT = 550
|
|
|
+
|
|
|
# package "Bouncing_Balls_Python_Version"
|
|
|
|
|
|
class MainAppInstance(RuntimeClassBase):
|
|
|
@@ -220,10 +223,10 @@ class MainApp(AtomicDEVS, ObjectManagerBase):
|
|
|
if isinstance(input, str):
|
|
|
tem = eval(input)
|
|
|
self.addInput(tem)
|
|
|
- if input[3].name == "create_instance":
|
|
|
+ elif input[3].name == "create_instance":
|
|
|
self.instances.add(MainAppInstance(self))
|
|
|
ev = Event("instance_created", None, parameters=[f"{input[3].parameters[0]}[{len(self.instances)-1}]"])
|
|
|
- self.to_send.append((self.name, input[1], input[2], ev))
|
|
|
+ self.to_send.append((input[1], input[0], input[2], ev))
|
|
|
elif input[3].name == "start_instance":
|
|
|
instance = list(self.instances)[input[2]]
|
|
|
instance.start()
|
|
|
@@ -267,8 +270,7 @@ class MainApp(AtomicDEVS, ObjectManagerBase):
|
|
|
self.handleInput()
|
|
|
self.stepAll()
|
|
|
next_earliest = min(self.getEarliestEventTime(), self.input_queue.getEarliestTime())
|
|
|
-
|
|
|
- if len(self.to_send) != 0:
|
|
|
+ if not (len(self.to_send) == 0):
|
|
|
self.next_time = 0
|
|
|
elif next_earliest == INFINITY:
|
|
|
self.next_time = INFINITY
|
|
|
@@ -323,19 +325,9 @@ class FieldInstance(RuntimeClassBase):
|
|
|
FieldInstance.user_defined_constructor(self)
|
|
|
|
|
|
def user_defined_constructor(self):
|
|
|
- # All of these TkInter calls have been converted to in/out-events:
|
|
|
- #self.field_window = ui.new_window(800,600,"BouncingBalls");
|
|
|
- #self.canvas = ui.append_canvas(self.field_window,800,550,{'background':'#eee'});
|
|
|
- #ui.bind_event(self.field_window, ui.EVENTS.WINDOW_CLOSE, self.controller, 'window_close', self.inports['field_ui']);
|
|
|
- #ui.bind_event(self.field_window, ui.EVENTS.KEY_PRESS, self.controller, 'key_press', self.inports['field_ui']);
|
|
|
- #ui.bind_event(self.canvas.element, ui.EVENTS.MOUSE_RIGHT_CLICK, self.controller, 'right_click', self.inports['field_ui']);
|
|
|
- #ui.bind_event(self.canvas.element, ui.EVENTS.MOUSE_MOVE, self.controller, 'mouse_move', self.inports['field_ui']);
|
|
|
- #ui.bind_event(self.canvas.element, ui.EVENTS.MOUSE_RELEASE, self.controller, 'mouse_release', self.inports['field_ui']);
|
|
|
- print("created field")
|
|
|
pass
|
|
|
|
|
|
def user_defined_destructor(self):
|
|
|
- # ui.close_window(self.field_window);
|
|
|
pass
|
|
|
|
|
|
|
|
|
@@ -493,10 +485,10 @@ class FieldInstance(RuntimeClassBase):
|
|
|
self.states["/root/running"].addTransition(_root_running_0)
|
|
|
|
|
|
def _root_creating_window_enter(self):
|
|
|
- self.big_step.outputEvent(Event("create_window", self.getOutPortName("ui"), [800, 600, "BouncingBalls", self.inports['field_ui']]))
|
|
|
+ self.big_step.outputEvent(Event("create_window", self.getOutPortName("ui"), [800, 600, "BouncingBalls", 'field_ui']))
|
|
|
|
|
|
def _root_creating_canvas_enter(self):
|
|
|
- self.big_step.outputEvent(Event("create_canvas", self.getOutPortName("ui"), [self.window_id, 800, 550, {'background':'#eee'}, self.inports['field_ui']]))
|
|
|
+ self.big_step.outputEvent(Event("create_canvas", self.getOutPortName("ui"), [self.window_id, CANVAS_WIDTH, CANVAS_HEIGHT, {'background':'#eee'}, 'field_ui']))
|
|
|
|
|
|
def _root_creating_button_enter(self):
|
|
|
self.big_step.outputEventOM(Event("create_instance", None, [self, "buttons", "Button", self.window_id, 'create_new_field', 'Spawn New Window']))
|
|
|
@@ -512,15 +504,15 @@ class FieldInstance(RuntimeClassBase):
|
|
|
def _root_creating_window_0_exec(self, parameters):
|
|
|
window_id = parameters[0]
|
|
|
self.window_id = window_id
|
|
|
- self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [window_id, ui.EVENTS.WINDOW_CLOSE, 'window_close', self.inports['field_ui']]))
|
|
|
- self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [window_id, ui.EVENTS.KEY_PRESS, 'key_press', self.inports['field_ui']]))
|
|
|
+ self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [window_id, ui.EVENTS.WINDOW_CLOSE, 'window_close', 'field_ui']))
|
|
|
+ self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [window_id, ui.EVENTS.KEY_PRESS, 'key_press', 'field_ui']))
|
|
|
|
|
|
def _root_creating_canvas_0_exec(self, parameters):
|
|
|
canvas_id = parameters[0]
|
|
|
self.canvas_id = canvas_id
|
|
|
- self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_RIGHT_CLICK, 'right_click', self.inports['field_ui']]))
|
|
|
- self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_MOVE, 'mouse_move', self.inports['field_ui']]))
|
|
|
- self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_RELEASE, 'mouse_release', self.inports['field_ui']]))
|
|
|
+ self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_RIGHT_CLICK, 'right_click', 'field_ui']))
|
|
|
+ self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_MOVE, 'mouse_move', 'field_ui']))
|
|
|
+ self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [canvas_id, ui.EVENTS.MOUSE_RELEASE, 'mouse_release', 'field_ui']))
|
|
|
|
|
|
def _root_creating_button_0_exec(self, parameters):
|
|
|
association_name = parameters[0]
|
|
|
@@ -530,7 +522,7 @@ class FieldInstance(RuntimeClassBase):
|
|
|
x = parameters[0]
|
|
|
y = parameters[1]
|
|
|
button = parameters[2]
|
|
|
- self.big_step.outputEventOM(Event("create_instance", None, [self, "balls", "Ball", self.canvas, x, y]))
|
|
|
+ self.big_step.outputEventOM(Event("create_instance", None, [self, "balls", "Ball", self.canvas_id, x, y]))
|
|
|
|
|
|
def _root_running_main_behaviour_creating_ball_0_exec(self, parameters):
|
|
|
association_name = parameters[0]
|
|
|
@@ -576,6 +568,9 @@ class Field(AtomicDEVS, ObjectManagerBase):
|
|
|
self.field_ui = self.addInPort("field_ui")
|
|
|
self.obj_manager_in = self.addInPort("obj_manager_in")
|
|
|
self.input = self.addInPort("input")
|
|
|
+
|
|
|
+ self.inports["field_ui"] = self.addInputPort("field_ui", self)
|
|
|
+
|
|
|
self.next_time = INFINITY
|
|
|
|
|
|
def extTransition(self, inputs):
|
|
|
@@ -591,7 +586,7 @@ class Field(AtomicDEVS, ObjectManagerBase):
|
|
|
if isinstance(input, str):
|
|
|
tem = eval(input)
|
|
|
self.addInput(tem)
|
|
|
- if input[3].name == "create_instance":
|
|
|
+ elif input[3].name == "create_instance":
|
|
|
self.instances.add(FieldInstance(self))
|
|
|
ev = Event("instance_created", None, parameters=[f"{input[3].parameters[0]}[{len(self.instances)-1}]"])
|
|
|
self.to_send.append((input[1], input[0], input[2], ev))
|
|
|
@@ -610,9 +605,12 @@ class Field(AtomicDEVS, ObjectManagerBase):
|
|
|
ev = Event("instance_disassociated", None, parameters=[TODO])
|
|
|
self.to_send.append((self.name, TODO, TODO, ev))
|
|
|
elif input[3].name == "instance_created":
|
|
|
+ association = self.processAssociationReference(input[3].parameters[0])[0]
|
|
|
instance = list(self.instances)[input[2]]
|
|
|
+ p = instance.associations.get(association[0])
|
|
|
+ if p:
|
|
|
+ p.addInstance(input[3].parameters[0])
|
|
|
instance.addEvent(input[3])
|
|
|
- #instance.associations['fields'].instances[0] = input[3].parameters[0]
|
|
|
elif input[3].name == "instance_started":
|
|
|
instance = list(self.instances)[input[2]]
|
|
|
instance.addEvent(input[3])
|
|
|
@@ -628,6 +626,9 @@ class Field(AtomicDEVS, ObjectManagerBase):
|
|
|
elif input[3].name == "set_association_name":
|
|
|
ev = input[3]
|
|
|
self.addInput(ev, force_internal=True)
|
|
|
+ else:
|
|
|
+ ev = input[3]
|
|
|
+ self.addInput(ev)
|
|
|
return self.instances
|
|
|
|
|
|
def intTransition(self):
|
|
|
@@ -638,7 +639,10 @@ class Field(AtomicDEVS, ObjectManagerBase):
|
|
|
self.handleInput()
|
|
|
self.stepAll()
|
|
|
next_earliest = min(self.getEarliestEventTime(), self.input_queue.getEarliestTime())
|
|
|
- if next_earliest == INFINITY:
|
|
|
+
|
|
|
+ if not (len(self.to_send) == 0):
|
|
|
+ self.next_time = 0
|
|
|
+ elif next_earliest == INFINITY:
|
|
|
self.next_time = INFINITY
|
|
|
else:
|
|
|
self.next_time = next_earliest - earliest
|
|
|
@@ -692,10 +696,6 @@ class ButtonInstance(RuntimeClassBase):
|
|
|
def user_defined_constructor(self, window_id, event_name, button_text):
|
|
|
self.window_id = window_id;
|
|
|
self.event_name = event_name;
|
|
|
-
|
|
|
- # Translated to events:
|
|
|
- #button = ui.append_button(tkparent, event_name);
|
|
|
- #ui.bind_event(button.element, ui.EVENTS.MOUSE_CLICK, self.controller, 'mouse_click', self.inports['button_ui']);
|
|
|
|
|
|
def user_defined_destructor(self):
|
|
|
pass
|
|
|
@@ -734,12 +734,12 @@ class ButtonInstance(RuntimeClassBase):
|
|
|
self.states["/running"].addTransition(_running_0)
|
|
|
|
|
|
def _creating_button_enter(self):
|
|
|
- self.big_step.outputEvent(Event("create_button", self.getOutPortName("ui"), [self.window_id, self.event_name, self.inports['button_ui']]))
|
|
|
+ self.big_step.outputEvent(Event("create_button", self.getOutPortName("ui"), [self.window_id, self.event_name, 'button_ui']))
|
|
|
|
|
|
def _creating_button_0_exec(self, parameters):
|
|
|
button_id = parameters[0]
|
|
|
self.button_id = button_id
|
|
|
- self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [button_id, ui.EVENTS.MOUSE_CLICK, "mouse_click", self.inports['button_ui']]))
|
|
|
+ self.big_step.outputEvent(Event("bind_event", self.getOutPortName("ui"), [button_id, ui.EVENTS.MOUSE_CLICK, "mouse_click", 'button_ui']))
|
|
|
|
|
|
def _running_0_exec(self, parameters):
|
|
|
x = parameters[0]
|
|
|
@@ -785,18 +785,61 @@ class Button(AtomicDEVS, ObjectManagerBase):
|
|
|
if isinstance(input, str):
|
|
|
tem = eval(input)
|
|
|
self.addInput(tem)
|
|
|
- if input[3].name == "create_instance":
|
|
|
- self.instances.add(ButtonInstance(self))
|
|
|
+ elif input[3].name == "create_instance":
|
|
|
+ '''
|
|
|
+ if len(parameters) < 2:
|
|
|
+ raise ParameterException ("The create event needs at least 2 parameters.")
|
|
|
+
|
|
|
+ source = parameters[0]
|
|
|
+ association_name = parameters[1]
|
|
|
+
|
|
|
+ traversal_list = self.processAssociationReference(association_name)
|
|
|
+ instances = self.getInstances(source, traversal_list)
|
|
|
+
|
|
|
+ association = source.associations[association_name]
|
|
|
+ # association = self.instances_map[source].getAssociation(association_name)
|
|
|
+ if association.allowedToAdd():
|
|
|
+ class_name = association.to_class if len(parameters) == 2 else parameters[2]
|
|
|
+ new_instance = self.createInstance(class_name, parameters[3:])
|
|
|
+ if not new_instance:
|
|
|
+ raise ParameterException("Creating instance: no such class: " + class_name)
|
|
|
+ # index = association.addInstance(new_instance)
|
|
|
+ try:
|
|
|
+ index = association.addInstance(new_instance)
|
|
|
+ except AssociationException as exception:
|
|
|
+ raise RuntimeException("Error adding instance to association '" + association_name + "': " + str(exception))
|
|
|
+ p = new_instance.associations.get("parent")
|
|
|
+ if p:
|
|
|
+ p.addInstance(source)
|
|
|
+ source.addEvent(Event("instance_created", None, [association_name+"["+str(index)+"]"]))
|
|
|
+ return [source, association_name+"["+str(index)+"]"]
|
|
|
+ else:
|
|
|
+ source.addEvent(Event("instance_creation_error", None, [association_name]))
|
|
|
+ return []
|
|
|
+ '''
|
|
|
+
|
|
|
+ new_instance = ButtonInstance(self, input[3].parameters[1], input[3].parameters[2], input[3].parameters[3])
|
|
|
+ self.instances.add(new_instance)
|
|
|
+
|
|
|
+ #p = new_instance.associations.get("parent")
|
|
|
+ #if p:
|
|
|
+ # p.addInstance(f"{input[3].parameters[0]}[{input[3].parameters[1]}]")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
ev = Event("instance_created", None, parameters=[f"{input[3].parameters[0]}[{len(self.instances)-1}]"])
|
|
|
- self.to_send.append((self.name, input[1], input[2], ev))
|
|
|
+ self.to_send.append((input[1], input[0], input[2], ev))
|
|
|
elif input[3].name == "start_instance":
|
|
|
instance = list(self.instances)[input[2]]
|
|
|
instance.start()
|
|
|
ev = Event("instance_started", None, parameters=[f"{input[0]}[{input[2]}]"])
|
|
|
self.to_send.append((input[0], input[1], input[2], ev))
|
|
|
elif input[3].name == "delete_instance":
|
|
|
- ev = Event("instance_deleted", None, parameters=[TODO])
|
|
|
- self.to_send.append((self.name, TODO, TODO, ev))
|
|
|
+ instance = list(self.instances)[input[2]]
|
|
|
+ instance.user_defined_destructor()
|
|
|
+ instance.stop()
|
|
|
+ ev = Event("instance_deleted", None, parameters=[self, input[0]])
|
|
|
+ self.to_send.append((self.name, input[1], input[2], ev))
|
|
|
elif input[3].name == "associate_instance":
|
|
|
ev = Event("instance_associated", None, parameters=[TODO])
|
|
|
self.to_send.append((self.name, TODO, TODO, ev))
|
|
|
@@ -832,7 +875,9 @@ class Button(AtomicDEVS, ObjectManagerBase):
|
|
|
self.handleInput()
|
|
|
self.stepAll()
|
|
|
next_earliest = min(self.getEarliestEventTime(), self.input_queue.getEarliestTime())
|
|
|
- if next_earliest == INFINITY:
|
|
|
+ if not (len(self.to_send) == 0):
|
|
|
+ self.next_time = 0
|
|
|
+ elif next_earliest == INFINITY:
|
|
|
self.next_time = INFINITY
|
|
|
else:
|
|
|
self.next_time = next_earliest - earliest
|
|
|
@@ -861,7 +906,7 @@ class Button(AtomicDEVS, ObjectManagerBase):
|
|
|
return self.next_time
|
|
|
|
|
|
class BallInstance(RuntimeClassBase):
|
|
|
- def __init__(self, atomdevs, canvas, x, y):
|
|
|
+ def __init__(self, atomdevs, canvas_id, x, y):
|
|
|
RuntimeClassBase.__init__(self, atomdevs)
|
|
|
self.associations = {}
|
|
|
self.associations["parent"] = Association("Field", 1, 1)
|
|
|
@@ -876,17 +921,18 @@ class BallInstance(RuntimeClassBase):
|
|
|
self.build_statechart_structure()
|
|
|
|
|
|
# user defined attributes
|
|
|
- self.canvas = None
|
|
|
+ self.canvas_id = None
|
|
|
+ self.pos = None
|
|
|
|
|
|
# call user defined constructor
|
|
|
- BallInstance.user_defined_constructor(self, canvas, x, y)
|
|
|
+ BallInstance.user_defined_constructor(self, canvas_id, x, y)
|
|
|
|
|
|
- def user_defined_constructor(self, canvas, x, y):
|
|
|
- self.canvas = canvas;
|
|
|
+ def user_defined_constructor(self, canvas_id, x, y):
|
|
|
+ self.canvas_id = canvas_id;
|
|
|
self.r = 20.0;
|
|
|
self.vel = {'x': random.uniform(-5.0, 5.0), 'y': random.uniform(-5.0, 5.0)};
|
|
|
- self.mouse_pos = {};
|
|
|
- self.smooth = 0.4; # value between 0 and 1
|
|
|
+ self.pos = {'x': x, 'y': y};
|
|
|
+ self.smooth = 0.6; # value between 0 and 1
|
|
|
|
|
|
# TODO:
|
|
|
#circle = self.canvas.add_circle(x, y, self.r, {'fill':'#000'});
|
|
|
@@ -896,7 +942,8 @@ class BallInstance(RuntimeClassBase):
|
|
|
#self.element = circle;
|
|
|
|
|
|
def user_defined_destructor(self):
|
|
|
- self.canvas.remove_element(self.element);
|
|
|
+ #self.canvas.remove_element(self.element);
|
|
|
+ pass
|
|
|
|
|
|
|
|
|
# builds Statechart structure
|
|
|
@@ -911,24 +958,29 @@ class BallInstance(RuntimeClassBase):
|
|
|
# state /main_behaviour/initializing
|
|
|
self.states["/main_behaviour/initializing"] = State(2, "/main_behaviour/initializing", self)
|
|
|
|
|
|
+ # state /main_behaviour/creating_circle
|
|
|
+ self.states["/main_behaviour/creating_circle"] = State(3, "/main_behaviour/creating_circle", self)
|
|
|
+ self.states["/main_behaviour/creating_circle"].setEnter(self._main_behaviour_creating_circle_enter)
|
|
|
+
|
|
|
# state /main_behaviour/bouncing
|
|
|
- self.states["/main_behaviour/bouncing"] = State(3, "/main_behaviour/bouncing", self)
|
|
|
+ self.states["/main_behaviour/bouncing"] = State(4, "/main_behaviour/bouncing", self)
|
|
|
self.states["/main_behaviour/bouncing"].setEnter(self._main_behaviour_bouncing_enter)
|
|
|
self.states["/main_behaviour/bouncing"].setExit(self._main_behaviour_bouncing_exit)
|
|
|
|
|
|
# state /main_behaviour/dragging
|
|
|
- self.states["/main_behaviour/dragging"] = State(4, "/main_behaviour/dragging", self)
|
|
|
+ self.states["/main_behaviour/dragging"] = State(5, "/main_behaviour/dragging", self)
|
|
|
|
|
|
# state /main_behaviour/selected
|
|
|
- self.states["/main_behaviour/selected"] = State(5, "/main_behaviour/selected", self)
|
|
|
+ self.states["/main_behaviour/selected"] = State(6, "/main_behaviour/selected", self)
|
|
|
|
|
|
# state /deleted
|
|
|
- self.states["/deleted"] = State(6, "/deleted", self)
|
|
|
+ self.states["/deleted"] = State(7, "/deleted", self)
|
|
|
|
|
|
# add children
|
|
|
self.states[""].addChild(self.states["/main_behaviour"])
|
|
|
self.states[""].addChild(self.states["/deleted"])
|
|
|
self.states["/main_behaviour"].addChild(self.states["/main_behaviour/initializing"])
|
|
|
+ self.states["/main_behaviour"].addChild(self.states["/main_behaviour/creating_circle"])
|
|
|
self.states["/main_behaviour"].addChild(self.states["/main_behaviour/bouncing"])
|
|
|
self.states["/main_behaviour"].addChild(self.states["/main_behaviour/dragging"])
|
|
|
self.states["/main_behaviour"].addChild(self.states["/main_behaviour/selected"])
|
|
|
@@ -937,11 +989,17 @@ class BallInstance(RuntimeClassBase):
|
|
|
self.states["/main_behaviour"].default_state = self.states["/main_behaviour/initializing"]
|
|
|
|
|
|
# transition /main_behaviour/initializing
|
|
|
- _main_behaviour_initializing_0 = Transition(self, self.states["/main_behaviour/initializing"], [self.states["/main_behaviour/bouncing"]])
|
|
|
+ _main_behaviour_initializing_0 = Transition(self, self.states["/main_behaviour/initializing"], [self.states["/main_behaviour/creating_circle"]])
|
|
|
_main_behaviour_initializing_0.setAction(self._main_behaviour_initializing_0_exec)
|
|
|
_main_behaviour_initializing_0.setTrigger(Event("set_association_name", None))
|
|
|
self.states["/main_behaviour/initializing"].addTransition(_main_behaviour_initializing_0)
|
|
|
|
|
|
+ # transition /main_behaviour/creating_circle
|
|
|
+ _main_behaviour_creating_circle_0 = Transition(self, self.states["/main_behaviour/creating_circle"], [self.states["/main_behaviour/bouncing"]])
|
|
|
+ _main_behaviour_creating_circle_0.setAction(self._main_behaviour_creating_circle_0_exec)
|
|
|
+ _main_behaviour_creating_circle_0.setTrigger(Event("circle_created", None))
|
|
|
+ self.states["/main_behaviour/creating_circle"].addTransition(_main_behaviour_creating_circle_0)
|
|
|
+
|
|
|
# transition /main_behaviour/bouncing
|
|
|
_main_behaviour_bouncing_0 = Transition(self, self.states["/main_behaviour/bouncing"], [self.states["/main_behaviour/bouncing"]])
|
|
|
_main_behaviour_bouncing_0.setAction(self._main_behaviour_bouncing_0_exec)
|
|
|
@@ -974,8 +1032,11 @@ class BallInstance(RuntimeClassBase):
|
|
|
_main_behaviour_selected_1.setTrigger(Event("delete_self", None))
|
|
|
self.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_1)
|
|
|
|
|
|
+ def _main_behaviour_creating_circle_enter(self):
|
|
|
+ self.big_step.outputEvent(Event("create_circle", self.getOutPortName("ui"), [self.canvas_id, self.pos['x'], self.pos['y'], self.r, {'fill':'#000'}, 'ball_ui']))
|
|
|
+
|
|
|
def _main_behaviour_bouncing_enter(self):
|
|
|
- self.addTimer(0, (20 - self.getSimulatedTime() % 20) / 1000.0)
|
|
|
+ self.addTimer(0, 0.02)
|
|
|
|
|
|
def _main_behaviour_bouncing_exit(self):
|
|
|
self.removeTimer(0)
|
|
|
@@ -984,19 +1045,29 @@ class BallInstance(RuntimeClassBase):
|
|
|
association_name = parameters[0]
|
|
|
self.association_name = association_name
|
|
|
|
|
|
+ def _main_behaviour_creating_circle_0_exec(self, parameters):
|
|
|
+ canvas_id = parameters[0]
|
|
|
+ circle_id = parameters[1]
|
|
|
+ self.circle_id = circle_id
|
|
|
+ self.big_step.outputEvent(Event("bind_canvas_event", self.getOutPortName("ui"), [self.canvas_id, circle_id, ui.EVENTS.MOUSE_PRESS, 'mouse_press', 'ball_ui']))
|
|
|
+ self.big_step.outputEvent(Event("bind_canvas_event", self.getOutPortName("ui"), [self.canvas_id, circle_id, ui.EVENTS.MOUSE_MOVE, 'mouse_move', 'ball_ui']))
|
|
|
+ self.big_step.outputEvent(Event("bind_canvas_event", self.getOutPortName("ui"), [self.canvas_id, circle_id, ui.EVENTS.MOUSE_RELEASE, 'mouse_release', 'ball_ui']))
|
|
|
+
|
|
|
def _main_behaviour_bouncing_0_exec(self, parameters):
|
|
|
- pos = self.element.get_position();
|
|
|
- if pos.x-self.r <= 0 or pos.x+self.r >= self.canvas.get_width():
|
|
|
+ # Invert velocity when colliding with canvas border:
|
|
|
+ if self.pos['x']-self.r <= 0 or self.pos['x']+self.r >= CANVAS_WIDTH:
|
|
|
self.vel['x'] = -self.vel['x'];
|
|
|
- if pos.y-self.r <= 0 or pos.y+self.r >= self.canvas.get_height():
|
|
|
+ if self.pos['y']-self.r <= 0 or self.pos['y']+self.r >= CANVAS_HEIGHT:
|
|
|
self.vel['y'] = -self.vel['y'];
|
|
|
- self.element.move(self.vel['x'], self.vel['y']);
|
|
|
+ self.big_step.outputEvent(Event("move_element", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, self.vel['x'], self.vel['y']]))
|
|
|
+ self.pos['x'] += self.vel['x']
|
|
|
+ self.pos['y'] += self.vel['y']
|
|
|
|
|
|
def _main_behaviour_bouncing_1_exec(self, parameters):
|
|
|
x = parameters[0]
|
|
|
y = parameters[1]
|
|
|
button = parameters[2]
|
|
|
- self.element.set_color("#ff0");
|
|
|
+ self.big_step.outputEvent(Event("set_element_color", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, '#ff0']))
|
|
|
|
|
|
def _main_behaviour_bouncing_1_guard(self, parameters):
|
|
|
x = parameters[0]
|
|
|
@@ -1008,32 +1079,25 @@ class BallInstance(RuntimeClassBase):
|
|
|
x = parameters[0]
|
|
|
y = parameters[1]
|
|
|
button = parameters[2]
|
|
|
- dx = x - self.mouse_pos['x'];
|
|
|
- dy = y - self.mouse_pos['y'];
|
|
|
-
|
|
|
- self.element.move(dx, dy);
|
|
|
-
|
|
|
- # keep ball within boundaries
|
|
|
- pos = self.element.get_position();
|
|
|
- if pos.x-self.r <= 0 :
|
|
|
- pos.x = self.r + 1;
|
|
|
- elif pos.x+self.r >= self.canvas.width :
|
|
|
- pos.x = self.canvas.width-self.r-1;
|
|
|
- if pos.y-self.r <= 0 :
|
|
|
- pos.y = self.r + 1;
|
|
|
- elif pos.y+self.r >= self.canvas.height :
|
|
|
- pos.y = self.canvas.height-self.r-1;
|
|
|
- self.element.set_position(pos.x, pos.y);
|
|
|
- self.mouse_pos = {'x':x, 'y':y};
|
|
|
+ # Always keep ball within canvas:
|
|
|
+ x = min(max(0+self.r, x), CANVAS_WIDTH-self.r)
|
|
|
+ y = min(max(0+self.r, y), CANVAS_HEIGHT-self.r)
|
|
|
+
|
|
|
+ dx = x - self.pos['x']
|
|
|
+ dy = y - self.pos['y']
|
|
|
+
|
|
|
self.vel = {
|
|
|
'x': (1-self.smooth)*dx + self.smooth*self.vel['x'],
|
|
|
'y': (1-self.smooth)*dy + self.smooth*self.vel['y']
|
|
|
- };
|
|
|
+ }
|
|
|
+
|
|
|
+ self.pos = {'x': x, 'y': y}
|
|
|
+ self.big_step.outputEvent(Event("set_element_pos", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, x-self.r, y-self.r]))
|
|
|
|
|
|
def _main_behaviour_dragging_1_exec(self, parameters):
|
|
|
x = parameters[0]
|
|
|
y = parameters[1]
|
|
|
- self.element.set_color("#f00");
|
|
|
+ self.big_step.outputEvent(Event("set_element_color", self.getOutPortName("ui"), [self.canvas_id, self.circle_id, '#f00']))
|
|
|
|
|
|
def _main_behaviour_selected_0_exec(self, parameters):
|
|
|
x = parameters[0]
|
|
|
@@ -1049,6 +1113,7 @@ class BallInstance(RuntimeClassBase):
|
|
|
|
|
|
def _main_behaviour_selected_1_exec(self, parameters):
|
|
|
self.big_step.outputEventOM(Event("narrow_cast", None, [self, 'parent', Event("delete_ball", None, [self.association_name])]))
|
|
|
+ self.big_step.outputEvent(Event("destroy_element", self.getOutPortName("ui"), [self.canvas_id, self.element_id]))
|
|
|
|
|
|
def initializeStatechart(self):
|
|
|
# enter default state
|
|
|
@@ -1082,18 +1147,19 @@ class Ball(AtomicDEVS, ObjectManagerBase):
|
|
|
if isinstance(input, str):
|
|
|
tem = eval(input)
|
|
|
self.addInput(tem)
|
|
|
- if input[3].name == "create_instance":
|
|
|
- self.instances.add(BallInstance(self))
|
|
|
+ elif input[3].name == "create_instance":
|
|
|
+ self.instances.add(BallInstance(self, input[3].parameters[1], input[3].parameters[2], input[3].parameters[3]))
|
|
|
ev = Event("instance_created", None, parameters=[f"{input[3].parameters[0]}[{len(self.instances)-1}]"])
|
|
|
- self.to_send.append((self.name, input[1], input[2], ev))
|
|
|
+ self.to_send.append((self.name, input[0], input[2], ev))
|
|
|
elif input[3].name == "start_instance":
|
|
|
- instance = list(self.instances)[input[2]]
|
|
|
+ association = self.processAssociationReference(input[3].parameters[0])[0]
|
|
|
+ instance = list(self.instances)[association[1]]
|
|
|
instance.start()
|
|
|
- ev = Event("instance_started", None, parameters=[f"{input[0]}[{input[2]}]"])
|
|
|
- self.to_send.append((input[0], input[1], input[2], ev))
|
|
|
+ ev = Event("instance_started", None, parameters=[input[3].parameters[0]])
|
|
|
+ self.to_send.append((input[0], input[1], association[1], ev))
|
|
|
elif input[3].name == "delete_instance":
|
|
|
- ev = Event("instance_deleted", None, parameters=[TODO])
|
|
|
- self.to_send.append((self.name, TODO, TODO, ev))
|
|
|
+ ev = Event("instance_deleted", None, parameters=[])
|
|
|
+ self.to_send.append((self.name, input[0], input[2], ev))
|
|
|
elif input[3].name == "associate_instance":
|
|
|
ev = Event("instance_associated", None, parameters=[TODO])
|
|
|
self.to_send.append((self.name, TODO, TODO, ev))
|
|
|
@@ -1101,11 +1167,13 @@ class Ball(AtomicDEVS, ObjectManagerBase):
|
|
|
ev = Event("instance_disassociated", None, parameters=[TODO])
|
|
|
self.to_send.append((self.name, TODO, TODO, ev))
|
|
|
elif input[3].name == "instance_created":
|
|
|
- instance = list(self.instances)[input[2]]
|
|
|
+ association = self.processAssociationReference(input[3].parameters[0])[0]
|
|
|
+ instance = list(self.instances)[association[1]]
|
|
|
instance.addEvent(input[3])
|
|
|
instance.associations['fields'].instances[0] = input[3].parameters[0]
|
|
|
elif input[3].name == "instance_started":
|
|
|
- instance = list(self.instances)[input[2]]
|
|
|
+ association = self.processAssociationReference(input[3].parameters[0])[0]
|
|
|
+ instance = list(self.instances)[association[1]]
|
|
|
instance.addEvent(input[3])
|
|
|
elif input[3].name == "instance_deleted":
|
|
|
instance = list(self.instances)[input[2]]
|
|
|
@@ -1129,7 +1197,9 @@ class Ball(AtomicDEVS, ObjectManagerBase):
|
|
|
self.handleInput()
|
|
|
self.stepAll()
|
|
|
next_earliest = min(self.getEarliestEventTime(), self.input_queue.getEarliestTime())
|
|
|
- if next_earliest == INFINITY:
|
|
|
+ if not (len(self.to_send) == 0):
|
|
|
+ self.next_time = 0
|
|
|
+ elif next_earliest == INFINITY:
|
|
|
self.next_time = INFINITY
|
|
|
else:
|
|
|
self.next_time = next_earliest - earliest
|