|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | + |
| 3 | +#include <scratchcpp/compiler.h> |
| 4 | +#include <scratchcpp/sprite.h> |
| 5 | + |
| 6 | +#include "penblocks.h" |
| 7 | +#include "penlayer.h" |
| 8 | +#include "spritemodel.h" |
| 9 | + |
| 10 | +using namespace scratchcpprender; |
| 11 | +using namespace libscratchcpp; |
| 12 | + |
| 13 | +std::string PenBlocks::name() const |
| 14 | +{ |
| 15 | + return "Pen"; |
| 16 | +} |
| 17 | + |
| 18 | +void PenBlocks::registerBlocks(IEngine *engine) |
| 19 | +{ |
| 20 | + // Blocks |
| 21 | + engine->addCompileFunction(this, "pen_clear", &compileClear); |
| 22 | + engine->addCompileFunction(this, "pen_penDown", &compilePenDown); |
| 23 | + engine->addCompileFunction(this, "pen_penUp", &compilePenUp); |
| 24 | +} |
| 25 | + |
| 26 | +void PenBlocks::compileClear(Compiler *compiler) |
| 27 | +{ |
| 28 | + compiler->addFunctionCall(&clear); |
| 29 | +} |
| 30 | + |
| 31 | +void PenBlocks::compilePenDown(Compiler *compiler) |
| 32 | +{ |
| 33 | + compiler->addFunctionCall(&penDown); |
| 34 | +} |
| 35 | + |
| 36 | +void PenBlocks::compilePenUp(Compiler *compiler) |
| 37 | +{ |
| 38 | + compiler->addFunctionCall(&penUp); |
| 39 | +} |
| 40 | + |
| 41 | +unsigned int PenBlocks::clear(VirtualMachine *vm) |
| 42 | +{ |
| 43 | + IPenLayer *penLayer = PenLayer::getProjectPenLayer(vm->engine()); |
| 44 | + |
| 45 | + if (penLayer) { |
| 46 | + penLayer->clear(); |
| 47 | + vm->engine()->requestRedraw(); |
| 48 | + } |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +unsigned int PenBlocks::penDown(VirtualMachine *vm) |
| 54 | +{ |
| 55 | + Target *target = vm->target(); |
| 56 | + |
| 57 | + if (!target || target->isStage()) |
| 58 | + return 0; |
| 59 | + |
| 60 | + Sprite *sprite = static_cast<Sprite *>(target); |
| 61 | + SpriteModel *model = static_cast<SpriteModel *>(sprite->getInterface()); |
| 62 | + |
| 63 | + if (model) |
| 64 | + model->setPenDown(true); |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
| 68 | + |
| 69 | +unsigned int PenBlocks::penUp(libscratchcpp::VirtualMachine *vm) |
| 70 | +{ |
| 71 | + Target *target = vm->target(); |
| 72 | + |
| 73 | + if (!target || target->isStage()) |
| 74 | + return 0; |
| 75 | + |
| 76 | + Sprite *sprite = static_cast<Sprite *>(target); |
| 77 | + SpriteModel *model = static_cast<SpriteModel *>(sprite->getInterface()); |
| 78 | + |
| 79 | + if (model) |
| 80 | + model->setPenDown(false); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
0 commit comments