1
|
|
|
import ctypes |
2
|
|
|
|
3
|
|
|
from pypen.drawing.color import Color |
4
|
|
|
import cairo |
5
|
|
|
from pyglet import gl, image |
6
|
|
|
|
7
|
|
|
class PrimitivesDrawer(): |
8
|
|
|
def __init__(self, settings): |
9
|
|
|
self.settings = settings |
10
|
|
|
self.surface_data = None |
11
|
|
|
self.surface = None |
12
|
|
|
self.context = None |
13
|
|
|
self.update_settings(settings) |
14
|
|
|
|
15
|
|
|
def rotate(self, angle=0): |
16
|
|
|
self.context.rotate(angle) |
17
|
|
|
|
18
|
|
|
def translate(self, x=0, y=0): |
19
|
|
|
self.context.translate(x, y) |
20
|
|
|
|
21
|
|
|
def scale(self, factor=1): |
22
|
|
|
self.context.scale(factor) |
23
|
|
|
|
24
|
|
|
def save(self): |
25
|
|
|
self.context.save() |
26
|
|
|
|
27
|
|
|
def restore(self): |
28
|
|
|
self.context.restore() |
29
|
|
|
|
30
|
|
|
def update_settings(self, settings): |
31
|
|
|
self.settings = settings |
32
|
|
|
|
33
|
|
|
self.surface_data = (ctypes.c_ubyte * (self.settings.width * self.settings.height * 4))() |
34
|
|
|
self.surface = cairo.ImageSurface.create_for_data(self.surface_data, |
35
|
|
|
cairo.FORMAT_ARGB32, |
36
|
|
|
self.settings.width, |
37
|
|
|
self.settings.height, |
38
|
|
|
self.settings.width * 4) |
39
|
|
|
self.context = cairo.Context(self.surface) |
40
|
|
|
|
41
|
|
|
self.texture = image.Texture.create_for_size(gl.GL_TEXTURE_2D, self.settings.width, self.settings.height, gl.GL_RGBA) |
42
|
|
|
|
43
|
|
|
def clear_screen(self): |
44
|
|
|
self.fill_screen("default_background_color") |
45
|
|
|
|
46
|
|
|
def clear(self): |
47
|
|
|
self.clear_screen() |
48
|
|
|
|
49
|
|
|
def fill_screen(self, color="default_background_color"): |
50
|
|
|
self.context.save() |
51
|
|
|
self.context.scale(self.settings.width, self.settings.height) |
52
|
|
|
self.rectangle(0, 0, 1, 1, color) |
53
|
|
|
self.context.restore() |
54
|
|
|
|
55
|
|
|
def fill(self): |
56
|
|
|
pass |
57
|
|
|
|
58
|
|
|
def rectangle(self, x, y, width, height, color="default_color"): |
59
|
|
|
color = Color.from_user_input(color) |
60
|
|
|
|
61
|
|
|
self.context.set_source_rgba(*color.rgba()) |
62
|
|
|
self.context.rectangle(x, y, width, height) |
63
|
|
|
self.context.fill() |
64
|
|
|
|
65
|
|
|
def circle(self, x, y, radius, color="default_color"): |
66
|
|
|
color = Color.from_user_input(color) |
67
|
|
|
|
68
|
|
|
self.context.set_source_rgba(*color.rgba()) |
69
|
|
|
self.context.arc(x, y, radius, 0, 3.141593*2) |
70
|
|
|
self.context.fill() |
71
|
|
|
|
72
|
|
|
def ellipse(self, x, y, width, height, color="default_color"): |
73
|
|
|
color = Color.from_user_input(color) |
74
|
|
|
display = pygame.display.get_surface() |
|
|
|
|
75
|
|
|
|
76
|
|
|
rect_x = int(x - width/2) |
77
|
|
|
rect_y = int(y - height/2) |
78
|
|
|
pygame.draw.ellipse(display, color.rgba(), (rect_x, rect_y, int(width), int(height))) |
79
|
|
|
|
80
|
|
|
def arc(self, x, y, radius, start_angle, stop_angle, color="default_color"): |
81
|
|
|
color = Color.from_user_input(color) |
82
|
|
|
|
83
|
|
|
self.context.set_source_rgba(*color.rgba()) |
84
|
|
|
self.context.set_line_width(1.4) |
85
|
|
|
self.context.arc(x, y, radius, start_angle, stop_angle) |
86
|
|
|
self.context.stroke() |
87
|
|
|
|