From 49dc9ccbc9bf3597254e0d165fd138c1eb6275a2 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 30 Oct 2013 00:52:29 +0000 Subject: new prim for reskin --- diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index e3d191c..b1db4ff 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -25,6 +25,7 @@ import gtk from time import time, sleep from operator import isNumberType +import os from os.path import exists as os_path_exists from UserDict import UserDict @@ -37,12 +38,13 @@ except ImportError: import traceback from tablock import (Block, Media, media_blocks_dictionary) -from taconstants import (TAB_LAYER, DEFAULT_SCALE) +from taconstants import (TAB_LAYER, DEFAULT_SCALE, ICON_SIZE) from tajail import myfunc from tapalette import (block_names, value_blocks) from tatype import (TATypeError, TYPES_NUMERIC) from tautils import (get_pixbuf_from_journal, data_from_file, get_stack_name, - text_media_type, round_int, debug_output, find_group) + text_media_type, round_int, debug_output, find_group, + get_path, image_to_base64, data_to_string) try: from util.RtfParser import RtfTextOnly @@ -937,6 +939,65 @@ class LogoCode: for blk in drag_group: blk.spr.move_relative((dx, 0)) + def reskin(self, obj): + """ Reskin the turtle with an image from a file """ + scale = int(ICON_SIZE * float(self.scale) / DEFAULT_SCALE) + if scale < 1: + return + self.filepath = None + self.dsobject = None + + if os_path_exists(obj.value): # file path + self.filepath = obj.value + elif self.tw.running_sugar: # datastore object + from sugar.datastore import datastore + try: + self.dsobject = datastore.get(obj.value) + except: + debug_output("Couldn't find dsobject %s" % + (obj.value), self.tw.running_sugar) + if self.dsobject is not None: + self.filepath = self.dsobject.file_path + + if self.filepath is None: + self.tw.showlabel('nojournal', self.filepath) + return + + pixbuf = None + try: + pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( + self.filepath, scale, scale) + except: + self.tw.showlabel('nojournal', self.filepath) + debug_output("Couldn't open skin %s" % (self.filepath), + self.tw.running_sugar) + if pixbuf is not None: + self.tw.turtles.get_active_turtle().set_shapes([pixbuf]) + pen_state = self.tw.turtles.get_active_turtle().get_pen_state() + if pen_state: + self.tw.turtles.get_active_turtle().set_pen_state(False) + self.tw.turtles.get_active_turtle().forward(0) + if pen_state: + self.tw.turtles.get_active_turtle().set_pen_state(True) + + if self.tw.sharing(): + if self.tw.running_sugar: + tmp_path = get_path(self.tw.activity, 'instance') + else: + tmp_path = '/tmp' + tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), + 'tmpfile.png') + pixbuf.save(tmp_file, 'png', {'quality': '100'}) + data = image_to_base64(tmp_file, tmp_path) + height = pixbuf.get_height() + width = pixbuf.get_width() + event = 'R|%s' % (data_to_string([self.tw.nick, + [round_int(width), + round_int(height), + data]])) + gobject.idle_add(self.tw.send_event, event) + os.remove(tmp_file) + def show(self, obj, center=False): """ Show is the general-purpose media-rendering block. """ # media diff --git a/plugins/turtle_blocks_extras/turtle_blocks_extras.py b/plugins/turtle_blocks_extras/turtle_blocks_extras.py index d1f25d0..c5357c4 100644 --- a/plugins/turtle_blocks_extras/turtle_blocks_extras.py +++ b/plugins/turtle_blocks_extras/turtle_blocks_extras.py @@ -30,7 +30,7 @@ from TurtleArt.tapalette import (make_palette, define_logo_function, palette_names, palette_i18n_names) from TurtleArt.talogo import (primitive_dictionary, logoerror, media_blocks_dictionary) -from TurtleArt.taconstants import (DEFAULT_SCALE, ICON_SIZE, CONSTANTS, +from TurtleArt.taconstants import (DEFAULT_SCALE, CONSTANTS, MEDIA_SHAPES, SKIN_PATHS, BLOCKS_WITH_SKIN, PYTHON_SKIN, MEDIA_BLOCK2TYPE, VOICES, MACROS, Color) @@ -845,7 +845,6 @@ module found in the Journal')) arg_descs=[ArgSlot(TYPE_OBJECT)], return_type=TYPE_BOX)) - primitive_dictionary['skin'] = self._prim_reskin palette.add_block('skin', hidden=True, colors=["#FF0000", "#A00000"], @@ -854,8 +853,8 @@ module found in the Journal')) prim_name='skin', help_string=_("put a custom 'shell' on the turtle")) self.tw.lc.def_prim('skin', 1, - lambda self, x: - primitive_dictionary['skin'](x)) + Primitive(self.tw.lc.reskin, + arg_descs=[ArgSlot(TYPE_OBJECT)])) # macro palette.add_block('reskin', @@ -1338,62 +1337,6 @@ Journal objects')) self.tw.lc.heap.append(g) self.tw.lc.heap.append(r) - def _prim_reskin(self, media): - """ Reskin the turtle with an image from a file """ - scale = int(ICON_SIZE * float(self.tw.lc.scale) / DEFAULT_SCALE) - if scale < 1: - return - self.tw.lc.filepath = None - dsobject = None - if os.path.exists(media[6:]): # is it a path? - self.tw.lc.filepath = media[6:] - elif self.tw.running_sugar: # is it a datastore object? - from sugar.datastore import datastore - try: - dsobject = datastore.get(media[6:]) - except: - debug_output("Couldn't open skin %s" % (media[6:]), - self.tw.running_sugar) - if dsobject is not None: - self.tw.lc.filepath = dsobject.file_path - if self.tw.lc.filepath is None: - self.tw.showlabel('nojournal', self.tw.lc.filepath) - return - pixbuf = None - try: - pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self.tw.lc.filepath, - scale, scale) - except: - self.tw.showlabel('nojournal', self.tw.lc.filepath) - debug_output("Couldn't open skin %s" % (self.tw.lc.filepath), - self.tw.running_sugar) - if pixbuf is not None: - self.tw.turtles.get_active_turtle().set_shapes([pixbuf]) - pen_state = self.tw.turtles.get_active_turtle().get_pen_state() - if pen_state: - self.tw.turtles.get_active_turtle().set_pen_state(False) - self.tw.turtles.get_active_turtle().forward(0) - if pen_state: - self.tw.turtles.get_active_turtle().set_pen_state(True) - - if self.tw.sharing(): - if self.tw.running_sugar: - tmp_path = get_path(self.tw.activity, 'instance') - else: - tmp_path = '/tmp' - tmp_file = os.path.join(get_path(self.tw.activity, 'instance'), - 'tmpfile.png') - pixbuf.save(tmp_file, 'png', {'quality': '100'}) - data = image_to_base64(tmp_file, tmp_path) - height = pixbuf.get_height() - width = pixbuf.get_width() - event = 'R|%s' % (data_to_string([self.tw.nick, - [round_int(width), - round_int(height), - data]])) - gobject.idle_add(self.tw.send_event, event) - os.remove(tmp_file) - def _prim_save_picture(self, name): """ Save canvas to file as PNG """ self.tw.save_as_image(name) -- cgit v0.9.1