Mul.flatten -- optimize for all-objects-are-commutative case
[sympy.git] / sympy / plotting / managed_window.py
blob01ef52cb2c95d15f43608d6d18fe90c763b8b679
1 from pyglet.gl import *
2 from pyglet.window import Window
3 from pyglet.clock import Clock
5 from threading import Thread, Lock, Event
7 gl_lock = Lock()
9 class ManagedWindow(Window):
10 """
11 A pyglet window with an event loop which executes automatically
12 in a separate thread. Behavior is added by creating a subclass
13 which overrides setup, update, and/or draw.
14 """
15 fps_limit = 30
16 default_win_args = dict(width=600,
17 height=500,
18 vsync=False,
19 resizable=True)
21 def __init__(self, **win_args):
22 """
23 It is best not to override this function in the child
24 class, unless you need to take additional arguments.
25 Do any OpenGL initialization calls in setup().
26 """
27 self.win_args = dict(self.default_win_args, **win_args)
28 self.Thread=Thread(target=self.__event_loop__)
29 self.Thread.start()
31 def __event_loop__(self, **win_args):
32 """
33 The event loop thread function. Do not override or call
34 directly (it is called by __init__).
35 """
36 gl_lock.acquire()
37 try:
38 try:
39 super(ManagedWindow, self).__init__(**self.win_args)
40 self.switch_to()
41 self.setup()
42 except Exception, e:
43 print "Window initialization failed: %s" % (str(e))
44 self.has_exit = True
45 finally:
46 gl_lock.release()
48 clock = Clock()
49 clock.set_fps_limit(self.fps_limit)
50 while not self.has_exit:
51 dt = clock.tick()
52 gl_lock.acquire()
53 try:
54 try:
55 self.switch_to()
56 self.dispatch_events()
57 self.clear()
58 self.update(dt)
59 self.draw()
60 self.flip()
61 except Exception, e:
62 print "Uncaught exception in event loop: %s" % str(e)
63 self.has_exit = True
64 finally:
65 gl_lock.release()
66 super(ManagedWindow, self).close()
68 def close(self):
69 """
70 Closes the window.
71 """
72 self.has_exit = True
74 def setup(self):
75 """
76 Called once before the event loop begins.
77 Override this method in a child class. This
78 is the best place to put things like OpenGL
79 initialization calls.
80 """
81 pass
83 def update(self, dt):
84 """
85 Called before draw during each iteration of
86 the event loop. dt is the elapsed time in
87 seconds since the last update. OpenGL rendering
88 calls are best put in draw() rather than here.
89 """
90 pass
92 def draw(self):
93 """
94 Called after update during each iteration of
95 the event loop. Put OpenGL rendering calls
96 here.
97 """
98 pass
100 if __name__ == '__main__':
101 ManagedWindow()