Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit 1e00234eb98fd7e6839048c465fb08bfc5a4325e

  • avatar
  • Raúl Gutiérrez S <rgs @paragu…duca.org>
  • Tue Dec 08 15:35:51 EST 2009
  • Tree SHA1: 1711ed1
  • Parent SHA1: b0c68db (tawindow now has a class where all of its functionality is encapsulated. TODO: define how taproject and tawindow relate.)
  • raw diff | raw patch
More cleanup. Now we have the Sprite class where all the selected, copied, paste, etc. logic should go. Cleaned the corresponding client code as well, not thoroughfully tested , though
talogo.py
(9 / 9)
  
158158 return res
159159
160160def setup_cmd(lc, str):
161 setlayer(lc.tw.turtle.spr,100)
161 lc.tw.turtle.spr.setlayer(100)
162162 lc.procstop=False
163163 list = readline(lc, str)
164164 lc.step = start_eval(lc, list)
176176 lc.arglist = None
177177 while lc.iline:
178178 if lc.tw.step_time > 0:
179 setlayer(lc.tw.turtle.spr,630)
179 lc.tw.turtle.spr.setlayer(630)
180180 endtime = millis()+an_int(lc,lc.tw.step_time)*100
181181 while millis()<endtime:
182182 yield True
183 setlayer(lc.tw.turtle.spr,100)
183 lc.tw.turtle.spr.setlayer(100)
184184 token = lc.iline[0]
185185 if token==lc.symopar: token=lc.iline[1]
186186 icall(lc, eval); yield True
274274 shp = 'info'
275275 setshape(lc.tw.status_spr, lc.tw.status_shapes[shp])
276276 setlabel(lc.tw.status_spr, _(my_string))
277 setlayer(lc.tw.status_spr, 710)
277 lc.tw.status_spr.setlayer(710)
278278 return
279279
280280def undefined_check(lc, token):
286286 raise logoerror("#noinput")
287287
288288def prim_wait(lc,time):
289 setlayer(lc.tw.turtle.spr,630)
289 lc.tw.turtle.spr.setlayer(630)
290290 endtime = millis()+an_int(lc,time*1000)
291291 while millis()<endtime:
292292 yield True
293 setlayer(lc.tw.turtle.spr,100)
293 lc.tw.turtle.spr.setlayer(100)
294294 ireturn(lc); yield True
295295
296296def prim_repeat(lc, num, list):
989989 try:
990990 lc.step.next()
991991 except StopIteration:
992 setlayer(lc.tw.turtle.spr,630)
992 lc.tw.turtle.spr.setlayer(630)
993993 return False
994994 except logoerror, e:
995995 showlabel(lc, str(e)[1:-1])
996 setlayer(lc.tw.turtle.spr,630)
996 lc.tw.turtle.spr.setlayer(630)
997997 return False
998998 return True
999999
10691069 shp = 'status'
10701070 setshape(lc.tw.status_spr, lc.tw.status_shapes[shp])
10711071 setlabel(lc.tw.status_spr, label)
1072 setlayer(lc.tw.status_spr, 710)
1072 lc.tw.status_spr.setlayer(710)
10731073
10741074def stop_logo(tw):
10751075 tw.step_time = 0
  
5959def new_project(tw):
6060 stop_logo(tw)
6161 for b in blocks(tw): hide(b)
62 setlayer(tw.turtle.canvas, 600)
62 tw.turtle.canvas.setlayer(600)
6363 clearscreen(tw.turtle)
6464 tw.save_file_name = None
6565
162162 media = label
163163 label = None
164164 proto = tw.protodict[btype]
165 spr = sprNew(tw,b[2]+tw.turtle.canvas.x,b[3]+tw.turtle.canvas.y, \
165 spr = Sprite(tw,b[2]+tw.turtle.canvas.x,b[3]+tw.turtle.canvas.y, \
166166 proto.image)
167167 spr.type = 'block'
168168 spr.proto = proto
181181 dsobject.destroy()
182182 except:
183183 print "couldn't open dsobject (" + str(spr.ds_id) + ")"
184 setlayer(spr,650)
184 spr.setlayer(650)
185185 return spr
186186
187187def load_turtle(tw,b):
tasetup.py
(15 / 15)
  
262262 tw.selbuttons.append(cat)
263263
264264def setup_misc(tw):
265 tw.category_spr = sprNew(tw,0, 0, tw.selbuttons[0].group)
265 tw.category_spr = Sprite(tw,0, 0, tw.selbuttons[0].group)
266266 tw.category_spr.type = 'category'
267 setlayer(tw.category_spr,660)
267 tw.category_spr.setlayer(660)
268268 # masks get positioned on top of other blocks
269 tw.select_mask = sprNew(tw,100,100,\
269 tw.select_mask = Sprite(tw,100,100,\
270270 load_image(tw, tw.path, '', 'masknumber'))
271271 tw.select_mask.type = 'selectmask'
272 tw.select_mask_string = sprNew(tw,100,100,load_image(tw, tw.path, '', \
272 tw.select_mask_string = Sprite(tw,100,100,load_image(tw, tw.path, '', \
273273 'maskstring'))
274274 tw.select_mask_string.type = 'selectmask'
275275 # used to hide the palette
284284 tw.media_shapes['pythonloaded'] = \
285285 load_image(tw, tw.path_lang, 'sensors', 'nop-loaded')
286286 # coordinare systems
287 tw.cartesian_coordinates_spr = sprNew(tw, tw.width/2-600, tw.height/2-450, \
287 tw.cartesian_coordinates_spr = Sprite(tw, tw.width/2-600, tw.height/2-450, \
288288 load_image(tw, tw.path, '', "Cartesian"))
289289 tw.cartesian_coordinates_spr.type = 'coordinates'
290 setlayer(tw.cartesian_coordinates_spr, 610)
291 hide(tw.cartesian_coordinates_spr)
292 tw.polar_coordinates_spr = sprNew(tw, tw.width/2-600, tw.height/2-450, \
290 tw.cartesian_coordinates_spr.setlayer(610)
291 tw.cartesian_coordinates_spr.hide()
292 tw.polar_coordinates_spr = Sprite(tw, tw.width/2-600, tw.height/2-450, \
293293 load_image(tw, tw.path, '', "polar"))
294294 tw.polar_coordinates_spr.type = 'coordinates'
295 setlayer(tw.polar_coordinates_spr, 610)
296 hide(tw.polar_coordinates_spr)
295 tw.polar_coordinates_spr.setlayer(610)
296 tw.polar_coordinates_spr.hide()
297297 # status shapes get positioned at the bottom of the screen
298298 tw.status_shapes = {}
299299 tw.status_shapes['status'] = load_image(tw, tw.path, '', 'status')
307307 tw.status_shapes['overflowerror'] = \
308308 load_image(tw, tw.path, '', 'overflowerror')
309309 tw.status_shapes['syntaxerror'] = load_image(tw, tw.path, '', 'syntaxerror')
310 tw.status_spr = sprNew(tw,0,(tw.height-175), \
310 tw.status_spr = Sprite(tw,0,(tw.height-175), \
311311 tw.status_shapes['status'],True)
312312 tw.status_spr.type = 'status'
313 setlayer(tw.status_spr,900)
314 hide(tw.status_spr)
313 tw.status_spr.setlayer(900)
314 tw.status_spr.hide()
315315 # everything should be loaded at this point
316316 # print tw.status_shapes
317317 tw.loaded = True
320320 # selector tabs
321321 offshape = load_image(tw, tw.path, 'palette', name+'off')
322322 onshape = load_image(tw, tw.path, 'palette', name+'on')
323 spr = sprNew(tw,143,y,offshape)
324 setlayer(spr,800)
323 spr = Sprite(tw,143,y,offshape)
324 spr.setlayer(800)
325325 spr.offshape = offshape
326326 spr.onshape = onshape
327327 # print 'setting up selector ' + name
tasprites.py
(128 / 116)
  
2626import gtk
2727import gobject
2828import pango
29class taSprite: pass
29import tasetup
3030
3131# Don't display the label for these blocks
3232nolabel = ['audiooff', 'descriptionoff','journal']
3535 list = tw.sprites[:]
3636 list.reverse()
3737 for s in list:
38 if hit(s,pos): return s
38 if s.hit(pos): return s
3939 return None
4040
4141def redrawsprites(tw):
42 for s in tw.sprites: draw(s)
42 for s in tw.sprites: s.draw()
4343
44def sprNew(tw,x,y,image,altlabel=False):
45 spr = taSprite()
46 spr.tw, spr.x, spr.y = tw,x,y
47 setimage(spr,image)
48 spr.label = None
49 spr.ds_id = None
50 if altlabel:
51 spr.draw_label = draw_label2
52 else: spr.draw_label = draw_label1
53 return spr
5444
55def setimage(spr,image):
56 spr.image = image
57 if isinstance(image,gtk.gdk.Pixbuf):
58 spr.width = image.get_width()
59 spr.height = image.get_height()
60 else: spr.width,spr.height=image.get_size()
45class Sprite():
6146
62def move(spr,pos):
63 inval(spr)
64 spr.x,spr.y = pos
65 inval(spr)
47 def __init__(self, tw, x, y, image, altlabel=False):
48 self.tw, self.x, self.y = tw,x,y
49 self.setimage(image)
50 self.label = None
51 self.ds_id = None
52 if altlabel:
53 self.draw_label = self.draw_label2
54 else: self.draw_label = self.draw_label1
6655
67def setshape(spr,image):
68 inval(spr)
69 setimage(spr,image)
70 inval(spr)
56 '''
57 mark block as selected or un-selected
58 FIXME: how we'll we create the images for selected blocks
59 '''
60 def set_selected(self, selected):
61 if selected:
62 img = tasetup.load_image(self.tw, self.tw.path, '', 'audioon')
63 else:
64 img = tasetup.load_image(self.tw, self.tw.path, '', 'audioon')
65 self.setimage(img)
7166
72def setshapex(spr):
73 inval(spr)
67 def setimage(self, image):
68 self.image = image
69 if isinstance(image,gtk.gdk.Pixbuf):
70 self.width = image.get_width()
71 self.height = image.get_height()
72 else: self.width,self.height=image.get_size()
7473
75def setlayer(spr, layer):
76 sprites = spr.tw.sprites
77 if spr in sprites: sprites.remove(spr)
78 spr.layer = layer
79 for i in range(len(sprites)):
80 if layer < sprites[i].layer:
81 sprites.insert(i, spr)
82 inval(spr)
83 return
84 sprites.append(spr)
85 inval(spr)
74 def move(self, pos):
75 self.inval()
76 self.x,self.y = pos
77 self.inval()
8678
87def hide(spr):
88 if spr not in spr.tw.sprites: return
89 inval(spr)
90 spr.tw.sprites.remove(spr)
79 def inval(self):
80 rect = gtk.gdk.Rectangle(self.x, self.y, self.width, self.height)
81 self.tw.area.invalidate_rect(rect, False)
9182
92def setlabel(spr,label):
93 spr.label = label
94 inval(spr)
83 def setshape(self, image):
84 self.inval()
85 self.setimage(image)
86 self.inval()
9587
96def inval(spr):
97 spr.tw.area.invalidate_rect(gtk.gdk.Rectangle(spr.x,spr.y,spr.width, \
98 spr.height), False)
88 def setshapex(self):
89 self.inval()
9990
100def draw(spr):
101 if isinstance(spr.image,gtk.gdk.Pixbuf):
102 spr.tw.area.draw_pixbuf(spr.tw.gc, spr.image, 0, 0, spr.x, spr.y)
103 else:
104 spr.tw.area.draw_drawable(spr.tw.gc,spr.image,0,0,spr.x,spr.y,-1,-1)
105 if spr.label!=None:
106 if hasattr(spr, 'proto') and hasattr(spr.proto, 'name'):
107 name = spr.proto.name
91 def setlayer(self, layer):
92 sprites = self.tw.sprites
93 if self in sprites: sprites.remove(self)
94 self.layer = layer
95 for i in range(len(sprites)):
96 if layer < sprites[i].layer:
97 sprites.insert(i, self)
98 self.inval()
99 return
100 sprites.append(self)
101 self.inval()
102
103 def hide(self):
104 if self not in self.tw.sprites: return
105 self.inval()
106 self.tw.sprites.remove(self)
107
108 def setlabel(self, label):
109 self.label = label
110 self.inval()
111
112 def draw(self):
113 if isinstance(self.image,gtk.gdk.Pixbuf):
114 self.tw.area.draw_pixbuf(self.tw.gc, self.image, 0, 0, self.x, self.y)
108115 else:
109 name = ""
110 if name not in nolabel:
111 spr.draw_label(spr,str(spr.label))
116 self.tw.area.draw_drawable(self.tw.gc,self.image,0,0,self.x,self.y,-1,-1)
117 if self.label!=None:
118 if hasattr(self, 'proto') and hasattr(self.proto, 'name'):
119 name = self.proto.name
120 else:
121 name = ""
122 if name not in nolabel:
123 self.draw_label1(str(self.label))
112124
113def hit(spr,pos):
114 x, y = pos
115 if x<spr.x: return False
116 if x>spr.x+spr.width-1: return False
117 if y<spr.y: return False
118 if y>spr.y+spr.height-1: return False
119 if isinstance(spr.image,gtk.gdk.Pixmap): return True
120 if hasattr(spr, 'proto') and hasattr(spr.proto, 'name') and \
121 spr.proto.name == 'journal':
125 def hit(self, pos):
126 x, y = pos
127 if x<self.x: return False
128 if x>self.x+self.width-1: return False
129 if y<self.y: return False
130 if y>self.y+self.height-1: return False
131 if isinstance(self.image,gtk.gdk.Pixmap): return True
132 if hasattr(self, 'proto') and hasattr(self.proto, 'name') and \
133 self.proto.name == 'journal':
134 return True
135 dx, dy = x-self.x, y-self.y
136 try:
137 return ord(self.image.get_pixels()[(dy*self.width+dx)*4+3]) == 255
138 except IndexError:
139 if hasattr(spr, 'proto') and hasattr(self.proto, 'name'):
140 print self.proto.name
141 print "IndexError: string index out of range: " + str(dx) + " " \
142 + str(dy) + " " + str(self.width) + " " + str(self.height)
122143 return True
123 dx, dy = x-spr.x, y-spr.y
124 try:
125 return ord(spr.image.get_pixels()[(dy*spr.width+dx)*4+3]) == 255
126 except IndexError:
127 if hasattr(spr, 'proto') and hasattr(spr.proto, 'name'):
128 print spr.proto.name
129 print "IndexError: string index out of range: " + str(dx) + " " \
130 + str(dy) + " " + str(spr.width) + " " + str(spr.height)
131 return True
132144
133def draw_label(spr, label, myscale, center_flag, truncate_flag):
134 fd = pango.FontDescription('Sans')
135 fd.set_size(int(myscale*spr.tw.scale*pango.SCALE))
136 if type(label) == str or type(label) == unicode:
137 mylabel = label.replace("\0"," ")
138 l = len(mylabel)
139 if truncate_flag and l > 8:
140 pl = spr.tw.window.create_pango_layout("..."+mylabel[l-8:])
145 def real_draw_label(self, label, myscale, center_flag, truncate_flag):
146 fd = pango.FontDescription('Sans')
147 fd.set_size(int(myscale*self.tw.scale*pango.SCALE))
148 if type(label) == str or type(label) == unicode:
149 mylabel = label.replace("\0"," ")
150 l = len(mylabel)
151 if truncate_flag and l > 8:
152 pl = self.tw.window.create_pango_layout("..."+mylabel[l-8:])
153 else:
154 pl = self.tw.window.create_pango_layout(mylabel)
155 pl.set_font_description(fd)
156 if center_flag:
157 swidth = pl.get_size()[0]/pango.SCALE
158 centerx = self.x+self.width/2
159 x = int(centerx-swidth/2)
160 else:
161 x = self.x+70
162 sheight = pl.get_size()[1]/pango.SCALE
163 centery = self.y+self.height/2
164 y = int(centery-sheight/2)
165 self.tw.gc.set_foreground(self.tw.msgcolor)
166 self.tw.area.draw_layout(self.tw.gc, x, y, pl)
141167 else:
142 pl = spr.tw.window.create_pango_layout(mylabel)
143 pl.set_font_description(fd)
144 if center_flag:
145 swidth = pl.get_size()[0]/pango.SCALE
146 centerx = spr.x+spr.width/2
147 x = int(centerx-swidth/2)
148 else:
149 x = spr.x+70
150 sheight = pl.get_size()[1]/pango.SCALE
151 centery = spr.y+spr.height/2
152 y = int(centery-sheight/2)
153 spr.tw.gc.set_foreground(spr.tw.msgcolor)
154 spr.tw.area.draw_layout(spr.tw.gc, x, y, pl)
155 else:
156 print type(label)
168 print type(label)
157169
158# used for most things
159def draw_label1(spr, label):
160 draw_label(spr, label, 7, True, True)
170 # used for most things
171 def draw_label1(self, label):
172 self.real_draw_label(label, 7, True, True)
173
174 # used for status blocks
175 def draw_label2(self, label):
176 self.real_draw_label(str(label), 9, False, False)
161177
162# used for status blocks
163def draw_label2(spr, label):
164 draw_label(spr, str(label), 9, False, False)
165
166# used to get pixel value from mask for category selector
167def getpixel(image,x,y):
168 array = image.get_pixels()
169 offset = (y*image.get_width()+x)*4
170 r,g,b,a = ord(array[offset]),ord(array[offset+1]),ord(array[offset+2]), \
171 ord(array[offset+3])
172 return (a<<24)+(b<<16)+(g<<8)+r
178 # used to get pixel value from mask for category selector
179 def getpixel(self, image,x,y):
180 array = image.get_pixels()
181 offset = (y*image.get_width()+x)*4
182 r,g,b,a = ord(array[offset]),ord(array[offset+1]),ord(array[offset+2]), \
183 ord(array[offset+3])
184 return (a<<24)+(b<<16)+(g<<8)+r
  
5454def tNew(tw,w,h):
5555 t = taTurtle()
5656 t.tw, t.width, t.height = tw, w, h
57 t.canvas = sprNew(tw,0,0,gtk.gdk.Pixmap(tw.area,w,h,-1))
57 t.canvas = Sprite(tw,0,0,gtk.gdk.Pixmap(tw.area,w,h,-1))
5858 t.canvas.type = 'canvas'
59 setlayer(t.canvas,600)
59 t.canvas.setlayer(600)
6060 t.shapelist = \
6161 [load_image(tw, tw.path, 'shapes','t'+str(i)) for i in range(36)]
62 t.spr = sprNew(tw,100,100,t.shapelist[0])
62 t.spr = Sprite(tw,100,100,t.shapelist[0])
6363 t.spr.type = 'turtle'
64 setlayer(t.spr, 630)
64 t.spr.setlayer(630)
6565 t.gc = t.canvas.image.new_gc()
6666 t.shade = 0
6767 clearscreen(t)
292292 h+t.pensize*t.tw.coord_scale+6)
293293
294294def turn_turtle(t):
295 setshape(t.spr, t.shapelist[(int(t.heading+5)%360)/10])
295 t.spr.setshape(t.shapelist[(int(t.heading+5)%360)/10])
296296
297297def move_turtle(t):
298298 x,y = t.width/2+int(t.xcor), t.height/2-int(t.ycor)
299 move(t.spr, (t.canvas.x+x-30,t.canvas.y+y-30))
299 t.spr.move((t.canvas.x+x-30,t.canvas.y+y-30))
300300 invalt(t,x-30,y-30,60,60)
301301
302302def invalt(t,x,y,w,h):
tawindow.py
(121 / 123)
  
2323
2424
2525# TODO:
26# - better comments!
27# - many methods could have their logic simplified!
2628# - we need a method to know if we are running inside Sugar (vs. stand-alone)
27# - we need helper methods to discriminate what XO version we are using (if any)
2829# - verbose flag should be in the scope of the object instance
29# - better comments!
30#
3130
3231
3332import pygtk
3838import os.path
3939import time
4040
41# Import from Journal for these blocks
42importblocks = ['audiooff', 'descriptionoff','journal']
43
44
4541from math import atan2, pi
4642DEGTOR = 2*pi/360
4743
5454from tahoverhelp import *
5555from gettext import gettext as _
5656
57# dead key dictionaries
58dead_grave = {'A':192,'E':200,'I':204,'O':210,'U':217,'a':224,'e':232,'i':236,\
59 'o':242,'u':249}
60dead_acute = {'A':193,'E':201,'I':205,'O':211,'U':218,'a':225,'e':233,'i':237,\
61 'o':243,'u':250}
62dead_circumflex = {'A':194,'E':202,'I':206,'O':212,'U':219,'a':226,'e':234,\
63 'i':238,'o':244,'u':251}
64dead_tilde = {'A':195,'O':211,'N':209,'U':360,'a':227,'o':245,'n':241,'u':361}
65dead_diaeresis = {'A':196,'E':203,'I':207,'O':211,'U':218,'a':228,'e':235,\
66 'i':239,'o':245,'u':252}
67dead_abovering = {'A':197,'a':229}
6857
69# Time out for triggering help
70timeout_tag = [0]
71
72
73
7458"""
7559TurtleArt Window class abstraction
7660"""
7761class TurtleArtWindow():
7862
63 # Import from Journal for these blocks
64 importblocks = ['audiooff', 'descriptionoff','journal']
65
66 # dead key dictionaries
67 dead_grave = {'A':192,'E':200,'I':204,'O':210,'U':217,'a':224,'e':232,'i':236,\
68 'o':242,'u':249}
69 dead_acute = {'A':193,'E':201,'I':205,'O':211,'U':218,'a':225,'e':233,'i':237,\
70 'o':243,'u':250}
71 dead_circumflex = {'A':194,'E':202,'I':206,'O':212,'U':219,'a':226,'e':234,\
72 'i':238,'o':244,'u':251}
73 dead_tilde = {'A':195,'O':211,'N':209,'U':360,'a':227,'o':245,'n':241,'u':361}
74 dead_diaeresis = {'A':196,'E':203,'I':207,'O':211,'U':218,'a':228,'e':235,\
75 'i':239,'o':245,'u':252}
76 dead_abovering = {'A':197,'a':229}
77
78 # Time out for triggering help
79 timeout_tag = [0]
80
81
7982 def __init__(self, win, path, lang, parent=None):
83 self._setup_initial_values(win, path, lang, parent)
84 prep_selectors(self) # i wonder where this method belongs
85 for s in selectors:
86 setup_selectors(self,s)
87 setup_misc(self)
88 self._select_category(self.selbuttons[0])
89
90
91 def _setup_initial_values(self, win, path, lang, parent):
8092 self.window = win
8193 self.path = os.path.join(path,'images')
8294 self.path_lang = os.path.join(path,'images',lang)
9696 self.load_save_folder = os.path.join(path,'samples')
9797 self.save_folder = None
9898 self.save_file_name = None
99 win.set_flags(gtk.CAN_FOCUS)
99 self.window.set_flags(gtk.CAN_FOCUS)
100100 self.width = gtk.gdk.screen_width()
101101 self.height = gtk.gdk.screen_height()
102102
103103 # starting from command line
104104 if parent is None:
105 win.show_all()
105 self.window.show_all()
106106 # starting from Sugar
107107 else:
108108 parent.show_all()
115115 self.area = self.window.window
116116 self.gc = self.area.new_gc()
117117 # on an OLPC-XO-1, there is a scaling factor
118 if os.path.exists('/etc/olpc-release') or \
119 os.path.exists('/sys/power/olpc-pm'):
118 if self._is_XO_1():
120119 self.lead = 1.6
121120 self.scale = 1.0
122121 else:
131131 self.sprites = []
132132 self.selected_block = None
133133 self.draggroup = None
134 prep_selectors(self)
135134 self.myblock = None
136135 self.nop = 'nop'
137136 self.loaded = 0
138 for s in selectors:
139 setup_selectors(self,s)
140 setup_misc(self)
141137 self.step_time = 0
142138 self.hide = False
143139 self.palette = True
144 self._select_category(self.selbuttons[0])
145140 self.coord_scale = 1
146141 self.turtle = tNew(self,self.width,self.height)
147142 self.lc = lcNew(self)
147147 self.polar = False
148148 self.spr = None # "currently selected spr"
149149
150
151150 """
152151 DEPRECATED
153152 """
157157 eraser_button: hide status block
158158 """
159159 def eraser_button(self):
160 setlayer(self.status_spr,400)
160 self.status_spr.setlayer(400)
161161 clear(self.lc)
162162 display_coordinates(self)
163163
167167 def stop_button(self):
168168 stop_logo(self)
169169
170
171170 """
172171 change the icon for user-defined blocks after Python code is loaded
173172 """
177177 setimage(spr,self.media_shapes['pythonloaded'])
178178 self.nop = 'pythonloaded'
179179
180
181180 """
182181 hideshow button
183182 """
184183 def hideshow_button(self):
185184 if self.hide is False:
186 for b in self._blocks(): setlayer(b,100)
185 for b in self._blocks(): b.setlayer(100)
187186 self._hide_palette()
188 hide(self.select_mask)
189 hide(self.select_mask_string)
187 self.select_mask.hide()
188 self.select_mask_string.hide()
190189 self.hide = True
191190 else:
192 for b in self._blocks(): setlayer(b,650)
191 for b in self._blocks(): b.setlayer(650)
193192 self.show_palette()
194193 self.hide = False
195194 inval(self.turtle.canvas)
225225 if self.selected_block != None:
226226 self._unselect()
227227 else:
228 setlayer(self.status_spr,400)
228 self.status_spr.setlayer(400)
229229 spr = findsprite(self,(x,y))
230230 self.x, self.y = x,y
231231 self.dx = 0
244244 self._turtle_pressed(x,y)
245245 self.spr = spr
246246
247
248247 """
249248 hideshow_palette
250249 """
265265 show palette
266266 """
267267 def show_palette(self):
268 for i in self.selbuttons: setlayer(i,800)
268 for i in self.selbuttons: i.setlayer(800)
269269 self._select_category(self.selbuttons[0])
270270 self.palette = True
271271
272 def xy(self, event):
273 return map(int, event.get_coords())
272274
273275 """
274276 unselect
293293 except ValueError:
294294 pass
295295
296 hide(self.select_mask)
297 hide(self.select_mask_string)
296 self.select_mask.hide()
297 self.select_mask_string.hide()
298298 self.selected_block = None
299299
300
301300 """
302301 select category
303302 """
304303 def _select_category(self, spr):
305304 if hasattr(self, 'current_category'):
306 setshape(self.current_category, self.current_category.offshape)
307 setshape(spr, spr.onshape)
305 self.current_category.setshape(self.current_category.offshape)
306 spr.setshape(spr.onshape)
308307 self.current_category = spr
309 setshape(self.category_spr,spr.group)
308 self.category_spr.setshape(spr.group)
310309
311310 """
312311 hide palette
313312 """
314313 def _hide_palette(self):
315 for i in self.selbuttons: hide(i)
316 setshape(self.category_spr, self.hidden_palette_icon)
314 for i in self.selbuttons: i.hide()
315 self.category_spr.setshape(self.hidden_palette_icon)
317316 self.palette = False
318317
319
320318 """
321319 register the events we listen to
322320 """
329329 self.window.connect("motion-notify-event", self._move_cb)
330330 self.window.connect("key_press_event", self._keypress_cb)
331331
332 def xy(self, event):
333 return map(int, event.get_coords())
332 """
333 XO-1 ?
334 """
335 def _is_XO_1(self):
336 return os.path.exists('/etc/olpc-release') or os.path.exists('/sys/power/olpc-pm')
334337
335
336338 """
337339 find a stack to run (any stack without a hat)
338340 """
374374 ret = True
375375 return ret
376376
377
378377 """
379378 Mouse move
380379 """
412412 dx += -(b.x+dx)
413413 # move the stack
414414 for b in self.draggroup:
415 move(b,(b.x+dx, b.y+dy))
415 b.move((b.x+dx, b.y+dy))
416416 elif spr.type=='turtle':
417417 type,dragx,dragy = self.dragpos
418418 if type == 'move':
420420 dx,dy = mdx,mdy
421421 else:
422422 dx,dy = x-dragx-spr.x,y-dragy-spr.y
423 move(spr, (spr.x+dx, spr.y+dy))
423 spr.move((spr.x+dx, spr.y+dy))
424424 else:
425425 if mdx != 0 or mdy != 0:
426426 dx,dy = mdx,mdy
438438 """
439439 def _get_proto_from_category(self, x, y):
440440 dx, dy = x-self.category_spr.x, y-self.category_spr.y,
441 pixel = getpixel(self.current_category.mask,dx,dy)
441 pixel = self.current_category.getpixel(self.current_category.mask,dx,dy)
442442 index = ((pixel%256)>>3)-1
443443 if index==0:
444444 return 'hide'
455455 if spr and spr.type == 'category':
456456 proto = self._get_proto_from_category(x, y)
457457 if proto and proto!='hide':
458 if timeout_tag[0] == 0:
459 timeout_tag[0] = self._do_show_popup(proto.name)
458 if self.timeout_tag[0] == 0:
459 self.timeout_tag[0] = self._do_show_popup(proto.name)
460460 self.spr = spr
461461 return
462462 else:
463 if timeout_tag[0] > 0:
463 if self.timeout_tag[0] > 0:
464464 try:
465 gobject.source_remove(timeout_tag[0])
466 timeout_tag[0] = 0
465 gobject.source_remove(self.timeout_tag[0])
466 self.timeout_tag[0] = 0
467467 except:
468 timeout_tag[0] = 0
468 self.timeout_tag[0] = 0
469469 elif spr and spr.type == 'selbutton':
470 if timeout_tag[0] == 0:
471 timeout_tag[0] = self._do_show_popup(spr.name)
470 if self.timeout_tag[0] == 0:
471 self.timeout_tag[0] = self._do_show_popup(spr.name)
472472 self.spr = spr
473473 else:
474 if timeout_tag[0] > 0:
474 if self.timeout_tag[0] > 0:
475475 try:
476 gobject.source_remove(timeout_tag[0])
477 timeout_tag[0] = 0
476 gobject.source_remove(self.timeout_tag[0])
477 self.timeout_tag[0] = 0
478478 except:
479 timeout_tag[0] = 0
479 self.timeout_tag[0] = 0
480480 elif spr and spr.type == 'block':
481 if timeout_tag[0] == 0:
482 timeout_tag[0] = self._do_show_popup(spr.proto.name)
481 if self.timeout_tag[0] == 0:
482 self.timeout_tag[0] = self._do_show_popup(spr.proto.name)
483483 self.spr = spr
484484 else:
485 if timeout_tag[0] > 0:
485 if self.timeout_tag[0] > 0:
486486 try:
487 gobject.source_remove(timeout_tag[0])
488 timeout_tag[0] = 0
487 gobject.source_remove(self.timeout_tag[0])
488 self.timeout_tag[0] = 0
489489 except:
490 timeout_tag[0] = 0
490 self.timeout_tag[0] = 0
491491 else:
492 if timeout_tag[0] > 0:
492 if self.timeout_tag[0] > 0:
493493 try:
494 gobject.source_remove(timeout_tag[0])
495 timeout_tag[0] = 0
494 gobject.source_remove(self.timeout_tag[0])
495 self.timeout_tag[0] = 0
496496 except:
497 timeout_tag[0] = 0
497 self.timeout_tag[0] = 0
498498
499499 """
500500 fetch the help text and display it
515515 self.win.set_title(_("Turtle Art") + " — " + label)
516516 return 0
517517
518
519518 """
520519 Keyboard
521520 """
610610 elif keyname == 'KP_Page_Down':
611611 if self.draggroup == None:
612612 self.draggroup = findgroup(self.spr)
613 for b in self.draggroup: hide(b)
613 for b in self.draggroup: b.hide()
614614 self.draggroup = None
615615 elif self.spr.type == 'selbutton':
616616 if keyname == 'Return' or keyname == 'KP_Page_Up':
620620 (x,y) = self.window.get_pointer()
621621 self._block_selector_pressed(x, y)
622622 for b in self.draggroup:
623 move(b, (b.x+200, b.y))
623 b.move((b.x+200, b.y))
624624 self.draggroup = None
625625 return True
626626 if self.selected_block is None:
651651 elif keyname is not '':
652652 # Hack until I sort out input and unicode + dead keys
653653 if self.dead_key == 'dead_grave':
654 keyunicode = dead_grave[keyname]
654 keyunicode = self.dead_grave[keyname]
655655 elif self.dead_key == 'dead_acute':
656 keyunicode = dead_acute[keyname]
656 keyunicode = self.dead_acute[keyname]
657657 elif self.dead_key == 'dead_circumflex':
658 keyunicode = dead_circumflex[keyname]
658 keyunicode = self.dead_circumflex[keyname]
659659 elif self.dead_key == 'dead_tilde':
660 keyunicode = dead_tilde[keyname]
660 keyunicode = self.dead_tilde[keyname]
661661 elif self.dead_key == 'dead_diaeresis':
662 keyunicode = dead_diaeresis[keyname]
662 keyunicode = self.dead_diaeresis[keyname]
663663 elif self.dead_key == 'dead_abovering':
664 keyunicode = dead_abovering[keyname]
664 keyunicode = self.dead_abovering[keyname]
665665 self.dead_key = ""
666666 if self.firstkey:
667667 newnum = selblock.check(unichr(keyunicode), \
677677 self.firstkey = False
678678 return True
679679
680
681
682
683680 """
684681 Button release
685682 """
711711 display_coordinates(self)
712712 self.draggroup = None
713713 return
714 if self.block_operation=='move' and hit(self.category_spr, (x,y)):
715 for b in self.draggroup: hide(b)
714 if self.block_operation=='move' and self.category_spr.hit((x,y)):
715 for b in self.draggroup: b.hide()
716716 self.draggroup = None
717717 return
718718 if self.block_operation=='new':
719719 for b in self.draggroup:
720 move(b, (b.x+200, b.y))
720 b.move((b.x+200, b.y))
721721 self._snap_to_dock()
722 for b in self.draggroup: setlayer(b,650)
722 for b in self.draggroup: b.setlayer(650)
723723 self.draggroup = None
724724 if self.block_operation=='click':
725725 if self.spr.proto.name=='number':
726726 self.selected_block = spr
727 move(self.select_mask, (spr.x-5,spr.y-5))
728 setlayer(self.select_mask, 660)
727 self.select_mask.move((spr.x-5,spr.y-5))
728 self.select_mask.setlayer(660)
729729 self.firstkey = True
730730 elif self.defdict.has_key(spr.proto.name):
731731 self.selected_block = spr
732732 if self.spr.proto.name=='string':
733 move(self.select_mask_string, (spr.x-5,spr.y-5))
734 setlayer(self.select_mask_string, 660)
733 self.select_mask_string.move((spr.x-5,spr.y-5))
734 self.select_mask_string.setlayer(660)
735735 self.firstkey = True
736 elif self.spr.proto.name in importblocks:
736 elif self.spr.proto.name in self.importblocks:
737737 self._import_from_journal(spr)
738738 elif self.spr.proto.name=='nop' and self.myblock==None:
739739 self.activity.import_py()
740 else: self._run_stack(spr)
740 else:
741 # TODO: mark block as selected
742 spr.set_selected(True)
743 self._run_stack(spr)
741744
742745 """
743746 click block
748748 def _click_block(self):
749749 if self.spr.proto.name=='number':
750750 self.selected_block = self.spr
751 move(self.select_mask, (self.spr.x-5,self.spr.y-5))
752 setlayer(self.select_mask, 660)
751 self.select_mask.move((self.spr.x-5,self.spr.y-5))
752 self.select_mask.setlayer(660)
753753 self.firstkey = True
754754 elif self.defdict.has_key(self.spr.proto.name):
755755 self.selected_block = self.spr
756756 if self.spr.proto.name=='string':
757 move(self.select_mask_string, (self.spr.x-5,self.spr.y-5))
758 setlayer(self.select_mask_string, 660)
757 self.select_mask_string.move((self.spr.x-5,self.spr.y-5))
758 self.select_mask_string.setlayer(660)
759759 self.firstkey = True
760 elif self.spr.proto.name in importblocks:
760 elif self.spr.proto.name in self.importblocks:
761761 self._import_from_journal(self.spr)
762762 elif self.spr.proto.name=='nop' and self.myblock==None:
763763 self.activity.import_py()
767767 Repaint
768768 """
769769 def _expose_cb(self, win, event):
770 # FIXME
771770 redrawsprites(self)
772771 return True
773772
787787 self.activity._send_event("p:"+str(x)+":"+str(y)+":"+'F')
788788 return True
789789
790
791790 """
792791 snap_to_dock
793792 """
808808 bestmydockn=mydockn
809809 if d<200:
810810 for b in self.draggroup:
811 move(b,(b.x+bestxy[0],b.y+bestxy[1]))
811 b.move((b.x+bestxy[0],b.y+bestxy[1]))
812812 blockindock=bestyou.connections[bestyourdockn]
813813 if blockindock!=None:
814814 for b in findgroup(blockindock):
815 hide(b)
815 b.hide()
816816 bestyou.connections[bestyourdockn]=me
817817 me.connections[bestmydockn]=bestyou
818818
870870 else:
871871 self.hideshow_palette(False)
872872
873
874873 """
875874 new block from category
876875 """
877876 def _new_block_from_category(self, proto, x, y):
878877 if proto is None:
879878 return True
879
880 #
881 # Create new instance of the block
882 #
883
880884 # load alternative image of nop block if python code is loaded
881885 if proto.name == 'nop' and self.nop == 'pythonloaded':
882 newspr = sprNew(self,x-20,y-20,self.media_shapes['pythonloaded'])
886 newspr = Sprite(self,x-20,y-20,self.media_shapes['pythonloaded'])
883887 else:
884 newspr = sprNew(self,x-20,y-20,proto.image)
885 setlayer(newspr,2000)
888 newspr = Sprite(self,x-20,y-20,proto.image)
889 newspr.setlayer(2000)
886890 self.dragpos = 20,20
887891 newspr.type = 'block'
888892 newspr.proto = proto
898898 argproto = self.protodict[self.valdict[dock[0]]]
899899 argdock = argproto.docks[0]
900900 nx,ny = newspr.x+dock[2]-argdock[2],newspr.y+dock[3]-argdock[3]
901 argspr = sprNew(self,nx,ny,argproto.image)
901 argspr = Sprite(self,nx,ny,argproto.image)
902902 argspr.type = 'block'
903903 argspr.proto = argproto
904904 argspr.label = str(proto.defaults[i])
905 setlayer(argspr,2000)
905 argspr.setlayer(2000)
906906 argspr.connections = [newspr,None]
907907 newspr.connections[i+1] = argspr
908908 self.draggroup = findgroup(newspr)
909 self.block_operation = 'new'
909 self.block_operation = 'new'
910910
911911
912912 """
913913 block pressed
914 TODO: mark block as selected
914915 """
915916 def _block_pressed(self, mask, x, y, spr):
916917 if spr is not None:
917918 self.draggroup = findgroup(spr)
918 for b in self.draggroup: setlayer(b,2000)
919 for b in self.draggroup: b.setlayer(2000)
919920 if spr.connections[0] != None and spr.proto.name == 'lock':
920921 b = self._find_top_block(spr)
921922 self.dragpos = x-b.x,y-b.y
924924 self.dragpos = x-spr.x,y-spr.y
925925 self._disconnect(spr)
926926
927
928927 """
929928 disconnect block
930929 """
958958 else:
959959 setimage(spr, self.media_shapes['texton'])
960960
961
962961 """
963962 dock_dx_dy
964963 """
10351035 dx += -(b.x+dx)
10361036 # move the stack
10371037 for b in self.draggroup:
1038 move(b,(b.x+dx, b.y-dy))
1038 b.move((b.x+dx, b.y-dy))
10391039 self._snap_to_dock()
10401040 self.draggroup = None