Skip to content

Commit 3b8d600

Browse files
committed
Implement sound_changevolumeby
1 parent b54ac00 commit 3b8d600

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/blocks/soundblocks.cpp

+13
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

+1
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

+30
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,33 @@ 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(42);
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+
sprite->setVolume(1.9);
1043+
builder.run();
1044+
ASSERT_EQ(std::round(sprite->volume() * 100) / 100, 67.1);
1045+
}
1046+
1047+
TEST_F(SoundBlocksTest, ChangeVolumeBy_Stage)
1048+
{
1049+
auto stage = std::make_shared<Stage>();
1050+
stage->setVolume(18);
1051+
1052+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1053+
builder.addBlock("sound_changevolumeby");
1054+
builder.addValueInput("VOLUME", -38.4);
1055+
builder.build();
1056+
1057+
stage->setVolume(78.5);
1058+
builder.run();
1059+
ASSERT_EQ(stage->volume(), 40.1);
1060+
}

0 commit comments

Comments
 (0)