[gcompris/gcomprixogoo] Now save/restore works with rectangle. The last time line mark is



commit e282df91da12e13433b1a2e00c15648e936eb8c7
Author: Bruno Coudoin <bruno coudoin free fr>
Date:   Sun Nov 1 12:42:14 2009 +0100

    Now save/restore works with rectangle. The last time line mark is
    also saved / restored.
    Next step is to implement save/restore for other objects than rectangle.

 src/anim-activity/AnimItem.py |   61 +++++++++++++++++-----------------------
 src/anim-activity/Timeline.py |   19 +++++++-----
 src/anim-activity/anim.py     |   17 +++++++----
 3 files changed, 48 insertions(+), 49 deletions(-)
---
diff --git a/src/anim-activity/AnimItem.py b/src/anim-activity/AnimItem.py
index 0759906..d796d5b 100644
--- a/src/anim-activity/AnimItem.py
+++ b/src/anim-activity/AnimItem.py
@@ -43,6 +43,9 @@ class AnimItem:
         # (properties, transformation).
         self.timeline = {}
 
+        # Wether an item is filled or not
+        self.filled = False
+
 
     def init(self, anim_):
         AnimItem.anim = anim_
@@ -84,12 +87,13 @@ class AnimItem:
         return result
 
     def __getstate__(self):
-        print self.timeline
-        return [self.visible, self.timelineDump(self.timeline)]
+        return [self.visible, self.filled,
+                self.timelineDump(self.timeline)]
 
     def __setstate__(self, dict):
         self.visible = dict[0]
-        self.timeline = self.timelineRestore(dict[1])
+        self.filled = dict[1]
+        self.timeline = self.timelineRestore(dict[2])
         self.anchor = None
 
     def dump(self):
@@ -99,11 +103,15 @@ class AnimItem:
 
 
     def restore(self, anim_):
-        print "restore AnimItem:"
-        print self.timeline
         self.init(anim_)
-        print AnimItem.anim
-        print AnimItem.anim.rootitem
+
+    # Each real item implementors call init_item to complete
+    # The item initialization.
+    def init_item(self):
+        self.item.set_data("AnimItem", self)
+        self.item.connect("button_press_event", AnimItem.anim.item_event)
+        self.item.connect("button_release_event", AnimItem.anim.item_event)
+        self.item.connect("motion_notify_event", AnimItem.anim.item_event)
 
     def test(self):
         self.set_visible(0, 10)
@@ -613,22 +621,14 @@ class Anchor:
 #
 class AnimItemRect(AnimItem):
 
-    filled = False
-
-    def dump(self):
-        print "Dump Rect:"
-        AnimItem.dump(self)
-
+    # This is called when an animation file is loaded
     def restore(self, anim_):
         AnimItem.restore(self, anim_)
         self.item = \
             goocanvas.Rect(
                 parent = self.rootitem
                 )
-        self.item.set_data("AnimItem", self)
-        self.item.connect("button_press_event", anim_.item_event)
-        self.item.connect("button_release_event", anim_.item_event)
-        self.item.connect("motion_notify_event", anim_.item_event)
+        AnimItem.init_item(self)
 
     def __init__(self, anim, x, y, color_fill, color_stroke, line_width):
         AnimItem.__init__(self, anim)
@@ -648,10 +648,8 @@ class AnimItemRect(AnimItem):
             self.filled = True
             self.item.set_properties(fill_color_rgba = color_fill)
 
-        self.item.set_data("AnimItem", self)
-        self.item.connect("button_press_event", anim.item_event)
-        self.item.connect("button_release_event", anim.item_event)
-        self.item.connect("motion_notify_event", anim.item_event)
+        AnimItem.init_item(self)
+
 
     # Fixme, should replace set_bounds in resize cases
     def scale_bounds(self, p1, p2):
@@ -700,19 +698,19 @@ class AnimItemRect(AnimItem):
 
     def fill(self, fill, stroke):
         gcompris.sound.play_ogg("sounds/paint1.wav")
+        print "Fill=", self.filled
         if self.filled:
             self.item.set_properties(fill_color_rgba = fill,
                                      stroke_color_rgba = stroke)
         else:
             self.item.set_properties(stroke_color_rgba = stroke)
+        self.save_at_time(AnimItem.anim.timeline.get_time())
 
 #
 # The ellipse (filled or not)
 #
 class AnimItemEllipse(AnimItem):
 
-    filled = False
-
     def __init__(self, anim, center_x, center_y,
                  color_fill, color_stroke, line_width):
         AnimItem.__init__(self, anim)
@@ -732,10 +730,8 @@ class AnimItemEllipse(AnimItem):
             self.filled = True
             self.item.set_properties(fill_color_rgba = color_fill)
 
-        self.item.set_data("AnimItem", self)
-        self.item.connect("button_press_event", anim.item_event)
-        self.item.connect("button_release_event", anim.item_event)
-        self.item.connect("motion_notify_event", anim.item_event)
+        AnimItem.init_item(self)
+
 
     def set_bounds(self, p1, p2):
         (x1, y1, x2, y2) = self.snap_obj_to_grid(p1, p2)
@@ -805,10 +801,8 @@ class AnimItemLine(AnimItem):
                 line_width = line_width,
                 line_cap = cairo.LINE_CAP_ROUND)
 
-        self.item.set_data("AnimItem", self)
-        self.item.connect("button_press_event", anim.item_event)
-        self.item.connect("button_release_event", anim.item_event)
-        self.item.connect("motion_notify_event", anim.item_event)
+        AnimItem.init_item(self)
+
 
     def set_bounds(self, p1, p2):
         (x1, y1, x2, y2) = self.snap_point_to_grid(p1, p2)
@@ -873,10 +867,7 @@ class AnimItemPixmap(AnimItem):
                 x = x,
                 y = y)
 
-        self.item.set_data("AnimItem", self)
-        self.item.connect("button_press_event", anim.item_event)
-        self.item.connect("button_release_event", anim.item_event)
-        self.item.connect("motion_notify_event", anim.item_event)
+        AnimItem.init_item(self)
 
         self.sx = self.sy = 1.0
 
diff --git a/src/anim-activity/Timeline.py b/src/anim-activity/Timeline.py
index 88c58b7..64c8507 100644
--- a/src/anim-activity/Timeline.py
+++ b/src/anim-activity/Timeline.py
@@ -39,7 +39,7 @@ class Timeline:
         self.selected = None
         self.timelinelist = []
         self.current_time = 0
-        self.last_mark = -1
+        self.lastmark = -1
 
     def hide(self):
         self.rootitem.props.visibility = goocanvas.ITEM_INVISIBLE
@@ -113,7 +113,7 @@ class Timeline:
     def next(self):
         self.current_time += 1
         if self.current_time >= min(len(self.timelinelist),
-                                    self.last_mark + 1):
+                                    self.lastmark + 1):
             self.current_time = 0
         self.select_it(self.timelinelist[self.current_time])
 
@@ -122,7 +122,7 @@ class Timeline:
         self.current_time -= 1
         if self.current_time < 0:
             self.current_time = min(len(self.timelinelist) - 1,
-                                    self.last_mark)
+                                    self.lastmark)
 
         self.select_it(self.timelinelist[self.current_time])
 
@@ -141,15 +141,18 @@ class Timeline:
 
     def lastmark_it(self, item):
         # Unmark previous mark
-        if self.last_mark >= 0:
-            marked_item = self.timelinelist[self.last_mark]
+        if self.lastmark >= 0:
+            marked_item = self.timelinelist[self.lastmark]
             marked_item.set_properties(stroke_color_rgba = self.default_stroke)
 
         item.set_properties(stroke_color_rgba = self.marked_stroke)
-        self.last_mark = item.get_data("time")
+        self.lastmark = item.get_data("time")
 
-    def get_last_mark(self):
-        return self.last_mark
+    def get_lastmark(self):
+        return self.lastmark
+
+    def set_lastmark(self, lastmark):
+        self.lastmark_it(self.timelinelist[lastmark])
 
     #
     def timeline_item_event(self, item, target, event):
diff --git a/src/anim-activity/anim.py b/src/anim-activity/anim.py
index 4e0111d..7f4f9d2 100644
--- a/src/anim-activity/anim.py
+++ b/src/anim-activity/anim.py
@@ -743,11 +743,14 @@ class Gcompris_anim:
     # Save the descriptif frame:
     pickle.dump(fles.format_string['gcompris'], file, self.pickle_protocol)
 
+    # Save the last mark
+    pickle.dump(self.timeline.get_lastmark(), file, self.pickle_protocol)
+
+    # Save the animation
     pickle.dump(self.animlist, file, self.pickle_protocol)
+
     file.close()
 
-    for item in self.animlist:
-      item.dump()
 
   def file_to_anim(self, filename):
 
@@ -764,13 +767,15 @@ class Gcompris_anim:
       if 'desc' != fles.format_string['gcompris']:
         if (desc == 'GCompris draw 3 cPikle file'
             or desc == 'GCompris anim 3 cPikle file'):
+
           for item in self.animlist:
             item.delete()
-          print "load"
+
+          self.timeline.set_lastmark(pickle.load(file))
           self.animlist = pickle.load(file)
           for item in self.animlist:
             item.restore(self)
-            item.dump()
+
           self.refresh(self.timeline.get_time())
         else:
           print "ERROR: Unrecognized file format, file", filename, ' has description : ', desc
@@ -795,11 +800,11 @@ class Gcompris_anim:
 #
 ###############################################
 def general_save(filename, filetype, fles):
-  print "filename=%s filetype=%s" %(filename, filetype)
+  #print "filename=%s filetype=%s" %(filename, filetype)
   fles.anim_to_file(filename)
 
 def general_restore(filename, filetype, fles):
-  print "general_restore : ", filename, " type ",filetype
+  #print "general_restore : ", filename, " type ",filetype
   fles.file_to_anim(filename)
 
 def image_selected(image, fles):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]