Skip to content

Commit 27a732f

Browse files
committed
Implement looks_say block
1 parent 1cb6a63 commit 27a732f

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ std::string LooksBlocks::name() const
2525
void LooksBlocks::registerBlocks(IEngine *engine)
2626
{
2727
// Blocks
28+
engine->addCompileFunction(this, "looks_say", &compileSay);
2829
engine->addCompileFunction(this, "looks_show", &compileShow);
2930
engine->addCompileFunction(this, "looks_hide", &compileHide);
3031
engine->addCompileFunction(this, "looks_changeeffectby", &compileChangeEffectBy);
@@ -49,6 +50,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
4950
engine->addMonitorNameFunction(this, "looks_size", &sizeMonitorName);
5051

5152
// Inputs
53+
engine->addInput(this, "MESSAGE", MESSAGE);
5254
engine->addInput(this, "CHANGE", CHANGE);
5355
engine->addInput(this, "SIZE", SIZE);
5456
engine->addInput(this, "COSTUME", COSTUME);
@@ -77,6 +79,12 @@ void LooksBlocks::registerBlocks(IEngine *engine)
7779
engine->addFieldValue(this, "backward", Backward);
7880
}
7981

82+
void LooksBlocks::compileSay(Compiler *compiler)
83+
{
84+
compiler->addInput(MESSAGE);
85+
compiler->addFunctionCall(&say);
86+
}
87+
8088
void LooksBlocks::compileShow(Compiler *compiler)
8189
{
8290
compiler->addFunctionCall(&show);
@@ -515,6 +523,18 @@ const std::string &LooksBlocks::sizeMonitorName(Block *block)
515523
return name;
516524
}
517525

526+
unsigned int LooksBlocks::say(VirtualMachine *vm)
527+
{
528+
Target *target = vm->target();
529+
530+
if (target) {
531+
target->setBubbleType(Target::BubbleType::Say);
532+
target->setBubbleText(vm->getInput(0, 1)->toString());
533+
}
534+
535+
return 1;
536+
}
537+
518538
unsigned int LooksBlocks::show(VirtualMachine *vm)
519539
{
520540
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());

src/blocks/looksblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class LooksBlocks : public IBlockSection
2020
public:
2121
enum Inputs
2222
{
23+
MESSAGE,
2324
CHANGE,
2425
SIZE,
2526
COSTUME,
@@ -57,6 +58,7 @@ class LooksBlocks : public IBlockSection
5758

5859
void registerBlocks(IEngine *engine) override;
5960

61+
static void compileSay(Compiler *compiler);
6062
static void compileShow(Compiler *compiler);
6163
static void compileHide(Compiler *compiler);
6264
static void compileChangeEffectBy(Compiler *compiler);
@@ -79,6 +81,7 @@ class LooksBlocks : public IBlockSection
7981
static const std::string &backdropNumberNameMonitorName(Block *block);
8082
static const std::string &sizeMonitorName(Block *block);
8183

84+
static unsigned int say(VirtualMachine *vm);
8285
static unsigned int show(VirtualMachine *vm);
8386
static unsigned int hide(VirtualMachine *vm);
8487

test/blocks/looks_blocks_test.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ TEST_F(LooksBlocksTest, CategoryVisible)
104104
TEST_F(LooksBlocksTest, RegisterBlocks)
105105
{
106106
// Blocks
107+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_say", &LooksBlocks::compileSay));
107108
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_show", &LooksBlocks::compileShow));
108109
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_hide", &LooksBlocks::compileHide));
109110
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "looks_changeeffectby", &LooksBlocks::compileChangeEffectBy));
@@ -128,6 +129,7 @@ TEST_F(LooksBlocksTest, RegisterBlocks)
128129
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "looks_size", &LooksBlocks::sizeMonitorName));
129130

130131
// Inputs
132+
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "MESSAGE", LooksBlocks::MESSAGE));
131133
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "CHANGE", LooksBlocks::CHANGE));
132134
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "SIZE", LooksBlocks::SIZE));
133135
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "COSTUME", LooksBlocks::COSTUME));
@@ -158,6 +160,52 @@ TEST_F(LooksBlocksTest, RegisterBlocks)
158160
m_section->registerBlocks(&m_engineMock);
159161
}
160162

163+
TEST_F(LooksBlocksTest, Say)
164+
{
165+
Compiler compiler(&m_engineMock);
166+
167+
// say "Hello!"
168+
auto block = std::make_shared<Block>("a", "looks_say");
169+
addValueInput(block, "MESSAGE", LooksBlocks::MESSAGE, "Hello!");
170+
171+
EXPECT_CALL(m_engineMock, functionIndex(&LooksBlocks::say)).WillOnce(Return(0));
172+
173+
compiler.init();
174+
compiler.setBlock(block);
175+
LooksBlocks::compileSay(&compiler);
176+
compiler.end();
177+
178+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }));
179+
ASSERT_EQ(compiler.constValues().size(), 1);
180+
ASSERT_EQ(compiler.constValues()[0].toString(), "Hello!");
181+
}
182+
183+
TEST_F(LooksBlocksTest, SayImpl)
184+
{
185+
static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT };
186+
static BlockFunc functions[] = { &LooksBlocks::say };
187+
static Value constValues[] = { "test" };
188+
189+
Target target;
190+
VirtualMachine vm(&target, nullptr, nullptr);
191+
vm.setBytecode(bytecode);
192+
vm.setFunctions(functions);
193+
vm.setConstValues(constValues);
194+
vm.run();
195+
196+
ASSERT_EQ(vm.registerCount(), 0);
197+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Say);
198+
ASSERT_EQ(target.bubbleText(), "test");
199+
200+
target.setBubbleType(Target::BubbleType::Think);
201+
vm.reset();
202+
vm.run();
203+
204+
ASSERT_EQ(vm.registerCount(), 0);
205+
ASSERT_EQ(target.bubbleType(), Target::BubbleType::Say);
206+
ASSERT_EQ(target.bubbleText(), "test");
207+
}
208+
161209
TEST_F(LooksBlocksTest, Show)
162210
{
163211
Compiler compiler(&m_engineMock);

0 commit comments

Comments
 (0)