From 56cea3b5c6361c7958e1c27182fcf117470a1861 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Fri, 30 Jan 2009 02:18:20 +0000 Subject: Implement character frames panel --- diff --git a/Char.py b/Char.py index f69d54b..23da9b7 100644 --- a/Char.py +++ b/Char.py @@ -13,22 +13,70 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import gtk +import glob from gettext import gettext as _ import Theme +import Document class Char: def __init__(self, name, file, dir, custom): self.name = name self.pixbuf = Theme.pixbuf(file, Theme.THUMB_SIZE) - self.dir = dir self.custom = custom + self.loaded = False + self.thumbs = {} + self.origs = {} + self.files = [] + + if custom: + self.empty_orig = Theme.pixbuf(file) + else: + self.files = sorted(glob.glob(Theme.path(dir + '/*'))) + + def change(self): + if not self.custom or self.loaded: + return + self.loaded = True + + for i in range(Theme.TAPE_COUNT): + self.origs[i] = Document.orig(i) + self.thumbs[i] = Document.thumb(i) def thumb(self, index = None): - return self.pixbuf + if index == None: + return self.pixbuf + + pix = self.thumbs.get(index) + + if pix == None: + if self.custom: + pix = self.pixbuf + else: + if index < len(self.files): + pix = Theme.pixbuf(self.files[index], Theme.THUMB_SIZE) + else: + pix = Theme.EMPTY_PIXBUF + self.thumbs[index] = pix + + return pix def orig(self, index): - return self.pixbuf + pix = self.origs.get(index) + + if pix == None: + if self.custom: + pix = Theme.choose_pixbuf(lambda t, file: Theme.pixbuf(file)) + if pix: + self.thumbs[index] = pix.scale_simple(Theme.THUMB_SIZE, + Theme.THUMB_SIZE, gtk.gdk.INTERP_BILINEAR) + self.origs[index] = pix + else: + if index < len(self.files): + pix = Theme.pixbuf(self.files[index]) + self.origs[index] = pix + + return pix THEMES = ( Char(_('Elephant'), 'images/pics/Elephant/bigelephant0.gif', diff --git a/Document.py b/Document.py index f435dc1..51320e6 100644 --- a/Document.py +++ b/Document.py @@ -75,51 +75,6 @@ import StringIO self.frameimgs[i].set_from_pixbuf(scaled_buf) - def loadfromzip(self, f): - # print filepath - #zf = zipfile.ZipFile(filepath,'r') - zf = zipfile.ZipFile(f) - fnames = zf.namelist() - framenames = [] - for fname in fnames: - if fname[-8:] == 'back.png': - backname = fname - else: - framenames.append(fname) - framenames.sort() - # set the background - tmpbgpath = os.path.join(TMPDIR,'back.png') - f = file(tmpbgpath,'w') - f.write(zf.read(backname)) - f.close() - self.setback(tmpbgpath) - os.remove(tmpbgpath) - self.imgdir = TMPDIR - for filepath in framenames: - fname = os.path.split(filepath)[1] - tmpfilepath = os.path.join(TMPDIR,fname) - f = file(tmpfilepath,'w') - f.write(zf.read(filepath)) - f.close() - zf.close() - self.loadimages() - # setup the filmstrip frames - pics = self.getpics(self.imgdir) - count = 0 - for imgpath in pics: - pixbuf = gtk.gdk.pixbuf_new_from_file(imgpath) - fgpixbuf = pixbuf.scale_simple(BGWIDTH,BGHEIGHT,gtk.gdk.INTERP_BILINEAR) - self.fgpixbufs[count] = fgpixbuf - if count == 0: - self.fgpixbuf = fgpixbuf - self.drawmain() - scaled_buf = pixbuf.scale_simple(IMGWIDTH,IMGHEIGHT,gtk.gdk.INTERP_BILINEAR) - self.frameimgs[count].set_from_pixbuf(scaled_buf) - count += 1 - entries = os.listdir(TMPDIR) - for entry in entries: - entrypath = os.path.join(TMPDIR,entry) - os.remove(entrypath) def savetozip(self, f): # print filepath diff --git a/Main.py b/Main.py index ab4715a..be48034 100644 --- a/Main.py +++ b/Main.py @@ -119,13 +119,19 @@ class CartoonBuilder: self.screen.draw() def _frame_cb(self, widget, event, frame): - Document.stamp(self.tape_selected, self.char.orig(frame)) - self.tape[self.tape_selected].child.set_from_pixbuf( - self.char.thumb(frame)) + orig = self.char.orig(frame) + if not orig: return + thumb = self.char.thumb(frame) + + Document.stamp(self.tape_selected, orig) + self.tape[self.tape_selected].child.set_from_pixbuf(thumb) + self.frames[frame].set_from_pixbuf(thumb) + self._tape_cb(None, None, self.tape_selected) def _char_cb(self, widget, combo): self.char = widget.props.value + self.char.change() for i in range(len(self.frames)): self.frames[i].set_from_pixbuf(self.char.thumb(i)) diff --git a/Theme.py b/Theme.py index 110ee63..6aa5fc8 100644 --- a/Theme.py +++ b/Theme.py @@ -90,6 +90,29 @@ def pixbuf(file, size = None): out = gtk.gdk.pixbuf_new_from_file(path(file)) return out +EMPTY_PIXBUF = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, 1, 1) + + +def choose_pixbuf(out_fun): + from sugar.graphics.objectchooser import ObjectChooser + + chooser = ObjectChooser() + jobject = None + + try: + result = chooser.run() + + if result == gtk.RESPONSE_ACCEPT: + jobject = chooser.get_selected_object() + if jobject and jobject.file_path: + return out_fun(jobject.metadata['title'], jobject.file_path) + finally: + if jobject: jobject.destroy() + chooser.destroy() + del chooser + + return None + # customize theme gtkrc = os.path.join(get_bundle_path(), 'gtkrc') gtk.rc_add_default_file(gtkrc) -- cgit v0.9.1