@@ -548,6 +548,131 @@ TEST_F(RenderedTargetTest, HullPoints)
548548 ASSERT_FALSE (target.contains ({ 3.3 , 3.5 }));
549549}
550550
551+ TEST_F (RenderedTargetTest, SpriteDragging)
552+ {
553+ RenderedTarget target;
554+ EngineMock engine;
555+ target.setEngine (&engine);
556+
557+ SpriteModel model;
558+ Sprite sprite;
559+ sprite.setX (64.08 );
560+ sprite.setY (-6.86 );
561+ model.init (&sprite);
562+ target.setSpriteModel (&model);
563+ target.setStageScale (3.5 );
564+
565+ SceneMouseArea mouseArea;
566+ target.setMouseArea (&mouseArea);
567+
568+ emit mouseArea.mouseMoved (1064 , 651 );
569+ ASSERT_EQ (sprite.x (), 64.08 );
570+ ASSERT_EQ (sprite.y (), -6.86 );
571+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
572+
573+ // Try right mouse button (should not work)
574+ QMouseEvent moveEventRightButton (QEvent::MouseMove, QPointF (), QPointF (), Qt::RightButton, Qt::RightButton, Qt::NoModifier);
575+ QMouseEvent pressEventRightButton (QEvent::MouseButtonPress, QPointF (), QPointF (), Qt::RightButton, Qt::RightButton, Qt::NoModifier);
576+ QMouseEvent releaseEventRightButton (QEvent::MouseButtonRelease, QPointF (), QPointF (), Qt::RightButton, Qt::RightButton, Qt::NoModifier);
577+ QCoreApplication::sendEvent (&target, &pressEventRightButton);
578+ QCoreApplication::sendEvent (&target, &moveEventRightButton);
579+ ASSERT_EQ (sprite.x (), 64.08 );
580+ ASSERT_EQ (sprite.y (), -6.86 );
581+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
582+ QCoreApplication::sendEvent (&target, &releaseEventRightButton);
583+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
584+
585+ emit mouseArea.mouseMoved (1064 , 651 );
586+ ASSERT_EQ (sprite.x (), 64.08 );
587+ ASSERT_EQ (sprite.y (), -6.86 );
588+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
589+
590+ // Try right mouse button with "draggable" set to true (should not work)
591+ sprite.setDraggable (true );
592+ QCoreApplication::sendEvent (&target, &pressEventRightButton);
593+ QCoreApplication::sendEvent (&target, &moveEventRightButton);
594+ ASSERT_EQ (sprite.x (), 64.08 );
595+ ASSERT_EQ (sprite.y (), -6.86 );
596+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
597+ QCoreApplication::sendEvent (&target, &releaseEventRightButton);
598+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
599+
600+ emit mouseArea.mouseMoved (1064 , 651 );
601+ ASSERT_EQ (sprite.x (), 64.08 );
602+ ASSERT_EQ (sprite.y (), -6.86 );
603+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
604+
605+ // Try left mouse button (should not work with "draggable" set to false)
606+ sprite.setDraggable (false );
607+ QMouseEvent moveEvent (QEvent::MouseMove, QPointF (), QPointF (), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
608+ QMouseEvent pressEvent (QEvent::MouseButtonPress, QPointF (), QPointF (), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
609+ QMouseEvent releaseEvent (QEvent::MouseButtonRelease, QPointF (), QPointF (), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
610+ QCoreApplication::sendEvent (&target, &pressEvent);
611+ QCoreApplication::sendEvent (&target, &moveEvent);
612+ ASSERT_EQ (sprite.x (), 64.08 );
613+ ASSERT_EQ (sprite.y (), -6.86 );
614+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
615+
616+ emit mouseArea.mouseMoved (1064 , 651 );
617+ ASSERT_EQ (sprite.x (), 64.08 );
618+ ASSERT_EQ (sprite.y (), -6.86 );
619+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
620+ QCoreApplication::sendEvent (&target, &releaseEvent);
621+
622+ // Try left mouse button with "draggable" set to true
623+ sprite.setDraggable (true );
624+ QCoreApplication::sendEvent (&target, &pressEvent);
625+ EXPECT_CALL (engine, mouseX ()).WillOnce (Return (67.95 ));
626+ EXPECT_CALL (engine, mouseY ()).WillOnce (Return (2.1 ));
627+ EXPECT_CALL (engine, moveSpriteToFront (&sprite));
628+ QCoreApplication::sendEvent (&target, &moveEvent);
629+ ASSERT_EQ (sprite.x (), 64.08 );
630+ ASSERT_EQ (sprite.y (), -6.86 );
631+ ASSERT_EQ (mouseArea.draggedSprite (), &target);
632+
633+ // Drag
634+ EXPECT_CALL (engine, stageWidth ()).WillOnce (Return (480 ));
635+ EXPECT_CALL (engine, stageHeight ()).WillOnce (Return (360 ));
636+ emit mouseArea.mouseMoved (1067.8 , 649.06 );
637+ ASSERT_EQ (std::round (sprite.x () * 100 ) / 100 , 61.22 );
638+ ASSERT_EQ (std::round (sprite.y () * 100 ) / 100 , -14.41 );
639+ ASSERT_EQ (mouseArea.draggedSprite (), &target);
640+
641+ EXPECT_CALL (engine, stageWidth ()).WillOnce (Return (480 ));
642+ EXPECT_CALL (engine, stageHeight ()).WillOnce (Return (360 ));
643+ emit mouseArea.mouseMoved (1092.47 , 605.46 );
644+ ASSERT_EQ (std::round (sprite.x () * 100 ) / 100 , 68.26 );
645+ ASSERT_EQ (std::round (sprite.y () * 100 ) / 100 , -1.95 );
646+ ASSERT_EQ (mouseArea.draggedSprite (), &target);
647+
648+ // Create another sprite
649+ RenderedTarget anotherTarget;
650+ anotherTarget.setEngine (&engine);
651+
652+ SpriteModel anotherModel;
653+ Sprite anotherSprite;
654+ anotherSprite.setX (64.08 );
655+ anotherSprite.setY (-6.86 );
656+ anotherSprite.setDraggable (true );
657+ anotherModel.init (&anotherSprite);
658+ anotherTarget.setSpriteModel (&model);
659+ anotherTarget.setStageScale (3.5 );
660+ anotherTarget.setMouseArea (&mouseArea);
661+
662+ // Try to drag the second sprite while the first is being dragged
663+ sprite.setDraggable (true );
664+ QCoreApplication::sendEvent (&anotherTarget, &pressEvent);
665+ QCoreApplication::sendEvent (&anotherTarget, &moveEvent);
666+ ASSERT_EQ (mouseArea.draggedSprite (), &target);
667+ QCoreApplication::sendEvent (&anotherTarget, &releaseEvent);
668+
669+ // Stop dragging
670+ QCoreApplication::sendEvent (&target, &releaseEvent);
671+ ASSERT_EQ (std::round (sprite.x () * 100 ) / 100 , 68.26 );
672+ ASSERT_EQ (std::round (sprite.y () * 100 ) / 100 , -1.95 );
673+ ASSERT_EQ (mouseArea.draggedSprite (), nullptr );
674+ }
675+
551676TEST_F (RenderedTargetTest, Engine)
552677{
553678 RenderedTarget target;
0 commit comments