Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fc7b850

Browse files
committedApr 18, 2025·
Implement sound_setvolumeto block
1 parent 3b8d600 commit fc7b850

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
@@ -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

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

+30
Original file line numberDiff line numberDiff line change
@@ -1058,3 +1058,33 @@ TEST_F(SoundBlocksTest, ChangeVolumeBy_Stage)
10581058
builder.run();
10591059
ASSERT_EQ(stage->volume(), 40.1);
10601060
}
1061+
1062+
TEST_F(SoundBlocksTest, SetVolumeTo_Sprite)
1063+
{
1064+
auto sprite = std::make_shared<Sprite>();
1065+
sprite->setVolume(18);
1066+
1067+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
1068+
builder.addBlock("sound_setvolumeto");
1069+
builder.addValueInput("VOLUME", 65.2);
1070+
builder.build();
1071+
1072+
sprite->setVolume(1.9);
1073+
builder.run();
1074+
ASSERT_EQ(sprite->volume(), 65.2);
1075+
}
1076+
1077+
TEST_F(SoundBlocksTest, SetVolumeTo_Stage)
1078+
{
1079+
auto stage = std::make_shared<Stage>();
1080+
stage->setVolume(42);
1081+
1082+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
1083+
builder.addBlock("sound_setvolumeto");
1084+
builder.addValueInput("VOLUME", 38.4);
1085+
builder.build();
1086+
1087+
stage->setVolume(78.5);
1088+
builder.run();
1089+
ASSERT_EQ(stage->volume(), 38.4);
1090+
}

0 commit comments

Comments
 (0)
Please sign in to comment.