forked from valentinbasel/icaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.py
executable file
·190 lines (170 loc) · 5.74 KB
/
motor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# sin título.py
#
# Copyright 2013 valentin <valentin@localhost.localdomain>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import cairo
import math
class MotorCairo():
tipo_letra = "sans"
tama_letra = 12
color_conectado = (100, 80, 100)
color_no_conectado = (0, 0, 0)
def __init():
pass
def zoom(self, valor, cr):
scaler = cairo.Matrix()
scaler.scale(valor, valor)
cr.set_matrix(scaler)
def imagen(self, im, x, y, cr):
try:
img = cairo.ImageSurface.create_from_png(im)
except:
return 1
cr.set_source_surface(img, x, y)
cr.paint()
def color(self, rgb):
rgbfin = (
(rgb[0] / 255.0),
(rgb[1] / 255.0),
(rgb[2] / 255.0)
)
return rgbfin
def draw(self, x, y, h, w, rgb, cr):
cr.set_line_width(1)
rgbfin = self.color(rgb)
cr.set_source_rgba(rgbfin[0], rgbfin[1], rgbfin[2], 1)
cr.rectangle(x, y, h, w)
cr.fill()
def texto(self, txt, x, y, rgb, cr):
cr.select_font_face(
self.tipo_letra,
cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_BOLD
)
cr.set_font_size(self.tama_letra)
cr.move_to(x, y)
rgbfin = self.color(rgb)
cr.set_source_rgb(rgbfin[0], rgbfin[1], rgbfin[2],)
cr.show_text(txt)
# estas son nuevas funciones para trabajar con archivs svg
def parse_xy(self, cad, val):
try:
xy = cad[val].split(",")
xx = xy[0].encode('ascii', 'ignore')
yy = xy[1].encode('ascii', 'ignore')
x = float(xx)
y = float(yy)
except Exception, ex:
return 0, 0
return x, y
def rel_c(self, b, f, cr):
b = b + 1
x1, y1 = self.parse_xy(f, b)
b = b + 1
x2, y2 = self.parse_xy(f, b)
b = b + 1
x, y = self.parse_xy(f, b)
cr.rel_curve_to(x1, y1, x2, y2, x, y)
b = b + 1
return b
def rel_l(self, b, f, cr):
b = b + 1
x, y = self.parse_xy(f, b)
cr.rel_line_to(x, y)
b = b + 1
return b
def render_svg(self, cr, path_strings, color, x, y, color_fon):
cadena_svg = path_strings.split(" ")
b = 0
letr = ""
cr.move_to(x, y)
rgbfin = self.color(color)
rad = cairo.RadialGradient(x + 50, y + 50, 10, x + 50, y + 50, 200)
rad.add_color_stop_rgba(
1, color_fon[0], color_fon[1], color_fon[2], 1)
rad.add_color_stop_rgba(0, rgbfin[0], rgbfin[1], rgbfin[2], 1)
cr.set_source(rad)
cr.set_source_rgb(rgbfin[0],rgbfin[1],rgbfin[2])
while b < len(cadena_svg):
if (cadena_svg[b] == u'm'):
letr = cadena_svg[b]
b = b + 2
if (cadena_svg[b] == u'c'):
letr = cadena_svg[b]
b = self.rel_c(b, cadena_svg, cr)
elif (cadena_svg[b] == u'l'):
letr = cadena_svg[b]
b = self.rel_l(b, cadena_svg, cr)
else:
if letr == u'c':
b = self.rel_c(b - 1, cadena_svg, cr)
else:
b = self.rel_l(b - 1, cadena_svg, cr)
cr.set_line_join(cairo.LINE_JOIN_ROUND)
cr.close_path()
cr.fill()
cr.stroke()
return 0
def crear_nodo_linea(self, x, y):
cadena = " l %s,%s " % (float(x), float(y))
return cadena
def collide(sef, rectan, x, y):
if (
x > rectan[0] and
x < rectan[0] + rectan[2]
):
if (
y > rectan[1] and
y < rectan[1] + rectan[3]
):
return True
else:
return False
def colliderect(sef, rectan, rectan2):
if rectan2[0] > rectan[0] and rectan2[0] < rectan[0] + rectan[2]:
if rectan2[1] > rectan[1] and rectan2[1] < rectan[1] + rectan[2]:
return True
if rectan2[0] + rectan2[2] > rectan[0] and rectan2[0] + rectan2[2] < rectan[0] + rectan[2]:
if rectan2[1] > rectan[1] and rectan2[1] < rectan[1] + rectan[2]:
return True
if rectan[0] > rectan2[0] and rectan[0] < rectan2[0] + rectan2[2]:
if rectan[1] > rectan2[1] and rectan[1] < rectan2[1] + rectan2[2]:
return True
if rectan[0] + rectan[2] > rectan2[0] and rectan[0] + rectan[2] < rectan2[0] + rectan2[2]:
if rectan[1] > rectan2[1] and rectan[1] < rectan2[1] + rectan2[2]:
return True
else:
return False
def line(self, x, y, cr):
cr.set_line_width(3)
cr.set_source_rgb(1, 1, 1)
cr.line_to(x, y)
cr.close_path()
cr.stroke_preserve()
def circle(self,x,y,cr,color):
cr.move_to(x,y)
cr.set_line_width(1)
cr.set_source_rgb(color[0],color[1],color[2])
cr.arc(x, y, 4, 0, 2*math.pi)
cr.close_path()
cr.stroke_preserve()
cr.fill()