1import weakref
2
3from arcade.glui.animation import AnimationSystem
4from arcade.glui.input import InputHandler
5from arcade.glui.state import State
6from arcade.glui.texturemanager import TextureManager
7
8
9class Callbacks:
10    def __init__(self):
11        self.width = 320
12        self.height = 200
13        self.window = None
14        self.idle_counter = 0
15
16    def initialize(self, width, height):
17        self.width = width
18        self.height = height
19
20        # Re-create state object
21        State.reset()
22
23        TextureManager.reset()
24
25        import arcade.glui.window
26
27        arcade.glui.window.main_window = self
28        arcade.glui.window.show()
29
30    def active(self):
31        """Check whether the user interface is active or not. If not active,
32        we do not want to force regular/timed refresh event."""
33        state = State.get()
34        # print("state.game_running", state.game_running)
35        if state.game_running:
36            # print("inactive")
37            return False
38        # print(state.idle_from)
39        # if state.idle_from and state.idle_from < get_current_time():
40        # print(InputHandler.peek_button())
41        if not InputHandler.peek_button():
42            if not state.dirty:
43                if not AnimationSystem.is_active():
44                    self.idle_counter += 1
45                    if self.idle_counter < 16:
46                        # We render 1 frame per second when "idling". We need
47                        # to "render" regularly, because some state is updated
48                        # during the render phase.
49                        return False
50        self.idle_counter = 0
51        return True
52
53    def resize(self, width, height):
54        print("Callbacks.resize", width, height)
55        from arcade.glui.window import on_resize
56
57        self.width = width
58        self.height = height
59        on_resize((width, height))
60
61    def render(self):
62        # print("render")
63        from arcade.glui.window import main_loop_iteration
64
65        if main_loop_iteration():
66            print("main_loop_iteration signalled quit")
67            TextureManager.get().unload_textures()
68            self.window().quit()
69
70    def timer(self):
71        InputHandler.update()
72
73    def restore_window_if_necessary(self):
74        pass
75        # noinspection PyCallingNonCallable
76        # window = self.window()
77        # under Gnome 3, the window is minized when launching FS-UAE
78        # full-screen from full-screen arcade/game center.
79        # window.restore_window_if_necessary()
80
81    def set_window(self, window):
82        self.window = weakref.ref(window)
83