Skip to content

Commit b01c1d7

Browse files
committed
Implement sound_setvolumeto block
1 parent 7d302a3 commit b01c1d7

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
@@ -38,6 +38,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3838
engine->addCompileFunction(this, "sound_changeeffectby", &compileChangeEffectBy);
3939
engine->addCompileFunction(this, "sound_cleareffects", &compileClearEffects);
4040
engine->addCompileFunction(this, "sound_changevolumeby", &compileChangeVolumeBy);
41+
engine->addCompileFunction(this, "sound_setvolumeto", &compileSetVolumeTo);
4142
}
4243

4344
void SoundBlocks::onInit(IEngine *engine)
@@ -136,6 +137,13 @@ CompilerValue *SoundBlocks::compileChangeVolumeBy(Compiler *compiler)
136137
return nullptr;
137138
}
138139

140+
CompilerValue *SoundBlocks::compileSetVolumeTo(Compiler *compiler)
141+
{
142+
auto volume = compiler->addInput("VOLUME");
143+
compiler->addTargetFunctionCall("sound_setvolumeto", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { volume });
144+
return nullptr;
145+
}
146+
139147
int sound_wrap_clamp_index(Target *target, int index)
140148
{
141149
const long soundCount = target->sounds().size();
@@ -235,3 +243,8 @@ extern "C" void sound_changevolumeby(Target *target, double volume)
235243
{
236244
target->setVolume(target->volume() + volume);
237245
}
246+
247+
extern "C" void sound_setvolumeto(Target *target, double volume)
248+
{
249+
target->setVolume(volume);
250+
}

src/blocks/soundblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class SoundBlocks : public IExtension
2727
static CompilerValue *compileChangeEffectBy(Compiler *compiler);
2828
static CompilerValue *compileClearEffects(Compiler *compiler);
2929
static CompilerValue *compileChangeVolumeBy(Compiler *compiler);
30+
static CompilerValue *compileSetVolumeTo(Compiler *compiler);
3031
};
3132

3233
} // namespace libscratchcpp

test/blocks/sound_blocks_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,3 +1056,31 @@ TEST_F(SoundBlocksTest, ChangeVolumeBy_Stage)
10561056
builder.run();
10571057
ASSERT_EQ(stage->volume(), 40.1);
10581058
}
1059+
1060+
TEST_F(SoundBlocksTest, SetVolumeTo_Sprite)
1061+
{
1062+
auto sprite = std::make_shared<Sprite>();
1063+
sprite->setVolume(1.9);
1064+
1065+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1066+
builder.addBlock("sound_setvolumeto");
1067+
builder.addValueInput("VOLUME", 65.2);
1068+
builder.build();
1069+
1070+
builder.run();
1071+
ASSERT_EQ(sprite->volume(), 65.2);
1072+
}
1073+
1074+
TEST_F(SoundBlocksTest, SetVolumeTo_Stage)
1075+
{
1076+
auto stage = std::make_shared<Stage>();
1077+
stage->setVolume(78.5);
1078+
1079+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1080+
builder.addBlock("sound_setvolumeto");
1081+
builder.addValueInput("VOLUME", 38.4);
1082+
builder.build();
1083+
1084+
builder.run();
1085+
ASSERT_EQ(stage->volume(), 38.4);
1086+
}

0 commit comments

Comments
 (0)