Gnumed.timelinelib.wxgui.components.maincanvas.scrollbydrag

Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Package maincanvas :: Module scrollbydrag |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.components.maincanvas.scrollbydrag

      1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  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.wxgui.components.maincanvas.inputhandler import InputHandler
     22
     23
     24 -class ScrollByDragInputHandler(InputHandler):
     25
     26 -    def __init__(self, state, timeline_canvas, start_time, y):
     27          InputHandler.__init__(self, timeline_canvas)
     28          self._state = state
     29          self.start_slider_pos = self.timeline_canvas.GetDividerPosition()
     30          self.start_mouse_pos = y
     31          self.last_mouse_pos = y
     32          self.view_height = self.timeline_canvas.GetSize()[1]
     33          self.start_time = start_time
     34          self.last_clock_time = time.clock()
     35          self.last_x = 0
     36          self.last_x_distance = 0
     37          self.last_y = 0
     38          self.last_y_distance = 0
     39          self.speed_px_per_sec = 0
     40          self.INERTIAL_SCROLLING_SPEED_THRESHOLD = 200
     41
     42 -    def mouse_moved(self, cursor, keyboard):
     43          self.last_mouse_pos = cursor.y
     44          self._calculate_sped(cursor.x)
     45          self._scroll_timeline(cursor.x)
     46          percentage_distance = int(100 * (cursor.y - self.start_mouse_pos) / self.view_height)
     47          self.timeline_canvas.SetDividerPosition(self.start_slider_pos + percentage_distance)
     48
     49 -    def left_mouse_up(self):
     50          if self.start_mouse_pos == self.last_mouse_pos:
     51              self.timeline_canvas.ClearSelectedEvents()
     52          self._state.change_to_no_op()
     53          self._state.edit_ends()
     54          if self.timeline_canvas.GetAppearance().get_use_inertial_scrolling():
     55              if self.speed_px_per_sec > self.INERTIAL_SCROLLING_SPEED_THRESHOLD:
     56                  self._inertial_scrolling()
     57
     58 -    def _calculate_sped(self, x):
     59          MAX_SPEED = 10000
     60          self.last_x_distance = x - self.last_x
     61          self.last_x = x
     62          current_clock_time = time.clock()
     63          elapsed_clock_time = current_clock_time - self.last_clock_time
     64          if elapsed_clock_time == 0:
     65              self.speed_px_per_sec = MAX_SPEED
     66          else:
     67              self.speed_px_per_sec = min(
     68                  MAX_SPEED,
     69                  int(abs(self.last_x_distance / elapsed_clock_time))
     70              )
     71          self.last_clock_time = current_clock_time
     72
     73 -    def _scroll_timeline(self, x):
     74          self.current_time = self.timeline_canvas.GetTimeAt(x)
     75          self.timeline_canvas.Navigate(lambda tp:
     76                                        tp.move_delta(self.start_time - self.current_time))
     77
     78 -    def _inertial_scrolling(self):
     79          frame_time = self._calculate_frame_time()
     80          value_factor = self._calculate_scroll_factor()
     81          inertial_func = (0.20, 0.15, 0.10, 0.10, 0.10, 0.08, 0.06, 0.06, 0.05)
     82          self.timeline_canvas.UseFastDraw(True)
     83          next_frame_time = time.clock()
     84          for value in inertial_func:
     85              self.timeline_canvas.Scroll(value * value_factor * 0.1)
     86              next_frame_time += frame_time
     87              sleep_time = next_frame_time - time.clock()
     88              if sleep_time >= 0:
     89                  time.sleep(sleep_time)
     90          self.timeline_canvas.UseFastDraw(False)
     91
     92 -    def _calculate_frame_time(self):
     93          MAX_FRAME_RATE = 26.0
     94          frames_per_second = (MAX_FRAME_RATE * self.speed_px_per_sec /
     95                               (100 + self.speed_px_per_sec))
     96          frame_time = 1.0 / frames_per_second
     97          return frame_time
     98
     99 -    def _calculate_scroll_factor(self):
    100          if self.current_time > self.start_time:
    101              direction = 1
    102          else:
    103              direction = -1
    104          scroll_factor = (direction * self.speed_px_per_sec / self.INERTIAL_SCROLLING_SPEED_THRESHOLD)
    105          return scroll_factor
    106