Skip to content

Commit 365c6a9

Browse files
committed
Implement sensing_mousedown block
1 parent bf01dd9 commit 365c6a9

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
4343
engine->addCompileFunction(this, "sensing_askandwait", &compileAskAndWait);
4444
engine->addCompileFunction(this, "sensing_answer", &compileAnswer);
4545
engine->addCompileFunction(this, "sensing_keypressed", &compileKeyPressed);
46+
engine->addCompileFunction(this, "sensing_mousedown", &compileMouseDown);
4647
}
4748

4849
void SensingBlocks::onInit(IEngine *engine)
@@ -167,6 +168,11 @@ CompilerValue *SensingBlocks::compileKeyPressed(Compiler *compiler)
167168
return compiler->addFunctionCallWithCtx("sensing_keypressed", Compiler::StaticType::Bool, { Compiler::StaticType::String }, { key });
168169
}
169170

171+
CompilerValue *SensingBlocks::compileMouseDown(Compiler *compiler)
172+
{
173+
return compiler->addFunctionCallWithCtx("sensing_mousedown", Compiler::StaticType::Bool);
174+
}
175+
170176
void SensingBlocks::onAnswer(const std::string &answer)
171177
{
172178
// https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L99-L115
@@ -328,3 +334,8 @@ extern "C" bool sensing_keypressed(ExecutionContext *ctx, const StringPtr *key)
328334
std::string u8name = utf8::utf16to8(std::u16string(key->data));
329335
return ctx->engine()->keyPressed(u8name);
330336
}
337+
338+
extern "C" bool sensing_mousedown(ExecutionContext *ctx)
339+
{
340+
return ctx->engine()->mousePressed();
341+
}

src/blocks/sensingblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SensingBlocks : public IExtension
4949
static CompilerValue *compileAskAndWait(Compiler *compiler);
5050
static CompilerValue *compileAnswer(Compiler *compiler);
5151
static CompilerValue *compileKeyPressed(Compiler *compiler);
52+
static CompilerValue *compileMouseDown(Compiler *compiler);
5253

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

test/blocks/sensing_blocks_test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,3 +1229,29 @@ TEST_F(SensingBlocksTest, KeyPressed_M)
12291229
ASSERT_FALSE(value_toBool(&value));
12301230
value_free(&value);
12311231
}
1232+
1233+
TEST_F(SensingBlocksTest, MouseDown)
1234+
{
1235+
auto targetMock = std::make_shared<TargetMock>();
1236+
targetMock->setEngine(&m_engineMock);
1237+
1238+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
1239+
builder.addBlock("sensing_mousedown");
1240+
Block *block = builder.currentBlock();
1241+
1242+
Compiler compiler(&m_engineMock, targetMock.get());
1243+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1244+
Script script(targetMock.get(), block, &m_engineMock);
1245+
script.setCode(code);
1246+
Thread thread(targetMock.get(), &m_engineMock, &script);
1247+
1248+
EXPECT_CALL(m_engineMock, mousePressed()).WillOnce(Return(true));
1249+
ValueData value = thread.runReporter();
1250+
ASSERT_TRUE(value_toBool(&value));
1251+
value_free(&value);
1252+
1253+
EXPECT_CALL(m_engineMock, mousePressed()).WillOnce(Return(false));
1254+
value = thread.runReporter();
1255+
ASSERT_FALSE(value_toBool(&value));
1256+
value_free(&value);
1257+
}

0 commit comments

Comments
 (0)