1818
1919using namespace libscratchcpp ;
2020
21- LooksBlocks::~LooksBlocks ()
22- {
23- if (m_engine)
24- m_instances.erase (m_engine);
25- }
26-
2721std::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
8677void 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
125103CompilerValue *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
304269extern " C" void looks_cleargraphiceffects (Target *target)
0 commit comments