Skip to content

Commit 7dfca76

Browse files
committed
Implement looks_size block
1 parent a1fb11d commit 7dfca76

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
5252
engine->addCompileFunction(this, "looks_cleargraphiceffects", &compileClearGraphicEffects);
5353
engine->addCompileFunction(this, "looks_changesizeby", &compileChangeSizeBy);
5454
engine->addCompileFunction(this, "looks_setsizeto", &compileSetSizeTo);
55+
engine->addCompileFunction(this, "looks_size", &compileSize);
5556
}
5657

5758
void LooksBlocks::onInit(IEngine *engine)
@@ -227,6 +228,14 @@ CompilerValue *LooksBlocks::compileSetSizeTo(Compiler *compiler)
227228
return nullptr;
228229
}
229230

231+
CompilerValue *LooksBlocks::compileSize(Compiler *compiler)
232+
{
233+
if (compiler->target()->isStage())
234+
return compiler->addConstValue(100);
235+
else
236+
return compiler->addTargetFunctionCall("looks_size", Compiler::StaticType::Number);
237+
}
238+
230239
extern "C" void looks_start_stack_timer(ExecutionContext *ctx, double duration)
231240
{
232241
ctx->stackTimer()->start(duration);
@@ -306,3 +315,8 @@ extern "C" void looks_setsizeto(Sprite *sprite, double size)
306315
{
307316
sprite->setSize(size);
308317
}
318+
319+
extern "C" double looks_size(Sprite *sprite)
320+
{
321+
return sprite->size();
322+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class LooksBlocks : public IExtension
4242
static CompilerValue *compileClearGraphicEffects(Compiler *compiler);
4343
static CompilerValue *compileChangeSizeBy(Compiler *compiler);
4444
static CompilerValue *compileSetSizeTo(Compiler *compiler);
45+
static CompilerValue *compileSize(Compiler *compiler);
4546

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

test/blocks/looks_blocks_test.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/thread.h>
88
#include <scratchcpp/executablecode.h>
99
#include <scratchcpp/executioncontext.h>
10+
#include <scratchcpp/list.h>
1011
#include <scratchcpp/scratchconfiguration.h>
1112
#include <enginemock.h>
1213
#include <graphicseffectmock.h>
@@ -582,3 +583,38 @@ TEST_F(LooksBlocksTest, SetSizeTo)
582583
builder.run();
583584
}
584585
}
586+
587+
TEST_F(LooksBlocksTest, Size)
588+
{
589+
{
590+
auto sprite = std::make_shared<Sprite>();
591+
ScriptBuilder builder(m_extension.get(), m_engine, sprite);
592+
builder.addBlock("looks_size");
593+
builder.captureBlockReturnValue();
594+
builder.build();
595+
596+
sprite->setEngine(nullptr);
597+
sprite->setSize(45.62);
598+
builder.run();
599+
600+
List *list = builder.capturedValues();
601+
ASSERT_EQ(list->size(), 1);
602+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 45.62);
603+
}
604+
605+
m_engine->clear();
606+
607+
{
608+
auto stage = std::make_shared<Stage>();
609+
ScriptBuilder builder(m_extension.get(), m_engine, stage);
610+
builder.addBlock("looks_size");
611+
builder.captureBlockReturnValue();
612+
613+
builder.build();
614+
builder.run();
615+
616+
List *list = builder.capturedValues();
617+
ASSERT_EQ(list->size(), 1);
618+
ASSERT_EQ(Value(list->data()[0]).toDouble(), 100);
619+
}
620+
}

0 commit comments

Comments
 (0)