Skip to content

Commit 7baf07a

Browse files
committed
Implement pen_changePenColorParamBy block
1 parent 3919ddc commit 7baf07a

File tree

3 files changed

+808
-20
lines changed

3 files changed

+808
-20
lines changed

src/blocks/penblocks.cpp

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <scratchcpp/compilerconstant.h>
99
#include <scratchcpp/stringptr.h>
1010
#include <scratchcpp/string_pool.h>
11+
#include <scratchcpp/string_functions.h>
1112

1213
#include "penblocks.h"
1314
#include "penlayer.h"
@@ -17,6 +18,16 @@
1718
using namespace scratchcpprender;
1819
using namespace libscratchcpp;
1920

21+
static const double COLOR_PARAM_MIN = 0;
22+
static const double COLOR_PARAM_MAX = 100;
23+
24+
inline double wrapClamp(double n, double min, double max)
25+
{
26+
// TODO: Move this to a separate class
27+
const double range = max - min /*+ 1*/;
28+
return n - (std::floor((n - min) / range) * range);
29+
}
30+
2031
inline QColor pen_convert_from_numeric_color(long color)
2132
{
2233
return QColor::fromRgba(static_cast<QRgb>(color));
@@ -68,6 +79,33 @@ void PenBlocks::registerBlocks(IEngine *engine)
6879
engine->addCompileFunction(this, "pen_penDown", &compilePenDown);
6980
engine->addCompileFunction(this, "pen_penUp", &compilePenUp);
7081
engine->addCompileFunction(this, "pen_setPenColorToColor", &compileSetPenColorToColor);
82+
engine->addCompileFunction(this, "pen_changePenColorParamBy", &compileChangePenColorParamBy);
83+
}
84+
85+
CompilerValue *PenBlocks::compileClear(Compiler *compiler)
86+
{
87+
compiler->addFunctionCallWithCtx("pen_clear");
88+
return nullptr;
89+
}
90+
91+
CompilerValue *PenBlocks::compileStamp(Compiler *compiler)
92+
{
93+
compiler->addTargetFunctionCall("pen_stamp");
94+
return nullptr;
95+
}
96+
97+
CompilerValue *PenBlocks::compilePenDown(Compiler *compiler)
98+
{
99+
CompilerValue *arg = compiler->addConstValue(true);
100+
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
101+
return nullptr;
102+
}
103+
104+
CompilerValue *PenBlocks::compilePenUp(Compiler *compiler)
105+
{
106+
CompilerValue *arg = compiler->addConstValue(false);
107+
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
108+
return nullptr;
71109
}
72110

73111
CompilerValue *PenBlocks::compileSetPenColorToColor(Compiler *compiler)
@@ -102,29 +140,18 @@ CompilerValue *PenBlocks::compileSetPenColorToColor(Compiler *compiler)
102140
return nullptr;
103141
}
104142

105-
CompilerValue *PenBlocks::compileClear(Compiler *compiler)
143+
CompilerValue *PenBlocks::compileChangePenColorParamBy(Compiler *compiler)
106144
{
107-
compiler->addFunctionCallWithCtx("pen_clear");
108-
return nullptr;
109-
}
145+
CompilerValue *param = compiler->addInput("COLOR_PARAM");
146+
CompilerValue *value = compiler->addInput("VALUE");
147+
CompilerValue *change = compiler->addConstValue(true);
110148

111-
CompilerValue *PenBlocks::compileStamp(Compiler *compiler)
112-
{
113-
compiler->addTargetFunctionCall("pen_stamp");
114-
return nullptr;
115-
}
149+
compiler->addTargetFunctionCall(
150+
"pen_set_or_change_color_param",
151+
Compiler::StaticType::Void,
152+
{ Compiler::StaticType::String, Compiler::StaticType::Number, Compiler::StaticType::Bool },
153+
{ param, value, change });
116154

117-
CompilerValue *PenBlocks::compilePenDown(Compiler *compiler)
118-
{
119-
CompilerValue *arg = compiler->addConstValue(true);
120-
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
121-
return nullptr;
122-
}
123-
124-
CompilerValue *PenBlocks::compilePenUp(Compiler *compiler)
125-
{
126-
CompilerValue *arg = compiler->addConstValue(false);
127-
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
128155
return nullptr;
129156
}
130157

@@ -210,3 +237,48 @@ BLOCK_EXPORT void pen_setPenColorToColor(Target *target, const ValueData *color)
210237

211238
pen_setPenColorToHsbColor(target, h, s, b, transparency);
212239
}
240+
241+
BLOCK_EXPORT void pen_set_or_change_color(Target *target, double value, bool change)
242+
{
243+
PenState &penState = getTargetModel(target)->penState();
244+
penState.color = wrapClamp(value + (change ? penState.color : 0), 0, 100);
245+
penState.updateColor();
246+
}
247+
248+
BLOCK_EXPORT void pen_set_or_change_saturation(Target *target, double value, bool change)
249+
{
250+
PenState &penState = getTargetModel(target)->penState();
251+
penState.saturation = std::clamp(value + (change ? penState.saturation : 0), COLOR_PARAM_MIN, COLOR_PARAM_MAX);
252+
penState.updateColor();
253+
}
254+
255+
BLOCK_EXPORT void pen_set_or_change_brightness(Target *target, double value, bool change)
256+
{
257+
PenState &penState = getTargetModel(target)->penState();
258+
penState.brightness = std::clamp(value + (change ? penState.brightness : 0), COLOR_PARAM_MIN, COLOR_PARAM_MAX);
259+
penState.updateColor();
260+
}
261+
262+
BLOCK_EXPORT void pen_set_or_change_transparency(Target *target, double value, bool change)
263+
{
264+
PenState &penState = getTargetModel(target)->penState();
265+
penState.transparency = std::clamp(value + (change ? penState.transparency : 0), COLOR_PARAM_MIN, COLOR_PARAM_MAX);
266+
penState.updateColor();
267+
}
268+
269+
BLOCK_EXPORT void pen_set_or_change_color_param(Target *target, const StringPtr *param, double value, bool change)
270+
{
271+
static const StringPtr COLOR_PARAM("color");
272+
static const StringPtr SATURATION_PARAM("saturation");
273+
static const StringPtr BRIGHTNESS_PARAM("brightness");
274+
static const StringPtr TRANSPARENCY_PARAM("transparency");
275+
276+
if (string_compare_case_sensitive(param, &COLOR_PARAM) == 0)
277+
pen_set_or_change_color(target, value, change);
278+
else if (string_compare_case_sensitive(param, &SATURATION_PARAM) == 0)
279+
pen_set_or_change_saturation(target, value, change);
280+
else if (string_compare_case_sensitive(param, &BRIGHTNESS_PARAM) == 0)
281+
pen_set_or_change_brightness(target, value, change);
282+
else if (string_compare_case_sensitive(param, &TRANSPARENCY_PARAM) == 0)
283+
pen_set_or_change_transparency(target, value, change);
284+
}

src/blocks/penblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class PenBlocks : public libscratchcpp::IExtension
1818
static libscratchcpp::CompilerValue *compilePenDown(libscratchcpp::Compiler *compiler);
1919
static libscratchcpp::CompilerValue *compilePenUp(libscratchcpp::Compiler *compiler);
2020
static libscratchcpp::CompilerValue *compileSetPenColorToColor(libscratchcpp::Compiler *compiler);
21+
static libscratchcpp::CompilerValue *compileChangePenColorParamBy(libscratchcpp::Compiler *compiler);
2122
};
2223

2324
} // namespace scratchcpprender

0 commit comments

Comments
 (0)