Package Gnumed :: Package timelinelib :: Package play :: Module playcontroller |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.play.playcontroller

      1  # Copyright (C) 2009, 2010, 2011  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  import time
     20
     21  from timelinelib.db.objects import TimePeriod
     22  from timelinelib.db.objects import time_period_center
     23  from timelinelib.drawing.viewproperties import ViewProperties
     24
     25
     26 -class PlayController(object):
     27
     28 -    def __init__(self, play_frame, timeline, drawing_algorithm,
     29              config):
     30          self.play_frame = play_frame
     31          self.timeline = timeline
     32          self.drawing_algorithm = drawing_algorithm
     33          self.config = config
     34
     35 -    def on_close_clicked(self):
     36          self.play_frame.close()
     37
     38 -    def start_movie(self):
     39          period_length = self.play_frame.get_view_period_length()
     40
     41          start_period = time_period_center(
     42              self.timeline.get_time_type(),
     43              self.timeline.get_first_event().time_period.start_time,
     44              period_length)
     45
     46          all_events = self.timeline.get_all_events()
     47          all_events.sort(key=lambda event: event.time_period.start_time)
     48          all_events = all_events[1:]
     49
     50          self.animations = []
     51          for event in all_events:
     52              period = time_period_center(self.timeline.get_time_type(),
     53                      event.time_period.start_time,
     54                      period_length)
     55              self.animations.append((3, period))
     56
     57          self.last_time = time.time()
     58
     59          self.current_animation = Animation(
     60              self.timeline,
     61              start_period, self.animations[0][0], self.animations[0][1])
     62
     63          self.play_frame.start_timer(50)
     64
     65 -    def tick(self):
     66          new_time = time.time()
     67          self.delta = new_time - self.last_time
     68          self.last_time = new_time
     69          print self.delta
     70          self.play_frame.redraw_drawing_area(self.draw_fn)
     71
     72 -    def draw_fn(self, dc):
     73          view_properties = ViewProperties()
     74          view_properties.set_displayed_period(self.get_period())
     75          self.drawing_algorithm.draw(
     76              dc, self.timeline, view_properties, self.config)
     77
     78 -    def get_period(self):
     79          self.current_animation.change_current_period(self.delta)
     80
     81          if self.current_animation.is_done():
     82              (speed, period) = self.animations.pop(0)
     83              if len(self.animations) == 0:
     84                  self.play_frame.stop_timer()
     85                  return period
     86              else:
     87                  self.current_animation = Animation(
     88                      self.timeline,
     89                      period, self.animations[0][0], self.animations[0][1])
     90
     91          return self.current_animation.current_period
     92
     93
     94 -class Animation(object):
     95
     96 -    def __init__(self, timeline, start_period, duration_in_seconds, end_period):
     97          self.timeline = timeline
     98          self.start_period = start_period
     99          self.duration_in_seconds = duration_in_seconds
    100          self.end_period = end_period
    101          self.current_period = start_period
    102          self.total_animation_delta = TimePeriod(self.timeline.get_time_type(),
    103                  self.start_period.start_time, self.end_period.start_time).delta()
    104          self.total_animation_time = 0.0
    105
    106 -    def is_done(self):
    107          return self.current_period.end_time >= self.end_period.end_time
    108
    109 -    def change_current_period(self, delta):
    110          self.total_animation_time += delta
    111
    112          delta_to_move = self.timeline.get_time_type().mult_timedelta(
    113              self.total_animation_delta,
    114              min(1, self.total_animation_time/self.duration_in_seconds))
    115
    116          self.current_period = self.start_period.move_delta(delta_to_move)
    117