Ejemplo n.º 1
0
    def _run_test_on_double_tap_with_init_args(self, *args, **kwargs):
        """
    The args passed to this function are forwarded to the constructor of the
    CardWidget used in the test.
    """
        window = self.window

        # Place the card in the center of the window.
        card_widget = CardWidget(*args, **kwargs)
        min_dimension = min(window.width, window.height)
        card_widget.size = min_dimension / 4, min_dimension / 2
        window_center = window.width / 2, window.height / 2
        card_widget.center = window_center

        float_layout = FloatLayout()
        float_layout.add_widget(card_widget)
        self.render(float_layout)

        on_double_tap_handler = Mock()
        card_widget.bind(on_double_tap=on_double_tap_handler)

        # Normal click on the card does not trigger on_double_tap.
        touch = UnitTestTouch(*card_widget.center)
        touch.touch_down()
        touch.touch_up()
        on_double_tap_handler.assert_not_called()

        # Double click on the card triggers on_double_tap.
        touch.is_double_tap = True
        touch.touch_down()
        touch.touch_up()
        on_double_tap_handler.assert_called_once_with(card_widget)
        on_double_tap_handler.reset_mock()

        # Double click outside of the card does not trigger on_double_tap.
        touch = UnitTestTouch(0, 0)
        touch.is_double_tap = True
        touch.touch_down()
        touch.touch_up()
        on_double_tap_handler.assert_not_called()

        # Two cards on top of each other. Only the top one triggers on_double_tap.
        another_card_widget = CardWidget(Card(Suit.SPADES, CardValue.ACE),
                                         aspect_ratio=0.5)
        another_card_widget.size = card_widget.size
        another_card_widget.center = card_widget.center
        another_card_widget.bind(on_double_tap=on_double_tap_handler)
        float_layout.add_widget(another_card_widget)

        touch = UnitTestTouch(*card_widget.center)
        touch.is_double_tap = True
        touch.touch_down()
        touch.touch_up()
        on_double_tap_handler.assert_called_once_with(another_card_widget)
Ejemplo n.º 2
0
 def _bind_card_action(self, card: CardWidget,
                       action: PlayerAction) -> None:
     self._actions[card] = action
     card.bind(on_double_tap=self._card_action_callback)
Ejemplo n.º 3
0
    def test_dragging_a_card_with_the_mouse(self):
        # pylint: disable=too-many-statements
        window = self.window

        on_card_moved_handler = Mock()

        # Place the card in the center of the window.
        card_widget = CardWidget(Card(Suit.SPADES, CardValue.ACE),
                                 aspect_ratio=0.5,
                                 do_translation=True)
        min_dimension = min(window.width, window.height)
        card_widget.size = min_dimension / 4, min_dimension / 2
        window_center = window.width / 2, window.height / 2
        card_widget.center = window_center
        card_widget.bind(on_card_moved=on_card_moved_handler)

        # Place a second card under the first card. Only the first one will be moved
        # and it will be the only one triggering the on_card_moved event.
        another_card_widget = CardWidget(Card(Suit.SPADES, CardValue.ACE),
                                         aspect_ratio=0.5)
        another_card_widget.size = card_widget.size
        another_card_widget.center = card_widget.center
        another_card_widget.bind(on_card_moved=on_card_moved_handler)

        float_layout = FloatLayout()
        float_layout.add_widget(another_card_widget)
        float_layout.add_widget(card_widget)
        self.render(float_layout)

        # Touch down on the card, nothing happens to the card.
        touch = UnitTestTouch(*card_widget.center)
        touch.touch_down()
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Drag in a random direction, check that the card moved accordingly.
        new_position = window.width / 3, window.height / 3
        touch.touch_move(*new_position)
        self.assertAlmostEqual(new_position[0], card_widget.center_x)
        self.assertAlmostEqual(new_position[1], card_widget.center_y)
        on_card_moved_handler.assert_not_called()

        # Drag in another random direction, check that the card moved accordingly.
        new_position = window.width * 0.7, window.height * 0.45
        touch.touch_move(*new_position)
        self.assertAlmostEqual(new_position[0], card_widget.center_x)
        self.assertAlmostEqual(new_position[1], card_widget.center_y)
        on_card_moved_handler.assert_not_called()

        # Touch up, nothing happens to the card. The on_card_moved event is
        # triggered.
        touch.touch_up()
        self.assertAlmostEqual(new_position[0], card_widget.center_x)
        self.assertAlmostEqual(new_position[1], card_widget.center_y)
        on_card_moved_handler.assert_called_once_with(card_widget,
                                                      card_widget.center)
        on_card_moved_handler.reset_mock()

        # Move the card back to the window center.
        card_widget.center = window_center

        # CardWidget's position has changed, but was not dragged by the user. The
        # on_card_moved event should not trigger.
        on_card_moved_handler.assert_not_called()

        # A click on the card, without dragging should not trigger the on_card_moved
        # event.
        touch = UnitTestTouch(*card_widget.center)
        touch.touch_down()
        touch.touch_up()
        on_card_moved_handler.assert_not_called()

        # Touch down in the bottom left corner of the window, outside of the card.
        # Nothing happens to the card.
        touch = UnitTestTouch(window.width * 0.1, window.height * 0.1)
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Drag to the window center, on the card. Nothing happens to the card.
        touch.touch_move(*window_center)
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Drag further to the top right corner of the window. Nothing happens to the
        # card.
        touch.touch_move(window.width * 0.95, window.height * 0.95)
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Touch up. Nothing happens to the card.
        touch.touch_up()
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()
Ejemplo n.º 4
0
    def test_dragging_a_card_with_the_mouse_translations_disabled(self):
        window = self.window

        on_card_moved_handler = Mock()

        # Place the card in the center of the window.
        card_widget = CardWidget(Card(Suit.SPADES, CardValue.ACE),
                                 aspect_ratio=0.5,
                                 do_translation=False)
        min_dimension = min(window.width, window.height)
        card_widget.size = min_dimension / 4, min_dimension / 2
        window_center = window.width / 2, window.height / 2
        card_widget.center = window_center
        card_widget.bind(on_card_moved=on_card_moved_handler)

        self.render(card_widget)

        # Touch down on the card, nothing happens to the card.
        touch = UnitTestTouch(*card_widget.center)
        touch.touch_down()
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Drag in a random direction, the card should not move.
        new_position = window.width / 3, window.height / 3
        touch.touch_move(*new_position)
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Touch up, nothing happens to the card.
        touch.touch_up()
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Move the card to a new position. Since it was not dragged by the user, the
        # on_card_moved event should not trigger.
        card_widget.center = window.width / 3, window.height / 3
        on_card_moved_handler.assert_not_called()

        # A click on the card, without dragging should not trigger the on_card_moved
        # event.
        card_widget.center = window_center
        touch = UnitTestTouch(*card_widget.center)
        touch.touch_down()
        touch.touch_up()
        on_card_moved_handler.assert_not_called()

        # Touch down in the bottom left corner of the window, outside of the card.
        # Nothing happens to the card.
        touch = UnitTestTouch(window.width * 0.1, window.height * 0.1)
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Drag to the window center, on the card. Nothing happens to the card.
        touch.touch_move(*window_center)
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Drag further to the top right corner of the window. Nothing happens to the
        # card.
        touch.touch_move(window.width * 0.95, window.height * 0.95)
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()

        # Touch up. Nothing happens to the card.
        touch.touch_up()
        self.assertEqual(window_center, card_widget.center)
        on_card_moved_handler.assert_not_called()
               Animation(center_x=_float_layout.width / 4 * 3,
                         center_y=_float_layout.height / 4) + \
               Animation(rotation=270 + 90) + \
               Animation(center_x=_float_layout.width / 4 * 3,
                         center_y=_float_layout.height / 4 * 3) + \
               Animation(rotation=90) + \
               Animation(center_x=_float_layout.width / 4,
                         center_y=_float_layout.height / 4 * 3) + \
               Animation(rotation=45) + \
               Animation(center_x=_float_layout.width / 2,
                         center_y=_float_layout.height / 2) + \
               Animation(rotation=0)

  _card_widget = CardWidget(Card(Suit.SPADES, CardValue.QUEEN))
  _card_widget.size_hint = None, None
  _card_widget.bind(on_double_tap=lambda *_: _animation_controller.start())
  _float_layout.add_widget(_card_widget)


  def _reset_pos():
    _card_widget.center = _float_layout.center
    _card_widget.visible = True
    _card_widget.rotation = 0
    Clock.schedule_once(
      lambda _: _animation_controller.add_card_animation(_card_widget,
                                                         _animation))


  def _cancel():
    if _animation_controller.is_running:
      _animation_controller.cancel()