Skip to content

Commit 006b8bb

Browse files
committed
Move graphics effect API to Target
1 parent 7eb661b commit 006b8bb

File tree

11 files changed

+117
-19
lines changed

11 files changed

+117
-19
lines changed

include/scratchcpp/sprite.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,9 @@ class LIBSCRATCHCPP_EXPORT Sprite : public Target
6767
Rect boundingRect() const;
6868
void keepInFence(double newX, double newY, double *fencedX, double *fencedY) const;
6969

70-
double graphicsEffectValue(IGraphicsEffect *effect) const;
71-
void setGraphicsEffectValue(IGraphicsEffect *effect, double value);
70+
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
7271

73-
void clearGraphicsEffects();
72+
void clearGraphicsEffects() override;
7473

7574
private:
7675
Target *dataSource() const override;

include/scratchcpp/stage.h

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

50+
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
51+
52+
void clearGraphicsEffects() override;
53+
5054
private:
5155
spimpl::unique_impl_ptr<StagePrivate> impl;
5256
};

include/scratchcpp/target.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Block;
1717
class Comment;
1818
class Costume;
1919
class Sound;
20+
class IGraphicsEffect;
2021
class TargetPrivate;
2122

2223
/*! \brief The Target class is the Stage or a Sprite. */
@@ -77,6 +78,11 @@ class LIBSCRATCHCPP_EXPORT Target
7778
double volume() const;
7879
void setVolume(double newVolume);
7980

81+
double graphicsEffectValue(IGraphicsEffect *effect) const;
82+
virtual void setGraphicsEffectValue(IGraphicsEffect *effect, double value);
83+
84+
virtual void clearGraphicsEffects();
85+
8086
IEngine *engine() const;
8187
void setEngine(IEngine *engine);
8288

src/scratch/sprite.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,10 @@ void Sprite::keepInFence(double newX, double newY, double *fencedX, double *fenc
400400
*fencedY = newY + dy;
401401
}
402402

403-
/*! Returns the value of the given graphics effect. */
404-
double Sprite::graphicsEffectValue(IGraphicsEffect *effect) const
405-
{
406-
auto it = impl->graphicsEffects.find(effect);
407-
408-
if (it == impl->graphicsEffects.cend())
409-
return 0;
410-
else
411-
return it->second;
412-
}
413-
414-
/*! Sets the value of the given graphics effect. */
403+
/*! Overrides Target#setGraphicsEffectValue(). */
415404
void Sprite::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
416405
{
417-
impl->graphicsEffects[effect] = value;
406+
Target::setGraphicsEffectValue(effect, value);
418407

419408
if (impl->visible) {
420409
IEngine *eng = engine();
@@ -424,10 +413,10 @@ void Sprite::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
424413
}
425414
}
426415

427-
/*! Sets the value of all graphics effects to 0 (clears them). */
416+
/*! Overrides Target#clearGraphicsEffects. */
428417
void Sprite::clearGraphicsEffects()
429418
{
430-
impl->graphicsEffects.clear();
419+
Target::clearGraphicsEffects();
431420

432421
if (impl->visible) {
433422
IEngine *eng = engine();

src/scratch/sprite_p.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ struct SpritePrivate
3131
double direction = 90;
3232
bool draggable = false;
3333
Sprite::RotationStyle rotationStyle = Sprite::RotationStyle::AllAround;
34-
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;
3534
};
3635

3736
} // namespace libscratchcpp

src/scratch/stage.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <scratchcpp/stage.h>
44
#include <scratchcpp/istagehandler.h>
5+
#include <scratchcpp/iengine.h>
56
#include <cassert>
67

78
#include "stage_p.h"
@@ -123,3 +124,23 @@ void Stage::setTextToSpeechLanguage(const std::string &newTextToSpeechLanguage)
123124
{
124125
impl->textToSpeechLanguage = newTextToSpeechLanguage;
125126
}
127+
128+
/*! Overrides Target#setGraphicsEffectValue(). */
129+
void Stage::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
130+
{
131+
Target::setGraphicsEffectValue(effect, value);
132+
IEngine *eng = engine();
133+
134+
if (eng)
135+
eng->requestRedraw();
136+
}
137+
138+
/*! Overrides Target#clearGraphicsEffects(). */
139+
void Stage::clearGraphicsEffects()
140+
{
141+
Target::clearGraphicsEffects();
142+
IEngine *eng = engine();
143+
144+
if (eng)
145+
eng->requestRedraw();
146+
}

src/scratch/target.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,29 @@ void Target::setVolume(double newVolume)
409409
impl->volume = newVolume;
410410
}
411411

412+
/*! Returns the value of the given graphics effect. */
413+
double Target::graphicsEffectValue(IGraphicsEffect *effect) const
414+
{
415+
auto it = impl->graphicsEffects.find(effect);
416+
417+
if (it == impl->graphicsEffects.cend())
418+
return 0;
419+
else
420+
return it->second;
421+
}
422+
423+
/*! Sets the value of the given graphics effect. */
424+
void Target::setGraphicsEffectValue(IGraphicsEffect *effect, double value)
425+
{
426+
impl->graphicsEffects[effect] = value;
427+
}
428+
429+
/*! Sets the value of all graphics effects to 0 (clears them). */
430+
void Target::clearGraphicsEffects()
431+
{
432+
impl->graphicsEffects.clear();
433+
}
434+
412435
/*! Returns the engine. */
413436
IEngine *Target::engine() const
414437
{

src/scratch/target_p.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <string>
66
#include <vector>
77
#include <memory>
8+
#include <unordered_map>
89
#include <scratchcpp/costume.h>
910
#include <scratchcpp/sound.h>
1011

@@ -16,6 +17,7 @@ class Variable;
1617
class List;
1718
class Block;
1819
class Comment;
20+
class IGraphicsEffect;
1921

2022
struct TargetPrivate
2123
{
@@ -33,6 +35,7 @@ struct TargetPrivate
3335
std::vector<std::shared_ptr<Sound>> sounds;
3436
int layerOrder = 0;
3537
double volume = 100;
38+
std::unordered_map<IGraphicsEffect *, double> graphicsEffects;
3639
};
3740

3841
} // namespace libscratchcpp

test/scratch_classes/sprite_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,4 +616,13 @@ TEST(SpriteTest, GraphicsEffects)
616616
sprite.clearGraphicsEffects();
617617
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 0);
618618
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), 0);
619+
620+
sprite.setVisible(false);
621+
sprite.setGraphicsEffectValue(&effect2, -107.08);
622+
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 0);
623+
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), -107.08);
624+
625+
sprite.clearGraphicsEffects();
626+
ASSERT_EQ(sprite.graphicsEffectValue(&effect1), 0);
627+
ASSERT_EQ(sprite.graphicsEffectValue(&effect2), 0);
619628
}

test/scratch_classes/stage_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <scratchcpp/stage.h>
22
#include <scratchcpp/costume.h>
33
#include <enginemock.h>
4+
#include <graphicseffectmock.h>
45

56
#include "../common.h"
67

@@ -105,3 +106,25 @@ TEST(StageTest, TextToSpeechLanguage)
105106
stage.setTextToSpeechLanguage("English");
106107
ASSERT_EQ(stage.textToSpeechLanguage(), "English");
107108
}
109+
110+
TEST(StageTest, GraphicsEffects)
111+
{
112+
Stage stage;
113+
114+
EngineMock engine;
115+
stage.setEngine(&engine);
116+
EXPECT_CALL(engine, requestRedraw()).Times(3);
117+
118+
GraphicsEffectMock effect1, effect2;
119+
stage.setGraphicsEffectValue(&effect1, 48.21);
120+
ASSERT_EQ(stage.graphicsEffectValue(&effect1), 48.21);
121+
ASSERT_EQ(stage.graphicsEffectValue(&effect2), 0);
122+
123+
stage.setGraphicsEffectValue(&effect2, -107.08);
124+
ASSERT_EQ(stage.graphicsEffectValue(&effect1), 48.21);
125+
ASSERT_EQ(stage.graphicsEffectValue(&effect2), -107.08);
126+
127+
stage.clearGraphicsEffects();
128+
ASSERT_EQ(stage.graphicsEffectValue(&effect1), 0);
129+
ASSERT_EQ(stage.graphicsEffectValue(&effect2), 0);
130+
}

test/scratch_classes/target_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/sound.h>
88
#include <enginemock.h>
99
#include <targetmock.h>
10+
#include <graphicseffectmock.h>
1011

1112
#include "../common.h"
1213

@@ -481,6 +482,27 @@ TEST(TargetTest, Volume)
481482
ASSERT_EQ(target.volume(), 0);
482483
}
483484

485+
TEST(TargetTest, GraphicsEffects)
486+
{
487+
Target target;
488+
489+
EngineMock engine;
490+
target.setEngine(&engine);
491+
492+
GraphicsEffectMock effect1, effect2;
493+
target.setGraphicsEffectValue(&effect1, 48.21);
494+
ASSERT_EQ(target.graphicsEffectValue(&effect1), 48.21);
495+
ASSERT_EQ(target.graphicsEffectValue(&effect2), 0);
496+
497+
target.setGraphicsEffectValue(&effect2, -107.08);
498+
ASSERT_EQ(target.graphicsEffectValue(&effect1), 48.21);
499+
ASSERT_EQ(target.graphicsEffectValue(&effect2), -107.08);
500+
501+
target.clearGraphicsEffects();
502+
ASSERT_EQ(target.graphicsEffectValue(&effect1), 0);
503+
ASSERT_EQ(target.graphicsEffectValue(&effect2), 0);
504+
}
505+
484506
TEST(TargetTest, Engine)
485507
{
486508
Target target;

0 commit comments

Comments
 (0)