Skip to content

Commit 9bc869e

Browse files
committed
Implement sensing_touchingcolor block
1 parent 0353b33 commit 9bc869e

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
3131
{
3232
// Blocks
3333
engine->addCompileFunction(this, "sensing_touchingobject", &compileTouchingObject);
34+
engine->addCompileFunction(this, "sensing_touchingcolor", &compileTouchingColor);
3435
engine->addCompileFunction(this, "sensing_distanceto", &compileDistanceTo);
3536
engine->addCompileFunction(this, "sensing_askandwait", &compileAskAndWait);
3637
engine->addCompileFunction(this, "sensing_answer", &compileAnswer);
@@ -58,6 +59,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
5859

5960
// Inputs
6061
engine->addInput(this, "TOUCHINGOBJECTMENU", TOUCHINGOBJECTMENU);
62+
engine->addInput(this, "COLOR", COLOR);
6163
engine->addInput(this, "DISTANCETOMENU", DISTANCETOMENU);
6264
engine->addInput(this, "QUESTION", QUESTION);
6365
engine->addInput(this, "KEY_OPTION", KEY_OPTION);
@@ -116,6 +118,12 @@ void SensingBlocks::compileTouchingObject(Compiler *compiler)
116118
}
117119
}
118120

121+
void SensingBlocks::compileTouchingColor(Compiler *compiler)
122+
{
123+
compiler->addInput(COLOR);
124+
compiler->addFunctionCall(&touchingColor);
125+
}
126+
119127
void SensingBlocks::compileDistanceTo(Compiler *compiler)
120128
{
121129
Input *input = compiler->input(DISTANCETOMENU);
@@ -495,6 +503,12 @@ unsigned int SensingBlocks::touchingEdge(VirtualMachine *vm)
495503
return 0;
496504
}
497505

506+
unsigned int SensingBlocks::touchingColor(VirtualMachine *vm)
507+
{
508+
vm->replaceReturnValue(vm->target()->touchingColor(*vm->getInput(0, 1)), 1);
509+
return 0;
510+
}
511+
498512
unsigned int SensingBlocks::keyPressed(VirtualMachine *vm)
499513
{
500514
vm->replaceReturnValue(vm->engine()->keyPressed(vm->getInput(0, 1)->toString()), 1);

src/blocks/sensingblocks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SensingBlocks : public IBlockSection
2121
enum Inputs
2222
{
2323
TOUCHINGOBJECTMENU,
24+
COLOR,
2425
DISTANCETOMENU,
2526
QUESTION,
2627
KEY_OPTION,
@@ -61,6 +62,7 @@ class SensingBlocks : public IBlockSection
6162
void registerBlocks(IEngine *engine) override;
6263

6364
static void compileTouchingObject(Compiler *compiler);
65+
static void compileTouchingColor(Compiler *compiler);
6466
static void compileDistanceTo(Compiler *compiler);
6567
static void compileAskAndWait(Compiler *compiler);
6668
static void compileAnswer(Compiler *compiler);
@@ -90,6 +92,8 @@ class SensingBlocks : public IBlockSection
9092
static unsigned int touchingMousePointer(VirtualMachine *vm);
9193
static unsigned int touchingEdge(VirtualMachine *vm);
9294

95+
static unsigned int touchingColor(VirtualMachine *vm);
96+
9397
static unsigned int keyPressed(VirtualMachine *vm);
9498
static unsigned int mouseDown(VirtualMachine *vm);
9599
static unsigned int mouseX(VirtualMachine *vm);

test/blocks/sensing_blocks_test.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ class SensingBlocksTest : public testing::Test
5454
return block;
5555
}
5656

57+
void addValueInput(std::shared_ptr<Block> block, const std::string &name, SensingBlocks::Inputs id, const Value &value) const
58+
{
59+
auto input = std::make_shared<Input>(name, Input::Type::Shadow);
60+
input->setPrimaryValue(value);
61+
input->setInputId(id);
62+
block->addInput(input);
63+
}
64+
5765
void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, SensingBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
5866
{
5967
auto input = std::make_shared<Input>(name, Input::Type::ObscuredShadow);
@@ -117,6 +125,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
117125
{
118126
// Blocks
119127
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_touchingobject", &SensingBlocks::compileTouchingObject));
128+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_touchingcolor", &SensingBlocks::compileTouchingColor));
120129
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_distanceto", &SensingBlocks::compileDistanceTo));
121130
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_askandwait", &SensingBlocks::compileAskAndWait));
122131
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "sensing_answer", &SensingBlocks::compileAnswer));
@@ -144,6 +153,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
144153

145154
// Inputs
146155
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "TOUCHINGOBJECTMENU", SensingBlocks::TOUCHINGOBJECTMENU));
156+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "COLOR", SensingBlocks::COLOR));
147157
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "DISTANCETOMENU", SensingBlocks::DISTANCETOMENU));
148158
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "QUESTION", SensingBlocks::QUESTION));
149159
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "KEY_OPTION", SensingBlocks::KEY_OPTION));
@@ -371,6 +381,78 @@ TEST_F(SensingBlocksTest, TouchingObjectImpl)
371381
ASSERT_FALSE(vm.getInput(0, 1)->toBool());
372382
}
373383

384+
TEST_F(SensingBlocksTest, TouchingColor)
385+
{
386+
Compiler compiler(&m_engineMock);
387+
388+
// touching color (#FFFF00)
389+
auto block1 = std::make_shared<Block>("a", "sensing_touchingcolor");
390+
addValueInput(block1, "COLOR", SensingBlocks::COLOR, "#FFFF00");
391+
392+
// touching color (null block)
393+
auto block2 = std::make_shared<Block>("b", "sensing_touchingcolor");
394+
addDropdownInput(block2, "COLOR", SensingBlocks::COLOR, "", createNullBlock("c"));
395+
396+
compiler.init();
397+
398+
EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::touchingColor)).WillOnce(Return(1));
399+
compiler.setBlock(block1);
400+
SensingBlocks::compileTouchingColor(&compiler);
401+
402+
EXPECT_CALL(m_engineMock, functionIndex(&SensingBlocks::touchingColor)).WillOnce(Return(1));
403+
compiler.setBlock(block2);
404+
SensingBlocks::compileTouchingColor(&compiler);
405+
406+
compiler.end();
407+
408+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 1, vm::OP_NULL, vm::OP_EXEC, 1, vm::OP_HALT }));
409+
ASSERT_EQ(compiler.constValues().size(), 1);
410+
ASSERT_EQ(compiler.constValues()[0].toString(), "#FFFF00");
411+
}
412+
413+
TEST_F(SensingBlocksTest, TouchingColorImpl)
414+
{
415+
static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
416+
static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT };
417+
static BlockFunc functions[] = { &SensingBlocks::touchingColor };
418+
static Value constValues[] = { "#FFFF00", 1946195606 };
419+
420+
TargetMock target;
421+
Sprite sprite;
422+
VirtualMachine vm(&target, nullptr, nullptr);
423+
vm.setFunctions(functions);
424+
vm.setConstValues(constValues);
425+
426+
EXPECT_CALL(target, touchingColor(constValues[0])).WillOnce(Return(false));
427+
vm.setBytecode(bytecode1);
428+
vm.run();
429+
430+
ASSERT_EQ(vm.registerCount(), 1);
431+
ASSERT_FALSE(vm.getInput(0, 1)->toBool());
432+
433+
EXPECT_CALL(target, touchingColor(constValues[0])).WillOnce(Return(true));
434+
vm.reset();
435+
vm.run();
436+
437+
ASSERT_EQ(vm.registerCount(), 1);
438+
ASSERT_TRUE(vm.getInput(0, 1)->toBool());
439+
440+
EXPECT_CALL(target, touchingColor(constValues[1])).WillOnce(Return(false));
441+
vm.reset();
442+
vm.setBytecode(bytecode2);
443+
vm.run();
444+
445+
ASSERT_EQ(vm.registerCount(), 1);
446+
ASSERT_FALSE(vm.getInput(0, 1)->toBool());
447+
448+
EXPECT_CALL(target, touchingColor(constValues[1])).WillOnce(Return(true));
449+
vm.reset();
450+
vm.run();
451+
452+
ASSERT_EQ(vm.registerCount(), 1);
453+
ASSERT_TRUE(vm.getInput(0, 1)->toBool());
454+
}
455+
374456
TEST_F(SensingBlocksTest, DistanceTo)
375457
{
376458
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)