Skip to content

Commit c05edee

Browse files
committed
Add touchingEdge() method to Target
1 parent 3359906 commit c05edee

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

include/scratchcpp/target.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class LIBSCRATCHCPP_EXPORT Target
8888
virtual Rect boundingRect() const;
8989
virtual Rect fastBoundingRect() const;
9090

91+
bool touchingEdge() const;
92+
9193
double graphicsEffectValue(IGraphicsEffect *effect) const;
9294
virtual void setGraphicsEffectValue(IGraphicsEffect *effect, double value);
9395

src/scratch/target.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,22 @@ Rect Target::fastBoundingRect() const
431431
return Rect();
432432
}
433433

434+
/*! Returns true if the target is touching the edge. */
435+
bool Target::touchingEdge() const
436+
{
437+
// https://github.com/scratchfoundation/scratch-vm/blob/8dbcc1fc8f8d8c4f1e40629fe8a388149d6dfd1c/src/sprites/rendered-target.js#L772-L785
438+
if (impl->engine) {
439+
const double stageWidth = impl->engine->stageWidth();
440+
const double stageHeight = impl->engine->stageHeight();
441+
Rect bounds = boundingRect();
442+
443+
if ((bounds.left() < -stageWidth / 2) || (bounds.right() > stageWidth / 2) || (bounds.top() > stageHeight / 2) || (bounds.bottom() < -stageHeight / 2))
444+
return true;
445+
}
446+
447+
return false;
448+
}
449+
434450
/*! Returns the value of the given graphics effect. */
435451
double Target::graphicsEffectValue(IGraphicsEffect *effect) const
436452
{

test/scratch_classes/target_test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <scratchcpp/target.h>
2+
#include <scratchcpp/sprite.h>
23
#include <scratchcpp/variable.h>
34
#include <scratchcpp/list.h>
45
#include <scratchcpp/block.h>
@@ -8,6 +9,7 @@
89
#include <scratch/sound_p.h>
910
#include <enginemock.h>
1011
#include <targetmock.h>
12+
#include <spritehandlermock.h>
1113
#include <graphicseffectmock.h>
1214
#include <audiooutputmock.h>
1315
#include <audioplayermock.h>
@@ -580,6 +582,48 @@ TEST(TargetTest, FastBoundingRect)
580582
ASSERT_EQ(rect.bottom(), 0);
581583
}
582584

585+
TEST(TargetTest, TouchingEdge)
586+
{
587+
Target target;
588+
EngineMock engine;
589+
EXPECT_CALL(engine, stageWidth()).WillRepeatedly(Return(480));
590+
EXPECT_CALL(engine, stageHeight()).WillRepeatedly(Return(360));
591+
ASSERT_FALSE(target.touchingEdge());
592+
target.setEngine(&engine);
593+
ASSERT_FALSE(target.touchingEdge());
594+
595+
Sprite sprite;
596+
sprite.setEngine(&engine);
597+
ASSERT_FALSE(sprite.touchingEdge());
598+
599+
SpriteHandlerMock iface;
600+
EXPECT_CALL(iface, init);
601+
sprite.setInterface(&iface);
602+
603+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-100, 100, 100, -100)));
604+
ASSERT_FALSE(sprite.touchingEdge());
605+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240, 100, 100, -100)));
606+
ASSERT_FALSE(sprite.touchingEdge());
607+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240.1, 100, 100, -100)));
608+
ASSERT_TRUE(sprite.touchingEdge());
609+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240, 180, 100, -100)));
610+
ASSERT_FALSE(sprite.touchingEdge());
611+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240, 180.1, 100, -100)));
612+
ASSERT_TRUE(sprite.touchingEdge());
613+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240, 180, 240, -100)));
614+
ASSERT_FALSE(sprite.touchingEdge());
615+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240, 180, 240.1, -100)));
616+
ASSERT_TRUE(sprite.touchingEdge());
617+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240, 180, 240, -180)));
618+
ASSERT_FALSE(sprite.touchingEdge());
619+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-240, 180, 240, -180.1)));
620+
ASSERT_TRUE(sprite.touchingEdge());
621+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-242, 183, 240, -100)));
622+
ASSERT_TRUE(sprite.touchingEdge());
623+
EXPECT_CALL(iface, boundingRect).WillOnce(Return(Rect(-242, 183, 280, -690)));
624+
ASSERT_TRUE(sprite.touchingEdge());
625+
}
626+
583627
TEST(TargetTest, GraphicsEffects)
584628
{
585629
Target target;

0 commit comments

Comments
 (0)