Skip to content

Commit 5191183

Browse files
authored
Merge pull request #267 from scratchcpp/mouse_blocks
Implement mouse sensing blocks
2 parents 61d83e7 + 89afe6a commit 5191183

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ 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);
29+
engine->addCompileFunction(this, "sensing_mousex", &compileMouseX);
30+
engine->addCompileFunction(this, "sensing_mousey", &compileMouseY);
2831
engine->addCompileFunction(this, "sensing_timer", &compileTimer);
2932
engine->addCompileFunction(this, "sensing_resettimer", &compileResetTimer);
3033
engine->addCompileFunction(this, "sensing_current", &compileCurrent);
@@ -74,6 +77,21 @@ void SensingBlocks::compileKeyPressed(Compiler *compiler)
7477
compiler->addFunctionCall(&keyPressed);
7578
}
7679

80+
void SensingBlocks::compileMouseDown(Compiler *compiler)
81+
{
82+
compiler->addFunctionCall(&mouseDown);
83+
}
84+
85+
void SensingBlocks::compileMouseX(Compiler *compiler)
86+
{
87+
compiler->addFunctionCall(&mouseX);
88+
}
89+
90+
void SensingBlocks::compileMouseY(Compiler *compiler)
91+
{
92+
compiler->addFunctionCall(&mouseY);
93+
}
94+
7795
void SensingBlocks::compileTimer(Compiler *compiler)
7896
{
7997
compiler->addFunctionCall(&timer);
@@ -133,6 +151,24 @@ unsigned int SensingBlocks::keyPressed(VirtualMachine *vm)
133151
return 0;
134152
}
135153

154+
unsigned int SensingBlocks::mouseDown(VirtualMachine *vm)
155+
{
156+
vm->addReturnValue(vm->engine()->mousePressed());
157+
return 0;
158+
}
159+
160+
unsigned int SensingBlocks::mouseX(VirtualMachine *vm)
161+
{
162+
vm->addReturnValue(vm->engine()->mouseX());
163+
return 0;
164+
}
165+
166+
unsigned int SensingBlocks::mouseY(VirtualMachine *vm)
167+
{
168+
vm->addReturnValue(vm->engine()->mouseY());
169+
return 0;
170+
}
171+
136172
unsigned int SensingBlocks::distanceTo(VirtualMachine *vm)
137173
{
138174
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());

src/blocks/sensingblocks.h

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

4141
static void compileDistanceTo(Compiler *compiler);
4242
static void compileKeyPressed(Compiler *compiler);
43+
static void compileMouseDown(Compiler *compiler);
44+
static void compileMouseX(Compiler *compiler);
45+
static void compileMouseY(Compiler *compiler);
4346
static void compileTimer(Compiler *compiler);
4447
static void compileResetTimer(Compiler *compiler);
4548
static void compileCurrent(Compiler *compiler);
4649
static void compileDaysSince2000(Compiler *compiler);
4750

4851
static unsigned int keyPressed(VirtualMachine *vm);
52+
static unsigned int mouseDown(VirtualMachine *vm);
53+
static unsigned int mouseX(VirtualMachine *vm);
54+
static unsigned int mouseY(VirtualMachine *vm);
4955

5056
static unsigned int distanceTo(VirtualMachine *vm);
5157
static unsigned int distanceToByIndex(VirtualMachine *vm);

test/blocks/sensing_blocks_test.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ 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));
103+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_mousex", &SensingBlocks::compileMouseX));
104+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_mousey", &SensingBlocks::compileMouseY));
102105
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_timer", &SensingBlocks::compileTimer));
103106
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_resettimer", &SensingBlocks::compileResetTimer));
104107
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_current", &SensingBlocks::compileCurrent));
@@ -339,6 +342,129 @@ TEST_F(SensingBlocksTest, KeyPressedImpl)
339342
ASSERT_EQ(vm.getInput(0, 1)->toBool(), false);
340343
}
341344

345+
TEST_F(SensingBlocksTest, MouseDown)
346+
{
347+
Compiler compiler(&m_engineMock);
348+
349+
auto block = std::make_shared<Block>("a", "sensing_mousedown");
350+
351+
EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::mouseDown)).WillOnce(Return(0));
352+
compiler.init();
353+
354+
compiler.setBlock(block);
355+
SensingBlocks::compileMouseDown(&compiler);
356+
357+
compiler.end();
358+
359+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
360+
ASSERT_TRUE(compiler.constValues().empty());
361+
}
362+
363+
TEST_F(SensingBlocksTest, MouseDownImpl)
364+
{
365+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
366+
static BlockFunc functions[] = { &SensingBlocks::mouseDown };
367+
368+
VirtualMachine vm(nullptr, &m_engineMock, nullptr);
369+
vm.setFunctions(functions);
370+
371+
EXPECT_CALL(m_engineMock, mousePressed()).WillOnce(Return(true));
372+
vm.setBytecode(bytecode);
373+
vm.run();
374+
375+
ASSERT_EQ(vm.registerCount(), 1);
376+
ASSERT_EQ(vm.getInput(0, 1)->toBool(), true);
377+
378+
EXPECT_CALL(m_engineMock, mousePressed()).WillOnce(Return(false));
379+
vm.reset();
380+
vm.run();
381+
382+
ASSERT_EQ(vm.registerCount(), 1);
383+
ASSERT_EQ(vm.getInput(0, 1)->toBool(), false);
384+
}
385+
386+
TEST_F(SensingBlocksTest, MouseX)
387+
{
388+
Compiler compiler(&m_engineMock);
389+
390+
auto block = std::make_shared<Block>("a", "sensing_mousex");
391+
392+
EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::mouseX)).WillOnce(Return(0));
393+
compiler.init();
394+
395+
compiler.setBlock(block);
396+
SensingBlocks::compileMouseX(&compiler);
397+
398+
compiler.end();
399+
400+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
401+
ASSERT_TRUE(compiler.constValues().empty());
402+
}
403+
404+
TEST_F(SensingBlocksTest, MouseXImpl)
405+
{
406+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
407+
static BlockFunc functions[] = { &SensingBlocks::mouseX };
408+
409+
VirtualMachine vm(nullptr, &m_engineMock, nullptr);
410+
vm.setFunctions(functions);
411+
412+
EXPECT_CALL(m_engineMock, mouseX()).WillOnce(Return(48.165));
413+
vm.setBytecode(bytecode);
414+
vm.run();
415+
416+
ASSERT_EQ(vm.registerCount(), 1);
417+
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), 48.165);
418+
419+
EXPECT_CALL(m_engineMock, mouseX()).WillOnce(Return(-239.09));
420+
vm.reset();
421+
vm.run();
422+
423+
ASSERT_EQ(vm.registerCount(), 1);
424+
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), -239.09);
425+
}
426+
427+
TEST_F(SensingBlocksTest, MouseY)
428+
{
429+
Compiler compiler(&m_engineMock);
430+
431+
auto block = std::make_shared<Block>("a", "sensing_mousey");
432+
433+
EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::mouseY)).WillOnce(Return(0));
434+
compiler.init();
435+
436+
compiler.setBlock(block);
437+
SensingBlocks::compileMouseY(&compiler);
438+
439+
compiler.end();
440+
441+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
442+
ASSERT_TRUE(compiler.constValues().empty());
443+
}
444+
445+
TEST_F(SensingBlocksTest, MouseYImpl)
446+
{
447+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
448+
static BlockFunc functions[] = { &SensingBlocks::mouseY };
449+
450+
VirtualMachine vm(nullptr, &m_engineMock, nullptr);
451+
vm.setFunctions(functions);
452+
453+
EXPECT_CALL(m_engineMock, mouseY()).WillOnce(Return(159.084));
454+
vm.setBytecode(bytecode);
455+
vm.run();
456+
457+
ASSERT_EQ(vm.registerCount(), 1);
458+
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), 159.084);
459+
460+
EXPECT_CALL(m_engineMock, mouseY()).WillOnce(Return(-95.564));
461+
vm.reset();
462+
vm.run();
463+
464+
ASSERT_EQ(vm.registerCount(), 1);
465+
ASSERT_EQ(vm.getInput(0, 1)->toDouble(), -95.564);
466+
}
467+
342468
TEST_F(SensingBlocksTest, Timer)
343469
{
344470
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)