Skip to content

Commit b7253e1

Browse files
committed
Add a way to register graphics effects
1 parent 7e17240 commit b7253e1

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

include/scratchcpp/scratchconfiguration.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace libscratchcpp
1313
class IExtension;
1414
class IImageFormat;
1515
class IImageFormatFactory;
16+
class IGraphicsEffect;
1617
class ScratchConfigurationPrivate;
1718

1819
/*! \brief The ScratchConfiguration class provides methods for adding custom extensions. */
@@ -41,6 +42,10 @@ class LIBSCRATCHCPP_EXPORT ScratchConfiguration
4142
static void removeImageFormat(const std::string &name);
4243
static std::shared_ptr<IImageFormat> createImageFormat(const std::string &name);
4344

45+
static void registerGraphicsEffect(std::shared_ptr<IGraphicsEffect> effect);
46+
static void removeGraphicsEffect(const std::string &name);
47+
static IGraphicsEffect *getGraphicsEffect(const std::string &name);
48+
4449
private:
4550
static const std::vector<std::shared_ptr<IExtension>> getExtensions();
4651

src/scratchconfiguration.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <scratchcpp/scratchconfiguration.h>
44
#include <scratchcpp/iimageformatfactory.h>
5+
#include <scratchcpp/igraphicseffect.h>
56
#include <iostream>
67

78
#include "scratchconfiguration_p.h"
@@ -50,6 +51,32 @@ std::shared_ptr<IImageFormat> ScratchConfiguration::createImageFormat(const std:
5051
return it->second->createInstance();
5152
}
5253

54+
/*! Registers the given graphics effect. */
55+
void ScratchConfiguration::registerGraphicsEffect(std::shared_ptr<IGraphicsEffect> effect)
56+
{
57+
if (!effect)
58+
return;
59+
60+
impl->graphicsEffects[effect->name()] = effect;
61+
}
62+
63+
/*! Removes the given graphics effect. */
64+
void ScratchConfiguration::removeGraphicsEffect(const std::string &name)
65+
{
66+
impl->graphicsEffects.erase(name);
67+
}
68+
69+
/*! Returns the graphics effect with the given name, or nullptr if it isn't registered. */
70+
IGraphicsEffect *ScratchConfiguration::getGraphicsEffect(const std::string &name)
71+
{
72+
auto it = impl->graphicsEffects.find(name);
73+
74+
if (it == impl->graphicsEffects.cend())
75+
return nullptr;
76+
else
77+
return it->second.get();
78+
}
79+
5380
const std::vector<std::shared_ptr<IExtension>> ScratchConfiguration::getExtensions()
5481
{
5582
return impl->extensions;

src/scratchconfiguration_p.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace libscratchcpp
1414

1515
class IExtension;
1616
class IImageFormatFactory;
17+
class IGraphicsEffect;
1718

1819
struct ScratchConfigurationPrivate
1920
{
@@ -22,6 +23,7 @@ struct ScratchConfigurationPrivate
2223

2324
std::vector<std::shared_ptr<IExtension>> extensions = { std::make_shared<StandardBlocks>() };
2425
std::unordered_map<std::string, std::shared_ptr<IImageFormatFactory>> imageFormats;
26+
std::unordered_map<std::string, std::shared_ptr<IGraphicsEffect>> graphicsEffects;
2527
};
2628

2729
} // namespace libscratchcpp

test/scratchconfiguration/scratchconfiguration_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <scratchcpp/scratchconfiguration.h>
22
#include <imageformatfactorymock.h>
3+
#include <graphicseffectmock.h>
34

45
#include "../common.h"
56
#include "imageformats/stub/imageformatstub.h"
@@ -80,3 +81,27 @@ TEST_F(ScratchConfigurationTest, ImageFormats)
8081
testing::Mock::AllowLeak(m_imageFormatMock1.get());
8182
testing::Mock::AllowLeak(m_imageFormatMock2.get());
8283
}
84+
85+
TEST_F(ScratchConfigurationTest, GraphicsEffects)
86+
{
87+
auto effect1 = std::make_shared<GraphicsEffectMock>();
88+
auto effect2 = std::make_shared<GraphicsEffectMock>();
89+
90+
EXPECT_CALL(*effect1, name()).WillOnce(Return("effect1"));
91+
EXPECT_CALL(*effect2, name()).WillOnce(Return("effect2"));
92+
ScratchConfiguration::registerGraphicsEffect(effect1);
93+
ScratchConfiguration::registerGraphicsEffect(effect2);
94+
auto format1 = std::make_shared<ImageFormatStub>();
95+
auto format2 = std::make_shared<ImageFormatStub>();
96+
97+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect1"), effect1.get());
98+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect2"), effect2.get());
99+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect3"), nullptr);
100+
101+
ScratchConfiguration::removeGraphicsEffect("effect2");
102+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect1"), effect1.get());
103+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect2"), nullptr);
104+
105+
ScratchConfiguration::removeGraphicsEffect("effect1");
106+
ASSERT_EQ(ScratchConfiguration::getGraphicsEffect("effect1"), nullptr);
107+
}

0 commit comments

Comments
 (0)