Skip to content

Commit 0a9314a

Browse files
committed
Implement sensing_mousedown block
1 parent 61d83e7 commit 0a9314a

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
2525
// Blocks
2626
engine->addCompileFunction(this, "sensing_distanceto", &compileDistanceTo);
2727
engine->addCompileFunction(this, "sensing_keypressed", &compileKeyPressed);
28+
engine->addCompileFunction(this, "sensing_mousedown", &compileMouseDown);
2829
engine->addCompileFunction(this, "sensing_timer", &compileTimer);
2930
engine->addCompileFunction(this, "sensing_resettimer", &compileResetTimer);
3031
engine->addCompileFunction(this, "sensing_current", &compileCurrent);
@@ -74,6 +75,11 @@ void SensingBlocks::compileKeyPressed(Compiler *compiler)
7475
compiler->addFunctionCall(&keyPressed);
7576
}
7677

78+
void SensingBlocks::compileMouseDown(Compiler *compiler)
79+
{
80+
compiler->addFunctionCall(&mouseDown);
81+
}
82+
7783
void SensingBlocks::compileTimer(Compiler *compiler)
7884
{
7985
compiler->addFunctionCall(&timer);
@@ -133,6 +139,12 @@ unsigned int SensingBlocks::keyPressed(VirtualMachine *vm)
133139
return 0;
134140
}
135141

142+
unsigned int SensingBlocks::mouseDown(VirtualMachine *vm)
143+
{
144+
vm->addReturnValue(vm->engine()->mousePressed());
145+
return 0;
146+
}
147+
136148
unsigned int SensingBlocks::distanceTo(VirtualMachine *vm)
137149
{
138150
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());

src/blocks/sensingblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ class SensingBlocks : public IBlockSection
4040

4141
static void compileDistanceTo(Compiler *compiler);
4242
static void compileKeyPressed(Compiler *compiler);
43+
static void compileMouseDown(Compiler *compiler);
4344
static void compileTimer(Compiler *compiler);
4445
static void compileResetTimer(Compiler *compiler);
4546
static void compileCurrent(Compiler *compiler);
4647
static void compileDaysSince2000(Compiler *compiler);
4748

4849
static unsigned int keyPressed(VirtualMachine *vm);
50+
static unsigned int mouseDown(VirtualMachine *vm);
4951

5052
static unsigned int distanceTo(VirtualMachine *vm);
5153
static unsigned int distanceToByIndex(VirtualMachine *vm);

test/blocks/sensing_blocks_test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
9999
// Blocks
100100
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_distanceto", &SensingBlocks::compileDistanceTo));
101101
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_keypressed", &SensingBlocks::compileKeyPressed));
102+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_mousedown", &SensingBlocks::compileMouseDown));
102103
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_timer", &SensingBlocks::compileTimer));
103104
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_resettimer", &SensingBlocks::compileResetTimer));
104105
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_current", &SensingBlocks::compileCurrent));
@@ -339,6 +340,47 @@ TEST_F(SensingBlocksTest, KeyPressedImpl)
339340
ASSERT_EQ(vm.getInput(0, 1)->toBool(), false);
340341
}
341342

343+
TEST_F(SensingBlocksTest, MouseDown)
344+
{
345+
Compiler compiler(&m_engineMock);
346+
347+
auto block = std::make_shared<Block>("a", "sensing_mousedown");
348+
349+
EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::mouseDown)).WillOnce(Return(0));
350+
compiler.init();
351+
352+
compiler.setBlock(block);
353+
SensingBlocks::compileMouseDown(&compiler);
354+
355+
compiler.end();
356+
357+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
358+
ASSERT_TRUE(compiler.constValues().empty());
359+
}
360+
361+
TEST_F(SensingBlocksTest, MouseDownImpl)
362+
{
363+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
364+
static BlockFunc functions[] = { &SensingBlocks::mouseDown };
365+
366+
VirtualMachine vm(nullptr, &m_engineMock, nullptr);
367+
vm.setFunctions(functions);
368+
369+
EXPECT_CALL(m_engineMock, mousePressed()).WillOnce(Return(true));
370+
vm.setBytecode(bytecode);
371+
vm.run();
372+
373+
ASSERT_EQ(vm.registerCount(), 1);
374+
ASSERT_EQ(vm.getInput(0, 1)->toBool(), true);
375+
376+
EXPECT_CALL(m_engineMock, mousePressed()).WillOnce(Return(false));
377+
vm.reset();
378+
vm.run();
379+
380+
ASSERT_EQ(vm.registerCount(), 1);
381+
ASSERT_EQ(vm.getInput(0, 1)->toBool(), false);
382+
}
383+
342384
TEST_F(SensingBlocksTest, Timer)
343385
{
344386
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)