Skip to content

Commit 3c62f57

Browse files
committed
Implement looks_hide block
1 parent ba776aa commit 3c62f57

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ std::string LooksBlocks::name() const
1616
void LooksBlocks::registerBlocks(IEngine *engine)
1717
{
1818
engine->addCompileFunction(this, "looks_show", &compileShow);
19+
engine->addCompileFunction(this, "looks_hide", &compileHide);
1920
}
2021

2122
void LooksBlocks::compileShow(Compiler *compiler)
2223
{
2324
compiler->addFunctionCall(&show);
2425
}
2526

27+
void LooksBlocks::compileHide(Compiler *compiler)
28+
{
29+
compiler->addFunctionCall(&hide);
30+
}
31+
2632
unsigned int LooksBlocks::show(VirtualMachine *vm)
2733
{
2834
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
@@ -32,3 +38,13 @@ unsigned int LooksBlocks::show(VirtualMachine *vm)
3238

3339
return 0;
3440
}
41+
42+
unsigned int LooksBlocks::hide(VirtualMachine *vm)
43+
{
44+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
45+
46+
if (sprite)
47+
sprite->setVisible(false);
48+
49+
return 0;
50+
}

src/blocks/looksblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ class LooksBlocks : public IBlockSection
2828
void registerBlocks(IEngine *engine) override;
2929

3030
static void compileShow(Compiler *compiler);
31+
static void compileHide(Compiler *compiler);
3132

3233
static unsigned int show(VirtualMachine *vm);
34+
static unsigned int hide(VirtualMachine *vm);
3335
};
3436

3537
} // namespace libscratchcpp

test/blocks/looks_blocks_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ TEST_F(LooksBlocksTest, RegisterBlocks)
9393
{
9494
// Blocks
9595
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_show", &LooksBlocks::compileShow));
96+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_hide", &LooksBlocks::compileHide));
9697

9798
m_section->registerBlocks(&m_engineMock);
9899
}
@@ -136,3 +137,43 @@ TEST_F(LooksBlocksTest, ShowImpl)
136137
ASSERT_EQ(vm.registerCount(), 0);
137138
ASSERT_TRUE(sprite.visible());
138139
}
140+
141+
TEST_F(LooksBlocksTest, Hide)
142+
{
143+
Compiler compiler(&m_engineMock);
144+
145+
auto block = std::make_shared<Block>("a", "looks_hide");
146+
147+
EXPECT_CALL(m_engineMock, functionIndex(&LooksBlocks::hide)).WillOnce(Return(0));
148+
149+
compiler.init();
150+
compiler.setBlock(block);
151+
LooksBlocks::compileHide(&compiler);
152+
compiler.end();
153+
154+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }));
155+
ASSERT_TRUE(compiler.constValues().empty());
156+
}
157+
158+
TEST_F(LooksBlocksTest, HideImpl)
159+
{
160+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
161+
static BlockFunc functions[] = { &LooksBlocks::hide };
162+
163+
Sprite sprite;
164+
sprite.setVisible(true);
165+
166+
VirtualMachine vm(&sprite, nullptr, nullptr);
167+
vm.setBytecode(bytecode);
168+
vm.setFunctions(functions);
169+
vm.run();
170+
171+
ASSERT_EQ(vm.registerCount(), 0);
172+
ASSERT_FALSE(sprite.visible());
173+
174+
vm.reset();
175+
vm.run();
176+
177+
ASSERT_EQ(vm.registerCount(), 0);
178+
ASSERT_FALSE(sprite.visible());
179+
}

0 commit comments

Comments
 (0)