def change_color_blocks(self): index1 = palette_name_to_index('nxt-motors') index2 = palette_name_to_index('nxt-sensors') if (index1 is not None) and (index2 is not None): nxt_palette_blocks = palette_blocks[index1] + palette_blocks[index2] for block in self.tw.block_list.list: if block.type in ['proto', 'block']: if block.name in nxt_palette_blocks: if (self._bricks) or (block.name == 'nxtrefresh'): special_block_colors[block.name] = COLOR_PRESENT[:] else: special_block_colors[block.name] = COLOR_NOTPRESENT[:] block.refresh() self.tw.regenerate_palette(index1) self.tw.regenerate_palette(index2)
def myblock(tw, arg): ''' Add a uturn block to the 'turtle' palette ''' from TurtleArt.tapalette import make_palette, palette_name_to_index from TurtleArt.talogo import primitive_dictionary from gettext import gettext as _ # Choose a palette for the new block. palette = make_palette('turtle') # Create a new block prototype. palette.add_block('uturn', style='basic-style-extended-vertical', label=_('uturn'), prim_name='uturn', help_string=_('make a uturn')) # Add its primitive to the LogoCode dictionary. tw.lc.def_prim('uturn', 0, lambda self: primitive_dictionary['set'] ('heading', tw.canvas.seth, tw.canvas.heading + 180)) # Regenerate the palette, which will now include the new block. tw.show_toolbar_palette(palette_name_to_index('turtle'), regenerate=True)
def myblock(tw, arg): ''' Add a uturn block to the 'turtle' palette ''' # def_prim takes 3 arguments: the primitive name, the number of # arguments -- 0 in this case -- and the function to call -- in this # case, we define the _prim_uturn function to set heading += 180. def _prim_uturn(tw): value = tw.turtles.get_active_turtle().get_heading() + 180 tw.turtles.get_active_turtle().set_heading(value) # We also update the label on the heading block to indicate # the current heading value if tw.lc.update_values: tw.lc.update_label_value('heading', value) from TurtleArt.tapalette import make_palette, palette_name_to_index from TurtleArt.talogo import primitive_dictionary from gettext import gettext as _ # Choose a palette for the new block. palette = make_palette('turtle') # Create a new block prototype. palette.add_block('uturn', style='basic-style-extended-vertical', label=_('uturn'), prim_name='uturn', help_string=_('turns the turtle 180 degrees')) # Add its primitive to the LogoCode dictionary. tw.lc.def_prim('uturn', 0, lambda self: _prim_uturn(tw)) # Regenerate the palette, which will now include the new block. tw.show_toolbar_palette(palette_name_to_index('turtle'), regenerate=True)
def change_color_blocks(self): index = palette_name_to_index('fischer') if (index is not None): ft_palette_blocks = palette_blocks[index] for block in self.tw.block_list.list: if block.type in ['proto', 'block']: if block.name in ft_palette_blocks: if (self._fischers) or (block.name == 'ftrefresh'): special_block_colors[block.name] = COLOR_PRESENT[:] else: special_block_colors[block.name] = COLOR_NOTPRESENT[:] block.refresh() self.tw.regenerate_palette(index)
def change_color_blocks(self): index = palette_name_to_index('wedo') if (index is not None): wedo_blocks = palette_blocks[index] for block in self.tw.block_list.list: if block.type in ['proto', 'block']: if block.name in wedo_blocks: if (self.WeDos) or (block.name == 'wedorefresh'): special_block_colors[block.name] = COLOR_PRESENT[:] else: special_block_colors[block.name] = COLOR_NOTPRESENT[:] block.refresh() self.tw.regenerate_palette(index)
def refreshAx(self): i = self.getID() index = palette_name_to_index('ax12') if index is not None: ax12_blocks = palette_blocks[index] for block in self.tw.block_list.list: if block.name in ax12_blocks: if (i != -1) or (block.name == 'refresh Ax'): special_block_colors[block.name] = COLOR_PRESENT[:] else: special_block_colors[block.name] = COLOR_NOTPRESENT[:] block.refresh() self.tw.regenerate_palette(index)
def change_color_blocks(self): index = palette_name_to_index('colorview') if index is not None: colorview_blocks = palette_blocks[index] for block in self.tw.block_list.list: if block.type in ['proto', 'block']: if block.name in colorview_blocks: if self.cam_present: special_block_colors[block.name] = COLOR_PRESENT[:] else: special_block_colors[block.name] = COLOR_NOTPRESENT[:] block.refresh() self.tw.regenerate_palette(index)
def change_color_blocks(self): if len(self._rodis) > 0: rodi_present = True else: rodi_present = False index = palette_name_to_index('rodi') if index is not None: rodi_blocks = palette_blocks[index] for block in self.tw.block_list.list: if block.type in ['proto', 'block']: if block.name in rodi_blocks: if (rodi_present) or (block.name == 'refresh_Rodi'): special_block_colors[block.name] = COLOR_PRESENT[:] else: special_block_colors[block.name] = COLOR_NOTPRESENT[:] block.refresh() self.tw.regenerate_palette(index)
def change_color_blocks(self): index = palette_name_to_index('arduino') arduino_blocks = palette_blocks[index] if len(self._arduinos) > 0: arduino_present = True else: arduino_present = False for block in self.tw.block_list.list: if block.type in ['proto', 'block']: if block.name in arduino_blocks: if (arduino_present) or (block.name == 'arduinorefresh'): special_block_colors[block.name] = COLOR_PRESENT[:] else: special_block_colors[block.name] = COLOR_NOTPRESENT[:] block.refresh() self.tw.show_toolbar_palette(index, regenerate=True, show=False)
def myblock(tw, name): ''' ''' def _prim_forward_push(tw, line_length): try: # make sure line_length is a number line_length = float(line_length) except ValueError: return penstatus = tw.canvas.pendown tw.canvas.setpen(False) tw.canvas.forward(line_length) r, g, b, a = tw.canvas.get_pixel() tw.lc.heap.append(b) tw.lc.heap.append(g) tw.lc.heap.append(r) tw.canvas.forward(-line_length) tw.canvas.setpen(penstatus) tw.canvas.forward(line_length) return from TurtleArt.tapalette import make_palette, palette_name_to_index from TurtleArt.talogo import primitive_dictionary from gettext import gettext as _ # Choose a palette for the new block. palette = make_palette('turtle') primitive_dictionary['forwardpush'] = _prim_forward_push # Create a new block prototype. palette.add_block('forwardpush', style='basic-style-1arg', label=name, default=100, prim_name='forwardpush', help_string=_('push destination rgb value to heap')) # Add its primitive to the LogoCode dictionary. tw.lc.def_prim('forwardpush', 1, lambda self, x: primitive_dictionary['forwardpush'](tw, x)) # Regenerate the palette, which will now include the new block. tw.show_toolbar_palette(palette_name_to_index('turtle'), regenerate=True)
def myblock(tw, args): """ """ def _prim_forward_push(tw, line_length): try: # make sure line_length is a number line_length = float(line_length) except ValueError: return penstatus = tw.turtles.get_active_turtle().get_pen_status() tw.turtles.get_active_turtle().set_pen_state(False) tw.turtles.get_active_turtle().forward(line_length) r, g, b, a = tw.turtles.get_active_turtle().get_pixel() tw.lc.heap.append(b) tw.lc.heap.append(g) tw.lc.heap.append(r) tw.turtles.get_active_turtle().forward(-line_length) tw.turtles.get_active_turtle().set_pen_state(penstatus) tw.turtles.get_active_turtle().forward(line_length) return from TurtleArt.tapalette import make_palette, palette_name_to_index from TurtleArt.talogo import primitive_dictionary from gettext import gettext as _ # Choose a palette for the new block. palette = make_palette("turtle") primitive_dictionary["forwardpush"] = _prim_forward_push # Create a new block prototype. palette.add_block( "forwardpush", style="basic-style-1arg", label=args[0], default=100, prim_name="forwardpush", help_string=_("push destination rgb value to heap"), ) # Add its primitive to the LogoCode dictionary. tw.lc.def_prim("forwardpush", 1, lambda self, x: primitive_dictionary["forwardpush"](tw, x)) # Regenerate the palette, which will now include the new block. tw.show_toolbar_palette(palette_name_to_index("turtle"), regenerate=True)
def castSenButia(self, new_name, original, function): global label_name_from_device_id new_name = str(new_name) function = str(function) original = str(original) original = original.upper() if original == 'A': module_block = 'modSenA' self.set_gconf(GCONF_CAST + 'modSenA_f', function) self.modsen_a_f = function elif original == 'B': module_block = 'modSenB' self.set_gconf(GCONF_CAST + 'modSenB_f', function) self.modsen_b_f = function elif original == 'C': module_block = 'modSenC' self.set_gconf(GCONF_CAST + 'modSenC_f', function) self.modsen_c_f = function else: raise logoerror(_('ERROR: You must cast sensor: A, B or C')) self.set_gconf(GCONF_CAST + module_block, new_name) label_name_from_device_id[module_block] = new_name for blk in self.tw.block_list.list: if (blk.type in ['proto', 'block']) and blk.name.endswith('Butia'): blk_name, blk_index = self.block_2_index_and_name(blk.name) if (blk_name == module_block): label = new_name + ' ' + _('Butia') if blk.type == 'proto': if blk_index == '0': blk.set_visibility(True) blk.spr.set_label(label) block_names[blk.name][0] = label blk.refresh() try: index = palette_name_to_index('butia-cast') self.tw.regenerate_palette(index) except: pass #TODO: pensar algo mejor self.list_connected_device_module = []
def update_palette_colors(self, flag): # if there exists new RELAY connected or disconections to the butia IO board, # then it change the color of the blocks corresponding global RELAY_PORT #print 'refreshing' regenerar_paleta = False cant_modulos_conectados = 0 l = self._list_connected_device_module[:] modulos_nuevos = [] self.modulos_conectados = [] mods = [] for e in l: t = self.robot._split_module(e) #t = ('5', 'relay', '0') #print t if t[1] == 'relay': self.modulos_conectados.append(t[0]) modulos_nuevos.append(t[0]) mods.append(t[1] + ":" + t[0]) #print mods #print 'mod conectados', self.modulos_conectados modulos_nuevos = self.modulos_conectados[:] #genera = self.prim_enggen() #valor = self.prim_engrec() genera = 68 valor = 60 cont_relay = 0 for blk in self._parent.block_list.list: if blk.name.endswith('agh'): #blk.name = 'relay2agh' if blk.name == 'enggenagh': if genera >= 0: if valor >= 0: if genera >= valor: special_block_colors[blk.name] = COLOR_PRESENT else: special_block_colors[blk.name] = COLOR_RED else: special_block_colors[blk.name] = COLOR_NOTPRESENT else: special_block_colors[blk.name] = COLOR_NOTPRESENT elif blk.name == 'engrecagh': if valor >= 0: special_block_colors[blk.name] = COLOR_PRESENT else: special_block_colors[blk.name] = COLOR_NOTPRESENT elif blk.name == 'engmaxagh': if valor >= 0: special_block_colors[blk.name] = COLOR_PRESENT else: special_block_colors[blk.name] = COLOR_NOTPRESENT elif blk.name[:5] == 'relay': #print blk.name #print RELAY_PORT[blk.name] #if RELAY_PORT[blk.name] == 0: if len(modulos_nuevos)>0: RELAY_PORT[blk.name] = modulos_nuevos[0] tmp = modulos_nuevos[0] modulos_nuevos.remove(tmp) tmp = 'relay:' + str(RELAY_PORT[blk.name]) #print 'tmp', tmp if tmp in mods: cant_modulos_conectados += 1 if (blk.type == 'proto'): blk.set_visibility(True) regenerar_paleta = True label = 'relay:' + str(RELAY_PORT[blk.name]) special_block_colors[blk.name] = COLOR_PRESENT else: label = 'relay' if(blk.type == 'proto'): regenerar_paleta = True #print 'tengo un proto', blk.name if (RELAY_PORT[blk.name] <> 0) | (blk.name == 'relay1agh'): #if cant_modulos_conectados == 0: #if len(modulos_nuevos) == 0: #cant_modulos_conectados = -1 if not blk.get_visibility(): blk.set_visibility(True) else: blk.set_visibility(False) special_block_colors[blk.name] = COLOR_NOTPRESENT blk.spr.set_label(label) block_names[blk.name][0] = label blk.refresh() if regenerar_paleta: index = palette_name_to_index('atyarandu') if index is not None: self._parent.regenerate_palette(index)
def _prim_add_food(self, name, media, calories, protein, carbohydrates, fiber, fat): # Add a new food item to the palette palette = make_palette('food', colors=["#FFFFFF", "#A0A0A0"], help_string=_('Palette of foods')) # We need to convert the media into two svgs, one for the # palette and one for the block. filepath = None if media is not None and os.path.exists(media.value): filepath = media.value elif self.tw.running_sugar: # datastore object from sugar.datastore import datastore try: dsobject = datastore.get(media.value) except: debug_output("Couldn't find dsobject %s" % (media.value), self.tw.running_sugar) if dsobject is not None: filepath = dsobject.file_path if filepath is None: self.tw.showlabel('nojournal', filepath) return pixbuf = None try: pixbufsmall = gtk.gdk.pixbuf_new_from_file_at_size( filepath, 40, 40) pixbufoff = gtk.gdk.pixbuf_new_from_file_at_size(filepath, 40, 40) except: self.tw.showlabel('nojournal', filepath) debug_output("Couldn't open food image %s" % (filepath), self.tw.running_sugar) return def _draw_pixbuf(cr, pixbuf, x, y, w, h): # Build a gtk.gdk.CairoContext from a cairo.Context to access # the set_source_pixbuf attribute. cc = gtk.gdk.CairoContext(cr) cc.save() # center the rotation on the center of the image cc.translate(x + w / 2., y + h / 2.) cc.translate(-x - w / 2., -y - h / 2.) cc.set_source_pixbuf(pixbuf, x, y) cc.rectangle(x, y, w, h) cc.fill() cc.restore() if self.tw.running_sugar: path = os.path.join(get_path(self.tw.activity, 'instance'), 'output.svg') else: path = TMP_SVG_PATH svg_surface = cairo.SVGSurface(path, 40, 40) cr_svg = cairo.Context(svg_surface) _draw_pixbuf(cr_svg, pixbufsmall, 0, 0, 40, 40) cr_svg.show_page() svg_surface.flush() svg_surface.finish() destination = os.path.join(self.tw.path, 'plugins', 'food', 'images', name.encode('ascii') + 'small.svg') check_output(['mv', path, destination], 'problem moving %s to %s' % (path, destination)) pixbufsmall = gtk.gdk.pixbuf_new_from_file_at_size(destination, 40, 40) self.tw.media_shapes[name.encode('ascii') + 'small'] = pixbufsmall svg_surface = cairo.SVGSurface(path, 40, 40) cr_svg = cairo.Context(svg_surface) _draw_pixbuf(cr_svg, pixbufoff, 0, 0, 40, 40) cr_svg.show_page() svg_surface.flush() svg_surface.finish() destination = os.path.join(self.tw.path, 'plugins', 'food', 'images', name.encode('ascii') + 'off.svg') check_output(['mv', path, destination], 'problem moving %s to %s' % (path, destination)) # Now that we have the images, we can make the new block self._make_polynominal(palette, name.encode('ascii'), name, [calories, protein, carbohydrates, fiber, fat], expand=(15, 15)) pixbufoff = gtk.gdk.pixbuf_new_from_file_at_size(destination, 40, 40) self.tw.media_shapes[name.encode('ascii') + 'off'] = pixbufoff # Show the new block self.tw.show_toolbar_palette(palette_name_to_index('food'), regenerate=True) # Finally, we need to save new food item to a database so it # loads next time. food = name.encode('ascii') self.food_dictionary[food] = {} self.food_dictionary[food]['name'] = name self.food_dictionary[food]['calories'] = calories self.food_dictionary[food]['protein'] = protein self.food_dictionary[food]['carbohydrates'] = carbohydrates self.food_dictionary[food]['fiber'] = fiber self.food_dictionary[food]['fat'] = fat path = os.path.join(self.tw.path, 'plugins', 'food', 'food.db') data_to_file(self.food_dictionary, path) return
def change_butia_palette_colors(self, force_refresh, change_statics_blocks, change_extras_blocks): self.make_match_dict() self.getCastButia() for blk in self.tw.block_list.list: #NOTE: blocks types: proto, block, trash, deleted if (blk.type in ['proto', 'block']) and blk.name.endswith('Butia'): if (blk.name in static_block_list): if change_statics_blocks: if (blk.name == 'batterychargeButia'): if self.use_cc: blk.set_visibility(False) else: blk.set_visibility(True) special_block_colors[ blk.name] = self.battery_color[:] else: special_block_colors[ blk.name] = self.statics_color[:] blk.refresh() elif (blk.name in extras_block_list): if change_extras_blocks: special_block_colors[blk.name] = self.extras_color[:] blk.refresh() else: blk_name, blk_index = self.block_2_index_and_name(blk.name) if (blk_name in refreshable_block_list): module = modules_name_from_device_id[blk_name] if (module in self.modules_changed) or force_refresh: s = module + blk_index label = label_name_from_device_id[blk_name] if not (s in self.match_dict): if blk_index != '': if blk.type == 'proto': # only make invisible the block in the palette not in the program area blk.set_visibility(False) value = str(blk_index) else: value = '0' label = label + ' ' + _('Butia') board = '0' special_block_colors[ blk.name] = COLOR_NOTPRESENT[:] else: val = self.match_dict[s] value = val[0] board = val[1] label = label + ':' + val[0] + ' ' + _('Butia') if self.butia_count > 1: label = label + ' ' + val[1] if blk.type == 'proto': # don't has sense to change the visibility of a block in the program area blk.set_visibility(True) special_block_colors[ blk.name] = COLOR_PRESENT[:] if module in [ 'led', 'modActA', 'modActB', 'modActC' ]: self.tw.lc.def_prim( blk.name, 1, Primitive(d[blk_name], arg_descs=[ ArgSlot(TYPE_NUMBER), ConstantArg(value), ConstantArg(board) ])) else: self.tw.lc.def_prim( blk.name, 0, Primitive(d[blk_name], return_type=TYPE_INT, arg_descs=[ ConstantArg(value), ConstantArg(board) ])) blk.spr.set_label(label) block_names[blk.name][0] = label blk.refresh() try: index = palette_name_to_index('butia') self.tw.regenerate_palette(index) except: pass try: index = palette_name_to_index('butia-extra') self.tw.regenerate_palette(index) except: pass try: index = palette_name_to_index('butia-cast') self.tw.regenerate_palette(index) except: pass