Skip to content

Commit d6190ef

Browse files
committed
Reset bubble text and graphic effects when project is stopped
1 parent 3bfd0dd commit d6190ef

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/blocks/looksblocks.cpp

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

3+
#include <scratchcpp/iengine.h>
4+
#include <scratchcpp/target.h>
5+
#include <scratchcpp/textbubble.h>
36
#include "looksblocks.h"
47

58
using namespace libscratchcpp;
@@ -22,3 +25,15 @@ Rgb LooksBlocks::color() const
2225
void LooksBlocks::registerBlocks(IEngine *engine)
2326
{
2427
}
28+
29+
void LooksBlocks::onInit(IEngine *engine)
30+
{
31+
engine->stopped().connect([engine]() {
32+
const auto &targets = engine->targets();
33+
34+
for (auto target : targets) {
35+
target->bubble()->setText("");
36+
target->clearGraphicsEffects();
37+
}
38+
});
39+
}

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class LooksBlocks : public IExtension
1515
Rgb color() const override;
1616

1717
void registerBlocks(IEngine *engine) override;
18+
void onInit(IEngine *engine) override;
1819
};
1920

2021
} // namespace libscratchcpp

test/blocks/looks_blocks_test.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
1+
#include <scratchcpp/project.h>
2+
#include <scratchcpp/test/scriptbuilder.h>
3+
#include <scratchcpp/sprite.h>
4+
#include <scratchcpp/stage.h>
5+
#include <scratchcpp/textbubble.h>
16
#include <enginemock.h>
7+
#include <graphicseffectmock.h>
28

39
#include "../common.h"
410
#include "blocks/looksblocks.h"
511

612
using namespace libscratchcpp;
713

14+
using ::testing::Return;
15+
816
class LooksBlocksTest : public testing::Test
917
{
1018
public:
11-
void SetUp() override { m_extension = std::make_unique<LooksBlocks>(); }
19+
void SetUp() override
20+
{
21+
m_extension = std::make_unique<LooksBlocks>();
22+
m_engine = m_project.engine().get();
23+
m_extension->registerBlocks(m_engine);
24+
m_extension->onInit(m_engine);
25+
}
1226

1327
std::unique_ptr<IExtension> m_extension;
28+
Project m_project;
29+
IEngine *m_engine = nullptr;
1430
EngineMock m_engineMock;
1531
};
32+
33+
TEST_F(LooksBlocksTest, StopProject)
34+
{
35+
auto stage = std::make_shared<Stage>();
36+
auto sprite = std::make_shared<Sprite>();
37+
GraphicsEffectMock effect;
38+
m_engine->setTargets({ stage, sprite });
39+
40+
stage->bubble()->setText("abc");
41+
sprite->bubble()->setText("def");
42+
EXPECT_CALL(effect, clamp(10)).WillOnce(Return(10));
43+
sprite->setGraphicsEffectValue(&effect, 10);
44+
EXPECT_CALL(effect, clamp(2.5)).WillOnce(Return(2.5));
45+
stage->setGraphicsEffectValue(&effect, 2.5);
46+
47+
m_engine->stop();
48+
ASSERT_TRUE(stage->bubble()->text().empty());
49+
ASSERT_TRUE(sprite->bubble()->text().empty());
50+
ASSERT_EQ(stage->graphicsEffectValue(&effect), 0);
51+
ASSERT_EQ(sprite->graphicsEffectValue(&effect), 0);
52+
}

0 commit comments

Comments
 (0)