Skip to content

Commit b691e30

Browse files
authored
Merge pull request #546 from scratchcpp/size_limit
Add sprite size limit
2 parents d9a7b76 + 5894ade commit b691e30

File tree

15 files changed

+186
-10
lines changed

15 files changed

+186
-10
lines changed

include/scratchcpp/ispritehandler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
6565
/*! Called when the bubble text changes. */
6666
virtual void onBubbleTextChanged(const std::string &text) = 0;
6767

68+
/*! Used to get the current costume width. */
69+
virtual int costumeWidth() const = 0;
70+
71+
/*! Used to get the current costume height. */
72+
virtual int costumeHeight() const = 0;
73+
6874
/*!
6975
* Used to get the bounding rectangle of the sprite.
7076
* \note The rectangle must be relative to the stage, so make sure to use the sprite's coordinates.

include/scratchcpp/istagehandler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
4646
/*! Called when the bubble text changes. */
4747
virtual void onBubbleTextChanged(const std::string &text) = 0;
4848

49+
/*! Used to get the current costume width. */
50+
virtual int costumeWidth() const = 0;
51+
52+
/*! Used to get the current costume height. */
53+
virtual int costumeHeight() const = 0;
54+
4955
/*! Used to get the bounding rectangle of the stage. */
5056
virtual Rect boundingRect() const = 0;
5157

include/scratchcpp/sprite.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class LIBSCRATCHCPP_EXPORT Sprite
5959

6060
void setCostumeIndex(int newCostumeIndex) override;
6161

62+
int currentCostumeWidth() const override;
63+
int currentCostumeHeight() const override;
64+
6265
double direction() const;
6366
void setDirection(double newDirection);
6467

include/scratchcpp/stage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
3333

3434
void setCostumeIndex(int newCostumeIndex) override;
3535

36+
int currentCostumeWidth() const override;
37+
int currentCostumeHeight() const override;
38+
3639
int tempo() const;
3740
void setTempo(int newTempo);
3841

include/scratchcpp/target.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class LIBSCRATCHCPP_EXPORT Target
6969
virtual void setCostumeIndex(int newCostumeIndex);
7070

7171
std::shared_ptr<Costume> currentCostume() const;
72+
virtual int currentCostumeWidth() const;
73+
virtual int currentCostumeHeight() const;
7274

7375
const std::vector<std::shared_ptr<Costume>> &costumes() const;
7476
int addCostume(std::shared_ptr<Costume> costume);

src/scratch/sprite.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,21 @@ double Sprite::size() const
258258
/*! Sets the size. */
259259
void Sprite::setSize(double newSize)
260260
{
261+
IEngine *engine = this->engine();
262+
263+
if (engine) {
264+
const int oldWidth = currentCostumeWidth();
265+
const int oldHeight = currentCostumeHeight();
266+
const double minScale = std::min(1.0, std::max(5.0 / oldWidth, 5.0 / oldHeight));
267+
const double maxScale = std::min((1.5 * engine->stageWidth()) / oldWidth, (1.5 * engine->stageHeight()) / oldHeight);
268+
newSize = std::clamp(newSize, minScale * 100, maxScale * 100);
269+
}
270+
261271
impl->size = newSize;
262272

263273
if (impl->visible) {
264-
IEngine *eng = engine();
265-
266-
if (eng)
267-
eng->requestRedraw();
274+
if (engine)
275+
engine->requestRedraw();
268276
}
269277

270278
if (impl->iface)
@@ -288,6 +296,24 @@ void Sprite::setCostumeIndex(int newCostumeIndex)
288296
impl->iface->onCostumeChanged(costume.get());
289297
}
290298

299+
/*! Overrides Target#currentCostumeWidth(). */
300+
int Sprite::currentCostumeWidth() const
301+
{
302+
if (!impl->iface)
303+
return 0;
304+
305+
return impl->iface->costumeWidth();
306+
}
307+
308+
/*! Overrides Target#currentCostumeHeight(). */
309+
int Sprite::currentCostumeHeight() const
310+
{
311+
if (!impl->iface)
312+
return 0;
313+
314+
return impl->iface->costumeHeight();
315+
}
316+
291317
/*! Returns the direction. */
292318
double Sprite::direction() const
293319
{

src/scratch/stage.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ void Stage::setCostumeIndex(int newCostumeIndex)
4646
impl->iface->onCostumeChanged(costume.get());
4747
}
4848

49+
/*! Overrides Target#currentCostumeWidth(). */
50+
int Stage::currentCostumeWidth() const
51+
{
52+
if (!impl->iface)
53+
return 0;
54+
55+
return impl->iface->costumeWidth();
56+
}
57+
58+
/*! Overrides Target#currentCostumeHeight(). */
59+
int Stage::currentCostumeHeight() const
60+
{
61+
if (!impl->iface)
62+
return 0;
63+
64+
return impl->iface->costumeHeight();
65+
}
66+
4967
/*! Returns the tempo. */
5068
int Stage::tempo() const
5169
{

src/scratch/target.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@ std::shared_ptr<Costume> Target::currentCostume() const
278278
return costumeAt(impl->costumeIndex);
279279
}
280280

281+
/*! Returns the width of the current costume. */
282+
int Target::currentCostumeWidth() const
283+
{
284+
return 0;
285+
}
286+
287+
/*! Returns the height of the current costume. */
288+
int Target::currentCostumeHeight() const
289+
{
290+
return 0;
291+
}
292+
281293
/*! Returns the list of costumes. */
282294
const std::vector<std::shared_ptr<Costume>> &Target::costumes() const
283295
{

test/mocks/spritehandlermock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class SpriteHandlerMock : public ISpriteHandler
2929
MOCK_METHOD(void, onBubbleTypeChanged, (Target::BubbleType), (override));
3030
MOCK_METHOD(void, onBubbleTextChanged, (const std::string &), (override));
3131

32+
MOCK_METHOD(int, costumeWidth, (), (const, override));
33+
MOCK_METHOD(int, costumeHeight, (), (const, override));
34+
3235
MOCK_METHOD(Rect, boundingRect, (), (const, override));
3336
MOCK_METHOD(Rect, fastBoundingRect, (), (const, override));
3437

test/mocks/stagehandlermock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class StageHandlerMock : public IStageHandler
2020
MOCK_METHOD(void, onBubbleTypeChanged, (Target::BubbleType), (override));
2121
MOCK_METHOD(void, onBubbleTextChanged, (const std::string &), (override));
2222

23+
MOCK_METHOD(int, costumeWidth, (), (const, override));
24+
MOCK_METHOD(int, costumeHeight, (), (const, override));
25+
2326
MOCK_METHOD(Rect, boundingRect, (), (const, override));
2427
MOCK_METHOD(Rect, fastBoundingRect, (), (const, override));
2528

0 commit comments

Comments
 (0)