| 1 |  |  | import ctypes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | from pypen.drawing.color import Color | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from pypen.utils.math import TAU | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import cairo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from pyglet import gl, image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class PyPen(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     def __init__(self, user_sketch): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         self.user_sketch = user_sketch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         self.surface_data = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         self.surface = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         self.context = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         self.update_settings() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         self._fix_primitive_functions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     def _fix_primitive_functions(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         self.user_sketch.fill_screen = self.fill_screen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self.user_sketch.clear_screen = self.clear_screen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         self.user_sketch.clear = self.clear | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         self.user_sketch.rectangle = self.rectangle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         self.user_sketch.circle = self.circle | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         self.user_sketch.ellipse = self.ellipse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         self.user_sketch.arc = self.arc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         self.user_sketch.arc = self.arc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         self.user_sketch.rotate = self.rotate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         self.user_sketch.translate = self.translate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         self.user_sketch.scale = self.scale | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         self.user_sketch.save = self.save | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         self.user_sketch.restore = self.restore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     def _fill(self, unparsed_fill_color): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if unparsed_fill_color != "": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             self.user_sketch.settings.fill_color = unparsed_fill_color | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         fill_color = Color.from_user_input(self.user_sketch.settings.fill_color) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self.context.set_source_rgba(*fill_color.rgba()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         self.context.fill() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     def _stroke(self, unparsed_stroke_color, unparsed_stroke_width): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if unparsed_stroke_color != "": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             self.user_sketch.settings.stroke_color = unparsed_stroke_color | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if unparsed_stroke_width >= 0: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             self.user_sketch.settings.stroke_width = unparsed_stroke_width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         stroke_color = Color.from_user_input(self.user_sketch.settings.stroke_color) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         stroke_width = self.user_sketch.settings.stroke_width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         self.context.set_line_width(stroke_width) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         self.context.set_source_rgba(*stroke_color.rgba()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         self.context.stroke_preserve() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     def rotate(self, angle=0): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         self.context.rotate(angle) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     def translate(self, x=0, y=0): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         self.context.translate(x, y) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     def scale(self, factor=1): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         self.context.scale(factor) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     def save(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         self.context.save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     def restore(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         self.context.restore() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     def update_settings(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         self.surface_data = (ctypes.c_ubyte * (self.user_sketch.settings.width * self.user_sketch.settings.height * 4))() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         self.surface = cairo.ImageSurface.create_for_data(self.surface_data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                                                           cairo.FORMAT_ARGB32, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                                                           self.user_sketch.settings.width, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                                                           self.user_sketch.settings.height, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                                                           self.user_sketch.settings.width * 4) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         self.context = cairo.Context(self.surface) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         self.texture = image.Texture.create_for_size(gl.GL_TEXTURE_2D, self.user_sketch.settings.width, self.user_sketch.settings.height, gl.GL_RGBA) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     def clear_screen(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         self.fill_screen("default_background_color") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     def clear(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         self.clear_screen() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     def fill_screen(self, color="default_background_color"): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         self.context.save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         self.context.scale(self.user_sketch.settings.width, self.user_sketch.settings.height) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         self.rectangle(0, 0, 1, 1, color) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         self.context.restore() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     def rectangle(self, x, y, width, height, fill_color="", stroke_color="", stroke_width=-1): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         self.context.rectangle(x, y, width, height) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         self._stroke(stroke_color, stroke_width) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         self._fill(fill_color) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     def circle(self, x, y, radius, fill_color="", stroke_color="", stroke_width=-1): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         self.context.arc(x, y, radius, 0, TAU) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         self._stroke(stroke_color, stroke_width) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         self._fill(fill_color) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     def ellipse(self, x, y, width, height, fill_color="", stroke_color="", stroke_width=-1): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         ratio = height/width | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         self.save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         self.context.scale(1, ratio) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         self.context.arc(x, y/ratio, width, 0, TAU) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         self.restore() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         self._stroke(stroke_color, stroke_width) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         self._fill(fill_color) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     def arc(self, x, y, radius, start_angle, stop_angle, fill_color="", stroke_color="", stroke_width=-1): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         self.context.arc(x, y, radius, start_angle, stop_angle) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         self._stroke(stroke_color, stroke_width) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |         self._fill(fill_color) | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  |