Esempio n. 1
0
class Document:
    """This holds everything releated to the animation itself"""
    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 = []

        # 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 __del__(self):
        self.rootitem.remove()

    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()

    # If an item is removed, we must remove it from all
    # the timelines in which we saved a z_order
    def delete_from_zorder(self, item_id):
        for z_order in self.zorder.values():
            try:
                z_order.remove(item_id)
            except ValueError:
                pass

    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)
            if item:
                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):

        file = open(filename, 'wb')

        # Save the descriptif frame:
        pickle.dump(self.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)

        # Save the z order
        pickle.dump(self.zorder, 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' != self.format_string['gcompris']:
                if (desc == 'GCompris anim 3 cPikle file'):

                    self.anim.deselect()
                    for item in self.animlist[:]:
                        item.delete()

                    self.timeline.set_lastmark(pickle.load(file))
                    self.animlist = pickle.load(file)
                    for item in self.animlist:
                        item.restore(self.anim)

                    self.zorder = pickle.load(file)

                    # Restore is complete
                    self.timeline.set_time(0)
                    self.refresh(0)
                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', \
                self.format_string['gcompris'],'?'

        file.close()
Esempio n. 2
0
class Document:
  """This holds everything releated to the animation itself"""

  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 = []

    # 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 = {}

    # Set to true when the order or the list of object has changed
    self.zorderDirty = False

    # 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 __del__(self):
    self.rootitem.remove()

  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 zorder_dirty(self):
    self.zorderDirty = True

  def save_zorder(self):
    if not self.zorderDirty:
      return

    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
    self.zorderDirty = False

  def restore_zorder(self):
    z_order = []
    if self.timeline.get_time() in self.zorder:
      z_order = self.zorder[self.timeline.get_time()]
    else:
      return

    # Build the list of items_is present in the image
    present_items = []
    for i in range(self.rootitem.get_n_children()):
      item = self.rootitem.get_child(i)
      if item:
        present_items.append( item.get_data("id") )

    # Remove items in z_order that are not in present_items
    z_order = [item for item in z_order if item in present_items]

    for z_item_id in z_order:
      for i in range(self.rootitem.get_n_children()):
        item = self.rootitem.get_child(i)
        if item:
          item_id = item.get_data("id")
          if ( item_id == z_item_id ):
            z_index = z_order.index(item_id)
            if ( i != z_index ):
              try:
                self.rootitem.move_child(i, z_index);
                break
              except ValueError:
                pass

  def anim_to_file(self, filename):

    file = open(filename, 'wb')

    # Save the descriptif frame:
    pickle.dump(self.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)

    # Save the z order
    pickle.dump(self.zorder, 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' != self.format_string['gcompris']:
        if (desc == 'GCompris anim 3 cPikle file'):

          self.anim.deselect()
          for item in self.animlist[:]:
            item.delete()

          self.timeline.set_lastmark(pickle.load(file))
          self.animlist = pickle.load(file)
          for item in self.animlist:
            item.restore(self.anim)

          self.zorder = pickle.load(file)

          # Restore is complete
          self.timeline.set_time(0)
          self.refresh(0)
        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', \
          self.format_string['gcompris'],'?'

    file.close()