Skip to content

Commit 22ef673

Browse files
committed
Implement sensing_mousex block
1 parent 365c6a9 commit 22ef673

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
4444
engine->addCompileFunction(this, "sensing_answer", &compileAnswer);
4545
engine->addCompileFunction(this, "sensing_keypressed", &compileKeyPressed);
4646
engine->addCompileFunction(this, "sensing_mousedown", &compileMouseDown);
47+
engine->addCompileFunction(this, "sensing_mousex", &compileMouseX);
4748
}
4849

4950
void SensingBlocks::onInit(IEngine *engine)
@@ -173,6 +174,11 @@ CompilerValue *SensingBlocks::compileMouseDown(Compiler *compiler)
173174
return compiler->addFunctionCallWithCtx("sensing_mousedown", Compiler::StaticType::Bool);
174175
}
175176

177+
CompilerValue *SensingBlocks::compileMouseX(Compiler *compiler)
178+
{
179+
return compiler->addFunctionCallWithCtx("sensing_mousex", Compiler::StaticType::Number);
180+
}
181+
176182
void SensingBlocks::onAnswer(const std::string &answer)
177183
{
178184
// https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L99-L115
@@ -339,3 +345,8 @@ extern "C" bool sensing_mousedown(ExecutionContext *ctx)
339345
{
340346
return ctx->engine()->mousePressed();
341347
}
348+
349+
extern "C" double sensing_mousex(ExecutionContext *ctx)
350+
{
351+
return ctx->engine()->mouseX();
352+
}

src/blocks/sensingblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class SensingBlocks : public IExtension
5050
static CompilerValue *compileAnswer(Compiler *compiler);
5151
static CompilerValue *compileKeyPressed(Compiler *compiler);
5252
static CompilerValue *compileMouseDown(Compiler *compiler);
53+
static CompilerValue *compileMouseX(Compiler *compiler);
5354

5455
static void onAnswer(const std::string &answer);
5556
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
@@ -1255,3 +1255,24 @@ TEST_F(SensingBlocksTest, MouseDown)
12551255
ASSERT_FALSE(value_toBool(&value));
12561256
value_free(&value);
12571257
}
1258+
1259+
TEST_F(SensingBlocksTest, MouseX)
1260+
{
1261+
auto targetMock = std::make_shared<TargetMock>();
1262+
targetMock->setEngine(&m_engineMock);
1263+
1264+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1265+
builder.addBlock("sensing_mousex");
1266+
Block *block = builder.currentBlock();
1267+
1268+
Compiler compiler(&m_engineMock, targetMock.get());
1269+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1270+
Script script(targetMock.get(), block, &m_engineMock);
1271+
script.setCode(code);
1272+
Thread thread(targetMock.get(), &m_engineMock, &script);
1273+
1274+
EXPECT_CALL(m_engineMock, mouseX()).WillOnce(Return(53.7));
1275+
ValueData value = thread.runReporter();
1276+
ASSERT_EQ(value_toDouble(&value), 53.7);
1277+
value_free(&value);
1278+
}

0 commit comments

Comments
 (0)