<?xml version="1.0" ?>
<diagram author="Sam Pieters" name="Timer (Eventloop Version)">
    <top>
        from sccd.runtime.libs.ui import ui
        import time

        CANVAS_WIDTH = 350
        CANVAS_HEIGHT = 300
    </top>
    
    <inport name="ui" />
    <class name="MainApp" default="true">
        <attribute name="window_id" />
        <attribute name="canvas_id" />
        <attribute name="clock_id" />
        <attribute name="actual_clock_id" />
        <attribute name="button_id" />
        <attribute name="starting_time" />
        <inport name="field_ui"/>
        <scxml initial="creating_window">
            <state id="creating_window">
                <onentry>
                    <raise port="ui" event="create_window">
                        <parameter expr="CANVAS_WIDTH"/><!-- width -->
                        <parameter expr="CANVAS_HEIGHT"/><!-- height -->
                        <parameter expr='"Fixed Timer"'/><!-- title -->
                        <parameter expr="self.inports['field_ui']"/><!-- inport for response -->
                    </raise>
                </onentry>
                <transition event="window_created" target="../creating_canvas">
                    <parameter name="window_id" type="int" />
                    <script>
                        <![CDATA[
                        self.window_id = window_id
                        self.starting_time = time.time()
                        ]]>
                    </script>
                    <raise port="ui" event="bind_event">
                        <parameter expr="window_id"/><!-- widget_id -->
                        <parameter expr="ui.EVENTS.WINDOW_CLOSE"/><!-- tk_event -->
                        <parameter expr="'window_close'"/><!-- sccd_event_name -->
                        <parameter expr="self.inports['field_ui']"/><!-- inport for response -->
                    </raise>
                    <raise port="ui" event="bind_event">
                        <parameter expr="window_id"/><!-- widget_id -->
                        <parameter expr="ui.EVENTS.KEY_PRESS"/><!-- tk_event -->
                        <parameter expr="'key_press'"/><!-- sccd_event_name -->
                        <parameter expr="self.inports['field_ui']"/><!-- inport for response -->
                    </raise>
                </transition>
            </state>
            <state id="creating_canvas">
                <onentry>
                    <raise port="ui" event="create_canvas">
                        <parameter expr="self.window_id"/><!-- window_id -->
                        <parameter expr="CANVAS_WIDTH"/><!-- width -->
                        <parameter expr="CANVAS_HEIGHT - 200"/><!-- height -->
                        <parameter expr="{'background':'#222222'}"/><!-- style -->
                        <parameter expr="self.inports['field_ui']"/><!-- inport for response -->
                    </raise>
                </onentry>
                <transition event="canvas_created" target="../creating_clock_text">
                    <parameter name="canvas_id" type="int"/>
                    <script>
                        <![CDATA[
                        self.canvas_id = canvas_id
                        ]]>
                    </script>
                </transition>
            </state>
            <state id="creating_clock_text">
                <onentry>
                    <raise port="ui" event="create_text">
                        <parameter expr="self.canvas_id" />
                        <parameter expr="50" />
                        <parameter expr="50" />
                        <parameter expr="''" />
                        <parameter expr="self.inports['field_ui']" />
                    </raise>
                </onentry>
                <transition event="text_created" target="../creating_actual_clock_text">
                    <parameter name="canvas_id" type="int"/>
                    <parameter name="text_id" type="int" />
                    <script>
                        self.clock_id = text_id
                    </script>
                </transition>
            </state>
            <state id="creating_actual_clock_text">
                <onentry>
                    <raise port="ui" event="create_text">
                        <parameter expr="self.canvas_id" />
                        <parameter expr="50" />
                        <parameter expr="100" />
                        <parameter expr="''" />
                        <parameter expr="self.inports['field_ui']" />
                    </raise>
                </onentry>
                <transition event="text_created" target="../creating_interrupt_button">
                    <parameter name="canvas_id" type="int"/>
                    <parameter name="text_id" type="int" />
                    <script>
                        self.actual_clock_id = text_id
                    </script>
                </transition>
            </state>
            <state id="creating_interrupt_button">
                <onentry>
                    <raise port="ui" event="create_button">
                        <parameter expr="self.window_id" />
                        <parameter expr="'INTERRUPT/CONTINUE'" />
                        <parameter expr="self.inports['field_ui']" />
                    </raise>
                </onentry>
                <transition event="button_created" target="../running">
                    <parameter name="button_id" type="int"/>
                    <script>
                        self.button_id = button_id
                    </script> 
                    <raise port="ui" event="bind_event">
                        <parameter expr="button_id"/><!-- widget_id -->
                        <parameter expr="ui.EVENTS.MOUSE_CLICK"/><!-- tk_event -->
                        <parameter expr='"mouse_click"'/><!-- sccd_event_name -->
                        <parameter expr="self.inports['field_ui']"/><!-- inport for response -->
                    </raise>
                </transition>
            </state>
            <state id="running">
                <transition target="." after="0.05">
                    <raise port="ui" event="update_text">
                        <parameter expr="self.canvas_id" />
                        <parameter expr="self.clock_id" />
                        <parameter expr="str('%.2f' % (self.getSimulatedTime() / 1000.0))" />
                        <parameter expr="self.inports['field_ui']" />
                    </raise>
                    <raise port="ui" event="update_text">
                        <parameter expr="self.canvas_id" />
                        <parameter expr="self.actual_clock_id" />
                        <parameter expr="str('%.2f' % (time.time() - self.starting_time))" />
                        <parameter expr="self.inports['field_ui']" />
                    </raise>
                </transition>
                <transition port='field_ui' event="mouse_click" target='../interrupted' cond="button == ui.MOUSE_BUTTONS.LEFT">
                    <parameter name="x" />
                    <parameter name="y" />
                    <parameter name="button" />
                </transition>
            </state>
            <state id="interrupted">
                <transition port='field_ui' event="mouse_click" target='../running' cond="button == ui.MOUSE_BUTTONS.LEFT">
                    <parameter name="x" />
                    <parameter name="y" />
                    <parameter name="button" />
                </transition>
            </state>
        </scxml>
    </class>
</diagram>