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
|
# -*- coding: utf-8 -*-
#Copyright (c) 2007-8, Playful Invention Company.
#Copyright (c) 2008-9, Walter Bender
#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
spr.ds_id = 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 setshapex(spr):
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:
if hasattr(spr, 'proto') and hasattr(spr.proto, 'name'):
name = spr.proto.name
else:
name = ""
spr.draw_label(spr,str(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
return True
def draw_label(spr, label, myscale, center_flag="False", vert_pos="middle"):
fd = pango.FontDescription('Sans')
fd.set_size(int(myscale*spr.tw.scale*pango.SCALE))
if type(label) == str or type(label) == unicode:
mylabel = label.replace("\0"," ")
l = len(mylabel)
pl = spr.tw.canvas.create_pango_layout(mylabel)
pl.set_font_description(fd)
if center_flag:
swidth = pl.get_size()[0]/pango.SCALE
centerx = spr.x+spr.width/2
x = int(centerx-swidth/2)
else:
x = spr.x+4 # small offset from left edge
sheight = pl.get_size()[1]/pango.SCALE
centery = spr.y+spr.height/2
if vert_pos == "middle":
y = int(centery-sheight/2)
elif vert_pos == "top":
y = int(sheight/2)
elif vert_pos == "bottom":
y = int(spr.height-sheight)
spr.tw.gc.set_foreground(spr.tw.msgcolor)
spr.tw.area.draw_layout(spr.tw.gc, x, y, pl)
else:
print type(label)
# used for sliders
def draw_label1(spr, label):
draw_label(spr, label, 12, True, "middle")
def draw_label2(spr, label):
draw_label(spr, str(label), 14, True, "top")
def draw_label3(spr, label):
draw_label(spr, str(label), 14, True, "bottom")
|