From 2c57833688b6cd36b284de7194fdad7c5571087d Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Sat, 31 Jan 2009 23:12:05 +0000 Subject: implement load/save operations --- diff --git a/Activity.py b/Activity.py index eccde28..4446a80 100644 --- a/Activity.py +++ b/Activity.py @@ -41,21 +41,13 @@ class CartoonBuilderActivity(activity.Activity): def __init__(self, handle): activity.Activity.__init__(self,handle) - self.connect("destroy",self.destroy_cb) self.app = View() - self.set_title('CartoonBuilder') toolbox = activity.ActivityToolbox(self) - bgtoolbar = Toolbar(self,self.app) - toolbox.add_toolbar(_('Background'),bgtoolbar) - bgtoolbar.show() self.set_toolbox(toolbox) toolbox.show() - if hasattr(self, '_jobject'): - self._jobject.metadata['title'] = 'CartoonBuilder' - title_widget = toolbox._activity_toolbar.title - title_widget.set_size_request(title_widget.get_layout().get_pixel_size()[0] + 20, -1) self.set_canvas(self.app.main) + """ # mesh stuff self.pservice = presenceservice.get_instance() owner = self.pservice.get_owner() @@ -81,6 +73,17 @@ class CartoonBuilderActivity(activity.Activity): else: # we are creating the activity pass + """ + + def read_file(self, filepath): + Document.load(filepath) + Char.load() + Ground.load() + Sound.load() + + def write_file(self, filepath): + Document.save(filepath) + def _shared_cb(self,activity): self.initiating = True @@ -176,20 +179,6 @@ class CartoonBuilderActivity(activity.Activity): id, group_iface=self.text_chan[telepathy.CHANNEL_INTERFACE_GROUP]) self.game = ConnectGame(tube_conn, self.initiating, self) - def destroy_cb(self, data=None): - return True - - def read_file(self, filepath): - Document.load(filepath) - Char.load() - Ground.load() - Sound.load() - - #def write_file(self, filepath): - #pass - #Document.save(filepath) - - class ConnectGame(ExportedGObject): def __init__(self,tube, is_initiator, activity): super(ConnectGame,self).__init__(tube,PATH) diff --git a/Char.py b/Char.py index d335a85..91feee2 100644 --- a/Char.py +++ b/Char.py @@ -12,78 +12,97 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import os import gtk import glob from gettext import gettext as _ import Theme -import Document +from Document import Document + +PREINSTALLED = 0 +CUSTOM = 1 def load(): custom = THEMES[-1] + index = 0 + loaded = {} for i in range(Theme.TAPE_COUNT): - custom.origs[i] = Document.tape_orig(i) - custom.thumbs[i] = Document.tape_thumb(i) + orig = Document.tape[i].orig + if Document.tape[i].filename or loaded.has_key(orig): + continue + loaded[orig] = True + custom._origs[index] = orig + custom._thumbs[index] = Theme.scale(orig) + index += 1 class Char: - def __init__(self, name, file, dir, custom): + def __init__(self, name, file, dir, type): self.name = name - self.pixbuf = Theme.pixbuf(file, Theme.THUMB_SIZE) - self.custom = custom - self.thumbs = {} - self.origs = {} - self.files = [] - - if custom: - self.empty_orig = Theme.pixbuf(file) + self._thumb = Theme.pixbuf(file, Theme.THUMB_SIZE) + self._type = type + self._thumbs = {} + self._origs = {} + self._filenames = [] + + if type != CUSTOM: + for i in sorted(glob.glob(Theme.path(dir + '/*'))): + self._filenames.append(os.path.join(dir, os.path.basename(i))) + + def filename(self, index): + if self._type == CUSTOM: + return None + elif index >= len(self._filenames): + return Theme.EMPTY_FILENAME else: - self.files = sorted(glob.glob(Theme.path(dir + '/*'))) + return self._filenames[index] def thumb(self, index = None): if index == None: - return self.pixbuf + return self._thumb - pix = self.thumbs.get(index) + pix = self._thumbs.get(index) if pix == None: - if self.custom: - pix = self.pixbuf + if self._type == CUSTOM: + pix = self._thumb else: - if index < len(self.files): - pix = Theme.pixbuf(self.files[index], Theme.THUMB_SIZE) + if index < len(self._filenames): + pix = Theme.pixbuf(self._filenames[index], Theme.THUMB_SIZE) else: - pix = Theme.EMPTY_PIXBUF - self.thumbs[index] = pix + pix = Theme.EMPTY_THUMB + self._thumbs[index] = pix return pix def orig(self, index): - pix = self.origs.get(index) + pix = self._origs.get(index) if pix == None: - if self.custom: + if self._type == CUSTOM: pix = Theme.choose(lambda t, file: Theme.pixbuf(file)) if pix: - self.thumbs[index] = Theme.scale(pix) - self.origs[index] = pix + self._thumbs[index] = Theme.scale(pix) + self._origs[index] = pix else: - if index < len(self.files): - pix = Theme.pixbuf(self.files[index]) - self.origs[index] = pix + if index < len(self._filenames): + pix = Theme.pixbuf(self._filenames[index]) + self._origs[index] = pix + else: + pix = Theme.EMPTY_ORIG return pix THEMES = ( Char(_('Elephant'), 'images/pics/Elephant/bigelephant0.gif', - 'images/pics/Elephant', None), + 'images/pics/Elephant', PREINSTALLED), Char(_('Space Blob'), 'images/pics/SpaceBlob/bigblob8.gif', - 'images/pics/SpaceBlob', None), + 'images/pics/SpaceBlob', PREINSTALLED), Char(_('Turkey'), 'images/pics/Turkey/bigturkey1.gif', - 'images/pics/Turkey', None), + 'images/pics/Turkey', PREINSTALLED), None, - Char(_('Custom'), 'images/pics/custom.png', - None, True) ) + Char(_('Custom'), 'images/pics/custom.png', None, CUSTOM)) diff --git a/Document.py b/Document.py index c1714b9..5db435a 100644 --- a/Document.py +++ b/Document.py @@ -12,146 +12,114 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import os import gtk +from xml.etree.ElementTree import Element, SubElement, tostring, fromstring import Theme +import Utils -def load(filepath): - """ - def loadfromzip(self, f): - - zf = zipfile.ZipFile(filepath) - 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) - """ - pass +class Document: + tape = [] + + ground_name = None + ground_orig = None + ground_filename = None + + sound_name = None + sound_filename = None + + class Tape: + def __init__(self): + self.clean() + + def clean(self): + self.orig = Theme.EMPTY_ORIG + self.filename = Theme.EMPTY_FILENAME + + for i in range(Theme.TAPE_COUNT): + tape.append(Tape()) def save(filepath): - pass - -def tape_orig(index): - return gtk.gdk.pixbuf_new_from_file( - Theme.path('images/pics/Elephant/bigelephant0.gif')) - -def tape_thumb(index): - return gtk.gdk.pixbuf_new_from_file_at_size( - Theme.path('images/pics/Elephant/bigelephant0.gif'), - Theme.THUMB_SIZE, Theme.THUMB_SIZE) - -def tape_clean(index): - pass - -def tape_stamp(index, pixbuf): - pass - -def ground(): - return ('foo', gtk.gdk.pixbuf_new_from_file( - Theme.path('images/pics/Elephant/bigelephant1.gif'))) - -def sound(): - return ('foo', 'sounds/jungle.wav') - -""" -import zipfile -import StringIO - - pics = self.getpics(self.imgdir) - pixbuf = gtk.gdk.pixbuf_new_from_file(pics[self.imgstartindex]) - scaled_buf = pixbuf.scale_simple(IMGWIDTH,IMGHEIGHT,gtk.gdk.INTERP_BILINEAR) - self.ccismall.set_from_pixbuf(scaled_buf) - self.charlabel.set_label(os.path.split(self.imgdir)[1]) - - - def restore(self, sdata): - # THE BELOW SHOULD WORK BUT DOESN'T - #zf = StringIO.StringIO(sdata) - #self.loadfromzip(zf) - # END OF STUFF THAT DOESN'T WORK - sdd = pickle.loads(sdata) - tmpbgpath = os.path.join(TMPDIR,'back.png') - f = file(tmpbgpath,'w') - f.write(sdd['pngdata']) - f.close() - self.setback(tmpbgpath) - os.remove(tmpbgpath) - transimgpath = os.path.join(self.iconsdir,TRANSIMG) - for i in range(len(sdd['fgpixbufpaths'])): - filepath = sdd['fgpixbufpaths'][i] - if filepath == transimgpath: - continue - pixbuf = gtk.gdk.pixbuf_new_from_file(filepath) - fgpixbuf = pixbuf.scale_simple(BGWIDTH,BGHEIGHT,gtk.gdk.INTERP_BILINEAR) - self.fgpixbufs[i] = fgpixbuf - if i == 0: - self.fgpixbuf = fgpixbuf - self.drawmain() - scaled_buf = pixbuf.scale_simple(IMGWIDTH,IMGHEIGHT,gtk.gdk.INTERP_BILINEAR) - self.frameimgs[i].set_from_pixbuf(scaled_buf) - - - - def savetozip(self, f): - # print filepath - #zf = zipfile.ZipFile(filepath,'w') - zf = zipfile.ZipFile(f,'w') - # add the background file - tmpbgpath = os.path.join(TMPDIR,'back.png') - self.bgpixbuf.save(tmpbgpath,'png') - zf.write(tmpbgpath) - os.remove(tmpbgpath) - # add the frames - count = 1 - for pixbuf in self.fgpixbufs: - filename = '%02d.png' % count - filepath = os.path.join(TMPDIR,filename) - pixbuf.save(filepath,'png') - zf.write(filepath) - os.remove(filepath) - count += 1 - zf.close() - - -""" + zip = Utils.Zip(filepath, 'w') + manifest = Element('memorize') + + ground = SubElement(manifest, 'ground') + if Document.ground_filename: + ground.attrib['preinstalled'] = '1' + ground.attrib['filename'] = Document.ground_filename + else: + ground.attrib['preinstalled'] = '0' + ground.attrib['filename'] = 'ground.png' + zip.write_pixbuf('ground.png', Document.ground_orig) + ground.text = Document.ground_name + + sound = SubElement(manifest, 'sound') + if not os.path.isabs(Document.sound_filename): + sound.attrib['preinstalled'] = '1' + sound.attrib['filename'] = Document.sound_filename + else: + sound.attrib['preinstalled'] = '0' + sound.attrib['filename'] = 'sound' + zip.write(Document.sound_filename, 'sound') + sound.text = Document.sound_name + + saved = {} + tape = SubElement(manifest, 'tape') + for i in range(Theme.TAPE_COUNT): + frame = SubElement(tape, 'frame') + if Document.tape[i].filename: + frame.attrib['preinstalled'] = '1' + frame.attrib['filename'] = Document.tape[i].filename + else: + frame.attrib['preinstalled'] = '0' + arcname = saved.get(Document.tape[i].orig) + if not arcname: + arcname = saved[Document.tape[i].orig] = 'frame%03d.png' % i + zip.write_pixbuf(arcname, Document.tape[i].orig) + frame.attrib['filename'] = arcname + + zip.writestr('MANIFEST.xml', tostring(manifest, encoding='utf-8')) + zip.close() + + import shutil + shutil.copy(filepath, '/tmp/foo.zip') + +def load(filepath): + zip = Utils.Zip(filepath, 'r') + manifest = fromstring(zip.read('MANIFEST.xml')) + + ground = manifest.find('ground') + if int(ground.attrib['preinstalled']): + Document.ground_orig = Theme.pixbuf(ground.attrib['filename']) + Document.ground_filename = ground.attrib['filename'] + else: + Document.ground_orig = zip.read_pixbuf(ground.attrib['filename']) + Document.ground_name = ground.text + + sound = manifest.find('sound') + if int(sound.attrib['preinstalled']): + Document.sound_filename = sound.attrib['filename'] + else: + arcfile = sound.attrib['filename'] + sndfile = os.path.join(Theme.SESSION_PATH, 'sound.001') + file(sndfile, 'w').write(zip.read(arcfile)) + Document.sound_filename = sndfile + Document.sound_name = sound.text + + loaded = {} + for i, frame in enumerate(manifest.findall('tape/frame')): + if i >= Theme.TAPE_COUNT: + continue + if int(frame.attrib['preinstalled']): + Document.tape[i].orig = Theme.pixbuf(frame.attrib['filename']) + Document.tape[i].filename = frame.attrib['filename'] + else: + pixbuf = loaded.get(frame.attrib['filename']) + if not pixbuf: + pixbuf = zip.read_pixbuf(frame.attrib['filename']) + loaded[frame.attrib['filename']] = pixbuf + Document.tape[i].orig = pixbuf + Document.tape[i].filename = None + + zip.close() diff --git a/Ground.py b/Ground.py index 512f2ad..a200281 100644 --- a/Ground.py +++ b/Ground.py @@ -18,20 +18,32 @@ from gettext import gettext as _ from sugar.graphics.objectchooser import ObjectChooser import Theme +from Document import Document + +PREINSTALLED = 0 +CUSTOM = 1 +TEMPORARY = 2 def load(): - ground = Document.ground() - custom = Ground(ground[0], None, False) - custom._pixbuf = ground[1] - THEMES.insert(-1, custom) + if not Document.ground_filename: + custom = Ground(Document.ground_name, None, TEMPORARY) + custom._pixbuf = Document.ground_orig + THEMES.insert(-1, custom) class Ground: - def __init__(self, name, file, custom): + def __init__(self, name, file, type): self.name = name + self._file = file if file: self._pixbuf = Theme.pixbuf(file) - self._custom = custom + self._type = type self._thumb = None + def filename(self): + if self._type == PREINSTALLED: + return self._file + else: + return None + def thumb(self): if not self._thumb: self._thumb = Theme.scale(self._pixbuf) @@ -41,28 +53,27 @@ class Ground: return self._pixbuf def change(self): - if self._custom in (None, False): + if self._type != CUSTOM: return self - else: - return Theme.choose(lambda title, file: Ground(title, file, False)) + return Theme.choose(lambda title, file: Ground(title, file, TEMPORARY)) -THEMES = ( - Ground(_('Saturn'), 'images/backpics/bigbg01.gif', None), - Ground(_('Snowflakes'), 'images/backpics/bigbg02.gif', None), - Ground(_('Eye'), 'images/backpics/bigbg03.gif', None), - Ground(_('Blobs'), 'images/backpics/bigbg04.gif', None), - Ground(_('Star Night'), 'images/backpics/bigbg05.gif', None), - Ground(_('Forest'), 'images/backpics/bigbg06.gif', None), - Ground(_('Spiral'), 'images/backpics/bigbg07.gif', None), - Ground(_('Beam'), 'images/backpics/bigbg08.gif', None), - Ground(_('Cloth'), 'images/backpics/bigbg09.gif', None), - Ground(_('Faces'), 'images/backpics/bigbg10.gif', None), - Ground(_('Leaves'), 'images/backpics/bigbg11.gif', None), - Ground(_('Vegetables'), 'images/backpics/bigbg12.gif', None), - Ground(_('Spotlight'), 'images/backpics/bigbg13.gif', None), - Ground(_('Strips'), 'images/backpics/bigbg14.gif', None), - Ground(_('Scene'), 'images/backpics/bigbg15.gif', None), - Ground(_('Rhombs'), 'images/backpics/bigbg16.gif', None), - Ground(_('Milky Way'), 'images/backpics/bigbg17.gif', None), +THEMES = [ + Ground(_('Saturn'), 'images/backpics/bigbg01.gif', PREINSTALLED), + Ground(_('Snowflakes'), 'images/backpics/bigbg02.gif', PREINSTALLED), + Ground(_('Eye'), 'images/backpics/bigbg03.gif', PREINSTALLED), + Ground(_('Blobs'), 'images/backpics/bigbg04.gif', PREINSTALLED), + Ground(_('Star Night'), 'images/backpics/bigbg05.gif', PREINSTALLED), + Ground(_('Forest'), 'images/backpics/bigbg06.gif', PREINSTALLED), + Ground(_('Spiral'), 'images/backpics/bigbg07.gif', PREINSTALLED), + Ground(_('Beam'), 'images/backpics/bigbg08.gif', PREINSTALLED), + Ground(_('Cloth'), 'images/backpics/bigbg09.gif', PREINSTALLED), + Ground(_('Faces'), 'images/backpics/bigbg10.gif', PREINSTALLED), + Ground(_('Leaves'), 'images/backpics/bigbg11.gif', PREINSTALLED), + Ground(_('Vegetables'), 'images/backpics/bigbg12.gif', PREINSTALLED), + Ground(_('Spotlight'), 'images/backpics/bigbg13.gif', PREINSTALLED), + Ground(_('Strips'), 'images/backpics/bigbg14.gif', PREINSTALLED), + Ground(_('Scene'), 'images/backpics/bigbg15.gif', PREINSTALLED), + Ground(_('Rhombs'), 'images/backpics/bigbg16.gif', PREINSTALLED), + Ground(_('Milky Way'), 'images/backpics/bigbg17.gif', PREINSTALLED), None, - Ground(_('Custom'), 'images/backpics/custom.png', True) ) + Ground(_('Custom'), 'images/backpics/custom.png', CUSTOM)] diff --git a/Sound.py b/Sound.py index 86a59c5..12b5ede 100644 --- a/Sound.py +++ b/Sound.py @@ -19,35 +19,41 @@ from glob import glob from gettext import gettext as _ import Theme +from Document import Document from Utils import * from sugar.activity.activity import get_bundle_path -TYPE_PREISTALLED = 0 -TYPE_CUSTOM = 1 -TYPE_JOURNAL = 2 +PREISTALLED = 0 +CUSTOM = 1 +TEMPORARY = 2 +JOURNAL = 3 def load(): - sound = Document.sound() - custom = Sound(sound[0], 'images/sounds/speaker.png', sound[1]) - THEMES.insert(-1, custom) + if os.path.isabs(Document.sound_filename): + custom = Sound(Document.sound_name, 'images/sounds/speaker.png', + Document.sound_filename, TEMPORARY) + THEMES.insert(-1, custom) class Sound: playing = False current = None player = None - def __init__(self, name, file, sound, type): + def __init__(self, name, imgfile, soundfile, type): self.name = name - self._thumb = Theme.pixbuf(file, THUMB_SIZE) + self._thumb = Theme.pixbuf(imgfile, THUMB_SIZE) self._type = type - if type == TYPE_JOURNAL: + if type == JOURNAL: l = sorted(glob(os.path.join(Theme.SESSION_PATH, 'sound*'))) - self._sound = os.path.join(Theme.SESSION_PATH, + self._soundfile = os.path.join(Theme.SESSION_PATH, 'sound.%03d' % (len(l)+1)) - os.rename(sound, self._sound) + os.rename(soundfile, self._soundfile) else: - self._sound = sound + self._soundfile = soundfile + + def filename(self): + return self._soundfile def thumb(self): return self._thumb @@ -55,38 +61,39 @@ class Sound: def change(self): out = self - if self._type == TYPE_CUSTOM: + if self._type == CUSTOM: out = Theme.choose( lambda title, file: Sound(title, - 'images/sounds/speaker.png', file, TYPE_JOURNAL)) + 'images/sounds/speaker.png', file, JOURNAL)) if not out: return None Sound.current = self if not Sound.playing: return out Sound.player.set_state(gst.STATE_NULL) - if not out._sound: return out + if len(out._soundfile) == 0: return out - Sound.player.set_property('uri', 'file://' + Theme.path(out._sound)) + Sound.player.set_property('uri', 'file://' + Theme.path(out._soundfile)) Sound.player.set_state(gst.STATE_NULL) Sound.player.set_state(gst.STATE_PLAYING) return out -THEMES = ( +THEMES = [ Sound(_('Gobble'), 'images/sounds/speaker.png', 'sounds/gobble.wav', - TYPE_PREISTALLED), + PREISTALLED), Sound(_('Funk'), 'images/sounds/speaker.png', 'sounds/funk.wav', - TYPE_PREISTALLED), + PREISTALLED), Sound(_('Giggle'), 'images/sounds/speaker.png', 'sounds/giggle.wav', - TYPE_PREISTALLED), + PREISTALLED), Sound(_('Jungle'), 'images/sounds/speaker.png', 'sounds/jungle.wav', - TYPE_PREISTALLED), - Sound(_('Mute'), 'images/sounds/mute.png', None, - TYPE_PREISTALLED), + PREISTALLED), + Sound(_('Mute'), 'images/sounds/mute.png', '', + PREISTALLED), None, Sound(_('Custom'), 'images/sounds/custom.png', None, - TYPE_CUSTOM) ) + CUSTOM)] + Sound.current = THEMES[0] def play(): diff --git a/Theme.py b/Theme.py index 8329bb6..343adfb 100644 --- a/Theme.py +++ b/Theme.py @@ -96,11 +96,13 @@ 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 scale(pixbuf, size = THUMB_SIZE): return pixbuf.scale_simple(size, size, gtk.gdk.INTERP_BILINEAR) +EMPTY_FILENAME = 'images/pics/empty.png' +EMPTY_ORIG = pixbuf(EMPTY_FILENAME) +EMPTY_THUMB = scale(EMPTY_ORIG) + def choose(out_fun): from sugar.graphics.objectchooser import ObjectChooser diff --git a/Utils.py b/Utils.py index 7931776..6983c56 100644 --- a/Utils.py +++ b/Utils.py @@ -12,8 +12,10 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +import os import gtk import pango +import zipfile from Theme import * import sugar.graphics @@ -21,18 +23,33 @@ from sugar.graphics import style from sugar.graphics.toolbutton import ToolButton from sugar.graphics.icon import Icon -class FileInstanceVariable: - def __init__(self, value = None): - self.value = value - def get(self): - return self.value - - def set(self, value): - self.value = value - - def __getitem__(self, key): - return self.value[key] +class Zip(zipfile.ZipFile): + def __init__(self, *args): + zipfile.ZipFile.__init__(self, *args) + + """ + def write_pixbuf(self, arcfile, pixbuf): + def push(data, buffer): + buffer += data + + buffer = '' + pixbuf.save_to_callback(push, 'png', user_data=buffer) + self.writestr(arcfile, buffer) + """ + + def write_pixbuf(self, arcfile, pixbuf): + tmpfile = os.path.join(SESSION_PATH, 'tmp.png') + pixbuf.save(tmpfile, 'png') + self.write(tmpfile, arcfile) + os.unlink(tmpfile) + + def read_pixbuf(self, arcfile): + tmpfile = os.path.join(SESSION_PATH, 'tmp.png') + file(tmpfile, 'w').write(self.read(arcfile)) + out = gtk.gdk.pixbuf_new_from_file(tmpfile) + os.unlink(tmpfile) + return out class ComboBox(sugar.graphics.combobox.ComboBox): def __init__(self): diff --git a/View.py b/View.py index 98815df..b657969 100644 --- a/View.py +++ b/View.py @@ -30,7 +30,7 @@ import Theme import Char import Ground import Sound -import Document +from Document import Document from Utils import * class FrameWidget(gtk.DrawingArea): @@ -160,7 +160,7 @@ class View: self.tape.append(frame) frame_image = gtk.Image() - frame_image.set_from_pixbuf(Document.tape_thumb(i)) + frame_image.set_from_pixbuf(Theme.EMPTY_THUMB) frame.add(frame_image) filmstrip = gtk.Image() @@ -170,7 +170,6 @@ class View: tape.pack_start(frame_box, False, False) self.tape_selected = -1 - self._tape_cb(None, None, 0) # left control box @@ -232,33 +231,39 @@ class View: yellowbox.show_all() self.main = yellowbox - yellowbox.connect_after('map', self.map_cb) + yellowbox.connect_after('map', self.restore) - def map_cb(self, widget): - def new_combo(themes, cb): + def restore(self, widget): + def new_combo(themes, cb, selname = None, closure = None): combo = ComboBox() - - for i in themes: - if not i: + sel = 0 + + for i, theme in enumerate(themes): + if theme: + combo.append_item(theme, text = theme.name, + size = (Theme.THUMB_SIZE, Theme.THUMB_SIZE), + pixbuf = theme.thumb()) + if theme.name == selname: + sel = i + else: combo.append_separator() - continue - - combo.append_item(i, text = i.name, - size = (Theme.THUMB_SIZE, Theme.THUMB_SIZE), - pixbuf = i.thumb()) - combo.connect('changed', cb) - combo.set_active(0) + combo.connect('changed', cb, closure) + combo.set_active(sel) combo.show() return combo self.controlbox.pack_start(new_combo(Char.THEMES, self._char_cb), True, False) - self.controlbox.pack_start(new_combo(Ground.THEMES, self._ground_cb), - True, False) - self.controlbox.pack_start(new_combo(Sound.THEMES, self._combo_cb), - True, False) + self.controlbox.pack_start(new_combo(Ground.THEMES, self._combo_cb, + Document.ground_name, self._ground_cb), True, False) + self.controlbox.pack_start(new_combo(Sound.THEMES, self._combo_cb, + Document.sound_name, self._sound_cb), True, False) + + for i in range(Theme.TAPE_COUNT): + self.tape[i].child.set_from_pixbuf(Theme.scale(Document.tape[i].orig)) + self._tape_cb(None, None, 0) return False @@ -277,12 +282,12 @@ class View: def clear_tape(self): for i in range(TAPE_COUNT): - Document.tape_clean(i) - self.screen.fgpixbuf = Document.tape_orig(self.tape_selected) + Document.tape[i].clean() + self.screen.fgpixbuf = Document.tape[self.tape_selected].orig self.screen.draw() def _play_tape(self): - self.screen.fgpixbuf = Document.tape_orig(self.play_tape_num) + self.screen.fgpixbuf = Document.tape[self.play_tape_num].orig self.screen.draw() self.play_tape_num += 1 @@ -306,7 +311,7 @@ class View: old_tape.modify_bg(gtk.STATE_PRELIGHT,gtk.gdk.color_parse(BLACK)) self.tape_selected = index - self.screen.fgpixbuf = Document.tape_orig(index) + self.screen.fgpixbuf = Document.tape[index].orig self.screen.draw() def _frame_cb(self, widget, event, frame): @@ -314,23 +319,24 @@ class View: if not orig: return thumb = self.char.thumb(frame) - Document.tape_stamp(self.tape_selected, orig) + Document.tape[self.tape_selected].orig = orig + Document.tape[self.tape_selected].filename = self.char.filename(frame) + 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): + def _char_cb(self, widget, closure): self.char = widget.props.value for i in range(len(self.frames)): self.frames[i].set_from_pixbuf(self.char.thumb(i)) - def _combo_cb(self, widget): + def _combo_cb(self, widget, cb): choice = widget.props.value.change() if not choice: widget.set_active(self._prev_combo_selected[widget]) - return None + return if id(choice) != id(widget.props.value): pos = widget.get_active() @@ -340,13 +346,18 @@ class View: widget.set_active(pos) self._prev_combo_selected[widget] = widget.get_active() - return choice + cb(choice) + + def _ground_cb(self, choice): + self.screen.bgpixbuf = choice.orig() + self.screen.draw() + Document.ground_name = choice.name + Document.ground_orig = choice.orig() + Document.ground_filename = choice.filename() - def _ground_cb(self, widget): - choice = self._combo_cb(widget) - if choice: - self.screen.bgpixbuf = choice.orig() - self.screen.draw() + def _sound_cb(self, choice): + Document.sound_name = choice.name + Document.sound_filename = choice.filename() def _screen_size_cb(self, widget, aloc): size = min(aloc.width, aloc.height) diff --git a/images/pics/empty.png b/images/pics/empty.png new file mode 100644 index 0000000..c14e8c5 --- /dev/null +++ b/images/pics/empty.png Binary files differ -- cgit v0.9.1