Skip to content

Commit 7f4bd5b

Browse files
authored
Merge pull request #243 from scratchcpp/looks_blocks1
Implement looks blocks (part 1)
2 parents 0ee4905 + 49071f7 commit 7f4bd5b

File tree

4 files changed

+433
-0
lines changed

4 files changed

+433
-0
lines changed

src/blocks/looksblocks.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
#include <scratchcpp/iengine.h>
4+
#include <scratchcpp/compiler.h>
5+
#include <scratchcpp/sprite.h>
6+
37
#include "looksblocks.h"
48

59
using namespace libscratchcpp;
@@ -11,4 +15,93 @@ std::string LooksBlocks::name() const
1115

1216
void LooksBlocks::registerBlocks(IEngine *engine)
1317
{
18+
// Blocks
19+
engine->addCompileFunction(this, "looks_show", &compileShow);
20+
engine->addCompileFunction(this, "looks_hide", &compileHide);
21+
engine->addCompileFunction(this, "looks_changesizeby", &compileChangeSizeBy);
22+
engine->addCompileFunction(this, "looks_setsizeto", &compileSetSizeTo);
23+
engine->addCompileFunction(this, "looks_size", &compileSize);
24+
25+
// Inputs
26+
engine->addInput(this, "CHANGE", CHANGE);
27+
engine->addInput(this, "SIZE", SIZE);
28+
}
29+
30+
void LooksBlocks::compileShow(Compiler *compiler)
31+
{
32+
compiler->addFunctionCall(&show);
33+
}
34+
35+
void LooksBlocks::compileHide(Compiler *compiler)
36+
{
37+
compiler->addFunctionCall(&hide);
38+
}
39+
40+
void LooksBlocks::compileChangeSizeBy(Compiler *compiler)
41+
{
42+
compiler->addInput(CHANGE);
43+
compiler->addFunctionCall(&changeSizeBy);
44+
}
45+
46+
void LooksBlocks::compileSetSizeTo(Compiler *compiler)
47+
{
48+
compiler->addInput(SIZE);
49+
compiler->addFunctionCall(&setSizeTo);
50+
}
51+
52+
void LooksBlocks::compileSize(Compiler *compiler)
53+
{
54+
compiler->addFunctionCall(&size);
55+
}
56+
57+
unsigned int LooksBlocks::show(VirtualMachine *vm)
58+
{
59+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
60+
61+
if (sprite)
62+
sprite->setVisible(true);
63+
64+
return 0;
65+
}
66+
67+
unsigned int LooksBlocks::hide(VirtualMachine *vm)
68+
{
69+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
70+
71+
if (sprite)
72+
sprite->setVisible(false);
73+
74+
return 0;
75+
}
76+
77+
unsigned int LooksBlocks::changeSizeBy(VirtualMachine *vm)
78+
{
79+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
80+
81+
if (sprite)
82+
sprite->setSize(sprite->size() + vm->getInput(0, 1)->toDouble());
83+
84+
return 1;
85+
}
86+
87+
unsigned int LooksBlocks::setSizeTo(VirtualMachine *vm)
88+
{
89+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
90+
91+
if (sprite)
92+
sprite->setSize(vm->getInput(0, 1)->toDouble());
93+
94+
return 1;
95+
}
96+
97+
unsigned int LooksBlocks::size(VirtualMachine *vm)
98+
{
99+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
100+
101+
if (sprite)
102+
vm->addReturnValue(sprite->size());
103+
else
104+
vm->addReturnValue(0);
105+
106+
return 0;
14107
}

src/blocks/looksblocks.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,35 @@ namespace libscratchcpp
1111
class LooksBlocks : public IBlockSection
1212
{
1313
public:
14+
enum Inputs
15+
{
16+
CHANGE,
17+
SIZE
18+
};
19+
20+
enum Fields
21+
{
22+
};
23+
24+
enum FieldValues
25+
{
26+
};
27+
1428
std::string name() const override;
1529

1630
void registerBlocks(IEngine *engine) override;
31+
32+
static void compileShow(Compiler *compiler);
33+
static void compileHide(Compiler *compiler);
34+
static void compileChangeSizeBy(Compiler *compiler);
35+
static void compileSetSizeTo(Compiler *compiler);
36+
static void compileSize(Compiler *compiler);
37+
38+
static unsigned int show(VirtualMachine *vm);
39+
static unsigned int hide(VirtualMachine *vm);
40+
static unsigned int changeSizeBy(VirtualMachine *vm);
41+
static unsigned int setSizeTo(VirtualMachine *vm);
42+
static unsigned int size(VirtualMachine *vm);
1743
};
1844

1945
} // namespace libscratchcpp

test/blocks/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,19 @@ target_link_libraries(
125125
)
126126

127127
gtest_discover_tests(motion_blocks_test)
128+
129+
# looks_blocks_test
130+
add_executable(
131+
looks_blocks_test
132+
looks_blocks_test.cpp
133+
)
134+
135+
target_link_libraries(
136+
looks_blocks_test
137+
GTest::gtest_main
138+
GTest::gmock_main
139+
scratchcpp
140+
scratchcpp_mocks
141+
)
142+
143+
gtest_discover_tests(looks_blocks_test)

0 commit comments

Comments
 (0)