Skip to content

Commit d32c78e

Browse files
committed
Use pointer type for graphic effects in looks blocks
1 parent 7dfca76 commit d32c78e

File tree

2 files changed

+15
-59
lines changed

2 files changed

+15
-59
lines changed

src/blocks/looksblocks.cpp

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818

1919
using namespace libscratchcpp;
2020

21-
LooksBlocks::~LooksBlocks()
22-
{
23-
if (m_engine)
24-
m_instances.erase(m_engine);
25-
}
26-
2721
std::string LooksBlocks::name() const
2822
{
2923
return "Looks";
@@ -78,9 +72,6 @@ void LooksBlocks::onInit(IEngine *engine)
7872
target->clearGraphicsEffects();
7973
}
8074
});
81-
82-
m_engine = engine;
83-
m_instances[engine] = this;
8475
}
8576

8677
void LooksBlocks::compileSayOrThinkForSecs(Compiler *compiler, const std::string function)
@@ -98,28 +89,15 @@ void LooksBlocks::compileSayOrThinkForSecs(Compiler *compiler, const std::string
9889
compiler->endLoop();
9990
}
10091

101-
long LooksBlocks::getEffectIndex(IEngine *engine, const std::string &name)
92+
void LooksBlocks::compileSetOrChangeEffect(Compiler *compiler, const std::string &function, const std::string &effectName, CompilerValue *arg)
10293
{
103-
assert(engine);
104-
assert(m_instances.find(engine) != m_instances.cend());
94+
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect(effectName);
10595

106-
LooksBlocks *instance = m_instances[engine];
107-
auto it = instance->m_effectMap.find(name);
108-
109-
if (it == instance->m_effectMap.cend()) {
110-
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect(name);
111-
112-
if (effect) {
113-
instance->m_effects.push_back(effect);
114-
instance->m_effectMap[name] = instance->m_effects.size() - 1;
115-
return instance->m_effects.size() - 1;
116-
} else {
117-
std::cout << "warning: graphic effect '" << name << "' is not registered" << std::endl;
118-
return -1;
119-
}
120-
}
121-
122-
return it->second;
96+
if (effect) {
97+
CompilerValue *effectPtr = compiler->addConstValue(effect);
98+
compiler->addTargetFunctionCall(function, Compiler::StaticType::Void, { Compiler::StaticType::Pointer, Compiler::StaticType::Number }, { effectPtr, arg });
99+
} else
100+
std::cout << "warning: graphic effect '" << effectName << "' is not registered" << std::endl;
123101
}
124102

125103
CompilerValue *LooksBlocks::compileSayForSecs(Compiler *compiler)
@@ -173,14 +151,8 @@ CompilerValue *LooksBlocks::compileChangeEffectBy(Compiler *compiler)
173151
if (!field)
174152
return nullptr;
175153

176-
auto index = getEffectIndex(compiler->engine(), field->value().toString());
177-
178-
if (index != -1) {
179-
auto indexValue = compiler->addConstValue(index);
180-
auto change = compiler->addInput("CHANGE");
181-
compiler->addTargetFunctionCall("looks_changeeffectby", Compiler::StaticType::Void, { Compiler::StaticType::Number, Compiler::StaticType::Number }, { indexValue, change });
182-
}
183-
154+
CompilerValue *change = compiler->addInput("CHANGE");
155+
compileSetOrChangeEffect(compiler, "looks_changeeffectby", field->value().toString(), change);
184156
return nullptr;
185157
}
186158

@@ -191,14 +163,8 @@ CompilerValue *LooksBlocks::compileSetEffectTo(Compiler *compiler)
191163
if (!field)
192164
return nullptr;
193165

194-
auto index = getEffectIndex(compiler->engine(), field->value().toString());
195-
196-
if (index != -1) {
197-
auto indexValue = compiler->addConstValue(index);
198-
auto value = compiler->addInput("VALUE");
199-
compiler->addTargetFunctionCall("looks_seteffectto", Compiler::StaticType::Void, { Compiler::StaticType::Number, Compiler::StaticType::Number }, { indexValue, value });
200-
}
201-
166+
CompilerValue *change = compiler->addInput("VALUE");
167+
compileSetOrChangeEffect(compiler, "looks_seteffectto", field->value().toString(), change);
202168
return nullptr;
203169
}
204170

@@ -290,15 +256,14 @@ extern "C" void looks_hide(Sprite *sprite)
290256
sprite->setVisible(false);
291257
}
292258

293-
extern "C" void looks_changeeffectby(Target *target, double index, double change)
259+
extern "C" void looks_changeeffectby(Target *target, IGraphicsEffect *effect, double change)
294260
{
295-
IGraphicsEffect *effect = LooksBlocks::getEffect(target->engine(), index);
296261
target->setGraphicsEffectValue(effect, target->graphicsEffectValue(effect) + change);
297262
}
298263

299-
extern "C" void looks_seteffectto(Target *target, double index, double value)
264+
extern "C" void looks_seteffectto(Target *target, IGraphicsEffect *effect, double value)
300265
{
301-
target->setGraphicsEffectValue(LooksBlocks::getEffect(target->engine(), index), value);
266+
target->setGraphicsEffectValue(effect, value);
302267
}
303268

304269
extern "C" void looks_cleargraphiceffects(Target *target)

src/blocks/looksblocks.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,16 @@ class IGraphicsEffect;
1616
class LooksBlocks : public IExtension
1717
{
1818
public:
19-
~LooksBlocks();
20-
2119
std::string name() const override;
2220
std::string description() const override;
2321
Rgb color() const override;
2422

2523
void registerBlocks(IEngine *engine) override;
2624
void onInit(IEngine *engine) override;
2725

28-
static inline IGraphicsEffect *getEffect(IEngine *engine, long index) { return m_instances[engine]->m_effects[index]; }
29-
3026
private:
3127
static void compileSayOrThinkForSecs(Compiler *compiler, const std::string function);
32-
static long getEffectIndex(IEngine *engine, const std::string &name);
28+
static void compileSetOrChangeEffect(Compiler *compiler, const std::string &function, const std::string &effectName, CompilerValue *arg);
3329

3430
static CompilerValue *compileSayForSecs(Compiler *compiler);
3531
static CompilerValue *compileSay(Compiler *compiler);
@@ -43,11 +39,6 @@ class LooksBlocks : public IExtension
4339
static CompilerValue *compileChangeSizeBy(Compiler *compiler);
4440
static CompilerValue *compileSetSizeTo(Compiler *compiler);
4541
static CompilerValue *compileSize(Compiler *compiler);
46-
47-
IEngine *m_engine = nullptr;
48-
std::unordered_map<std::string, long> m_effectMap;
49-
std::vector<IGraphicsEffect *> m_effects;
50-
static inline std::unordered_map<IEngine *, LooksBlocks *> m_instances;
5142
};
5243

5344
} // namespace libscratchcpp

0 commit comments

Comments
 (0)