Skip to content

Commit 3709135

Browse files
committed
Implement sensing_resettimer block
1 parent 2e5d4dc commit 3709135

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
5454
engine->addCompileFunction(this, "sensing_loudness", &compileLoudness);
5555
engine->addCompileFunction(this, "sensing_loud", &compileLoud);
5656
engine->addCompileFunction(this, "sensing_timer", &compileTimer);
57+
engine->addCompileFunction(this, "sensing_resettimer", &compileResetTimer);
5758
}
5859

5960
void SensingBlocks::onInit(IEngine *engine)
@@ -234,6 +235,14 @@ CompilerValue *SensingBlocks::compileTimer(Compiler *compiler)
234235
return compiler->addFunctionCall("sensing_timer", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { timerPtr });
235236
}
236237

238+
CompilerValue *SensingBlocks::compileResetTimer(Compiler *compiler)
239+
{
240+
ITimer *timer = compiler->engine()->timer();
241+
CompilerValue *timerPtr = compiler->addConstValue(timer);
242+
compiler->addFunctionCall("sensing_resettimer", Compiler::StaticType::Void, { Compiler::StaticType::Pointer }, { timerPtr });
243+
return nullptr;
244+
}
245+
237246
void SensingBlocks::onAnswer(const std::string &answer)
238247
{
239248
// https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L99-L115
@@ -429,3 +438,8 @@ extern "C" double sensing_timer(ITimer *timer)
429438
{
430439
return timer->value();
431440
}
441+
442+
extern "C" void sensing_resettimer(ITimer *timer)
443+
{
444+
timer->reset();
445+
}

src/blocks/sensingblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class SensingBlocks : public IExtension
5959
static CompilerValue *compileLoudness(Compiler *compiler);
6060
static CompilerValue *compileLoud(Compiler *compiler);
6161
static CompilerValue *compileTimer(Compiler *compiler);
62+
static CompilerValue *compileResetTimer(Compiler *compiler);
6263

6364
static void onAnswer(const std::string &answer);
6465
static void enqueueAsk(const std::string &question, Thread *thread);

test/blocks/sensing_blocks_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,3 +1480,24 @@ TEST_F(SensingBlocksTest, Timer)
14801480
ASSERT_EQ(value_toDouble(&value), 23.4);
14811481
value_free(&value);
14821482
}
1483+
1484+
TEST_F(SensingBlocksTest, ResetTimer)
1485+
{
1486+
auto targetMock = std::make_shared<TargetMock>();
1487+
1488+
TimerMock timer;
1489+
EXPECT_CALL(m_engineMock, timer()).WillOnce(Return(&timer));
1490+
1491+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1492+
builder.addBlock("sensing_resettimer");
1493+
Block *block = builder.currentBlock();
1494+
1495+
Compiler compiler(&m_engineMock, targetMock.get());
1496+
auto code = compiler.compile(block);
1497+
Script script(targetMock.get(), block, &m_engineMock);
1498+
script.setCode(code);
1499+
Thread thread(targetMock.get(), &m_engineMock, &script);
1500+
1501+
EXPECT_CALL(timer, reset());
1502+
thread.run();
1503+
}

0 commit comments

Comments
 (0)