/* Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)

Date:   Wed Aug 03 16:58:10 2016

Model author: Joeri Exelmans+Raphael Mannadiar+Simon Van Mierlo
Model name:   Bouncing_Balls_JavaScript_Version
Model description:
SVG canvas with bouncing balls in it.*/


// package "Bouncing_Balls_JavaScript_Version"
var Bouncing_Balls_JavaScript_Version = {};
(function() {

var MainApp = function(controller) {
    RuntimeClassBase.call(this, controller);
    
    this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
    this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
    this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
    this.semantics.priority = StatechartSemantics.SourceParent;
    this.semantics.concurrency = StatechartSemantics.Single;
    
    // build Statechart structure
    this.build_statechart_structure();
    
    // call user defined constructor
    MainApp.prototype.user_defined_constructor.call(this);
};
MainApp.prototype = new Object();
(function() {
    var proto = new RuntimeClassBase();
    for (prop in proto) {
        MainApp.prototype[prop] = proto[prop];
    }
})();

MainApp.prototype.user_defined_constructor = function() {
    this.nr_of_fields = 0;
};

MainApp.prototype.user_defined_destructor = function() {
};


// builds Statechart structure
MainApp.prototype.build_statechart_structure = function() {
    
    // state <root>
    this.states[""] = new State(0, this);
    
    // state /running
    this.states["/running"] = new State(1, this);
    
    // state /running/root
    this.states["/running/root"] = new ParallelState(2, this);
    
    // state /running/root/main_behaviour
    this.states["/running/root/main_behaviour"] = new State(3, this);
    
    // state /running/root/main_behaviour/initializing
    this.states["/running/root/main_behaviour/initializing"] = new State(4, this);
    
    // state /running/root/main_behaviour/running
    this.states["/running/root/main_behaviour/running"] = new State(5, this);
    
    // state /running/root/cd_behaviour
    this.states["/running/root/cd_behaviour"] = new State(6, this);
    
    // state /running/root/cd_behaviour/waiting
    this.states["/running/root/cd_behaviour/waiting"] = new State(7, this);
    
    // state /running/root/cd_behaviour/creating
    this.states["/running/root/cd_behaviour/creating"] = new State(8, this);
    
    // state /running/root/cd_behaviour/check_nr_of_fields
    this.states["/running/root/cd_behaviour/check_nr_of_fields"] = new State(9, this);
    
    // state /running/stopped
    this.states["/running/stopped"] = new State(10, this);
    
    // add children
    this.states[""].addChild(this.states["/running"]);
    this.states["/running"].addChild(this.states["/running/root"]);
    this.states["/running"].addChild(this.states["/running/stopped"]);
    this.states["/running/root"].addChild(this.states["/running/root/main_behaviour"]);
    this.states["/running/root"].addChild(this.states["/running/root/cd_behaviour"]);
    this.states["/running/root/main_behaviour"].addChild(this.states["/running/root/main_behaviour/initializing"]);
    this.states["/running/root/main_behaviour"].addChild(this.states["/running/root/main_behaviour/running"]);
    this.states["/running/root/cd_behaviour"].addChild(this.states["/running/root/cd_behaviour/waiting"]);
    this.states["/running/root/cd_behaviour"].addChild(this.states["/running/root/cd_behaviour/creating"]);
    this.states["/running/root/cd_behaviour"].addChild(this.states["/running/root/cd_behaviour/check_nr_of_fields"]);
    this.states[""].fixTree();
    this.states[""].default_state = this.states["/running"];
    this.states["/running"].default_state = this.states["/running/root"];
    this.states["/running/root/main_behaviour"].default_state = this.states["/running/root/main_behaviour/initializing"];
    this.states["/running/root/cd_behaviour"].default_state = this.states["/running/root/cd_behaviour/waiting"];
    
    // transition /running/root/main_behaviour/initializing
    var _running_root_main_behaviour_initializing_0 = new Transition(this, this.states["/running/root/main_behaviour/initializing"], [this.states["/running/root/main_behaviour/running"]]);
    _running_root_main_behaviour_initializing_0.setAction(this._running_root_main_behaviour_initializing_0_exec);
    this.states["/running/root/main_behaviour/initializing"].addTransition(_running_root_main_behaviour_initializing_0);
    
    // transition /running/root/main_behaviour/running
    var _running_root_main_behaviour_running_0 = new Transition(this, this.states["/running/root/main_behaviour/running"], [this.states["/running/root/main_behaviour/running"]]);
    _running_root_main_behaviour_running_0.setAction(this._running_root_main_behaviour_running_0_exec);
    _running_root_main_behaviour_running_0.trigger = new Event("button_pressed", null);
    _running_root_main_behaviour_running_0.setGuard(this._running_root_main_behaviour_running_0_guard);
    this.states["/running/root/main_behaviour/running"].addTransition(_running_root_main_behaviour_running_0);
    
    // transition /running/root/cd_behaviour/waiting
    var _running_root_cd_behaviour_waiting_0 = new Transition(this, this.states["/running/root/cd_behaviour/waiting"], [this.states["/running/root/cd_behaviour/creating"]]);
    _running_root_cd_behaviour_waiting_0.setAction(this._running_root_cd_behaviour_waiting_0_exec);
    _running_root_cd_behaviour_waiting_0.trigger = new Event("create_field", null);
    this.states["/running/root/cd_behaviour/waiting"].addTransition(_running_root_cd_behaviour_waiting_0);
    var _running_root_cd_behaviour_waiting_1 = new Transition(this, this.states["/running/root/cd_behaviour/waiting"], [this.states["/running/root/cd_behaviour/check_nr_of_fields"]]);
    _running_root_cd_behaviour_waiting_1.setAction(this._running_root_cd_behaviour_waiting_1_exec);
    _running_root_cd_behaviour_waiting_1.trigger = new Event("delete_field", null);
    this.states["/running/root/cd_behaviour/waiting"].addTransition(_running_root_cd_behaviour_waiting_1);
    
    // transition /running/root/cd_behaviour/creating
    var _running_root_cd_behaviour_creating_0 = new Transition(this, this.states["/running/root/cd_behaviour/creating"], [this.states["/running/root/cd_behaviour/waiting"]]);
    _running_root_cd_behaviour_creating_0.setAction(this._running_root_cd_behaviour_creating_0_exec);
    _running_root_cd_behaviour_creating_0.trigger = new Event("instance_created", null);
    this.states["/running/root/cd_behaviour/creating"].addTransition(_running_root_cd_behaviour_creating_0);
    
    // transition /running/root/cd_behaviour/check_nr_of_fields
    var _running_root_cd_behaviour_check_nr_of_fields_0 = new Transition(this, this.states["/running/root/cd_behaviour/check_nr_of_fields"], [this.states["/running/stopped"]]);
    _running_root_cd_behaviour_check_nr_of_fields_0.setAction(this._running_root_cd_behaviour_check_nr_of_fields_0_exec);
    _running_root_cd_behaviour_check_nr_of_fields_0.setGuard(this._running_root_cd_behaviour_check_nr_of_fields_0_guard);
    this.states["/running/root/cd_behaviour/check_nr_of_fields"].addTransition(_running_root_cd_behaviour_check_nr_of_fields_0);
    var _running_root_cd_behaviour_check_nr_of_fields_1 = new Transition(this, this.states["/running/root/cd_behaviour/check_nr_of_fields"], [this.states["/running/root/cd_behaviour/waiting"]]);
    _running_root_cd_behaviour_check_nr_of_fields_1.setGuard(this._running_root_cd_behaviour_check_nr_of_fields_1_guard);
    this.states["/running/root/cd_behaviour/check_nr_of_fields"].addTransition(_running_root_cd_behaviour_check_nr_of_fields_1);
};

MainApp.prototype._running_root_main_behaviour_initializing_0_exec = function(parameters) {
    this.raiseInternalEvent(new Event("create_field", null, new Array()));
};

MainApp.prototype._running_root_main_behaviour_running_0_exec = function(parameters) {
    var event_name = parameters[0];
    this.raiseInternalEvent(new Event("create_field", null, new Array()));
};

MainApp.prototype._running_root_main_behaviour_running_0_guard = function(parameters) {
    var event_name = parameters[0];
    return event_name == "create_new_field";
};

MainApp.prototype._running_root_cd_behaviour_waiting_0_exec = function(parameters) {
    this.big_step.outputEventOM(new Event("create_instance", null, [this, "fields"]));
};

MainApp.prototype._running_root_cd_behaviour_waiting_1_exec = function(parameters) {
    var association_name = parameters[0];
    this.big_step.outputEventOM(new Event("delete_instance", null, [this, association_name]));
    this.nr_of_fields -= 1
};

MainApp.prototype._running_root_cd_behaviour_creating_0_exec = function(parameters) {
    var association_name = parameters[0];
    this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, association_name, new Event("set_association_name", null, [association_name])]));
    this.nr_of_fields += 1
};

MainApp.prototype._running_root_cd_behaviour_check_nr_of_fields_0_exec = function(parameters) {
    //NOTE: ideally, this should be ui.close_window(ui.window). we can't do this though 
    //		  due to a security measure preventing scripts from closing pages they didn't
    //		  open themselves
    ui.println('refresh to play again');
};

MainApp.prototype._running_root_cd_behaviour_check_nr_of_fields_0_guard = function(parameters) {
    return this.nr_of_fields == 0;
};

MainApp.prototype._running_root_cd_behaviour_check_nr_of_fields_1_guard = function(parameters) {
    return this.nr_of_fields != 0;
};

MainApp.prototype.initializeStatechart = function() {
    // enter default state
    var states = this.states["/running"].getEffectiveTargetStates();
    this.updateConfiguration(states);
    for (var state_idx in states) {
        if (!states.hasOwnProperty(state_idx)) continue;
        var state = states[state_idx]
        if (state.enter) {
            state.enter();
        }
    }
};

// add symbol 'MainApp' to package 'Bouncing_Balls_JavaScript_Version'
Bouncing_Balls_JavaScript_Version.MainApp = MainApp;

var Field = function(controller) {
    RuntimeClassBase.call(this, controller);
    
    this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
    this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
    this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
    this.semantics.priority = StatechartSemantics.SourceParent;
    this.semantics.concurrency = StatechartSemantics.Single;
    
    // build Statechart structure
    this.build_statechart_structure();
    this.inports["field_ui"] = controller.addInputPort("field_ui", this);
    
    // user defined attributes
    this.canvas = null;
    this.field_window = null;
    
    // call user defined constructor
    Field.prototype.user_defined_constructor.call(this);
};
Field.prototype = new Object();
(function() {
    var proto = new RuntimeClassBase();
    for (prop in proto) {
        Field.prototype[prop] = proto[prop];
    }
})();

Field.prototype.user_defined_constructor = function() {
    this.field_window = ui.new_window(400,450);
    this.canvas	= ui.append_canvas(this.field_window,400,400,{'background':'#eee'});
    ui.bind_event(this.field_window, ui.EVENTS.WINDOW_CLOSE, this.controller, 'window_close', this.inports['field_ui']);
    ui.bind_event(this.field_window, ui.EVENTS.KEY_PRESS, this.controller, 'key_press', this.inports['field_ui']);
    ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_RIGHT_CLICK, this.controller, 'right_click', this.inports['field_ui']);
    ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_MOVE, this.controller, 'mouse_move');
    ui.bind_event(this.canvas.element, ui.EVENTS.MOUSE_RELEASE, this.controller, 'mouse_release');
};

Field.prototype.user_defined_destructor = function() {
    ui.close_window(this.field_window);
};


// builds Statechart structure
Field.prototype.build_statechart_structure = function() {
    
    // state <root>
    this.states[""] = new State(0, this);
    
    // state /root
    this.states["/root"] = new State(1, this);
    
    // state /root/waiting
    this.states["/root/waiting"] = new State(2, this);
    
    // state /root/initializing
    this.states["/root/initializing"] = new State(3, this);
    
    // state /root/creating
    this.states["/root/creating"] = new State(4, this);
    
    // state /root/packing
    this.states["/root/packing"] = new State(5, this);
    
    // state /root/running
    this.states["/root/running"] = new ParallelState(6, this);
    
    // state /root/running/main_behaviour
    this.states["/root/running/main_behaviour"] = new State(7, this);
    
    // state /root/running/main_behaviour/running
    this.states["/root/running/main_behaviour/running"] = new State(8, this);
    
    // state /root/running/main_behaviour/creating
    this.states["/root/running/main_behaviour/creating"] = new State(9, this);
    
    // state /root/running/deleting_behaviour
    this.states["/root/running/deleting_behaviour"] = new State(10, this);
    
    // state /root/running/deleting_behaviour/running
    this.states["/root/running/deleting_behaviour/running"] = new State(11, this);
    
    // state /root/running/child_behaviour
    this.states["/root/running/child_behaviour"] = new State(12, this);
    
    // state /root/running/child_behaviour/listening
    this.states["/root/running/child_behaviour/listening"] = new State(13, this);
    
    // state /root/running/deleting_balls_behaviour
    this.states["/root/running/deleting_balls_behaviour"] = new State(14, this);
    
    // state /root/running/deleting_balls_behaviour/listening
    this.states["/root/running/deleting_balls_behaviour/listening"] = new State(15, this);
    
    // state /root/deleting
    this.states["/root/deleting"] = new State(16, this);
    
    // state /root/deleted
    this.states["/root/deleted"] = new State(17, this);
    
    // add children
    this.states[""].addChild(this.states["/root"]);
    this.states["/root"].addChild(this.states["/root/waiting"]);
    this.states["/root"].addChild(this.states["/root/initializing"]);
    this.states["/root"].addChild(this.states["/root/creating"]);
    this.states["/root"].addChild(this.states["/root/packing"]);
    this.states["/root"].addChild(this.states["/root/running"]);
    this.states["/root"].addChild(this.states["/root/deleting"]);
    this.states["/root"].addChild(this.states["/root/deleted"]);
    this.states["/root/running"].addChild(this.states["/root/running/main_behaviour"]);
    this.states["/root/running"].addChild(this.states["/root/running/deleting_behaviour"]);
    this.states["/root/running"].addChild(this.states["/root/running/child_behaviour"]);
    this.states["/root/running"].addChild(this.states["/root/running/deleting_balls_behaviour"]);
    this.states["/root/running/main_behaviour"].addChild(this.states["/root/running/main_behaviour/running"]);
    this.states["/root/running/main_behaviour"].addChild(this.states["/root/running/main_behaviour/creating"]);
    this.states["/root/running/deleting_behaviour"].addChild(this.states["/root/running/deleting_behaviour/running"]);
    this.states["/root/running/child_behaviour"].addChild(this.states["/root/running/child_behaviour/listening"]);
    this.states["/root/running/deleting_balls_behaviour"].addChild(this.states["/root/running/deleting_balls_behaviour/listening"]);
    this.states[""].fixTree();
    this.states[""].default_state = this.states["/root"];
    this.states["/root"].default_state = this.states["/root/waiting"];
    this.states["/root/running/main_behaviour"].default_state = this.states["/root/running/main_behaviour/running"];
    this.states["/root/running/deleting_behaviour"].default_state = this.states["/root/running/deleting_behaviour/running"];
    this.states["/root/running/child_behaviour"].default_state = this.states["/root/running/child_behaviour/listening"];
    this.states["/root/running/deleting_balls_behaviour"].default_state = this.states["/root/running/deleting_balls_behaviour/listening"];
    
    // transition /root/waiting
    var _root_waiting_0 = new Transition(this, this.states["/root/waiting"], [this.states["/root/initializing"]]);
    _root_waiting_0.setAction(this._root_waiting_0_exec);
    _root_waiting_0.trigger = new Event("set_association_name", null);
    this.states["/root/waiting"].addTransition(_root_waiting_0);
    
    // transition /root/initializing
    var _root_initializing_0 = new Transition(this, this.states["/root/initializing"], [this.states["/root/creating"]]);
    _root_initializing_0.setAction(this._root_initializing_0_exec);
    this.states["/root/initializing"].addTransition(_root_initializing_0);
    
    // transition /root/creating
    var _root_creating_0 = new Transition(this, this.states["/root/creating"], [this.states["/root/packing"]]);
    _root_creating_0.setAction(this._root_creating_0_exec);
    _root_creating_0.trigger = new Event("instance_created", null);
    this.states["/root/creating"].addTransition(_root_creating_0);
    
    // transition /root/packing
    var _root_packing_0 = new Transition(this, this.states["/root/packing"], [this.states["/root/running"]]);
    _root_packing_0.trigger = new Event("button_created", null);
    this.states["/root/packing"].addTransition(_root_packing_0);
    
    // transition /root/running/main_behaviour/running
    var _root_running_main_behaviour_running_0 = new Transition(this, this.states["/root/running/main_behaviour/running"], [this.states["/root/running/main_behaviour/creating"]]);
    _root_running_main_behaviour_running_0.setAction(this._root_running_main_behaviour_running_0_exec);
    _root_running_main_behaviour_running_0.trigger = new Event("right_click", "field_ui");
    this.states["/root/running/main_behaviour/running"].addTransition(_root_running_main_behaviour_running_0);
    
    // transition /root/running/main_behaviour/creating
    var _root_running_main_behaviour_creating_0 = new Transition(this, this.states["/root/running/main_behaviour/creating"], [this.states["/root/running/main_behaviour/running"]]);
    _root_running_main_behaviour_creating_0.setAction(this._root_running_main_behaviour_creating_0_exec);
    _root_running_main_behaviour_creating_0.trigger = new Event("instance_created", null);
    this.states["/root/running/main_behaviour/creating"].addTransition(_root_running_main_behaviour_creating_0);
    
    // transition /root/running/deleting_behaviour/running
    var _root_running_deleting_behaviour_running_0 = new Transition(this, this.states["/root/running/deleting_behaviour/running"], [this.states["/root/running/deleting_behaviour/running"]]);
    _root_running_deleting_behaviour_running_0.setAction(this._root_running_deleting_behaviour_running_0_exec);
    _root_running_deleting_behaviour_running_0.trigger = new Event("delete_ball", null);
    this.states["/root/running/deleting_behaviour/running"].addTransition(_root_running_deleting_behaviour_running_0);
    
    // transition /root/running/child_behaviour/listening
    var _root_running_child_behaviour_listening_0 = new Transition(this, this.states["/root/running/child_behaviour/listening"], [this.states["/root/running/child_behaviour/listening"]]);
    _root_running_child_behaviour_listening_0.setAction(this._root_running_child_behaviour_listening_0_exec);
    _root_running_child_behaviour_listening_0.trigger = new Event("button_pressed", null);
    this.states["/root/running/child_behaviour/listening"].addTransition(_root_running_child_behaviour_listening_0);
    
    // transition /root/running/deleting_balls_behaviour/listening
    var _root_running_deleting_balls_behaviour_listening_0 = new Transition(this, this.states["/root/running/deleting_balls_behaviour/listening"], [this.states["/root/running/deleting_balls_behaviour/listening"]]);
    _root_running_deleting_balls_behaviour_listening_0.setAction(this._root_running_deleting_balls_behaviour_listening_0_exec);
    _root_running_deleting_balls_behaviour_listening_0.trigger = new Event("key_press", "field_ui");
    _root_running_deleting_balls_behaviour_listening_0.setGuard(this._root_running_deleting_balls_behaviour_listening_0_guard);
    this.states["/root/running/deleting_balls_behaviour/listening"].addTransition(_root_running_deleting_balls_behaviour_listening_0);
    
    // transition /root/deleting
    var _root_deleting_0 = new Transition(this, this.states["/root/deleting"], [this.states["/root/deleted"]]);
    _root_deleting_0.setAction(this._root_deleting_0_exec);
    this.states["/root/deleting"].addTransition(_root_deleting_0);
    
    // transition /root/running
    var _root_running_0 = new Transition(this, this.states["/root/running"], [this.states["/root/deleting"]]);
    _root_running_0.setAction(this._root_running_0_exec);
    _root_running_0.trigger = new Event("window_close", "field_ui");
    this.states["/root/running"].addTransition(_root_running_0);
};

Field.prototype._root_running_0_exec = function(parameters) {
    this.big_step.outputEventOM(new Event("delete_instance", null, [this, "buttons"]));
    this.big_step.outputEventOM(new Event("delete_instance", null, [this, "balls"]));
};

Field.prototype._root_waiting_0_exec = function(parameters) {
    var association_name = parameters[0];
    this.association_name = association_name
};

Field.prototype._root_initializing_0_exec = function(parameters) {
    this.big_step.outputEventOM(new Event("create_instance", null, [this, "buttons", "Button", this, 'create_new_field', 'Spawn New Window']));
};

Field.prototype._root_creating_0_exec = function(parameters) {
    var association_name = parameters[0];
    this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
};

Field.prototype._root_running_main_behaviour_running_0_exec = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    this.big_step.outputEventOM(new Event("create_instance", null, [this, "balls", "Ball", this.canvas, x, y]));
};

Field.prototype._root_running_main_behaviour_creating_0_exec = function(parameters) {
    var association_name = parameters[0];
    this.big_step.outputEventOM(new Event("start_instance", null, [this, association_name]));
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, association_name, new Event("set_association_name", null, [association_name])]));
};

Field.prototype._root_running_deleting_behaviour_running_0_exec = function(parameters) {
    var association_name = parameters[0];
    this.big_step.outputEventOM(new Event("delete_instance", null, [this, association_name]));
};

Field.prototype._root_running_child_behaviour_listening_0_exec = function(parameters) {
    var event_name = parameters[0];
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("button_pressed", null, [event_name])]));
};

Field.prototype._root_running_deleting_balls_behaviour_listening_0_exec = function(parameters) {
    var key = parameters[0];
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'balls', new Event("delete_self", null, new Array())]));
};

Field.prototype._root_running_deleting_balls_behaviour_listening_0_guard = function(parameters) {
    var key = parameters[0];
    return key == ui.KEYCODES.DELETE;
};

Field.prototype._root_deleting_0_exec = function(parameters) {
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("delete_field", null, [this.association_name])]));
};

Field.prototype.initializeStatechart = function() {
    // enter default state
    var states = this.states["/root"].getEffectiveTargetStates();
    this.updateConfiguration(states);
    for (var state_idx in states) {
        if (!states.hasOwnProperty(state_idx)) continue;
        var state = states[state_idx]
        if (state.enter) {
            state.enter();
        }
    }
};

// add symbol 'Field' to package 'Bouncing_Balls_JavaScript_Version'
Bouncing_Balls_JavaScript_Version.Field = Field;

var Button = function(controller, parent, event_name, button_text) {
    RuntimeClassBase.call(this, controller);
    
    this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
    this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
    this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
    this.semantics.priority = StatechartSemantics.SourceParent;
    this.semantics.concurrency = StatechartSemantics.Single;
    
    // build Statechart structure
    this.build_statechart_structure();
    this.inports["button_ui"] = controller.addInputPort("button_ui", this);
    
    // call user defined constructor
    Button.prototype.user_defined_constructor.call(this, parent, event_name, button_text);
};
Button.prototype = new Object();
(function() {
    var proto = new RuntimeClassBase();
    for (prop in proto) {
        Button.prototype[prop] = proto[prop];
    }
})();

Button.prototype.user_defined_constructor = function(parent, event_name, button_text) {
    this.event_name = event_name;
    var button = ui.append_button(parent.field_window, event_name);
    ui.bind_event(button.element, ui.EVENTS.MOUSE_CLICK, this.controller, 'mouse_click', this.inports["button_ui"]);
};

Button.prototype.user_defined_destructor = function() {
};


// builds Statechart structure
Button.prototype.build_statechart_structure = function() {
    
    // state <root>
    this.states[""] = new State(0, this);
    
    // state /initializing
    this.states["/initializing"] = new State(1, this);
    
    // state /running
    this.states["/running"] = new State(2, this);
    
    // add children
    this.states[""].addChild(this.states["/initializing"]);
    this.states[""].addChild(this.states["/running"]);
    this.states[""].fixTree();
    this.states[""].default_state = this.states["/initializing"];
    
    // transition /initializing
    var _initializing_0 = new Transition(this, this.states["/initializing"], [this.states["/running"]]);
    _initializing_0.setAction(this._initializing_0_exec);
    this.states["/initializing"].addTransition(_initializing_0);
    
    // transition /running
    var _running_0 = new Transition(this, this.states["/running"], [this.states["/running"]]);
    _running_0.setAction(this._running_0_exec);
    _running_0.trigger = new Event("mouse_click", "button_ui");
    _running_0.setGuard(this._running_0_guard);
    this.states["/running"].addTransition(_running_0);
};

Button.prototype._initializing_0_exec = function(parameters) {
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("button_created", null, new Array())]));
};

Button.prototype._running_0_exec = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("button_pressed", null, [this.event_name])]));
};

Button.prototype._running_0_guard = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    return button == ui.MOUSE_BUTTONS.LEFT;
};

Button.prototype.initializeStatechart = function() {
    // enter default state
    var states = this.states["/initializing"].getEffectiveTargetStates();
    this.updateConfiguration(states);
    for (var state_idx in states) {
        if (!states.hasOwnProperty(state_idx)) continue;
        var state = states[state_idx]
        if (state.enter) {
            state.enter();
        }
    }
};

// add symbol 'Button' to package 'Bouncing_Balls_JavaScript_Version'
Bouncing_Balls_JavaScript_Version.Button = Button;

var Ball = function(controller, canvas, x, y) {
    RuntimeClassBase.call(this, controller);
    
    this.semantics.big_step_maximality = StatechartSemantics.TakeMany;
    this.semantics.internal_event_lifeline = StatechartSemantics.Queue;
    this.semantics.input_event_lifeline = StatechartSemantics.FirstComboStep;
    this.semantics.priority = StatechartSemantics.SourceParent;
    this.semantics.concurrency = StatechartSemantics.Single;
    
    // build Statechart structure
    this.build_statechart_structure();
    this.inports["ball_ui"] = controller.addInputPort("ball_ui", this);
    
    // user defined attributes
    this.canvas = null;
    
    // call user defined constructor
    Ball.prototype.user_defined_constructor.call(this, canvas, x, y);
};
Ball.prototype = new Object();
(function() {
    var proto = new RuntimeClassBase();
    for (prop in proto) {
        Ball.prototype[prop] = proto[prop];
    }
})();

Ball.prototype.user_defined_constructor = function(canvas, x, y) {
    this.canvas 		= canvas;
    this.r 				= 20.0;
    this.vel 			= {'x': utils.random() * 3.0, 'y': utils.random() * 3.0};
    this.mouse_pos		= {};
    this.smooth 		= 0.4; // value between 0 and 1
    
    var circle = this.canvas.add_circle(x, y, this.r, {'fill':'#000'});
    ui.bind_event(circle, ui.EVENTS.MOUSE_PRESS, this.controller, 'mouse_press', this.inports["ball_ui"]);
    this.element = circle;
};

Ball.prototype.user_defined_destructor = function() {
    this.canvas.remove_element(this.element);
};


// builds Statechart structure
Ball.prototype.build_statechart_structure = function() {
    
    // state <root>
    this.states[""] = new State(0, this);
    
    // state /main_behaviour
    this.states["/main_behaviour"] = new State(1, this);
    
    // state /main_behaviour/initializing
    this.states["/main_behaviour/initializing"] = new State(2, this);
    
    // state /main_behaviour/bouncing
    this.states["/main_behaviour/bouncing"] = new State(3, this);
    this.states["/main_behaviour/bouncing"].setEnter(this._main_behaviour_bouncing_enter);
    this.states["/main_behaviour/bouncing"].setExit(this._main_behaviour_bouncing_exit);
    
    // state /main_behaviour/dragging
    this.states["/main_behaviour/dragging"] = new State(4, this);
    
    // state /main_behaviour/selected
    this.states["/main_behaviour/selected"] = new State(5, this);
    
    // state /deleted
    this.states["/deleted"] = new State(6, this);
    
    // add children
    this.states[""].addChild(this.states["/main_behaviour"]);
    this.states[""].addChild(this.states["/deleted"]);
    this.states["/main_behaviour"].addChild(this.states["/main_behaviour/initializing"]);
    this.states["/main_behaviour"].addChild(this.states["/main_behaviour/bouncing"]);
    this.states["/main_behaviour"].addChild(this.states["/main_behaviour/dragging"]);
    this.states["/main_behaviour"].addChild(this.states["/main_behaviour/selected"]);
    this.states[""].fixTree();
    this.states[""].default_state = this.states["/main_behaviour"];
    this.states["/main_behaviour"].default_state = this.states["/main_behaviour/initializing"];
    
    // transition /main_behaviour/initializing
    var _main_behaviour_initializing_0 = new Transition(this, this.states["/main_behaviour/initializing"], [this.states["/main_behaviour/bouncing"]]);
    _main_behaviour_initializing_0.setAction(this._main_behaviour_initializing_0_exec);
    _main_behaviour_initializing_0.trigger = new Event("set_association_name", null);
    this.states["/main_behaviour/initializing"].addTransition(_main_behaviour_initializing_0);
    
    // transition /main_behaviour/bouncing
    var _main_behaviour_bouncing_0 = new Transition(this, this.states["/main_behaviour/bouncing"], [this.states["/main_behaviour/bouncing"]]);
    _main_behaviour_bouncing_0.setAction(this._main_behaviour_bouncing_0_exec);
    _main_behaviour_bouncing_0.trigger = new Event("_0after");
    this.states["/main_behaviour/bouncing"].addTransition(_main_behaviour_bouncing_0);
    var _main_behaviour_bouncing_1 = new Transition(this, this.states["/main_behaviour/bouncing"], [this.states["/main_behaviour/selected"]]);
    _main_behaviour_bouncing_1.setAction(this._main_behaviour_bouncing_1_exec);
    _main_behaviour_bouncing_1.trigger = new Event("mouse_press", "ball_ui");
    _main_behaviour_bouncing_1.setGuard(this._main_behaviour_bouncing_1_guard);
    this.states["/main_behaviour/bouncing"].addTransition(_main_behaviour_bouncing_1);
    
    // transition /main_behaviour/dragging
    var _main_behaviour_dragging_0 = new Transition(this, this.states["/main_behaviour/dragging"], [this.states["/main_behaviour/dragging"]]);
    _main_behaviour_dragging_0.setAction(this._main_behaviour_dragging_0_exec);
    _main_behaviour_dragging_0.trigger = new Event("mouse_move", "ui");
    this.states["/main_behaviour/dragging"].addTransition(_main_behaviour_dragging_0);
    var _main_behaviour_dragging_1 = new Transition(this, this.states["/main_behaviour/dragging"], [this.states["/main_behaviour/bouncing"]]);
    _main_behaviour_dragging_1.setAction(this._main_behaviour_dragging_1_exec);
    _main_behaviour_dragging_1.trigger = new Event("mouse_release", "ui");
    this.states["/main_behaviour/dragging"].addTransition(_main_behaviour_dragging_1);
    
    // transition /main_behaviour/selected
    var _main_behaviour_selected_0 = new Transition(this, this.states["/main_behaviour/selected"], [this.states["/main_behaviour/dragging"]]);
    _main_behaviour_selected_0.setAction(this._main_behaviour_selected_0_exec);
    _main_behaviour_selected_0.trigger = new Event("mouse_press", "ball_ui");
    _main_behaviour_selected_0.setGuard(this._main_behaviour_selected_0_guard);
    this.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_0);
    var _main_behaviour_selected_1 = new Transition(this, this.states["/main_behaviour/selected"], [this.states["/deleted"]]);
    _main_behaviour_selected_1.setAction(this._main_behaviour_selected_1_exec);
    _main_behaviour_selected_1.trigger = new Event("delete_self", null);
    this.states["/main_behaviour/selected"].addTransition(_main_behaviour_selected_1);
};

Ball.prototype._main_behaviour_bouncing_enter = function() {
    this.addTimer(0, 0.05);
};

Ball.prototype._main_behaviour_bouncing_exit = function() {
    this.removeTimer(0);
};

Ball.prototype._main_behaviour_initializing_0_exec = function(parameters) {
    var association_name = parameters[0];
    this.association_name = association_name;
};

Ball.prototype._main_behaviour_bouncing_0_exec = function(parameters) {
    var pos = this.element.get_position();	
    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);
};

Ball.prototype._main_behaviour_bouncing_1_exec = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    this.element.set_color("#ff0");
};

Ball.prototype._main_behaviour_bouncing_1_guard = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    return button == ui.MOUSE_BUTTONS.LEFT;
};

Ball.prototype._main_behaviour_dragging_0_exec = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    var dx = x - this.mouse_pos.x;
    var dy = y - this.mouse_pos.y;
    
    this.element.move(dx, dy);
    
    // keep ball within boundaries
    var pos = this.element.get_position();
    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.set_position(pos.x, pos.y);
    this.mouse_pos = {'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
    };
};

Ball.prototype._main_behaviour_dragging_1_exec = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    this.element.set_color("#f00");
};

Ball.prototype._main_behaviour_selected_0_exec = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    this.mouse_pos = {'x':x, 'y':y};
};

Ball.prototype._main_behaviour_selected_0_guard = function(parameters) {
    var x = parameters[0];
    var y = parameters[1];
    var button = parameters[2];
    return button == ui.MOUSE_BUTTONS.LEFT;
};

Ball.prototype._main_behaviour_selected_1_exec = function(parameters) {
    this.big_step.outputEventOM(new Event("narrow_cast", null, [this, 'parent', new Event("delete_ball", null, [this.association_name])]));
};

Ball.prototype.initializeStatechart = function() {
    // enter default state
    var states = this.states["/main_behaviour"].getEffectiveTargetStates();
    this.updateConfiguration(states);
    for (var state_idx in states) {
        if (!states.hasOwnProperty(state_idx)) continue;
        var state = states[state_idx]
        if (state.enter) {
            state.enter();
        }
    }
};

// add symbol 'Ball' to package 'Bouncing_Balls_JavaScript_Version'
Bouncing_Balls_JavaScript_Version.Ball = Ball;

var ObjectManager = function(controller) {
    ObjectManagerBase.call(this, controller);
};
ObjectManager.prototype = new Object();
(function() {
    var proto = new ObjectManagerBase();
    for (prop in proto) {
        ObjectManager.prototype[prop] = proto[prop];
    }
})();

ObjectManager.prototype.instantiate = function(class_name, construct_params) {
    if (class_name === "MainApp") {
        var instance = new MainApp(this.controller);
        instance.associations = new Object();
        instance.associations["fields"] = new Association("Field", 0, -1);
    } else if (class_name === "Field") {
        var instance = new Field(this.controller);
        instance.associations = new Object();
        instance.associations["balls"] = new Association("Ball", 0, -1);
        instance.associations["buttons"] = new Association("Button", 0, -1);
        instance.associations["parent"] = new Association("MainApp", 1, 1);
    } else if (class_name === "Button") {
        var instance = new Button(this.controller, construct_params[0], construct_params[1], construct_params[2]);
        instance.associations = new Object();
        instance.associations["parent"] = new Association("Field", 1, 1);
    } else if (class_name === "Ball") {
        var instance = new Ball(this.controller, construct_params[0], construct_params[1], construct_params[2]);
        instance.associations = new Object();
        instance.associations["parent"] = new Association("Field", 1, 1);
    }
    return instance;
};

// add symbol 'ObjectManager' to package 'Bouncing_Balls_JavaScript_Version'
Bouncing_Balls_JavaScript_Version.ObjectManager = ObjectManager;

var Controller = function(event_loop_callbacks, finished_callback) {
    if (finished_callback === undefined) finished_callback = null;
    EventLoopControllerBase.call(this, new ObjectManager(this), event_loop_callbacks, finished_callback);
    this.addInputPort("ui");
    this.object_manager.createInstance("MainApp", new Array());
};
Controller.prototype = new Object();
(function() {
    var proto = new EventLoopControllerBase();
    for (prop in proto) {
        Controller.prototype[prop] = proto[prop];
    }
})();

// add symbol 'Controller' to package 'Bouncing_Balls_JavaScript_Version'
Bouncing_Balls_JavaScript_Version.Controller = Controller;
})();