Esempio n. 1
0
    def testScheduleEvent(self):

        CoreUtilities.init_timebase()
        Scheduler.schedule_event(3000, TestScheduler.cbScheduledEvent)
        TestScheduler.ourLogger.info("Wait for scheduled event")
        time.sleep(6)
        Scheduler.make_scheduled_calls()
        TestScheduler.ourLogger.info("Did it happen?")
Esempio n. 2
0
    def testMakeScheduledCalls(self):

        TestScheduler.ourLogger.info("Test legacy make_scheduled_calls")

        retToken = Scheduler.schedule_call(delay=0, func=TestScheduler.callbackFunction, repeat=False)

        TestScheduler.ourLogger.info("Task %s scheduled", retToken)

        TestScheduler.ourLogger.info("Sleepy time ...")
        time.sleep(5.0)

        Scheduler.make_scheduled_calls()
        self.assertTrue(TestScheduler.weHaveBeenCalledToken, "The token should have been set, but was not")
Esempio n. 3
0
    def testSchedulingOrder(self):

        #
        # Purposely schedule in wrong order; Scheduler should execute in correct order
        #
        retToken1 = Scheduler.schedule_call(delay=1000, func=TestScheduler.callbackFunction1, repeat=False)
        retToken2 = Scheduler.schedule_call(delay=9000, func=TestScheduler.callbackFunction3, repeat=False)
        retToken3 = Scheduler.schedule_call(delay=5000, func=TestScheduler.callbackFunction2, repeat=False)

        TestScheduler.ourLogger.debug(f"{retToken1}, {retToken2}, {retToken3}")
        TestScheduler.ourLogger.info("Wait long enough so that all tasks will be called in one shot")
        time.sleep(10.0)

        Scheduler.make_scheduled_calls()
        self.assertTrue((TestScheduler.callTime1 < TestScheduler.callTime2) and
                        TestScheduler.callTime2 < TestScheduler.callTime3,
                        "Scheduler called tasks out of order")
Esempio n. 4
0
    def run_modal(self, modal_widget: Widget):
        """
            Runs a modal event loop. The widget is run as a modal dialog until its dismiss() method is called.
        Args:
            modal_widget:  The modal widget
        """
        is_modal = modal_widget is not None
        modal_widget = modal_widget or self
        relative_pause = False
        relative_warmup = 0

        was_modal = None
        try:
            RootWidget.old_top_widget = RootWidget.top_widget
            RootWidget.top_widget = modal_widget
            was_modal = modal_widget.is_modal
            modal_widget.is_modal = True

            modal_widget.modal_result = None
            if not modal_widget.focus_switch:
                modal_widget.tab_to_first()

            self.do_draw = True
            use_sleep = self._use_sleep

            from albow.core.ui.AlbowEventLoop import AlbowEventLoop
            from albow.core.ui.EventLoopParams import EventLoopParams

            eventLoop: AlbowEventLoop = AlbowEventLoop(
                modalWidget=modal_widget, containingWidget=self)

            last_click_time = 0
            num_clicks = 0
            while modal_widget.modal_result is None:

                defer_drawing = self.frame_time != 0.0 and modal_widget.defer_drawing(
                )
                try:
                    if not is_modal:
                        if RootWidget.ourTimerEvent:
                            if not use_sleep and defer_drawing:
                                Scheduler.make_scheduled_calls()
                            CoreUtilities.add_modifiers(
                                RootWidget.ourTimerEvent)
                            if RootWidget.last_mouse_event:
                                RootWidget.ourTimerEvent.dict[
                                    'pos'] = RootWidget.last_mouse_event.pos
                                RootWidget.ourTimerEvent.dict[
                                    'local'] = RootWidget.last_mouse_event.local
                            if RootWidget.last_mouse_event_handler:
                                RootWidget.last_mouse_event_handler.setup_cursor(
                                    RootWidget.ourTimerEvent)
                            self.do_draw = self.timer_event(
                                RootWidget.ourTimerEvent)
                            RootWidget.ourTimerEvent = None
                        else:
                            if defer_drawing:
                                self.do_draw = False
                    # RootWidget.classLogger.info(f"self.do_draw: {self.do_draw}")
                    if self.do_draw:
                        if self.is_gl:
                            gl_surface = self.gl_surface
                            gl_surface.gl_clear(self.bg_color)
                            self.gl_draw_all(gl_surface)
                            gl_surface.gl_flush()
                        else:
                            self.draw_all(self.surface)
                        self.do_draw = False
                        # tb1 = timestamp() ###
                        pygame.display.flip()

                    in_relative_mode = bool(modal_widget.relative_mode())
                    grab = in_relative_mode and not relative_pause
                    if grab != get_grab():
                        set_grab(grab)
                        set_mouse_visible(not grab)
                        relative_warmup = 3  # Ignore spurious deltas on entering relative mode
                    if use_sleep and defer_drawing:

                        time_now = Scheduler.timestamp()

                        if RootWidget.nextFrameDue < time_now:
                            RootWidget.nextFrameDue = time_now

                        while True:
                            sleep_time = Scheduler.make_due_calls(
                                time_now, RootWidget.nextFrameDue)
                            if sleep_time <= 0.0:
                                break

                            sleep(sleep_time / 1000.0)
                            time_now = Scheduler.timestamp()
                        RootWidget.nextFrameDue += self.frame_time
                        RootWidget.ourTimerEvent = Event(
                            USEREVENT, {'time': time_now})
                        events = []
                    else:
                        events = [pygame.event.wait()]
                    events.extend(pygame.event.get())

                    loopParams: EventLoopParams = EventLoopParams(
                        use_sleep=use_sleep,
                        relative_pause=relative_pause,
                        do_draw=self.do_draw,
                        relative_warmup=relative_warmup,
                        last_click_time=last_click_time,
                        num_clicks=num_clicks)

                    newParams: EventLoopParams = eventLoop.processEvents(
                        eventList=events,
                        relativeMode=in_relative_mode,
                        deferDrawing=defer_drawing,
                        eventLoopParams=loopParams)

                    use_sleep = newParams.use_sleep
                    relative_pause = newParams.relative_pause
                    self.do_draw = newParams.do_draw
                    relative_warmup = newParams.relative_warmup
                    last_click_time = newParams.last_click_time
                    num_clicks = newParams.num_clicks

                except CancelException:
                    pass

                except ApplicationException as e:
                    self.report_error(e)
        finally:
            modal_widget.is_modal = was_modal
            RootWidget.top_widget = RootWidget.old_top_widget

        RootWidget.clicked_widget = None