|
8 | 8 | #include <scratchcpp/compilerconstant.h> |
9 | 9 | #include <scratchcpp/stringptr.h> |
10 | 10 | #include <scratchcpp/string_pool.h> |
| 11 | +#include <scratchcpp/string_functions.h> |
11 | 12 |
|
12 | 13 | #include "penblocks.h" |
13 | 14 | #include "penlayer.h" |
|
17 | 18 | using namespace scratchcpprender; |
18 | 19 | using namespace libscratchcpp; |
19 | 20 |
|
| 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 | + |
20 | 31 | inline QColor pen_convert_from_numeric_color(long color) |
21 | 32 | { |
22 | 33 | return QColor::fromRgba(static_cast<QRgb>(color)); |
@@ -68,6 +79,33 @@ void PenBlocks::registerBlocks(IEngine *engine) |
68 | 79 | engine->addCompileFunction(this, "pen_penDown", &compilePenDown); |
69 | 80 | engine->addCompileFunction(this, "pen_penUp", &compilePenUp); |
70 | 81 | 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; |
71 | 109 | } |
72 | 110 |
|
73 | 111 | CompilerValue *PenBlocks::compileSetPenColorToColor(Compiler *compiler) |
@@ -102,29 +140,18 @@ CompilerValue *PenBlocks::compileSetPenColorToColor(Compiler *compiler) |
102 | 140 | return nullptr; |
103 | 141 | } |
104 | 142 |
|
105 | | -CompilerValue *PenBlocks::compileClear(Compiler *compiler) |
| 143 | +CompilerValue *PenBlocks::compileChangePenColorParamBy(Compiler *compiler) |
106 | 144 | { |
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); |
110 | 148 |
|
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 }); |
116 | 154 |
|
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 }); |
128 | 155 | return nullptr; |
129 | 156 | } |
130 | 157 |
|
@@ -210,3 +237,48 @@ BLOCK_EXPORT void pen_setPenColorToColor(Target *target, const ValueData *color) |
210 | 237 |
|
211 | 238 | pen_setPenColorToHsbColor(target, h, s, b, transparency); |
212 | 239 | } |
| 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 | +} |
0 commit comments