Skip to content

Commit a54ffa3

Browse files
committed
Update libscratchcpp to v0.11.0
1 parent 4610e76 commit a54ffa3

File tree

13 files changed

+140
-90
lines changed

13 files changed

+140
-90
lines changed

libscratchcpp

Submodule libscratchcpp updated 48 files

src/graphicseffect.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
using namespace scratchcpprender;
66

7+
static std::unordered_map<ShaderManager::Effect, std::pair<double, double>> EFFECT_RANGE = {
8+
{ ShaderManager::Effect::Ghost, { 0, 100 } },
9+
{ ShaderManager::Effect::Brightness, { -100, 100 } }
10+
};
11+
712
GraphicsEffect::GraphicsEffect(ShaderManager::Effect effect, const std::string &name) :
813
m_effect(effect),
914
m_name(name)
@@ -19,3 +24,13 @@ std::string GraphicsEffect::name() const
1924
{
2025
return m_name;
2126
}
27+
28+
29+
double GraphicsEffect::clamp(double value) const
30+
{
31+
// https://github.com/scratchfoundation/scratch-vm/blob/8dbcc1fc8f8d8c4f1e40629fe8a388149d6dfd1c/src/blocks/scratch3_looks.js#L523-L538
32+
if(m_effect == ShaderManager::Effect::Ghost || m_effect == ShaderManager::Effect::Brightness)
33+
return std::clamp(value, EFFECT_RANGE[m_effect].first, EFFECT_RANGE[m_effect].second);
34+
35+
return value;
36+
}

src/graphicseffect.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class GraphicsEffect : public libscratchcpp::IGraphicsEffect
1717
ShaderManager::Effect effect() const;
1818
std::string name() const override;
1919

20+
double clamp(double value) const override;
21+
2022
private:
2123
ShaderManager::Effect m_effect = static_cast<ShaderManager::Effect>(0);
2224
std::string m_name;

src/renderedtarget.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,6 @@ void RenderedTarget::mouseMoveEvent(QMouseEvent *event)
503503
m_dragDeltaX = m_engine->mouseX() - sprite->x();
504504
m_dragDeltaY = m_engine->mouseY() - sprite->y();
505505
m_mouseArea->setDraggedSprite(this);
506-
507-
// Move the sprite to the front layer
508-
m_engine->moveSpriteToFront(sprite);
509506
}
510507
}
511508

src/spritemodel.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/costume.h>
44
#include <scratchcpp/rect.h>
55
#include <scratchcpp/iengine.h>
6+
#include <scratchcpp/textbubble.h>
67

78
#include "spritemodel.h"
89
#include "renderedtarget.h"
@@ -19,7 +20,35 @@ SpriteModel::SpriteModel(QObject *parent) :
1920

2021
void SpriteModel::init(libscratchcpp::Sprite *sprite)
2122
{
23+
if (!sprite)
24+
return;
25+
2226
m_sprite = sprite;
27+
28+
m_sprite->bubble()->typeChanged().connect([this](libscratchcpp::TextBubble::Type type) {
29+
if (type == libscratchcpp::TextBubble::Type::Say) {
30+
if (m_bubbleType == TextBubbleShape::Type::Say)
31+
return;
32+
33+
m_bubbleType = TextBubbleShape::Type::Say;
34+
} else {
35+
if (m_bubbleType == TextBubbleShape::Type::Think)
36+
return;
37+
38+
m_bubbleType = TextBubbleShape::Type::Think;
39+
}
40+
41+
emit bubbleTypeChanged();
42+
});
43+
44+
m_sprite->bubble()->textChanged().connect([this](const std::string &text) {
45+
QString newText = QString::fromStdString(text);
46+
47+
if (m_bubbleText != newText) {
48+
m_bubbleText = newText;
49+
emit bubbleTextChanged();
50+
}
51+
});
2352
}
2453

2554
void SpriteModel::deinitClone()
@@ -113,33 +142,6 @@ void SpriteModel::onGraphicsEffectsCleared()
113142
m_renderedTarget->clearGraphicEffects();
114143
}
115144

116-
void SpriteModel::onBubbleTypeChanged(libscratchcpp::Target::BubbleType type)
117-
{
118-
if (type == libscratchcpp::Target::BubbleType::Say) {
119-
if (m_bubbleType == TextBubbleShape::Type::Say)
120-
return;
121-
122-
m_bubbleType = TextBubbleShape::Type::Say;
123-
} else {
124-
if (m_bubbleType == TextBubbleShape::Type::Think)
125-
return;
126-
127-
m_bubbleType = TextBubbleShape::Type::Think;
128-
}
129-
130-
emit bubbleTypeChanged();
131-
}
132-
133-
void SpriteModel::onBubbleTextChanged(const std::string &text)
134-
{
135-
QString newText = QString::fromStdString(text);
136-
137-
if (m_bubbleText != newText) {
138-
m_bubbleText = newText;
139-
emit bubbleTextChanged();
140-
}
141-
}
142-
143145
int SpriteModel::costumeWidth() const
144146
{
145147
return m_renderedTarget->costumeWidth();

src/spritemodel.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ class SpriteModel
5151
void onGraphicsEffectChanged(libscratchcpp::IGraphicsEffect *effect, double value) override;
5252
void onGraphicsEffectsCleared() override;
5353

54-
void onBubbleTypeChanged(libscratchcpp::Target::BubbleType type) override;
55-
void onBubbleTextChanged(const std::string &text) override;
56-
5754
int costumeWidth() const override;
5855
int costumeHeight() const override;
5956

src/stagemodel.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: LGPL-3.0-or-later
22

33
#include <scratchcpp/costume.h>
4+
#include <scratchcpp/textbubble.h>
45

56
#include "stagemodel.h"
67
#include "renderedtarget.h"
@@ -15,7 +16,35 @@ StageModel::StageModel(QObject *parent) :
1516

1617
void StageModel::init(libscratchcpp::Stage *stage)
1718
{
19+
if (!stage)
20+
return;
21+
1822
m_stage = stage;
23+
24+
m_stage->bubble()->typeChanged().connect([this](libscratchcpp::TextBubble::Type type) {
25+
if (type == libscratchcpp::TextBubble::Type::Say) {
26+
if (m_bubbleType == TextBubbleShape::Type::Say)
27+
return;
28+
29+
m_bubbleType = TextBubbleShape::Type::Say;
30+
} else {
31+
if (m_bubbleType == TextBubbleShape::Type::Think)
32+
return;
33+
34+
m_bubbleType = TextBubbleShape::Type::Think;
35+
}
36+
37+
emit bubbleTypeChanged();
38+
});
39+
40+
m_stage->bubble()->textChanged().connect([this](const std::string &text) {
41+
QString newText = QString::fromStdString(text);
42+
43+
if (m_bubbleText != newText) {
44+
m_bubbleText = newText;
45+
emit bubbleTextChanged();
46+
}
47+
});
1948
}
2049

2150
void StageModel::onCostumeChanged(libscratchcpp::Costume *costume)
@@ -50,33 +79,6 @@ void StageModel::onGraphicsEffectsCleared()
5079
m_renderedTarget->clearGraphicEffects();
5180
}
5281

53-
void StageModel::onBubbleTypeChanged(libscratchcpp::Target::BubbleType type)
54-
{
55-
if (type == libscratchcpp::Target::BubbleType::Say) {
56-
if (m_bubbleType == TextBubbleShape::Type::Say)
57-
return;
58-
59-
m_bubbleType = TextBubbleShape::Type::Say;
60-
} else {
61-
if (m_bubbleType == TextBubbleShape::Type::Think)
62-
return;
63-
64-
m_bubbleType = TextBubbleShape::Type::Think;
65-
}
66-
67-
emit bubbleTypeChanged();
68-
}
69-
70-
void StageModel::onBubbleTextChanged(const std::string &text)
71-
{
72-
QString newText = QString::fromStdString(text);
73-
74-
if (m_bubbleText != newText) {
75-
m_bubbleText = newText;
76-
emit bubbleTextChanged();
77-
}
78-
}
79-
8082
int StageModel::costumeWidth() const
8183
{
8284
return m_renderedTarget->costumeWidth();

src/stagemodel.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ class StageModel
3737
void onGraphicsEffectChanged(libscratchcpp::IGraphicsEffect *effect, double value) override;
3838
void onGraphicsEffectsCleared() override;
3939

40-
void onBubbleTypeChanged(libscratchcpp::Target::BubbleType type) override;
41-
void onBubbleTextChanged(const std::string &text) override;
42-
4340
int costumeWidth() const override;
4441
int costumeHeight() const override;
4542

test/graphicseffect/graphicseffect_test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,32 @@ TEST(GraphicsEffectTest, Constructor)
2727
ASSERT_EQ(iface->name(), "brightness");
2828
}
2929
}
30+
31+
TEST(GraphicsEffectTest, Clamp)
32+
{
33+
{
34+
GraphicsEffect effect(ShaderManager::Effect::Color, "color");
35+
ASSERT_EQ(effect.clamp(-500), -500);
36+
ASSERT_EQ(effect.clamp(0), 0);
37+
ASSERT_EQ(effect.clamp(500), 500);
38+
}
39+
40+
{
41+
GraphicsEffect effect(ShaderManager::Effect::Brightness, "brightness");
42+
ASSERT_EQ(effect.clamp(-125), -100);
43+
ASSERT_EQ(effect.clamp(-100), -100);
44+
ASSERT_EQ(effect.clamp(0), 0);
45+
ASSERT_EQ(effect.clamp(100), 100);
46+
ASSERT_EQ(effect.clamp(125), 100);
47+
}
48+
49+
{
50+
GraphicsEffect effect(ShaderManager::Effect::Ghost, "ghost");
51+
ASSERT_EQ(effect.clamp(-50), 0);
52+
ASSERT_EQ(effect.clamp(0), 0);
53+
ASSERT_EQ(effect.clamp(100), 100);
54+
ASSERT_EQ(effect.clamp(125), 100);
55+
}
56+
57+
// TODO: Test remaining effects
58+
}

test/mocks/enginemock.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ class EngineMock : public IEngine
106106

107107
MOCK_METHOD(void, addWhenTouchingObjectScript, (std::shared_ptr<Block>), (override));
108108
MOCK_METHOD(void, addGreenFlagScript, (std::shared_ptr<Block>), (override));
109-
MOCK_METHOD(void, addBroadcastScript, (std::shared_ptr<Block>, int, Broadcast *), (override));
110-
MOCK_METHOD(void, addBackdropChangeScript, (std::shared_ptr<Block>, int), (override));
109+
MOCK_METHOD(void, addBroadcastScript, (std::shared_ptr<Block>, Field *, Broadcast *), (override));
110+
MOCK_METHOD(void, addBackdropChangeScript, (std::shared_ptr<Block>, Field *), (override));
111111
MOCK_METHOD(void, addCloneInitScript, (std::shared_ptr<Block>), (override));
112-
MOCK_METHOD(void, addKeyPressScript, (std::shared_ptr<Block>, int), (override));
112+
MOCK_METHOD(void, addKeyPressScript, (std::shared_ptr<Block>, Field *), (override));
113113
MOCK_METHOD(void, addTargetClickScript, (std::shared_ptr<Block>), (override));
114114
MOCK_METHOD(void, addWhenGreaterThanScript, (std::shared_ptr<Block>), (override));
115115

@@ -119,11 +119,11 @@ class EngineMock : public IEngine
119119
MOCK_METHOD(void, getVisibleTargets, (std::vector<Target *> &), (const, override));
120120
MOCK_METHOD(int, findTarget, (const std::string &), (const, override));
121121

122-
MOCK_METHOD(void, moveSpriteToFront, (Sprite * sprite), (override));
123-
MOCK_METHOD(void, moveSpriteToBack, (Sprite * sprite), (override));
124-
MOCK_METHOD(void, moveSpriteForwardLayers, (Sprite * sprite, int layers), (override));
125-
MOCK_METHOD(void, moveSpriteBackwardLayers, (Sprite * sprite, int layers), (override));
126-
MOCK_METHOD(void, moveSpriteBehindOther, (Sprite * sprite, Sprite *other), (override));
122+
MOCK_METHOD(void, moveDrawableToFront, (Drawable *), (override));
123+
MOCK_METHOD(void, moveDrawableToBack, (Drawable *), (override));
124+
MOCK_METHOD(void, moveDrawableForwardLayers, (Drawable *, int), (override));
125+
MOCK_METHOD(void, moveDrawableBackwardLayers, (Drawable *, int), (override));
126+
MOCK_METHOD(void, moveDrawableBehindOther, (Drawable *, Drawable *), (override));
127127

128128
MOCK_METHOD(Stage *, stage, (), (const, override));
129129

0 commit comments

Comments
 (0)