Skip to content

Commit 3d2faa4

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

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-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: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,51 @@
1+
#include <scratchcpp/project.h>
2+
#include <scratchcpp/test/scriptbuilder.h>
3+
#include <scratchcpp/sprite.h>
4+
#include <scratchcpp/stage.h>
15
#include <enginemock.h>
6+
#include <graphicseffectmock.h>
27

38
#include "../common.h"
49
#include "blocks/looksblocks.h"
510

611
using namespace libscratchcpp;
712

13+
using ::testing::Return;
14+
815
class LooksBlocksTest : public testing::Test
916
{
1017
public:
11-
void SetUp() override { m_extension = std::make_unique<LooksBlocks>(); }
18+
void SetUp() override
19+
{
20+
m_extension = std::make_unique<LooksBlocks>();
21+
m_engine = m_project.engine().get();
22+
m_extension->registerBlocks(m_engine);
23+
m_extension->onInit(m_engine);
24+
}
1225

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

0 commit comments

Comments
 (0)