1
|
|
|
import ctypes |
2
|
|
|
|
3
|
|
|
from pypen.drawing.color import Color |
4
|
|
|
from pypen.utils.math import TAU |
5
|
|
|
from pypen.settings import default_settings |
6
|
|
|
import cairo |
7
|
|
|
from pyglet import gl, image |
8
|
|
|
|
9
|
|
|
class PyPen(): |
10
|
|
|
def __init__(self, user_sketch): |
11
|
|
|
self.user_sketch = user_sketch |
12
|
|
|
|
13
|
|
|
self.surface_data = None |
14
|
|
|
self.surface = None |
15
|
|
|
self.context = None |
16
|
|
|
|
17
|
|
|
self.update_settings() |
18
|
|
|
self._fix_primitive_functions() |
19
|
|
|
|
20
|
|
|
def _fix_primitive_functions(self): |
21
|
|
|
self.user_sketch.fill_screen = self.fill_screen |
22
|
|
|
self.user_sketch.clear_screen = self.clear_screen |
23
|
|
|
self.user_sketch.clear = self.clear |
24
|
|
|
|
25
|
|
|
self.user_sketch.begin_shape = self.begin_shape |
26
|
|
|
self.user_sketch.vertex = self.vertex |
27
|
|
|
self.user_sketch.end_shape = self.end_shape |
28
|
|
|
self.user_sketch.rectangle = self.rectangle |
29
|
|
|
self.user_sketch.circle = self.circle |
30
|
|
|
self.user_sketch.ellipse = self.ellipse |
31
|
|
|
self.user_sketch.arc = self.arc |
32
|
|
|
self.user_sketch.triangle = self.triangle |
33
|
|
|
self.user_sketch.line = self.line |
34
|
|
|
|
35
|
|
|
self.user_sketch.arc = self.arc |
36
|
|
|
|
37
|
|
|
self.user_sketch.rotate = self.rotate |
38
|
|
|
self.user_sketch.translate = self.translate |
39
|
|
|
self.user_sketch.scale = self.scale |
40
|
|
|
self.user_sketch.save = self.save |
41
|
|
|
self.user_sketch.restore = self.restore |
42
|
|
|
|
43
|
|
|
self.user_sketch.reset_style = self.reset_style |
44
|
|
|
|
45
|
|
|
def _fill(self, unparsed_fill_color): |
46
|
|
|
if unparsed_fill_color != "": |
47
|
|
|
self.user_sketch.settings.fill_color = unparsed_fill_color |
48
|
|
|
|
49
|
|
|
fill_color = Color.from_user_input(self.user_sketch.settings.fill_color) |
50
|
|
|
self.context.set_source_rgba(*fill_color.rgba()) |
51
|
|
|
self.context.fill() |
52
|
|
|
|
53
|
|
|
def _stroke(self, unparsed_stroke_color, unparsed_stroke_width): |
54
|
|
|
if unparsed_stroke_color != "": |
55
|
|
|
self.user_sketch.settings.stroke_color = unparsed_stroke_color |
56
|
|
|
|
57
|
|
|
if unparsed_stroke_width >= 0: |
58
|
|
|
self.user_sketch.settings.stroke_width = unparsed_stroke_width |
59
|
|
|
|
60
|
|
|
stroke_color = Color.from_user_input(self.user_sketch.settings.stroke_color) |
61
|
|
|
stroke_width = self.user_sketch.settings.stroke_width |
62
|
|
|
|
63
|
|
|
self.context.set_line_width(stroke_width) |
64
|
|
|
self.context.set_source_rgba(*stroke_color.rgba()) |
65
|
|
|
self.context.stroke_preserve() |
66
|
|
|
|
67
|
|
|
def rotate(self, angle=0): |
68
|
|
|
self.context.rotate(angle) |
69
|
|
|
|
70
|
|
|
def translate(self, x=0, y=0): |
71
|
|
|
self.context.translate(x, y) |
72
|
|
|
|
73
|
|
|
def scale(self, factor=1): |
74
|
|
|
self.context.scale(factor) |
75
|
|
|
|
76
|
|
|
def save(self): |
77
|
|
|
self.context.save() |
78
|
|
|
|
79
|
|
|
def restore(self): |
80
|
|
|
self.context.restore() |
81
|
|
|
|
82
|
|
|
def reset_style(self): |
83
|
|
|
self.user_sketch.settings.fill_color = default_settings.fill_color |
84
|
|
|
self.user_sketch.settings.stroke_color = default_settings.stroke_color |
85
|
|
|
self.user_sketch.settings.stroke_width = default_settings.stroke_width |
86
|
|
|
|
87
|
|
|
def update_settings(self): |
88
|
|
|
self.surface_data = (ctypes.c_ubyte * (self.user_sketch.settings.width * self.user_sketch.settings.height * 4))() |
89
|
|
|
self.surface = cairo.ImageSurface.create_for_data(self.surface_data, |
90
|
|
|
cairo.FORMAT_ARGB32, |
91
|
|
|
self.user_sketch.settings.width, |
92
|
|
|
self.user_sketch.settings.height, |
93
|
|
|
self.user_sketch.settings.width * 4) |
94
|
|
|
self.context = cairo.Context(self.surface) |
95
|
|
|
self.texture = image.Texture.create_for_size(gl.GL_TEXTURE_2D, self.user_sketch.settings.width, self.user_sketch.settings.height, gl.GL_RGBA) |
96
|
|
|
|
97
|
|
|
def clear_screen(self): |
98
|
|
|
self.fill_screen("default_background_color") |
99
|
|
|
|
100
|
|
|
def clear(self): |
101
|
|
|
self.clear_screen() |
102
|
|
|
|
103
|
|
|
def fill_screen(self, color="default_background_color"): |
104
|
|
|
background_color = Color.from_user_input(color) |
105
|
|
|
self.context.save() |
106
|
|
|
self.context.scale(self.user_sketch.settings.width, self.user_sketch.settings.height) |
107
|
|
|
self.context.rectangle(0, 0, 1, 1) |
108
|
|
|
self.context.set_source_rgba(*background_color.rgba()) |
109
|
|
|
self.context.fill() |
110
|
|
|
self.context.restore() |
111
|
|
|
|
112
|
|
|
def begin_shape(self): |
113
|
|
|
self.user_sketch.settings._shape_begun = True |
114
|
|
|
|
115
|
|
|
def vertex(self, x, y): |
116
|
|
|
if self.user_sketch.settings._shape_begun: |
117
|
|
|
self.context.move_to(x, y) |
118
|
|
|
self.user_sketch.settings._starting_point = (x, y) |
119
|
|
|
self.user_sketch.settings._shape_begun = False |
120
|
|
|
else: |
121
|
|
|
self.context.line_to(x, y) |
122
|
|
|
|
123
|
|
|
def end_shape(self, fill_color="", stroke_color="", stroke_width=-1): |
124
|
|
|
if self.user_sketch.settings._starting_point is not None: |
125
|
|
|
starting_point = self.user_sketch.settings._starting_point |
126
|
|
|
self.context.line_to(starting_point[0], starting_point[1]) |
127
|
|
|
self.user_sketch.settings._starting_point = None |
128
|
|
|
self._stroke(stroke_color, stroke_width) |
129
|
|
|
self._fill(fill_color) |
130
|
|
|
|
131
|
|
|
def rectangle(self, x, y, width, height, fill_color="", stroke_color="", stroke_width=-1): |
132
|
|
|
self.context.rectangle(x, y, width, height) |
133
|
|
|
self._stroke(stroke_color, stroke_width) |
134
|
|
|
self._fill(fill_color) |
135
|
|
|
|
136
|
|
|
def circle(self, x, y, radius, fill_color="", stroke_color="", stroke_width=-1): |
137
|
|
|
self.context.arc(x, y, radius, 0, TAU) |
138
|
|
|
self._stroke(stroke_color, stroke_width) |
139
|
|
|
self._fill(fill_color) |
140
|
|
|
|
141
|
|
|
def ellipse(self, x, y, width, height, fill_color="", stroke_color="", stroke_width=-1): |
142
|
|
|
ratio = height/width |
143
|
|
|
self.save() |
144
|
|
|
self.context.scale(1, ratio) |
145
|
|
|
self.context.arc(x, y/ratio, width, 0, TAU) |
146
|
|
|
self.restore() |
147
|
|
|
self._stroke(stroke_color, stroke_width) |
148
|
|
|
self._fill(fill_color) |
149
|
|
|
|
150
|
|
|
def arc(self, x, y, radius, start_angle, stop_angle, fill_color="", stroke_color="", stroke_width=-1): |
151
|
|
|
self.context.arc(x, y, radius, start_angle, stop_angle) |
152
|
|
|
self._stroke(stroke_color, stroke_width) |
153
|
|
|
self._fill(fill_color) |
154
|
|
|
|
155
|
|
|
def triangle(self, x1_or_x, y1_or_y, x2_or_width, y2_or_height, x3_or_p=0.5, y3=None, fill_color="", stroke_color="", stroke_width=-1): |
156
|
|
|
if y3 is not None: |
157
|
|
|
x1 = x1_or_x |
158
|
|
|
y1 = y1_or_y |
159
|
|
|
x2 = x2_or_width |
160
|
|
|
y2 = y2_or_height |
161
|
|
|
x3 = x3_or_p |
162
|
|
|
else: |
163
|
|
|
x = x1_or_x |
164
|
|
|
y = y1_or_y |
165
|
|
|
width = x2_or_width |
166
|
|
|
height = y2_or_height |
167
|
|
|
p = x3_or_p |
168
|
|
|
|
169
|
|
|
x1 = x - width/2 |
170
|
|
|
y1 = y + height/2 |
171
|
|
|
x2 = x + width/2 |
172
|
|
|
y2 = x + height/2 |
173
|
|
|
x3 = (x2 - x1) * p + x1 |
174
|
|
|
y3 = y - height/2 |
175
|
|
|
|
176
|
|
|
self.context.move_to(x1, y1) |
177
|
|
|
self.context.line_to(x2, y2) |
178
|
|
|
self.context.line_to(x3, y3) |
179
|
|
|
self.context.line_to(x1, y1) |
180
|
|
|
self._stroke(stroke_color, stroke_width) |
181
|
|
|
self._fill(fill_color) |
182
|
|
|
|
183
|
|
|
def line(self, x1, y1, x2, y2, stroke_color="", stroke_width=-1): |
184
|
|
|
self.context.move_to(x1, y1) |
185
|
|
|
self.context.line_to(x2, y2) |
186
|
|
|
self._stroke(stroke_color, stroke_width) |
187
|
|
|
|