Skip to content

Commit 1254eef

Browse files
committed
Set graphics effects in costumes
1 parent b7253e1 commit 1254eef

File tree

7 files changed

+87
-5
lines changed

7 files changed

+87
-5
lines changed

include/scratchcpp/costume.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ class LIBSCRATCHCPP_EXPORT Costume : public Asset
4141

4242
Rgb **bitmap() const;
4343

44-
void setGraphicsEffect(IGraphicsEffect *effect, double value);
44+
double graphicsEffectValue(IGraphicsEffect *effect) const;
45+
void setGraphicsEffectValue(IGraphicsEffect *effect, double value);
4546

4647
Broadcast *broadcast();
4748

include/scratchcpp/sprite.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace libscratchcpp
99

1010
class ISpriteHandler;
1111
class Rect;
12+
class IGraphicsEffect;
1213
class SpritePrivate;
1314

1415
/*! \brief The Sprite class represents a Scratch sprite. */
@@ -65,6 +66,9 @@ class LIBSCRATCHCPP_EXPORT Sprite : public Target
6566

6667
Rect boundingRect() const;
6768

69+
double graphicsEffectValue(IGraphicsEffect *effect) const;
70+
void setGraphicsEffectValue(IGraphicsEffect *effect, double value);
71+
6872
private:
6973
Target *dataSource() const override;
7074
void setXY(double x, double y);

src/scratch/costume.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,19 @@ Rgb **Costume::bitmap() const
107107
return impl->bitmap;
108108
}
109109

110-
/*! Sets the value of the given graphics effect. */
111-
void Costume::setGraphicsEffect(IGraphicsEffect *effect, double value)
110+
/*! Returns the value of the given graphics effect. */
111+
double Costume::graphicsEffectValue(IGraphicsEffect *effect) const
112+
{
113+
auto it = impl->graphicsEffects.find(effect);
114+
115+
if (it == impl->graphicsEffects.cend())
116+
return 0;
117+
else
118+
return it->second;
119+
}
120+
121+
/*! Sets the value of the given graphics effect (this is automatically set by the sprite). */
122+
void Costume::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
112123
{
113124
auto it = impl->graphicsEffects.find(effect);
114125
bool update = ((it == impl->graphicsEffects.cend()) || (it->second != value));

src/scratch/sprite.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ void Sprite::setCurrentCostume(int newCostume)
212212
if (costume) {
213213
costume->setScale(impl->size / 100);
214214
costume->setMirrorHorizontally(impl->rotationStyle == RotationStyle::LeftRight);
215+
216+
for (const auto &[effect, value] : impl->graphicsEffects)
217+
costume->setGraphicsEffectValue(effect, value);
215218
}
216219

217220
Target::setCurrentCostume(newCostume);
@@ -309,6 +312,29 @@ Rect Sprite::boundingRect() const
309312
return ret;
310313
}
311314

315+
/*! Returns the value of the given graphics effect. */
316+
double Sprite::graphicsEffectValue(IGraphicsEffect *effect) const
317+
{
318+
auto it = impl->graphicsEffects.find(effect);
319+
320+
if (it == impl->graphicsEffects.cend())
321+
return 0;
322+
else
323+
return it->second;
324+
}
325+
326+
/*! Sets the value of the given graphics effect. */
327+
void Sprite::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
328+
{
329+
impl->graphicsEffects[effect] = value;
330+
331+
// TODO: Make currentCostume() return the costume (not index)
332+
auto costume = costumeAt(currentCostume() - 1);
333+
334+
if (costume)
335+
costume->setGraphicsEffectValue(effect, value);
336+
}
337+
312338
Target *Sprite::dataSource() const
313339
{
314340
return impl->cloneRoot;

src/scratch/sprite_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <scratchcpp/sprite.h>
6+
#include <unordered_map>
67

78
namespace libscratchcpp
89
{
@@ -32,6 +33,7 @@ struct SpritePrivate
3233
double direction = 90;
3334
bool draggable = false;
3435
Sprite::RotationStyle rotationStyle = Sprite::RotationStyle::AllAround;
36+
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;
3537
};
3638

3739
} // namespace libscratchcpp

test/assets/costume_test.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,13 @@ TEST_F(CostumeTest, BitmapWithGraphicsEffects)
333333
Costume costume("costume1", "a", "test");
334334

335335
GraphicsEffectMock effect1, effect2;
336-
costume.setGraphicsEffect(&effect1, 54.12);
337-
costume.setGraphicsEffect(&effect2, -89.03);
336+
costume.setGraphicsEffectValue(&effect1, 54.12);
337+
ASSERT_EQ(costume.graphicsEffectValue(&effect1), 54.12);
338+
ASSERT_EQ(costume.graphicsEffectValue(&effect2), 0);
339+
340+
costume.setGraphicsEffectValue(&effect2, -89.03);
341+
ASSERT_EQ(costume.graphicsEffectValue(&effect1), 54.12);
342+
ASSERT_EQ(costume.graphicsEffectValue(&effect2), -89.03);
338343

339344
EXPECT_CALL(*m_imageFormat, width()).WillOnce(Return(4));
340345
EXPECT_CALL(*m_imageFormat, height()).WillOnce(Return(3));

test/scratch_classes/sprite_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <enginemock.h>
1010
#include <imageformatfactorymock.h>
1111
#include <imageformatmock.h>
12+
#include <graphicseffectmock.h>
1213

1314
#include "../common.h"
1415

@@ -616,3 +617,35 @@ TEST(SpriteTest, BoundingRect)
616617

617618
ScratchConfiguration::removeImageFormat("test");
618619
}
620+
621+
TEST(SpriteTest, GraphicsEffects)
622+
{
623+
auto c1 = std::make_shared<Costume>("", "", "");
624+
auto c2 = std::make_shared<Costume>("", "", "");
625+
626+
Sprite sprite;
627+
sprite.addCostume(c1);
628+
sprite.addCostume(c2);
629+
sprite.setCurrentCostume(1);
630+
631+
GraphicsEffectMock effect1, effect2;
632+
sprite.setGraphicsEffectValue(&effect1, 48.21);
633+
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 48.21);
634+
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), 0);
635+
ASSERT_EQ(c1->graphicsEffectValue(&effect1), 48.21);
636+
ASSERT_EQ(c1->graphicsEffectValue(&effect2), 0);
637+
638+
sprite.setCurrentCostume(2);
639+
ASSERT_EQ(c1->graphicsEffectValue(&effect1), 48.21);
640+
ASSERT_EQ(c1->graphicsEffectValue(&effect2), 0);
641+
642+
sprite.setGraphicsEffectValue(&effect2, -107.08);
643+
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 48.21);
644+
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), -107.08);
645+
ASSERT_EQ(c2->graphicsEffectValue(&effect1), 48.21);
646+
ASSERT_EQ(c2->graphicsEffectValue(&effect2), -107.08);
647+
648+
sprite.setCurrentCostume(1);
649+
ASSERT_EQ(c1->graphicsEffectValue(&effect1), 48.21);
650+
ASSERT_EQ(c1->graphicsEffectValue(&effect2), -107.08);
651+
}

0 commit comments

Comments
 (0)