-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcairoEng.py
More file actions
executable file
·266 lines (223 loc) · 7.84 KB
/
Copy pathcairoEng.py
File metadata and controls
executable file
·266 lines (223 loc) · 7.84 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python2
##
# This file is part of pyFidget, licensed under the MIT License (MIT).
#
# Copyright (c) 2016 Wolf480pl <wolf480@interia.pl>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
##
import pygtk
pygtk.require('2.0')
import gtk, gobject, cairo
from time import time, sleep
import threading
def mkMatrix(p0, p1, p2, size):
w, h = size
w, h = float(w), float(h)
x0, y0 = p0
if p1:
x1, y1 = p1
dx1, dy1 = x1 - x0, y1 - y0
ex1, ey1 = dx1 / w, dy1 / w
else:
ex1, ey1 = 1, 0
if p2:
x2, y2 = p2
dx2, dy2 = x2 - x0, y2 - y0
ex2, ey2 = dx2 / h, dy2 / h
else:
ex2, ey2 = 0, 1
return cairo.Matrix(ex1, ey1,
ex2, ey2,
x0 , y0 )
def toShapeMap(surface):
width = surface.get_width()
height = surface.get_height()
bitmap = gtk.gdk.Pixmap(None, width, height, 1)
bmpCr = bitmap.cairo_create()
patt = cairo.SurfacePattern(surface)
bmpCr.set_source(patt)
bmpCr.set_operator(cairo.OPERATOR_SOURCE)
bmpCr.paint()
return bmpCr, bitmap
class Screen(gtk.DrawingArea):
# Draw in response to an expose-event
__gsignals__ = { "expose-event": "override" }
_time = time()
_shapemap = None
def __init__(self, animation, texture, getFrameRect, offset):
gtk.DrawingArea.__init__(self)
self._fidget = animation
self._texture = cairo.ImageSurface.create_from_png(texture)
self._getFrameRect = getFrameRect
self._patt = cairo.SurfacePattern(self._texture)
self._offset = offset
# Handle the expose-event by drawing
def do_expose_event(self, event):
if not hasattr(self, 'bg') :
if self.is_composited():
print("Composite! \o/")
self.bg = None
else:
self.bg = capt_screen(self)
# Create the cairo context
cr = self.window.cairo_create()
# Restrict Cairo to the exposed area; avoid extra work
cr.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
cr.clip()
self.draw(cr, *self.window.get_size())
def draw(self, cr, width, height):
t = time()
dt = t - self._time
self._time = t
dtmill = int(dt * 1000)
if (dtmill > 20000):
# We're realy late, waaay behind the schedule
# There's no way we're gonna catch up
# But guess what? It doesn't matter!
# We can just set dt to something low, and the next frame will be correct
print("warning: skipping a very long tick: %d" % dtmill)
dtmill = 1
self._fidget.update(dtmill)
self.clear(cr)
cr.save()
cr.translate(*self._offset)
if hasattr(self._fidget, 'transforms'):
for mtx in self._fidget.transforms():
cr.transform(cairo.Matrix(*mtx))
for state in self._fidget.state():
cr.save()
mtx = self._patt.get_matrix()
self.drawState(cr, state)
self._patt.set_matrix(mtx)
cr.restore()
cr.restore()
tgtSurface = cr.get_target()
bmpCr, shapemap = toShapeMap(tgtSurface)
self._shapemap = shapemap
#dbgPatt = cairo.SurfacePattern(bmpCr.get_target())
#cr.set_source(dbgPatt)
#cr.set_operator(cairo.OPERATOR_SOURCE)
#cr.paint()
def shapemap(self):
return self._shapemap
def clear(self, cr):
cr.save()
cr.set_operator(cairo.OPERATOR_SOURCE)
if self.bg:
bg = cairo.SurfacePattern(self.bg)
cr.set_source(bg)
else:
cr.set_source_rgba(0, 0, 0, 0)
cr.paint()
cr.restore()
def drawState(self, cr, state):
((x, y), f, p1, p2) = state
(sx, sy, w, h) = self._getFrameRect(f)
surf = self._patt
mdst = mkMatrix((x, y), p1, p2, (w, h))
cr.transform(mdst)
m = cairo.Matrix()
m.translate(sx, sy)
surf.set_matrix(m)
cr.set_source(surf)
cr.rectangle(0, 0, w, h)
cr.fill()
class Refresher(threading.Thread):
def __init__(self, window):
threading.Thread.__init__(self)
self.setDaemon(True)
self.window = window
def run(self):
fps = 60
tick = 1.0 / fps
while True:
sleep(tick)
gtk.threads_enter()
self.window.queue_draw()
self.window.queue_resize()
gtk.threads_leave()
# GTK mumbo-jumbo to show the widget in a window and quit when it's closed
def run(animation, texture, getFrameRect, size=(200, 200), offset=(0, 0)):
gtk.threads_init()
gtk.threads_enter()
window = gtk.Window()
widget = Screen(animation, texture, getFrameRect, offset)
widget.show()
def on_size_allocate(wind, rect):
#print("walloc")
shapemap = widget.shapemap()
if shapemap:
window.input_shape_combine_mask(shapemap, 0, 0)
#window.reset_shapes()
#print("walloc with bitmap")
window.connect("delete-event", gtk.main_quit)
window.connect("size-allocate", on_size_allocate)
window.add(widget)
window.set_decorated(False)
window.set_skip_taskbar_hint(True)
window.set_skip_pager_hint(True)
window.set_keep_above(True)
window.stick()
window.set_default_size(*size)
colormap = window.get_screen().get_rgba_colormap()
gtk.widget_set_default_colormap(colormap)
window.present()
refresher = Refresher(window)
refresher.start()
try:
gtk.main()
finally:
gtk.threads_leave()
def rgb24to32(data):
itr = iter(data)
out = bytearray()
try:
while True:
r = next(itr)
g = next(itr)
b = next(itr)
out.append(b)
out.append(g)
out.append(r)
out.append(0)
except StopIteration:
pass
return out
def capt_screen(widget):
x, y = widget.window.get_position()
win = gtk.gdk.get_default_root_window()
w, h = win.get_size()
pb = gtk.gdk.Pixbuf(0 , False, 8, w, h)
widget.hide()
pb = pb.get_from_drawable(win, win.get_colormap(), 0, 0, 0, 0, w, h)
widget.show_all()
if (pb != None):
myw, myh = 512, 300
pb = pb.subpixbuf(x, y, myw, myh)
w, h = myw * 2, myh / 2
format = cairo.FORMAT_ARGB32
stride = cairo.ImageSurface.format_stride_for_width(format, w)
im = cairo.ImageSurface.create_for_data(rgb24to32(pb.get_pixels()), format, w, h, stride)
return im
import fidget
if __name__ == "__main__":
print("This way of starting Fidget is deprecated. Please run cairoFidget.py instead.")
run(fidget.Fidget(), "fidget-sprites.png", fidget.getFrameRect, (121, 121), (-53, -17))