~autopilot/autopilot/trunk

« back to all changes in this revision

Viewing changes to autopilot/input/_uinput.py

  • Committer: CI bot
  • Author(s): Leo Arias
  • Date: 2014-02-19 21:40:40 UTC
  • mfrom: (415.2.20 fix1257055-slow_drag)
  • Revision ID: ps-jenkins@lists.canonical.com-20140219214040-bzd9hsqtme1k22gm
Added Mouse, Touch and Pointer drags with rate. Fixes: 1257055

Show diffs side-by-side

added added

removed removed

Lines of Context:
521
521
        """
522
522
        self._device.finger_move(x, y)
523
523
 
524
 
    def drag(self, x1, y1, x2, y2):
525
 
        """Perform a drag gesture from (x1,y1) to (x2,y2).
 
524
    def drag(self, x1, y1, x2, y2, rate=10, time_between_events=0.01):
 
525
        """Perform a drag gesture.
 
526
 
 
527
        The finger will be dragged from the starting point to the ending point
 
528
        with multiple moves. The number of moves, and thus the time that it
 
529
        will take to complete the drag can be altered with the `rate`
 
530
        parameter.
 
531
 
 
532
        :param x1: The point on the x axis where the drag will start from.
 
533
        :param y1: The point on the y axis where the drag will starts from.
 
534
        :param x2: The point on the x axis where the drag will end at.
 
535
        :param y2: The point on the y axis where the drag will end at.
 
536
        :param rate: The number of pixels the finger will be moved per
 
537
            iteration. Default is 10 pixels. A higher rate will make the drag
 
538
            faster, and lower rate will make it slower.
 
539
        :param time_between_events: The number of seconds that the drag will
 
540
            wait between iterations.
526
541
 
527
542
        :raises RuntimeError: if the finger is already pressed.
528
543
        :raises RuntimeError: if no more finger slots are available.
531
546
        logger.debug("Dragging from %d,%d to %d,%d", x1, y1, x2, y2)
532
547
        self._device.finger_down(x1, y1)
533
548
 
534
 
        # Let's drag in 100 steps for now...
535
 
        dx = 1.0 * (x2 - x1) / 100
536
 
        dy = 1.0 * (y2 - y1) / 100
537
 
        cur_x = x1 + dx
538
 
        cur_y = y1 + dy
539
 
        for i in range(0, 100):
540
 
            self._device.finger_move(int(cur_x), int(cur_y))
541
 
            sleep(0.002)
542
 
            cur_x += dx
543
 
            cur_y += dy
544
 
        # Make sure we actually end up at target
545
 
        self._device.finger_move(x2, y2)
 
549
        current_x, current_y = x1, y1
 
550
        while current_x != x2 or current_y != y2:
 
551
            dx = abs(x2 - current_x)
 
552
            dy = abs(y2 - current_y)
 
553
 
 
554
            intx = float(dx) / max(dx, dy)
 
555
            inty = float(dy) / max(dx, dy)
 
556
 
 
557
            step_x = min(rate * intx, dx)
 
558
            step_y = min(rate * inty, dy)
 
559
 
 
560
            if x2 < current_x:
 
561
                step_x *= -1
 
562
            if y2 < current_y:
 
563
                step_y *= -1
 
564
 
 
565
            current_x += step_x
 
566
            current_y += step_y
 
567
            self._device.finger_move(current_x, current_y)
 
568
 
 
569
            sleep(time_between_events)
 
570
 
546
571
        self._device.finger_up()
547
572
 
548
573