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
|
#Copyright (c) 2007-8, Playful Invention Company.
#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
import gobject
import pango
class taSprite: pass
def findsprite(tw,pos):
list = tw.sprites[:]
list.reverse()
for s in list:
if hit(s,pos): return s
return None
def redrawsprites(tw):
for s in tw.sprites: draw(s)
def sprNew(tw,x,y,image,altlabel=False):
spr = taSprite()
spr.tw, spr.x, spr.y = tw,x,y
setimage(spr,image)
spr.label = None
if altlabel: spr.draw_label = draw_label2
else: spr.draw_label = draw_label1
return spr
def setimage(spr,image):
spr.image = image
if isinstance(image,gtk.gdk.Pixbuf):
spr.width = image.get_width()
spr.height = image.get_height()
else: spr.width,spr.height=image.get_size()
def move(spr,pos):
inval(spr)
spr.x,spr.y = pos
inval(spr)
def setshape(spr,image):
inval(spr)
setimage(spr,image)
inval(spr)
def setlayer(spr, layer):
sprites = spr.tw.sprites
if spr in sprites: sprites.remove(spr)
spr.layer = layer
for i in range(len(sprites)):
if layer < sprites[i].layer: sprites.insert(i, spr); inval(spr); return
sprites.append(spr)
inval(spr)
def hide(spr):
if spr not in spr.tw.sprites: return
inval(spr)
spr.tw.sprites.remove(spr)
def setlabel(spr,label):
spr.label = label
inval(spr)
def inval(spr):
spr.tw.area.invalidate_rect(gtk.gdk.Rectangle(spr.x,spr.y,spr.width,spr.height), False)
def draw(spr):
if isinstance(spr.image,gtk.gdk.Pixbuf):
spr.tw.area.draw_pixbuf(spr.tw.gc, spr.image, 0, 0, spr.x, spr.y)
else: spr.tw.area.draw_drawable(spr.tw.gc,spr.image,0,0,spr.x,spr.y,-1,-1)
if spr.label!=None: spr.draw_label(spr,spr.label)
def hit(spr,pos):
x,y = pos
if x<spr.x: return False
if x>spr.x+spr.width: return False
if y<spr.y: return False
if y>spr.y+spr.height: return False
if isinstance(spr.image,gtk.gdk.Pixmap): return True
dx,dy = x-spr.x, y-spr.y
return ord(spr.image.get_pixels()[(dy*spr.width+dx)*4+3]) == 255
def draw_label1(spr, label):
fd = pango.FontDescription('Sans')
fd.set_size(7*pango.SCALE)
pl = spr.tw.window.create_pango_layout(str(label))
pl.set_font_description(fd)
swidth = pl.get_size()[0]/pango.SCALE
sheight = pl.get_size()[1]/pango.SCALE
centerx = spr.x+spr.width/2
centery = spr.y+spr.height/2
spr.tw.gc.set_foreground(spr.tw.textcolor)
spr.tw.area.draw_layout(spr.tw.gc,centerx-swidth/2,centery-sheight/2,pl)
def draw_label2(spr, label):
fd = pango.FontDescription('Sans')
fd.set_size(9*pango.SCALE)
pl = spr.tw.window.create_pango_layout(str(label))
pl.set_font_description(fd)
sheight = pl.get_size()[1]/pango.SCALE
centery = spr.y+spr.height/2
spr.tw.gc.set_foreground(spr.tw.textcolor)
spr.tw.area.draw_layout(spr.tw.gc,spr.x+70,centery-sheight/2,pl)
def getpixel(image,x,y):
array = image.get_pixels()
offset = (y*image.get_width()+x)*4
r,g,b,a = ord(array[offset]),ord(array[offset+1]),ord(array[offset+2]),ord(array[offset+3])
return (a<<24)+(b<<16)+(g<<8)+r
|