[gcompris/gcomprixogoo] Started the implementation of the load/save feature of anim.



commit d257a967d71c07835ad08ec6c107d3022225c279
Author: Bruno Coudoin <bruno coudoin free fr>
Date:   Tue Oct 27 00:36:57 2009 +0100

    Started the implementation of the load/save feature of anim.
    For now, the save 'does something' but the load crashes GCompris.

 src/anim-activity/AnimItem.py |   38 +++++++++++++-----------
 src/anim-activity/anim.py     |   63 ++++++++++++++++++++++++++++++++++------
 2 files changed, 74 insertions(+), 27 deletions(-)
---
diff --git a/src/anim-activity/AnimItem.py b/src/anim-activity/AnimItem.py
index 2e620c6..eed54bd 100644
--- a/src/anim-activity/AnimItem.py
+++ b/src/anim-activity/AnimItem.py
@@ -29,11 +29,12 @@ import cairo
 import sys
 
 class AnimItem:
+    anim = None
 
-    def __init__(self, anim):
-        self.anim = anim
-        self.rootitem = goocanvas.Group(parent = anim.rootitem)
-        self.drawing_area = anim.drawing_area
+    def __init__(self, anim_):
+        AnimItem.anim = anim_
+        self.rootitem = goocanvas.Group(parent = anim_.rootitem)
+        self.drawing_area = anim_.drawing_area
         gcompris.sound.play_ogg("sounds/bleep.wav")
 
         self.step = 1
@@ -129,7 +130,7 @@ class AnimItem:
     def snap_to_grid(self, x, y):
 
         if self.item:
-            (x, y) = self.anim.gcomprisBoard.canvas.\
+            (x, y) = AnimItem.anim.gcomprisBoard.canvas.\
                 convert_from_item_space(self.item, x, y)
 
         # Check drawing boundaries
@@ -152,7 +153,7 @@ class AnimItem:
         result.append(float(self.drawing_area[1] + tmp*self.step))
 
         if self.item:
-            return self.anim.gcomprisBoard.canvas.\
+            return AnimItem.anim.gcomprisBoard.canvas.\
                 convert_to_item_space(self.item, x, y)
 
         return result
@@ -212,9 +213,9 @@ class AnimItem:
     def delete(self):
         gcompris.sound.play_ogg("sounds/eraser1.wav",
                                 "sounds/eraser2.wav")
-        self.delete_at_time(self.anim.timeline.get_time())
+        self.delete_at_time(AnimItem.anim.timeline.get_time())
         if not self.visible:
-            self.anim.deleteItem(self)
+            AnimItem.anim.deleteItem(self)
         self.show(False)
 
     def raise_(self):
@@ -236,7 +237,7 @@ class AnimItem:
         bounds = self.item.get_bounds()
         (cx, cy) = ( bounds.x1 + (bounds.x2-bounds.x1)/2,
                      bounds.y1 + (bounds.y2-bounds.y1)/2 )
-        (cx, cy) = self.anim.gcomprisBoard.canvas.\
+        (cx, cy) = AnimItem.anim.gcomprisBoard.canvas.\
             convert_to_item_space(self.item, cx, cy)
         self.item.rotate(angle, cx, cy)
 
@@ -257,24 +258,24 @@ class AnimItem:
     def create_item_event(self, item, target):
 
         self.refpoint = None
-        self.save_at_time(self.anim.timeline.get_time())
+        self.save_at_time(AnimItem.anim.timeline.get_time())
         # By default, an object is displayed till the timeline end
-        self.set_visible_to_end(self.anim.timeline.get_time())
+        self.set_visible_to_end(AnimItem.anim.timeline.get_time())
 
     def create_item_drag_event(self, item, target, event):
         if event.type == gtk.gdk.BUTTON_RELEASE:
-            self.save_at_time(self.anim.timeline.get_time())
+            self.save_at_time(AnimItem.anim.timeline.get_time())
 
         if (event.type == gtk.gdk.MOTION_NOTIFY
             and event.state & gtk.gdk.BUTTON1_MASK):
 
             if not self.refpoint:
-                self.refpoint = self.anim.gcomprisBoard.canvas.\
+                self.refpoint = AnimItem.anim.gcomprisBoard.canvas.\
                     convert_from_item_space(target,
                                           self.get_x1y1()[0],
                                           self.get_x1y1()[1])
 
-            (x, y) = self.anim.gcomprisBoard.canvas.\
+            (x, y) = AnimItem.anim.gcomprisBoard.canvas.\
                 convert_from_item_space(item, event.x, event.y)
 
             self.set_bounds(
@@ -288,7 +289,7 @@ class AnimItem:
         if event.type == gtk.gdk.BUTTON_RELEASE:
             self.old_x = 0
             self.old_y = 0
-            self.save_at_time(self.anim.timeline.get_time())
+            self.save_at_time(AnimItem.anim.timeline.get_time())
         elif event.type == gtk.gdk.BUTTON_PRESS:
             self.old_x = event.x
             self.old_y = event.y
@@ -369,8 +370,8 @@ class AnimItem:
 class Anchor:
     # group contains normal items.
 
-    A_WIDTH = 14
-    A_HEIGHT = 14
+    A_WIDTH = -1
+    A_HEIGHT = -1
 
     # anchortype
     ANCHOR_NW = 1
@@ -397,6 +398,9 @@ class Anchor:
     def __init__(self, animitem):
         self.animitem = animitem
 
+        self.A_WIDTH = animitem.anim.DEFAULT_ANCHOR_SIZE
+        self.A_HEIGHT = animitem.anim.DEFAULT_ANCHOR_SIZE
+
         self.anchorgroup = None
         self.anchors = []
 
diff --git a/src/anim-activity/anim.py b/src/anim-activity/anim.py
index 61c39f0..e5228a6 100644
--- a/src/anim-activity/anim.py
+++ b/src/anim-activity/anim.py
@@ -69,31 +69,30 @@ class Gcompris_anim:
 
     # There is two board in the same code
     # here the diff in parameters
+    self.pickle_protocol = 2
     if self.gcomprisBoard.mode == 'draw':
-      self.format_string = { 'gcompris' : 'GCompris draw 2 cPikle file',
-                             'svg' : 'GCompris draw 2 svg file'
+      self.format_string = { 'gcompris' : 'GCompris draw 3 cPikle file',
                              }
     else:
-      self.format_string = { 'gcompris' : 'GCompris anim 2 cPikle file',
-                             'svg' : 'GCompris anim 2 svg file'
+      self.format_string = { 'gcompris' : 'GCompris anim 3 cPikle file',
                              }
 
     if self.gcomprisBoard.mode == 'draw':
       # DRAW
       #
       # draw is adapted to little kids : big anchors
-      self.DEFAULT_ANCHOR_SIZE	= 12
+      self.DEFAULT_ANCHOR_SIZE	= 14
 
       # draw specific UI
-      self.selector_section = "draw2"
+      self.selector_section = "draw3"
     else:
       # Anim
       #
       # Normal anchors
-      self.DEFAULT_ANCHOR_SIZE	= 8
+      self.DEFAULT_ANCHOR_SIZE	= 10
 
       # anim specific UI
-      self.selector_section = "anim2"
+      self.selector_section = "anim3"
 
     # Initialisation. Should not change in draw.
     self.running = False
@@ -737,7 +736,50 @@ class Gcompris_anim:
       font = gcompris.skin.get_font("gcompris/board/medium"),
       fill_color = "white")
 
+  def anim_to_file(self, filename):
 
+    file = open(filename, 'wb')
+
+    # Save the descriptif frame:
+    pickle.dump(fles.format_string['gcompris'], file, self.pickle_protocol)
+
+    pickle.dump(self.animlist, file, self.pickle_protocol)
+    file.close()
+
+  def file_to_anim(self, filename):
+
+    file = open(filename, 'rb')
+    try:
+      desc = pickle.load(file)
+    except:
+      file.close()
+      print 'Cannot load ', filename , " as a GCompris animation"
+      return
+
+    if type(desc) == type('str'):
+      # string
+      if 'desc' != fles.format_string['gcompris']:
+        if (desc == 'GCompris draw 3 cPikle file'
+            or desc == 'GCompris anim 3 cPikle file'):
+          print "load"
+          self.animlist = pickle.load(file)
+          for item in self.animlist:
+            item.anim = self
+        else:
+          print "ERROR: Unrecognized file format, file", filename, ' has description : ', desc
+          file.close()
+          return
+      else:
+        print "ERROR: Unrecognized file format (desc), file", filename, ' has description : ', desc
+        file.close()
+        return
+
+    elif type(desc) == type(1):
+      print filename, 'has no description. Are you sure it\'s', fles.format_string['gcompris'],'?'
+      # int
+      fles.frames_total = desc
+
+    file.close()
 
 
 ###############################################
@@ -747,11 +789,11 @@ class Gcompris_anim:
 ###############################################
 def general_save(filename, filetype, fles):
   print "filename=%s filetype=%s" %(filename, filetype)
-
+  fles.anim_to_file(filename)
 
 def general_restore(filename, filetype, fles):
   print "general_restore : ", filename, " type ",filetype
-
+  fles.file_to_anim(filename)
 
 def image_selected(image, fles):
   #fles is used because self is not passed through callback
@@ -770,3 +812,4 @@ def image_selected(image, fles):
 
   # We keep all object in a unique list
   fles.animlist.append(fles.created_object)
+



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