Skip to content

Commit f87c48b

Browse files
committed
Implement looks_setsizeto block
1 parent a8525ba commit f87c48b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
5151
engine->addCompileFunction(this, "looks_seteffectto", &compileSetEffectTo);
5252
engine->addCompileFunction(this, "looks_cleargraphiceffects", &compileClearGraphicEffects);
5353
engine->addCompileFunction(this, "looks_changesizeby", &compileChangeSizeBy);
54+
engine->addCompileFunction(this, "looks_setsizeto", &compileSetSizeTo);
5455
}
5556

5657
void LooksBlocks::onInit(IEngine *engine)
@@ -216,6 +217,16 @@ CompilerValue *LooksBlocks::compileChangeSizeBy(Compiler *compiler)
216217
return nullptr;
217218
}
218219

220+
CompilerValue *LooksBlocks::compileSetSizeTo(Compiler *compiler)
221+
{
222+
if (compiler->target()->isStage())
223+
return nullptr;
224+
225+
auto size = compiler->addInput("SIZE");
226+
compiler->addTargetFunctionCall("looks_setsizeto", Compiler::StaticType::Void, { Compiler::StaticType::Number }, { size });
227+
return nullptr;
228+
}
229+
219230
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
220231
{
221232
ctx->stackTimer()->start(duration);
@@ -290,3 +301,8 @@ extern "C" void looks_changesizeby(Sprite *sprite, double change)
290301
{
291302
sprite->setSize(sprite->size() + change);
292303
}
304+
305+
extern "C" void looks_setsizeto(Sprite *sprite, double size)
306+
{
307+
sprite->setSize(size);
308+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class LooksBlocks : public IExtension
4141
static CompilerValue *compileSetEffectTo(Compiler *compiler);
4242
static CompilerValue *compileClearGraphicEffects(Compiler *compiler);
4343
static CompilerValue *compileChangeSizeBy(Compiler *compiler);
44+
static CompilerValue *compileSetSizeTo(Compiler *compiler);
4445

4546
IEngine *m_engine = nullptr;
4647
std::unordered_map<std::string, long> m_effectMap;

test/blocks/looks_blocks_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,31 @@ TEST_F(LooksBlocksTest, ChangeSizeBy)
554554
builder.run();
555555
}
556556
}
557+
558+
TEST_F(LooksBlocksTest, SetSizeTo)
559+
{
560+
{
561+
auto sprite = std::make_shared<Sprite>();
562+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
563+
builder.addBlock("looks_setsizeto");
564+
builder.addValueInput("SIZE", 2.5);
565+
builder.build();
566+
567+
sprite->setEngine(nullptr);
568+
sprite->setSize(45.62);
569+
builder.run();
570+
ASSERT_EQ(sprite->size(), 2.5);
571+
}
572+
573+
m_engine->clear();
574+
575+
{
576+
auto stage = std::make_shared<Stage>();
577+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
578+
builder.addBlock("looks_setsizeto");
579+
builder.addValueInput("SIZE", 2.5);
580+
581+
builder.build();
582+
builder.run();
583+
}
584+
}

0 commit comments

Comments
 (0)