diff options
author | Walter Bender <walter.bender@gmail.com> | 2012-05-15 03:12:01 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2012-05-15 03:12:01 (GMT) |
commit | 1fdec555981d691ac57307f25ed92b91716220b1 (patch) | |
tree | 9eb3e267582b3c5caacbb789c12ce7a1ad67eeca | |
parent | 40ca229069e688f547f18bf181cbcf02e7fb95ab (diff) |
add some color; save image to journal
-rw-r--r-- | StoryActivity.py | 40 | ||||
-rw-r--r-- | game.py | 50 |
2 files changed, 81 insertions, 9 deletions
diff --git a/StoryActivity.py b/StoryActivity.py index 41cd5eb..929c76a 100644 --- a/StoryActivity.py +++ b/StoryActivity.py @@ -11,9 +11,12 @@ import gtk +import cairo +import os from sugar.activity import activity from sugar import profile +from sugar.datastore import datastore try: from sugar.graphics.toolbarbox import ToolbarBox _have_toolbox = True @@ -24,6 +27,8 @@ if _have_toolbox: from sugar.activity.widgets import ActivityToolbarButton from sugar.activity.widgets import StopButton +from sugar.graphics.alert import NotifyAlert + from toolbar_utils import button_factory, label_factory, separator_factory from utils import json_load, json_dump @@ -116,6 +121,10 @@ class StoryActivity(activity.Activity): 'view-refresh', self.toolbar, self._new_game_cb, tooltip=_('Load new images.')) + self.save_as_image = button_factory( + 'image-saveoff', self.toolbar, self.do_save_as_image_cb, + tooltip=_('Save as image')) + self.status = label_factory(self.toolbar, '') if _have_toolbox: @@ -148,6 +157,37 @@ class StoryActivity(activity.Activity): dot_list.append(int(dot)) self._game.restore_game(dot_list) + def do_save_as_image_cb(self, button): + """ Grab the current canvas and save it to the Journal. """ + file_path = os.path.join(activity.get_activity_root(), + 'instance', 'story.png') + png_surface = self._game.export() + png_surface.write_to_png(file_path) + + dsobject = datastore.create() + dsobject.metadata['title'] = "%s %s" % \ + (self.metadata['title'], _("image")) + dsobject.metadata['icon-color'] = profile.get_color().to_string() + dsobject.metadata['mime_type'] = 'image/png' + dsobject.set_file_path(file_path) + datastore.write(dsobject) + dsobject.destroy() + os.remove(file_path) + self._notify_successful_save(title=_('Save as image')) + + def _notify_successful_save(self, title='', msg=''): + ''' Notify user when saves are completed ''' + + def _notification_alert_response_cb(alert, response_id, self): + self.remove_alert(alert) + + alert = NotifyAlert() + alert.props.title = title + alert.connect('response', _notification_alert_response_cb, self) + alert.props.msg = msg + self.add_alert(alert) + alert.show() + # Collaboration-related methods def _setup_presence_service(self): @@ -29,10 +29,11 @@ try: except ImportError: GRID_CELL_SIZE = 0 -DOT_SIZE = 40 +from sprites import Sprites, Sprite -from sprites import Sprites, Sprite +DOT_SIZE = 40 +COLORS = ['#000000', '#ff0000', '#907000', '#009000', '#0000ff', '#9000a0'] class Game(): @@ -54,6 +55,7 @@ class Game(): self._width = gtk.gdk.screen_width() self._height = gtk.gdk.screen_height() - (GRID_CELL_SIZE * 1.5) self._scale = self._height / (3 * DOT_SIZE * 1.2) + self._scale /= 1.5 self._dot_size = int(DOT_SIZE * self._scale) self._space = int(self._dot_size / 5.) self.we_are_sharing = False @@ -67,7 +69,7 @@ class Game(): # Generate the sprites we'll need... self._sprites = Sprites(self._canvas) self._dots = [] - yoffset = int(self._space / 2.) + yoffset = self._space * 2 # int(self._space / 2.) for y in range(3): for x in range(3): xoffset = int((self._width - 3 * self._dot_size - \ @@ -165,13 +167,42 @@ class Game(): def _destroy_cb(self, win, event): gtk.main_quit() + def export(self): + ''' Write dot to cairo surface. ''' + w = h = 4 * self._space + 3 * self._dot_size + png_surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) + cr = cairo.Context(png_surface) + cr.set_source_rgb(192, 192, 192) + cr.rectangle(0, 0, w, h) + cr.fill() + for i in range(9): + y = self._space + int(i / 3.) * (self._dot_size + self._space) + x = self._space + (i % 3) * (self._dot_size + self._space) + cr.save() + cr = gtk.gdk.CairoContext(cr) + cr.set_source_surface(self._dots[i].cached_surfaces[0], x, y) + cr.rectangle(x, y, self._dot_size, self._dot_size) + cr.fill() + cr.restore() + return png_surface + def _new_dot_surface(self, color='#000000', image=None): ''' generate a dot of a color color ''' self._dot_cache = {} if image is not None: + color = COLORS[int(uniform(0, 6))] + fd = open(os.path.join(self._path, self._PATHS[image]), 'r') + svg_string = '' + for line in fd: + svg_string += line.replace('#000000', color) + fd.close() + pixbuf = svg_str_to_pixbuf(svg_string, w=self._dot_size, + h = self._dot_size) + ''' pixbuf = gtk.gdk.pixbuf_new_from_file_at_size( os.path.join(self._path, self._PATHS[image]), - self._svg_width, self._svg_height) + self._dot_size, self._dot_size) + ''' else: if color in self._dot_cache: return self._dot_cache[color] @@ -224,10 +255,11 @@ class Game(): return '</svg>\n' -def svg_str_to_pixbuf(svg_string): - """ Load pixbuf from SVG string """ - pl = gtk.gdk.PixbufLoader('svg') +def svg_str_to_pixbuf(svg_string, w=None, h=None): + ''' Load pixbuf from SVG string ''' + pl = gtk.gdk.PixbufLoader('svg') + if w is not None: + pl.set_size(w, h) pl.write(svg_string) pl.close() - pixbuf = pl.get_pixbuf() - return pixbuf + return pl.get_pixbuf() |