Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
qt_add_library(scratchcpp-render STATIC)
qt_add_library(scratchcpp-render SHARED)

set_target_properties(scratchcpp-render PROPERTIES AUTOMOC ON)

Expand Down Expand Up @@ -78,6 +78,11 @@ qt_add_qml_module(scratchcpp-render
effecttransform.h
)

target_sources(scratchcpp-render
PRIVATE
blocks/penblocks.cpp
blocks/penblocks.h)

list(APPEND QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
list(REMOVE_DUPLICATES QML_IMPORT_PATH)
set(QML_IMPORT_PATH ${QML_IMPORT_PATH} CACHE STRING "" FORCE)
113 changes: 113 additions & 0 deletions src/blocks/penblocks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <scratchcpp/iengine.h>
#include <scratchcpp/compiler.h>
#include <scratchcpp/executioncontext.h>
#include <scratchcpp/target.h>
#include <scratchcpp/ispritehandler.h>
#include <scratchcpp/istagehandler.h>
#include <scratchcpp/value.h>
#include <scratchcpp/compilerconstant.h>

#include "penblocks.h"
#include "penlayer.h"
#include "spritemodel.h"
#include "stagemodel.h"

using namespace scratchcpprender;
using namespace libscratchcpp;

std::string PenBlocks::name() const
{
return "pen";
}

std::string PenBlocks::description() const
{
return name() + " blocks";
}

Rgb PenBlocks::color() const
{
return rgb(15, 189, 140);
}

void PenBlocks::registerBlocks(IEngine *engine)
{
engine->addCompileFunction(this, "pen_clear", &compileClear);
engine->addCompileFunction(this, "pen_stamp", &compileStamp);
engine->addCompileFunction(this, "pen_penDown", &compilePenDown);
engine->addCompileFunction(this, "pen_penUp", &compilePenUp);
}

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;
}

static TargetModel *getTargetModel(Target *target)
{
if (target->isStage()) {
Stage *stage = static_cast<Stage *>(target);
return static_cast<StageModel *>(stage->getInterface());
} else {
Sprite *sprite = static_cast<Sprite *>(target);
return static_cast<SpriteModel *>(sprite->getInterface());
}
}

BLOCK_EXPORT void pen_clear(ExecutionContext *ctx)
{
IEngine *engine = ctx->engine();
IPenLayer *penLayer = PenLayer::getProjectPenLayer(engine);

if (penLayer) {
penLayer->clear();
engine->requestRedraw();
}
}

BLOCK_EXPORT void pen_stamp(Target *target)
{
IEngine *engine = target->engine();
IPenLayer *penLayer = PenLayer::getProjectPenLayer(engine);

if (penLayer) {
IRenderedTarget *renderedTarget = nullptr;

if (target->isStage()) {
IStageHandler *iface = static_cast<Stage *>(target)->getInterface();
renderedTarget = static_cast<StageModel *>(iface)->renderedTarget();
} else {
ISpriteHandler *iface = static_cast<Sprite *>(target)->getInterface();
renderedTarget = static_cast<SpriteModel *>(iface)->renderedTarget();
}

penLayer->stamp(renderedTarget);
engine->requestRedraw();
}
}

BLOCK_EXPORT void pen_set_pen_down(Target *target, bool down)
{
getTargetModel(target)->setPenDown(down);
}
22 changes: 22 additions & 0 deletions src/blocks/penblocks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <scratchcpp/iextension.h>

namespace scratchcpprender
{

class PenBlocks : public libscratchcpp::IExtension
{
public:
std::string name() const override;
std::string description() const override;
libscratchcpp::Rgb color() const override;

void registerBlocks(libscratchcpp::IEngine *engine) override;

private:
static libscratchcpp::CompilerValue *compileClear(libscratchcpp::Compiler *compiler);
static libscratchcpp::CompilerValue *compileStamp(libscratchcpp::Compiler *compiler);
static libscratchcpp::CompilerValue *compilePenDown(libscratchcpp::Compiler *compiler);
static libscratchcpp::CompilerValue *compilePenUp(libscratchcpp::Compiler *compiler);
};

} // namespace scratchcpprender
3 changes: 3 additions & 0 deletions src/ipenlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class IPenLayer : public QNanoQuickItem
virtual libscratchcpp::IEngine *engine() const = 0;
virtual void setEngine(libscratchcpp::IEngine *newEngine) = 0;

virtual void beginFrame() = 0;
virtual void endFrame() = 0;

virtual void clear() = 0;
virtual void drawPoint(const PenAttributes &penAttributes, double x, double y) = 0;
virtual void drawLine(const PenAttributes &penAttributes, double x0, double y0, double x1, double y1) = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/irenderedtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class IRenderedTarget : public QNanoQuickItem

virtual bool mirrorHorizontally() const = 0;

virtual void render(double scale) const = 0;

virtual Texture texture() const = 0;
virtual const Texture &cpuTexture() const = 0;
virtual int costumeWidth() const = 0;
Expand Down
Loading
Loading