Skip to content

Commit b39781f

Browse files
committed
Implement looks_changeeffectby
1 parent b2f15f7 commit b39781f

File tree

3 files changed

+503
-2
lines changed

3 files changed

+503
-2
lines changed

src/blocks/looksblocks.cpp

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/input.h>
88
#include <scratchcpp/field.h>
99
#include <scratchcpp/costume.h>
10+
#include <scratchcpp/scratchconfiguration.h>
1011

1112
#include "looksblocks.h"
1213
#include "../engine/internal/randomgenerator.h"
@@ -25,6 +26,7 @@ void LooksBlocks::registerBlocks(IEngine *engine)
2526
// Blocks
2627
engine->addCompileFunction(this, "looks_show", &compileShow);
2728
engine->addCompileFunction(this, "looks_hide", &compileHide);
29+
engine->addCompileFunction(this, "looks_changeeffectby", &compileChangeEffectBy);
2830
engine->addCompileFunction(this, "looks_changesizeby", &compileChangeSizeBy);
2931
engine->addCompileFunction(this, "looks_setsizeto", &compileSetSizeTo);
3032
engine->addCompileFunction(this, "looks_size", &compileSize);
@@ -44,10 +46,18 @@ void LooksBlocks::registerBlocks(IEngine *engine)
4446

4547
// Fields
4648
engine->addField(this, "NUMBER_NAME", NUMBER_NAME);
49+
engine->addField(this, "EFFECT", EFFECT);
4750

4851
// Field values
4952
engine->addFieldValue(this, "number", Number);
5053
engine->addFieldValue(this, "name", Name);
54+
engine->addFieldValue(this, "COLOR", ColorEffect);
55+
engine->addFieldValue(this, "FISHEYE", FisheyeEffect);
56+
engine->addFieldValue(this, "WHIRL", WhirlEffect);
57+
engine->addFieldValue(this, "PIXELATE", PixelateEffect);
58+
engine->addFieldValue(this, "MOSAIC", MosaicEffect);
59+
engine->addFieldValue(this, "BRIGHTNESS", BrightnessEffect);
60+
engine->addFieldValue(this, "GHOST", GhostEffect);
5161
}
5262

5363
void LooksBlocks::compileShow(Compiler *compiler)
@@ -60,6 +70,89 @@ void LooksBlocks::compileHide(Compiler *compiler)
6070
compiler->addFunctionCall(&hide);
6171
}
6272

73+
void LooksBlocks::compileChangeEffectBy(Compiler *compiler)
74+
{
75+
int option = compiler->field(EFFECT)->specialValueId();
76+
77+
switch (option) {
78+
case ColorEffect:
79+
if (!m_colorEffect)
80+
m_colorEffect = ScratchConfiguration::getGraphicsEffect("color");
81+
82+
compiler->addInput(CHANGE);
83+
compiler->addFunctionCall(&changeColorEffectBy);
84+
break;
85+
86+
case FisheyeEffect:
87+
if (!m_fisheyeEffect)
88+
m_fisheyeEffect = ScratchConfiguration::getGraphicsEffect("fisheye");
89+
90+
compiler->addInput(CHANGE);
91+
compiler->addFunctionCall(&changeFisheyeEffectBy);
92+
break;
93+
94+
case WhirlEffect:
95+
if (!m_whirlEffect)
96+
m_whirlEffect = ScratchConfiguration::getGraphicsEffect("whirl");
97+
98+
compiler->addInput(CHANGE);
99+
compiler->addFunctionCall(&changeWhirlEffectBy);
100+
break;
101+
102+
case PixelateEffect:
103+
if (!m_pixelateEffect)
104+
m_pixelateEffect = ScratchConfiguration::getGraphicsEffect("pixelate");
105+
106+
compiler->addInput(CHANGE);
107+
compiler->addFunctionCall(&changePixelateEffectBy);
108+
break;
109+
110+
case MosaicEffect:
111+
if (!m_mosaicEffect)
112+
m_mosaicEffect = ScratchConfiguration::getGraphicsEffect("mosaic");
113+
114+
compiler->addInput(CHANGE);
115+
compiler->addFunctionCall(&changeMosaicEffectBy);
116+
break;
117+
118+
case BrightnessEffect:
119+
if (!m_brightnessEffect)
120+
m_brightnessEffect = ScratchConfiguration::getGraphicsEffect("brightness");
121+
122+
compiler->addInput(CHANGE);
123+
compiler->addFunctionCall(&changeBrightnessEffectBy);
124+
break;
125+
126+
case GhostEffect:
127+
if (!m_ghostEffect)
128+
m_ghostEffect = ScratchConfiguration::getGraphicsEffect("ghost");
129+
130+
compiler->addInput(CHANGE);
131+
compiler->addFunctionCall(&changeGhostEffectBy);
132+
break;
133+
134+
default:
135+
IGraphicsEffect *effect = ScratchConfiguration::getGraphicsEffect(compiler->field(EFFECT)->value().toString());
136+
137+
if (effect) {
138+
auto it = std::find(m_customGraphicsEffects.begin(), m_customGraphicsEffects.end(), effect);
139+
size_t index;
140+
141+
if (it == m_customGraphicsEffects.end()) {
142+
index = m_customGraphicsEffects.size();
143+
m_customGraphicsEffects.push_back(effect);
144+
} else
145+
index = it - m_customGraphicsEffects.begin();
146+
147+
compiler->addConstValue(index);
148+
compiler->addInput(CHANGE);
149+
compiler->addFunctionCall(&changeEffectBy);
150+
}
151+
152+
break;
153+
}
154+
}
155+
63156
void LooksBlocks::compileChangeSizeBy(Compiler *compiler)
64157
{
65158
compiler->addInput(CHANGE);
@@ -254,6 +347,88 @@ unsigned int LooksBlocks::hide(VirtualMachine *vm)
254347
return 0;
255348
}
256349

350+
unsigned int LooksBlocks::changeEffectBy(VirtualMachine *vm)
351+
{
352+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
353+
354+
if (sprite) {
355+
IGraphicsEffect *effect = m_customGraphicsEffects[vm->getInput(0, 2)->toLong()];
356+
sprite->setGraphicsEffectValue(effect, sprite->graphicsEffectValue(effect) + vm->getInput(1, 2)->toDouble());
357+
}
358+
359+
return 2;
360+
}
361+
362+
unsigned int LooksBlocks::changeColorEffectBy(VirtualMachine *vm)
363+
{
364+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
365+
366+
if (sprite)
367+
sprite->setGraphicsEffectValue(m_colorEffect, sprite->graphicsEffectValue(m_colorEffect) + vm->getInput(0, 1)->toDouble());
368+
369+
return 1;
370+
}
371+
372+
unsigned int LooksBlocks::changeFisheyeEffectBy(VirtualMachine *vm)
373+
{
374+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
375+
376+
if (sprite)
377+
sprite->setGraphicsEffectValue(m_fisheyeEffect, sprite->graphicsEffectValue(m_fisheyeEffect) + vm->getInput(0, 1)->toDouble());
378+
379+
return 1;
380+
}
381+
382+
unsigned int LooksBlocks::changeWhirlEffectBy(VirtualMachine *vm)
383+
{
384+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
385+
386+
if (sprite)
387+
sprite->setGraphicsEffectValue(m_whirlEffect, sprite->graphicsEffectValue(m_whirlEffect) + vm->getInput(0, 1)->toDouble());
388+
389+
return 1;
390+
}
391+
392+
unsigned int LooksBlocks::changePixelateEffectBy(VirtualMachine *vm)
393+
{
394+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
395+
396+
if (sprite)
397+
sprite->setGraphicsEffectValue(m_pixelateEffect, sprite->graphicsEffectValue(m_pixelateEffect) + vm->getInput(0, 1)->toDouble());
398+
399+
return 1;
400+
}
401+
402+
unsigned int LooksBlocks::changeMosaicEffectBy(VirtualMachine *vm)
403+
{
404+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
405+
406+
if (sprite)
407+
sprite->setGraphicsEffectValue(m_mosaicEffect, sprite->graphicsEffectValue(m_mosaicEffect) + vm->getInput(0, 1)->toDouble());
408+
409+
return 1;
410+
}
411+
412+
unsigned int LooksBlocks::changeBrightnessEffectBy(VirtualMachine *vm)
413+
{
414+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
415+
416+
if (sprite)
417+
sprite->setGraphicsEffectValue(m_brightnessEffect, sprite->graphicsEffectValue(m_brightnessEffect) + vm->getInput(0, 1)->toDouble());
418+
419+
return 1;
420+
}
421+
422+
unsigned int LooksBlocks::changeGhostEffectBy(VirtualMachine *vm)
423+
{
424+
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());
425+
426+
if (sprite)
427+
sprite->setGraphicsEffectValue(m_ghostEffect, sprite->graphicsEffectValue(m_ghostEffect) + vm->getInput(0, 1)->toDouble());
428+
429+
return 1;
430+
}
431+
257432
unsigned int LooksBlocks::changeSizeBy(VirtualMachine *vm)
258433
{
259434
Sprite *sprite = dynamic_cast<Sprite *>(vm->target());

src/blocks/looksblocks.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
#pragma once
44

55
#include <scratchcpp/iblocksection.h>
6+
#include <vector>
67

78
namespace libscratchcpp
89
{
910

1011
class Target;
1112
class Stage;
1213
class Value;
14+
class IGraphicsEffect;
1315
class IRandomGenerator;
1416

1517
/*! \brief The LooksBlocks class contains the implementation of looks blocks. */
@@ -26,13 +28,21 @@ class LooksBlocks : public IBlockSection
2628

2729
enum Fields
2830
{
29-
NUMBER_NAME
31+
NUMBER_NAME,
32+
EFFECT
3033
};
3134

3235
enum FieldValues
3336
{
3437
Number,
35-
Name
38+
Name,
39+
ColorEffect,
40+
FisheyeEffect,
41+
WhirlEffect,
42+
PixelateEffect,
43+
MosaicEffect,
44+
BrightnessEffect,
45+
GhostEffect
3646
};
3747

3848
std::string name() const override;
@@ -41,6 +51,7 @@ class LooksBlocks : public IBlockSection
4151

4252
static void compileShow(Compiler *compiler);
4353
static void compileHide(Compiler *compiler);
54+
static void compileChangeEffectBy(Compiler *compiler);
4455
static void compileChangeSizeBy(Compiler *compiler);
4556
static void compileSetSizeTo(Compiler *compiler);
4657
static void compileSize(Compiler *compiler);
@@ -54,6 +65,16 @@ class LooksBlocks : public IBlockSection
5465

5566
static unsigned int show(VirtualMachine *vm);
5667
static unsigned int hide(VirtualMachine *vm);
68+
69+
static unsigned int changeEffectBy(VirtualMachine *vm);
70+
static unsigned int changeColorEffectBy(VirtualMachine *vm);
71+
static unsigned int changeFisheyeEffectBy(VirtualMachine *vm);
72+
static unsigned int changeWhirlEffectBy(VirtualMachine *vm);
73+
static unsigned int changePixelateEffectBy(VirtualMachine *vm);
74+
static unsigned int changeMosaicEffectBy(VirtualMachine *vm);
75+
static unsigned int changeBrightnessEffectBy(VirtualMachine *vm);
76+
static unsigned int changeGhostEffectBy(VirtualMachine *vm);
77+
5778
static unsigned int changeSizeBy(VirtualMachine *vm);
5879
static unsigned int setSizeTo(VirtualMachine *vm);
5980
static unsigned int size(VirtualMachine *vm);
@@ -88,6 +109,15 @@ class LooksBlocks : public IBlockSection
88109
static unsigned int backdropNumber(VirtualMachine *vm);
89110
static unsigned int backdropName(VirtualMachine *vm);
90111

112+
static inline std::vector<IGraphicsEffect *> m_customGraphicsEffects;
113+
static inline IGraphicsEffect *m_colorEffect = nullptr;
114+
static inline IGraphicsEffect *m_fisheyeEffect = nullptr;
115+
static inline IGraphicsEffect *m_whirlEffect = nullptr;
116+
static inline IGraphicsEffect *m_pixelateEffect = nullptr;
117+
static inline IGraphicsEffect *m_mosaicEffect = nullptr;
118+
static inline IGraphicsEffect *m_brightnessEffect = nullptr;
119+
static inline IGraphicsEffect *m_ghostEffect = nullptr;
120+
91121
static IRandomGenerator *rng;
92122
};
93123

0 commit comments

Comments
 (0)