Skip to content

Commit 121d00d

Browse files
committed
Implement sensing_mousey block
1 parent 22ef673 commit 121d00d

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
@@ -45,6 +45,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
4545
engine->addCompileFunction(this, "sensing_keypressed", &compileKeyPressed);
4646
engine->addCompileFunction(this, "sensing_mousedown", &compileMouseDown);
4747
engine->addCompileFunction(this, "sensing_mousex", &compileMouseX);
48+
engine->addCompileFunction(this, "sensing_mousey", &compileMouseY);
4849
}
4950

5051
void SensingBlocks::onInit(IEngine *engine)
@@ -179,6 +180,11 @@ CompilerValue *SensingBlocks::compileMouseX(Compiler *compiler)
179180
return compiler->addFunctionCallWithCtx("sensing_mousex", Compiler::StaticType::Number);
180181
}
181182

183+
CompilerValue *SensingBlocks::compileMouseY(Compiler *compiler)
184+
{
185+
return compiler->addFunctionCallWithCtx("sensing_mousey", Compiler::StaticType::Number);
186+
}
187+
182188
void SensingBlocks::onAnswer(const std::string &answer)
183189
{
184190
// https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L99-L115
@@ -350,3 +356,8 @@ extern "C" double sensing_mousex(ExecutionContext *ctx)
350356
{
351357
return ctx->engine()->mouseX();
352358
}
359+
360+
extern "C" double sensing_mousey(ExecutionContext *ctx)
361+
{
362+
return ctx->engine()->mouseY();
363+
}

src/blocks/sensingblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class SensingBlocks : public IExtension
5151
static CompilerValue *compileKeyPressed(Compiler *compiler);
5252
static CompilerValue *compileMouseDown(Compiler *compiler);
5353
static CompilerValue *compileMouseX(Compiler *compiler);
54+
static CompilerValue *compileMouseY(Compiler *compiler);
5455

5556
static void onAnswer(const std::string &answer);
5657
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
@@ -1276,3 +1276,24 @@ TEST_F(SensingBlocksTest, MouseX)
12761276
ASSERT_EQ(value_toDouble(&value), 53.7);
12771277
value_free(&value);
12781278
}
1279+
1280+
TEST_F(SensingBlocksTest, MouseY)
1281+
{
1282+
auto targetMock = std::make_shared<TargetMock>();
1283+
targetMock->setEngine(&m_engineMock);
1284+
1285+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1286+
builder.addBlock("sensing_mousey");
1287+
Block *block = builder.currentBlock();
1288+
1289+
Compiler compiler(&m_engineMock, targetMock.get());
1290+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1291+
Script script(targetMock.get(), block, &m_engineMock);
1292+
script.setCode(code);
1293+
Thread thread(targetMock.get(), &m_engineMock, &script);
1294+
1295+
EXPECT_CALL(m_engineMock, mouseY()).WillOnce(Return(-78.21));
1296+
ValueData value = thread.runReporter();
1297+
ASSERT_EQ(value_toDouble(&value), -78.21);
1298+
value_free(&value);
1299+
}

0 commit comments

Comments
 (0)