Skip to content

Commit 2e5d4dc

Browse files
committed
Implement sensing_timer block
1 parent c43ee3a commit 2e5d4dc

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <scratchcpp/stringptr.h>
1515
#include <scratchcpp/string_functions.h>
1616
#include <scratchcpp/string_pool.h>
17+
#include <scratchcpp/itimer.h>
1718
#include <utf8.h>
1819

1920
#include "sensingblocks.h"
@@ -52,6 +53,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
5253
engine->addCompileFunction(this, "sensing_setdragmode", &compileSetDragMode);
5354
engine->addCompileFunction(this, "sensing_loudness", &compileLoudness);
5455
engine->addCompileFunction(this, "sensing_loud", &compileLoud);
56+
engine->addCompileFunction(this, "sensing_timer", &compileTimer);
5557
}
5658

5759
void SensingBlocks::onInit(IEngine *engine)
@@ -225,6 +227,13 @@ CompilerValue *SensingBlocks::compileLoud(Compiler *compiler)
225227
return compiler->createCmpGT(loudness, treshold);
226228
}
227229

230+
CompilerValue *SensingBlocks::compileTimer(Compiler *compiler)
231+
{
232+
ITimer *timer = compiler->engine()->timer();
233+
CompilerValue *timerPtr = compiler->addConstValue(timer);
234+
return compiler->addFunctionCall("sensing_timer", Compiler::StaticType::Number, { Compiler::StaticType::Pointer }, { timerPtr });
235+
}
236+
228237
void SensingBlocks::onAnswer(const std::string &answer)
229238
{
230239
// https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L99-L115
@@ -415,3 +424,8 @@ extern "C" double sensing_loudness()
415424
auto audioLoudness = SensingBlocks::audioInput->audioLoudness();
416425
return audioLoudness->getLoudness();
417426
}
427+
428+
extern "C" double sensing_timer(ITimer *timer)
429+
{
430+
return timer->value();
431+
}

src/blocks/sensingblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class SensingBlocks : public IExtension
5858
static CompilerValue *compileSetDragMode(Compiler *compiler);
5959
static CompilerValue *compileLoudness(Compiler *compiler);
6060
static CompilerValue *compileLoud(Compiler *compiler);
61+
static CompilerValue *compileTimer(Compiler *compiler);
6162

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

test/blocks/sensing_blocks_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <targetmock.h>
1212
#include <audioinputmock.h>
1313
#include <audioloudnessmock.h>
14+
#include <timermock.h>
1415

1516
#include "../common.h"
1617
#include "blocks/sensingblocks.h"
@@ -1456,3 +1457,26 @@ TEST_F(SensingBlocksTest, Loud_Above)
14561457
ASSERT_TRUE(value_toBool(&value));
14571458
value_free(&value);
14581459
}
1460+
1461+
TEST_F(SensingBlocksTest, Timer)
1462+
{
1463+
auto targetMock = std::make_shared<TargetMock>();
1464+
1465+
TimerMock timer;
1466+
EXPECT_CALL(m_engineMock, timer()).WillOnce(Return(&timer));
1467+
1468+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1469+
builder.addBlock("sensing_timer");
1470+
Block *block = builder.currentBlock();
1471+
1472+
Compiler compiler(&m_engineMock, targetMock.get());
1473+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1474+
Script script(targetMock.get(), block, &m_engineMock);
1475+
script.setCode(code);
1476+
Thread thread(targetMock.get(), &m_engineMock, &script);
1477+
1478+
EXPECT_CALL(timer, value()).WillOnce(Return(23.4));
1479+
ValueData value = thread.runReporter();
1480+
ASSERT_EQ(value_toDouble(&value), 23.4);
1481+
value_free(&value);
1482+
}

0 commit comments

Comments
 (0)