[gcompris/gcomprixogoo] Added support for loading/saving z-order.



commit 0828f7fac42e370007f8bd19d6fdeda020ffc259
Author: Bruno Coudoin <bruno coudoin free fr>
Date:   Wed Nov 11 17:25:29 2009 +0100

    Added support for loading/saving z-order.

 src/anim-activity/AnimItem.py |   31 ++++++++++++++------
 src/anim-activity/Timeline.py |    2 +-
 src/anim-activity/anim.py     |   62 ++++++++++++++++++++++++++++++++++------
 3 files changed, 74 insertions(+), 21 deletions(-)
---
diff --git a/src/anim-activity/AnimItem.py b/src/anim-activity/AnimItem.py
index 426a635..a61f302 100644
--- a/src/anim-activity/AnimItem.py
+++ b/src/anim-activity/AnimItem.py
@@ -31,8 +31,12 @@ import sys
 
 class AnimItem:
     anim = None
+    next_id = 0
 
     def __init__(self, anim_):
+        self.id = AnimItem.next_id
+        AnimItem.next_id += 1
+
         self.init(anim_)
         gcompris.sound.play_ogg("sounds/bleep.wav")
         # We keep the timeline index to which we are visible
@@ -50,7 +54,7 @@ class AnimItem:
 
     def init(self, anim_):
         AnimItem.anim = anim_
-        self.rootitem = goocanvas.Group(parent = anim_.rootitem)
+        self.rootitem = goocanvas.Group(parent = anim_.doc.rootitem)
         self.drawing_area = anim_.drawing_area
 
         self.step = 1
@@ -61,6 +65,7 @@ class AnimItem:
         self.old_x = 0
         self.old_y = 0
 
+        self.rootitem.set_data("id", self.id)
 
     # Return the type name of the managed object
     def type_name(self):
@@ -97,15 +102,19 @@ class AnimItem:
         return result
 
     def __getstate__(self):
-        return [self.visible, self.filled,
+        return [self.id,
+                self.visible, self.filled,
                 self.timelineDump(self.timeline),
                 self.save_addon()]
 
     def __setstate__(self, dict):
-        self.visible = dict[0]
-        self.filled = dict[1]
-        self.timeline = self.timelineRestore(dict[2])
-        self.load_addon(dict[3])
+        self.id = dict[0]
+        if AnimItem.next_id <= self.id:
+            AnimItem.next_id = self.id + 1
+        self.visible = dict[1]
+        self.filled = dict[2]
+        self.timeline = self.timelineRestore(dict[3])
+        self.load_addon(dict[4])
         self.anchor = None
 
     # Some item types need to save/load more
@@ -117,8 +126,9 @@ class AnimItem:
         pass
 
     def dump(self):
-        print "Dump AnimItem:"
-        print self.timeline
+        print "Dump AnimItem: ", self.id
+        print self.rootitem.get_data("id")
+        #print self.timeline
         pass
 
 
@@ -308,6 +318,7 @@ class AnimItem:
         child_num = rootparent.find_child (parent);
         if child_num < rootparent.get_n_children() - 1:
             rootparent.move_child (child_num, child_num + 1);
+            AnimItem.anim.doc.save_zorder()
 
     def lower(self):
         parent = self.item.get_parent()
@@ -315,6 +326,7 @@ class AnimItem:
         child_num = rootparent.find_child (parent);
         if child_num > 0:
             rootparent.move_child (child_num, child_num - 1);
+            AnimItem.anim.doc.save_zorder()
 
 
     def rotate(self, angle):
@@ -429,14 +441,13 @@ class AnimItem:
             lastkey = k
             lastval = v
 
-
     # Return the (properties, transformation) of this
     # object.
     def get(self):
         result = {}
         for prop in self.get_properties():
             result[prop] = self.item.get_property(prop)
-        return(result, self.item.get_transform())
+        return( result, self.item.get_transform() )
 
     # Apply the given properties and transformation to this
     # object.
diff --git a/src/anim-activity/Timeline.py b/src/anim-activity/Timeline.py
index 0aa3775..91de608 100644
--- a/src/anim-activity/Timeline.py
+++ b/src/anim-activity/Timeline.py
@@ -137,7 +137,7 @@ class Timeline:
         self.current_time = item.get_data("time")
 
         # Let anim knows there is a new time set
-        self.anim.refresh(self.get_time())
+        self.anim.doc.refresh(self.get_time())
 
     def lastmark_it(self, item):
         # Unmark previous mark
diff --git a/src/anim-activity/anim.py b/src/anim-activity/anim.py
index bbb68f4..bc2f39e 100644
--- a/src/anim-activity/anim.py
+++ b/src/anim-activity/anim.py
@@ -197,10 +197,10 @@ class Gcompris_anim:
     return
 
   def repeat(self):
-    print("Gcompris_anim repeat.")
+    pass
 
   def config(self):
-    print("Gcompris_anim config.")
+    pass
 
   def key_press(self, keyval, commit_str, preedit_str):
     #
@@ -625,12 +625,6 @@ class Gcompris_anim:
 
     return False
 
-  def refresh(self, time):
-    # We keep all object in a unique list
-    # Here we order them to refresh them at the given time
-    for item in self.doc.animlist:
-      item.display_at_time(time)
-
   def refresh_loop(self):
     self.doc.timeline.next()
     return True
@@ -688,14 +682,56 @@ class Document:
 
   def __init__(self, anim_):
     self.anim = anim_
+    # This is the time line object that if at to bottom of
+    # the screen.
     self.timeline = Timeline(self.anim)
     # The list of all the user's objects
     self.animlist = []
-    self.item_id = 0
+
+    # This stores the Z order list of items at a given time.
+    # The key is the time (number) and the value is
+    # a list of items id in the order they appear on screen.
+    self.zorder = {}
+
+    # Create our rootitem. We put each canvas item in it so at the end we
+    # only have to kill it. The canvas deletes all the items it contains
+    # automaticaly.
+    self.rootitem = goocanvas.Group(
+      parent =  self.anim.gcomprisBoard.canvas.get_root_item())
 
     self.pickle_protocol = 2
     self.format_string = { 'gcompris' : 'GCompris anim 3 cPikle file' }
 
+  def refresh(self, time):
+    # We keep all object in a unique list
+    # Here we call them to give them a chance to
+    # display them if they have to
+    for item in self.animlist:
+      item.display_at_time(time)
+    self.restore_zorder()
+
+  def save_zorder(self):
+    z_order = []
+    for i in range(self.rootitem.get_n_children()):
+      item = self.rootitem.get_child(i)
+      if item.props.visibility == goocanvas.ITEM_VISIBLE:
+        z_order.append(item.get_data("id"))
+
+    self.zorder[self.timeline.get_time()] = z_order
+
+  def restore_zorder(self):
+    z_order = []
+    if self.timeline.get_time() in self.zorder:
+      z_order = self.zorder[self.timeline.get_time()]
+    for i in range(self.rootitem.get_n_children()):
+      item = self.rootitem.get_child(i)
+      item_id = item.get_data("id")
+      try:
+        z_index = z_order.index(item_id)
+        self.rootitem.move_child(i, z_index);
+      except ValueError:
+        pass
+
 
   def anim_to_file(self, filename):
 
@@ -710,6 +746,9 @@ class Document:
     # Save the animation
     pickle.dump(self.animlist, file, self.pickle_protocol)
 
+    # Save the z order
+    pickle.dump(self.zorder, file, self.pickle_protocol)
+
     file.close()
 
 
@@ -737,7 +776,10 @@ class Document:
           for item in self.animlist:
             item.restore(self.anim)
 
-          self.anim.refresh(self.timeline.get_time())
+          self.zorder = pickle.load(file)
+
+          # Restore is complete
+          self.refresh(self.timeline.get_time())
           self.timeline.set_time(0)
         else:
           print "ERROR: Unrecognized file format, file", filename, ' has description : ', desc



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