Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9f3d979
Prepare code for pen blocks
adazem009 Mar 21, 2026
8a09468
Switch to shared library
adazem009 Mar 21, 2026
98b0ead
Implement pen_clear block
adazem009 Mar 21, 2026
52855ea
Implement pen_stamp block
adazem009 Mar 21, 2026
fcb8cf6
Implement pen_penDown block
adazem009 Mar 21, 2026
c0f7013
Implement pen_penUp block
adazem009 Mar 21, 2026
d6def8f
Fix pen blocks extension name
adazem009 Mar 21, 2026
887823d
Fix precision issues in ShaderManager test suite
adazem009 Mar 22, 2026
77b4d1c
Refactor pen frame state management
adazem009 Mar 22, 2026
381b4c8
PenLayer: Remove unused variables
adazem009 Mar 22, 2026
8e8abc1
PenLayer: Drop stamp FBO
adazem009 Mar 22, 2026
db08870
Move stamp rendering to RenderedTarget
adazem009 Mar 22, 2026
f3c4918
RenderedTarget: Cache matrices
adazem009 Mar 22, 2026
ab64766
ShaderManager: Make setUniforms() static
adazem009 Mar 22, 2026
9c8640d
RenderedTarget: Cache shader program
adazem009 Mar 22, 2026
2f627e6
Implement pen_setPenColorToColor block
adazem009 Mar 27, 2026
3919ddc
Optimize color constants in set pen color block
adazem009 Mar 27, 2026
7baf07a
Implement pen_changePenColorParamBy block
adazem009 Mar 28, 2026
9da34e4
Optimize constant param names in change color param block
adazem009 Mar 28, 2026
ed691ee
Implement pen_setPenColorParamTo block
adazem009 Mar 28, 2026
544f4d3
Add missing pen block stage test cases
adazem009 Mar 28, 2026
7efdd13
Implement pen_changePenSizeBy block
adazem009 Mar 28, 2026
d153557
Implement pen_setPenSizeTo block
adazem009 Mar 28, 2026
e7b3ea7
Implement pen_changePenShadeBy block
adazem009 Mar 28, 2026
2773b4b
Implement pen_setPenShadeToNumber block
adazem009 Mar 28, 2026
0087806
Implement pen_changePenHueBy block
adazem009 Mar 28, 2026
a005131
Implement pen_setPenHueToNumber block
adazem009 Mar 28, 2026
3ea17ae
ProjectLoader: Add missing pen layer null check
adazem009 Mar 28, 2026
fb92ade
PenLayer: Make sure FBO is bound when clearing in setEngine()
adazem009 Mar 28, 2026
db269be
Add missing include guard to pen blocks
adazem009 Mar 28, 2026
b27b72c
Refactor shader program cache
adazem009 Mar 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement pen_changePenColorParamBy block
  • Loading branch information
adazem009 committed Mar 28, 2026
commit 7baf07a74b40290984774d39e8fcd889dfff9c89
112 changes: 92 additions & 20 deletions src/blocks/penblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <scratchcpp/compilerconstant.h>
#include <scratchcpp/stringptr.h>
#include <scratchcpp/string_pool.h>
#include <scratchcpp/string_functions.h>

#include "penblocks.h"
#include "penlayer.h"
Expand All @@ -17,6 +18,16 @@
using namespace scratchcpprender;
using namespace libscratchcpp;

static const double COLOR_PARAM_MIN = 0;
static const double COLOR_PARAM_MAX = 100;

inline double wrapClamp(double n, double min, double max)
{
// TODO: Move this to a separate class
const double range = max - min /*+ 1*/;
return n - (std::floor((n - min) / range) * range);
}

inline QColor pen_convert_from_numeric_color(long color)
{
return QColor::fromRgba(static_cast<QRgb>(color));
Expand Down Expand Up @@ -68,6 +79,33 @@ void PenBlocks::registerBlocks(IEngine *engine)
engine->addCompileFunction(this, "pen_penDown", &compilePenDown);
engine->addCompileFunction(this, "pen_penUp", &compilePenUp);
engine->addCompileFunction(this, "pen_setPenColorToColor", &compileSetPenColorToColor);
engine->addCompileFunction(this, "pen_changePenColorParamBy", &compileChangePenColorParamBy);
}

CompilerValue *PenBlocks::compileClear(Compiler *compiler)
{
compiler->addFunctionCallWithCtx("pen_clear");
return nullptr;
}

CompilerValue *PenBlocks::compileStamp(Compiler *compiler)
{
compiler->addTargetFunctionCall("pen_stamp");
return nullptr;
}

CompilerValue *PenBlocks::compilePenDown(Compiler *compiler)
{
CompilerValue *arg = compiler->addConstValue(true);
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
return nullptr;
}

CompilerValue *PenBlocks::compilePenUp(Compiler *compiler)
{
CompilerValue *arg = compiler->addConstValue(false);
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
return nullptr;
}

CompilerValue *PenBlocks::compileSetPenColorToColor(Compiler *compiler)
Expand Down Expand Up @@ -102,29 +140,18 @@ CompilerValue *PenBlocks::compileSetPenColorToColor(Compiler *compiler)
return nullptr;
}

CompilerValue *PenBlocks::compileClear(Compiler *compiler)
CompilerValue *PenBlocks::compileChangePenColorParamBy(Compiler *compiler)
{
compiler->addFunctionCallWithCtx("pen_clear");
return nullptr;
}
CompilerValue *param = compiler->addInput("COLOR_PARAM");
CompilerValue *value = compiler->addInput("VALUE");
CompilerValue *change = compiler->addConstValue(true);

CompilerValue *PenBlocks::compileStamp(Compiler *compiler)
{
compiler->addTargetFunctionCall("pen_stamp");
return nullptr;
}
compiler->addTargetFunctionCall(
"pen_set_or_change_color_param",
Compiler::StaticType::Void,
{ Compiler::StaticType::String, Compiler::StaticType::Number, Compiler::StaticType::Bool },
{ param, value, change });

CompilerValue *PenBlocks::compilePenDown(Compiler *compiler)
{
CompilerValue *arg = compiler->addConstValue(true);
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
return nullptr;
}

CompilerValue *PenBlocks::compilePenUp(Compiler *compiler)
{
CompilerValue *arg = compiler->addConstValue(false);
compiler->addTargetFunctionCall("pen_set_pen_down", Compiler::StaticType::Void, { Compiler::StaticType::Bool }, { arg });
return nullptr;
}

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

pen_setPenColorToHsbColor(target, h, s, b, transparency);
}

BLOCK_EXPORT void pen_set_or_change_color(Target *target, double value, bool change)
{
PenState &penState = getTargetModel(target)->penState();
penState.color = wrapClamp(value + (change ? penState.color : 0), 0, 100);
penState.updateColor();
}

BLOCK_EXPORT void pen_set_or_change_saturation(Target *target, double value, bool change)
{
PenState &penState = getTargetModel(target)->penState();
penState.saturation = std::clamp(value + (change ? penState.saturation : 0), COLOR_PARAM_MIN, COLOR_PARAM_MAX);
penState.updateColor();
}

BLOCK_EXPORT void pen_set_or_change_brightness(Target *target, double value, bool change)
{
PenState &penState = getTargetModel(target)->penState();
penState.brightness = std::clamp(value + (change ? penState.brightness : 0), COLOR_PARAM_MIN, COLOR_PARAM_MAX);
penState.updateColor();
}

BLOCK_EXPORT void pen_set_or_change_transparency(Target *target, double value, bool change)
{
PenState &penState = getTargetModel(target)->penState();
penState.transparency = std::clamp(value + (change ? penState.transparency : 0), COLOR_PARAM_MIN, COLOR_PARAM_MAX);
penState.updateColor();
}

BLOCK_EXPORT void pen_set_or_change_color_param(Target *target, const StringPtr *param, double value, bool change)
{
static const StringPtr COLOR_PARAM("color");
static const StringPtr SATURATION_PARAM("saturation");
static const StringPtr BRIGHTNESS_PARAM("brightness");
static const StringPtr TRANSPARENCY_PARAM("transparency");

if (string_compare_case_sensitive(param, &COLOR_PARAM) == 0)
pen_set_or_change_color(target, value, change);
else if (string_compare_case_sensitive(param, &SATURATION_PARAM) == 0)
pen_set_or_change_saturation(target, value, change);
else if (string_compare_case_sensitive(param, &BRIGHTNESS_PARAM) == 0)
pen_set_or_change_brightness(target, value, change);
else if (string_compare_case_sensitive(param, &TRANSPARENCY_PARAM) == 0)
pen_set_or_change_transparency(target, value, change);
}
1 change: 1 addition & 0 deletions src/blocks/penblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PenBlocks : public libscratchcpp::IExtension
static libscratchcpp::CompilerValue *compilePenDown(libscratchcpp::Compiler *compiler);
static libscratchcpp::CompilerValue *compilePenUp(libscratchcpp::Compiler *compiler);
static libscratchcpp::CompilerValue *compileSetPenColorToColor(libscratchcpp::Compiler *compiler);
static libscratchcpp::CompilerValue *compileChangePenColorParamBy(libscratchcpp::Compiler *compiler);
};

} // namespace scratchcpprender
Loading
Loading