| 1 |  |  | import time | 
            
                                                        
            
                                    
            
            
                | 2 |  |  | import sys | 
            
                                                        
            
                                    
            
            
                | 3 |  |  | import os | 
            
                                                        
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 5 |  |  | from pypen.drawing.pypen_class import PyPen | 
            
                                                        
            
                                    
            
            
                | 6 |  |  | from pyglet import clock, gl, image, window, canvas | 
            
                                                        
            
                                    
            
            
                | 7 |  |  | import cairo | 
            
                                                        
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 10 |  |  | # In order to display our Icon properly on Windows, | 
            
                                                        
            
                                    
            
            
                | 11 |  |  | # we need to have a "unique" (different from python's) app_id set | 
            
                                                        
            
                                    
            
            
                | 12 |  |  | if sys.platform == "win32": | 
            
                                                        
            
                                    
            
            
                | 13 |  |  |     import ctypes | 
            
                                                        
            
                                    
            
            
                | 14 |  |  |     app_id = "canvim.pypen"  # arbitrary string | 
            
                                                        
            
                                    
            
            
                | 15 |  |  |     ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id) | 
            
                                                        
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 18 |  |  | class PyPenWindow(window.Window): | 
            
                                                        
            
                                    
            
            
                | 19 |  |  |     def __init__(self, user_sketch=None, window_title="Example", arguments={}): | 
            
                                                        
            
                                    
            
            
                | 20 |  |  |         super().__init__(visible=False, resizable=True, caption=window_title, fullscreen=arguments.fullscreen) | 
            
                                                        
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 22 |  |  |         self.set_vsync(True) | 
            
                                                        
            
                                    
            
            
                | 23 |  |  |         self._current_path = os.path.dirname(__file__) | 
            
                                                        
            
                                    
            
            
                | 24 |  |  |         self.set_icon(image.load(os.path.join(self._current_path, "..", "resources", "icon.png"))) | 
            
                                                        
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 26 |  |  |         self.pypen = PyPen(user_sketch) | 
            
                                                        
            
                                    
            
            
                | 27 |  |  |         self.window_title = window_title | 
            
                                                        
            
                                    
            
            
                | 28 |  |  |         self.arguments = arguments | 
            
                                                        
            
                                    
            
            
                | 29 |  |  |         self.passed_time = self.delta_time = self.frame_count = 0 | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |         if not arguments.fullscreen: | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |             self.set_size(self.pypen.user_sketch.settings.width, self.pypen.user_sketch.settings.height) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |         else: | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |             display = canvas.Display() | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |             screen = display.get_default_screen() | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |             self.pypen.user_sketch.settings.width = screen.width | 
            
                                                        
            
                                    
            
            
                | 37 |  |  |             self.pypen.user_sketch.settings.height = screen.height | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 39 |  |  |         clock.schedule_once(self.call_user_start, 0) | 
            
                                                        
            
                                    
            
            
                | 40 |  |  |         if self.arguments.timeout: | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |             clock.schedule_once(self.destroy, self.arguments.timeout/1000) | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |     def on_mouse_motion(self, x, y, dx, dy): | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         self.pypen.user_sketch.MOUSE.x = x | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         self.pypen.user_sketch.MOUSE.y = self.pypen.user_sketch.settings.height - y | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |     def destroy(self, *args): | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         self.close() | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |     def on_draw(self): | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         gl.glEnable(gl.GL_TEXTURE_2D) | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         gl.glBindTexture(gl.GL_TEXTURE_2D, self.pypen.texture.id) | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         gl.glTexImage2D(gl.GL_TEXTURE_2D, | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |                         0, | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |                         gl.GL_RGBA, | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |                         self.pypen.user_sketch.settings.width, | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |                         self.pypen.user_sketch.settings.height, | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |                         0, | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |                         gl.GL_BGRA, | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |                         gl.GL_UNSIGNED_BYTE, | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |                         self.pypen.surface_data) | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         gl.glBegin(gl.GL_QUADS) | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         gl.glTexCoord2f(0.0, 1.0) | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         gl.glVertex2i(0, 0) | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         gl.glTexCoord2f(1.0, 1.0) | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         gl.glVertex2i(self.pypen.user_sketch.settings.width, 0) | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         gl.glTexCoord2f(1.0, 0.0) | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         gl.glVertex2i(self.pypen.user_sketch.settings.width, self.pypen.user_sketch.settings.height) | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         gl.glTexCoord2f(0.0, 0.0) | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         gl.glVertex2i(0, self.pypen.user_sketch.settings.height) | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         gl.glEnd() | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |     def on_resize(self, width, height): | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |         super().on_resize(width, height) | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         if width <= 0 or height <= 0: | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |             return | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         self.pypen.user_sketch.settings.width = max(width, 1) | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |         self.pypen.user_sketch.settings.height = max(height, 1) | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         self.pypen.update_settings() | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     def call_user_start(self, dt): | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         if self.pypen.user_sketch.settings._user_has_start: | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |             self.pypen.user_sketch.start() | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             if not self.arguments.fullscreen: | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |                 self.set_size(self.pypen.user_sketch.settings.width, self.pypen.user_sketch.settings.height) | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         self.set_visible() | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         clock.schedule_interval_soft(self.pypen_loop, 1/self.pypen.user_sketch.settings.fps) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     def call_user_update(self): | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         self.pypen.user_sketch.TIME = self.pypen.user_sketch.T = self.passed_time | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         self.pypen.user_sketch.FRAME = self.pypen.user_sketch.F = self.frame_count | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         self.pypen.user_sketch.DELTA_TIME = self.pypen.user_sketch.DT = self.delta_time | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         self.pypen.user_sketch.FPS = clock.get_fps() | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         self.pypen.user_sketch.WIDTH = self.pypen.user_sketch.settings.width | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         self.pypen.user_sketch.HEIGHT = self.pypen.user_sketch.settings.height | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         if self.pypen.user_sketch.settings._user_has_update: | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |             try: | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |                 self.pypen.user_sketch.update() | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             except KeyboardInterrupt as exception: | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |                 sys.exit(self.destroy()) | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |     def pypen_loop(self, dt, *args): | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         self.delta_time = dt | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |         self.passed_time += dt | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |         self.frame_count += 1 | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |         clock.unschedule(self.pypen_loop) | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |         self.call_user_update() | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         clock.schedule_interval_soft(self.pypen_loop, 1/self.pypen.user_sketch.settings.fps) | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |