Skip to content

Commit 3359906

Browse files
committed
Move bounding rect methods to Target
1 parent 35ea917 commit 3359906

File tree

11 files changed

+118
-8
lines changed

11 files changed

+118
-8
lines changed

include/scratchcpp/istagehandler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
4343

4444
/*! Called when the bubble text changes. */
4545
virtual void onBubbleTextChanged(const std::string &text) = 0;
46+
47+
/*! Used to get the bounding rectangle of the stage. */
48+
virtual Rect boundingRect() const = 0;
49+
50+
/*!
51+
* Used to get a less accurate bounding rectangle of the stage
52+
* which is calculated by transforming the costume rectangle.
53+
*/
54+
virtual Rect fastBoundingRect() const = 0;
4655
};
4756

4857
} // namespace libscratchcpp

include/scratchcpp/sprite.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace libscratchcpp
88
{
99

1010
class ISpriteHandler;
11-
class Rect;
1211
class IGraphicsEffect;
1312
class SpritePrivate;
1413

@@ -69,8 +68,8 @@ class LIBSCRATCHCPP_EXPORT Sprite
6968

7069
void setLayerOrder(int newLayerOrder) override;
7170

72-
Rect boundingRect() const;
73-
Rect fastBoundingRect() const;
71+
Rect boundingRect() const override;
72+
Rect fastBoundingRect() const override;
7473
void keepInFence(double newX, double newY, double *fencedX, double *fencedY) const;
7574

7675
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;

include/scratchcpp/stage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
4848
const std::string &textToSpeechLanguage() const;
4949
void setTextToSpeechLanguage(const std::string &newTextToSpeechLanguage);
5050

51+
Rect boundingRect() const override;
52+
Rect fastBoundingRect() const override;
53+
5154
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
5255

5356
void clearGraphicsEffects() override;

include/scratchcpp/target.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "global.h"
88
#include "spimpl.h"
9+
#include "rect.h"
910

1011
namespace libscratchcpp
1112
{
@@ -84,6 +85,9 @@ class LIBSCRATCHCPP_EXPORT Target
8485
double volume() const;
8586
void setVolume(double newVolume);
8687

88+
virtual Rect boundingRect() const;
89+
virtual Rect fastBoundingRect() const;
90+
8791
double graphicsEffectValue(IGraphicsEffect *effect) const;
8892
virtual void setGraphicsEffectValue(IGraphicsEffect *effect, double value);
8993

src/scratch/sprite.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ void Sprite::setLayerOrder(int newLayerOrder)
341341
impl->iface->onLayerOrderChanged(newLayerOrder);
342342
}
343343

344-
/*! Returns the bounding rectangle of the sprite. */
344+
/*! Overrides Target#boundingRect(). */
345345
Rect Sprite::boundingRect() const
346346
{
347347
if (!impl->iface)
@@ -350,10 +350,7 @@ Rect Sprite::boundingRect() const
350350
return impl->iface->boundingRect();
351351
}
352352

353-
/*!
354-
* Returns the less accurate bounding rectangle of the sprite
355-
* which is calculated by transforming the costume rectangle.
356-
*/
353+
/*! Overrides Target#fastBoundingRect(). */
357354
Rect Sprite::fastBoundingRect() const
358355
{
359356
if (!impl->iface)

src/scratch/stage.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ void Stage::setTextToSpeechLanguage(const std::string &newTextToSpeechLanguage)
131131
impl->textToSpeechLanguage = newTextToSpeechLanguage;
132132
}
133133

134+
/*! Overrides Target#boundingRect(). */
135+
Rect Stage::boundingRect() const
136+
{
137+
if (!impl->iface)
138+
return Rect();
139+
140+
return impl->iface->boundingRect();
141+
}
142+
143+
/*! Overrides Target#boundingRect(). */
144+
Rect Stage::fastBoundingRect() const
145+
{
146+
if (!impl->iface)
147+
return Rect();
148+
149+
return impl->iface->fastBoundingRect();
150+
}
151+
134152
/*! Overrides Target#setGraphicsEffectValue(). */
135153
void Stage::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
136154
{

src/scratch/target.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,21 @@ void Target::setVolume(double newVolume)
416416
}
417417
}
418418

419+
/*! Returns the bounding rectangle of the sprite. */
420+
Rect Target::boundingRect() const
421+
{
422+
return Rect();
423+
}
424+
425+
/*!
426+
* Returns the less accurate bounding rectangle of the sprite
427+
* which is calculated by transforming the costume rectangle.
428+
*/
429+
Rect Target::fastBoundingRect() const
430+
{
431+
return Rect();
432+
}
433+
419434
/*! Returns the value of the given graphics effect. */
420435
double Target::graphicsEffectValue(IGraphicsEffect *effect) const
421436
{

test/mocks/stagehandlermock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ class StageHandlerMock : public IStageHandler
1919
MOCK_METHOD(void, onGraphicsEffectsCleared, (), (override));
2020
MOCK_METHOD(void, onBubbleTypeChanged, (Target::BubbleType), (override));
2121
MOCK_METHOD(void, onBubbleTextChanged, (const std::string &), (override));
22+
23+
MOCK_METHOD(Rect, boundingRect, (), (const, override));
24+
MOCK_METHOD(Rect, fastBoundingRect, (), (const, override));
2225
};

test/scratch_classes/stage_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,26 @@ TEST(StageTest, TextToSpeechLanguage)
119119
ASSERT_EQ(stage.textToSpeechLanguage(), "English");
120120
}
121121

122+
TEST(StageTest, DefaultBoundingRect)
123+
{
124+
Stage stage;
125+
Rect rect = stage.boundingRect();
126+
ASSERT_EQ(rect.left(), 0);
127+
ASSERT_EQ(rect.top(), 0);
128+
ASSERT_EQ(rect.right(), 0);
129+
ASSERT_EQ(rect.bottom(), 0);
130+
}
131+
132+
TEST(StageTest, DefaultFastBoundingRect)
133+
{
134+
Stage stage;
135+
Rect rect = stage.fastBoundingRect();
136+
ASSERT_EQ(rect.left(), 0);
137+
ASSERT_EQ(rect.top(), 0);
138+
ASSERT_EQ(rect.right(), 0);
139+
ASSERT_EQ(rect.bottom(), 0);
140+
}
141+
122142
TEST(StageTest, GraphicsEffects)
123143
{
124144
Stage stage;

test/scratch_classes/target_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,26 @@ TEST(TargetTest, Volume)
560560
SoundPrivate::audioOutput = nullptr;
561561
}
562562

563+
TEST(TargetTest, BoundingRect)
564+
{
565+
Target target;
566+
Rect rect = target.boundingRect();
567+
ASSERT_EQ(rect.left(), 0);
568+
ASSERT_EQ(rect.top(), 0);
569+
ASSERT_EQ(rect.right(), 0);
570+
ASSERT_EQ(rect.bottom(), 0);
571+
}
572+
573+
TEST(TargetTest, FastBoundingRect)
574+
{
575+
Target target;
576+
Rect rect = target.fastBoundingRect();
577+
ASSERT_EQ(rect.left(), 0);
578+
ASSERT_EQ(rect.top(), 0);
579+
ASSERT_EQ(rect.right(), 0);
580+
ASSERT_EQ(rect.bottom(), 0);
581+
}
582+
563583
TEST(TargetTest, GraphicsEffects)
564584
{
565585
Target target;

0 commit comments

Comments
 (0)