Skip to content

Commit 7d302a3

Browse files
committed
Implement sound_changevolumeby
1 parent b54ac00 commit 7d302a3

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/blocks/soundblocks.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3737
engine->addCompileFunction(this, "sound_seteffectto", &compileSetEffectTo);
3838
engine->addCompileFunction(this, "sound_changeeffectby", &compileChangeEffectBy);
3939
engine->addCompileFunction(this, "sound_cleareffects", &compileClearEffects);
40+
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
4041
}
4142

4243
void SoundBlocks::onInit(IEngine *engine)
@@ -128,6 +129,13 @@ CompilerValue *SoundBlocks::compileClearEffects(Compiler *compiler)
128129
return nullptr;
129130
}
130131

132+
CompilerValue *SoundBlocks::compileChangeVolumeBy(Compiler *compiler)
133+
{
134+
auto volume = compiler->addInput("VOLUME");
135+
compiler->addTargetFunctionCall("sound_changevolumeby", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { volume });
136+
return nullptr;
137+
}
138+
131139
int sound_wrap_clamp_index(Target *target, int index)
132140
{
133141
const long soundCount = target->sounds().size();
@@ -222,3 +230,8 @@ extern "C" void sound_cleareffects(Target *target)
222230
{
223231
target->clearSoundEffects();
224232
}
233+
234+
extern "C" void sound_changevolumeby(Target *target, double volume)
235+
{
236+
target->setVolume(target->volume() + volume);
237+
}

src/blocks/soundblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class SoundBlocks : public IExtension
2626
static CompilerValue *compileSetEffectTo(Compiler *compiler);
2727
static CompilerValue *compileChangeEffectBy(Compiler *compiler);
2828
static CompilerValue *compileClearEffects(Compiler *compiler);
29+
static CompilerValue *compileChangeVolumeBy(Compiler *compiler);
2930
};
3031

3132
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,31 @@ TEST_F(SoundBlocksTest, ClearEffects)
10281028
EXPECT_CALL(*target, setSoundEffectValue).Times(0);
10291029
thread.run();
10301030
}
1031+
1032+
TEST_F(SoundBlocksTest, ChangeVolumeBy_Sprite)
1033+
{
1034+
auto sprite = std::make_shared<Sprite>();
1035+
sprite->setVolume(1.9);
1036+
1037+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1038+
builder.addBlock("sound_changevolumeby");
1039+
builder.addValueInput("VOLUME", 65.2);
1040+
builder.build();
1041+
1042+
builder.run();
1043+
ASSERT_EQ(std::round(sprite->volume() * 100) / 100, 67.1);
1044+
}
1045+
1046+
TEST_F(SoundBlocksTest, ChangeVolumeBy_Stage)
1047+
{
1048+
auto stage = std::make_shared<Stage>();
1049+
stage->setVolume(78.5);
1050+
1051+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1052+
builder.addBlock("sound_changevolumeby");
1053+
builder.addValueInput("VOLUME", -38.4);
1054+
builder.build();
1055+
1056+
builder.run();
1057+
ASSERT_EQ(stage->volume(), 40.1);
1058+
}

0 commit comments

Comments
 (0)