Skip to content

Commit 4e077a2

Browse files
committed
Target: Add clearSoundEffects() method
1 parent 91c7655 commit 4e077a2

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

include/scratchcpp/target.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class LIBSCRATCHCPP_EXPORT Target
9292
virtual double soundEffect(Sound::Effect effect) const;
9393
virtual void setSoundEffect(Sound::Effect effect, double value);
9494

95+
virtual void clearSoundEffects();
96+
9597
virtual Rect boundingRect() const;
9698
virtual Rect fastBoundingRect() const;
9799

src/scratch/target.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,17 @@ void Target::setSoundEffect(Sound::Effect effect, double value)
444444
}
445445
}
446446

447+
/*! Sets the value of all sound effects to 0 (clears them). */
448+
void Target::clearSoundEffects()
449+
{
450+
std::unordered_map<Sound::Effect, double> effects = impl->soundEffects; // must copy!
451+
452+
for (const auto &[effect, value] : effects)
453+
setSoundEffect(effect, 0);
454+
455+
impl->soundEffects.clear();
456+
}
457+
447458
/*! Returns the bounding rectangle of the sprite. */
448459
Rect Target::boundingRect() const
449460
{

test/mocks/targetmock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class TargetMock : public Target
2020
MOCK_METHOD(double, soundEffect, (Sound::Effect), (const, override));
2121
MOCK_METHOD(void, setSoundEffect, (Sound::Effect, double), (override));
2222

23+
MOCK_METHOD(void, clearSoundEffects, (), (override));
24+
2325
MOCK_METHOD(Rect, boundingRect, (), (const, override));
2426
MOCK_METHOD(Rect, fastBoundingRect, (), (const, override));
2527

test/scratch_classes/target_test.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ TEST(TargetTest, Volume)
512512
SoundPrivate::audioOutput = nullptr;
513513
}
514514

515-
TEST(TargetTest, SoundEffect)
515+
TEST(TargetTest, SoundEffects)
516516
{
517517
Target target;
518518
ASSERT_EQ(target.soundEffect(Sound::Effect::Pitch), 0);
@@ -548,6 +548,18 @@ TEST(TargetTest, SoundEffect)
548548
EXPECT_CALL(*s4, setEffect(Sound::Effect::Pitch, 12.5));
549549
EXPECT_CALL(*s4, setEffect(Sound::Effect::Pan, -56.7));
550550
target.addSound(s4);
551+
552+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pitch, 0));
553+
EXPECT_CALL(*s1, setEffect(Sound::Effect::Pan, 0));
554+
EXPECT_CALL(*s2, setEffect(Sound::Effect::Pitch, 0));
555+
EXPECT_CALL(*s2, setEffect(Sound::Effect::Pan, 0));
556+
EXPECT_CALL(*s3, setEffect(Sound::Effect::Pitch, 0));
557+
EXPECT_CALL(*s3, setEffect(Sound::Effect::Pan, 0));
558+
EXPECT_CALL(*s4, setEffect(Sound::Effect::Pitch, 0));
559+
EXPECT_CALL(*s4, setEffect(Sound::Effect::Pan, 0));
560+
target.clearSoundEffects();
561+
ASSERT_EQ(target.soundEffect(Sound::Effect::Pitch), 0);
562+
ASSERT_EQ(target.soundEffect(Sound::Effect::Pan), 0);
551563
}
552564

553565
TEST(TargetTest, CurrentCostumeWidth)

0 commit comments

Comments
 (0)