|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 |
|
3 | 3 | #include "eventblocks.h" |
| 4 | +#include "../engine/compiler.h" |
| 5 | +#include "../scratch/broadcast.h" |
4 | 6 |
|
5 | 7 | using namespace libscratchcpp; |
6 | 8 |
|
7 | 9 | EventBlocks::EventBlocks() |
8 | 10 | { |
| 11 | + // Blocks |
9 | 12 | addHatBlock("event_whenflagclicked"); |
| 13 | + addCompileFunction("event_broadcast", &compileBroadcast); |
| 14 | + addCompileFunction("event_whenbroadcastreceived", &compileWhenBroadcastReceived); |
| 15 | + |
| 16 | + // Inputs |
| 17 | + addInput("BROADCAST_INPUT", BROADCAST_INPUT); |
| 18 | + |
| 19 | + // Fields |
| 20 | + addField("BROADCAST_OPTION", BROADCAST_OPTION); |
10 | 21 | } |
11 | 22 |
|
12 | 23 | std::string EventBlocks::name() const |
13 | 24 | { |
14 | 25 | return "Events"; |
15 | 26 | } |
| 27 | + |
| 28 | +void EventBlocks::compileBroadcast(Compiler *compiler) |
| 29 | +{ |
| 30 | + auto input = compiler->input(BROADCAST_INPUT); |
| 31 | + compiler->addInput(input); |
| 32 | + if (input->type() != Input::Type::ObscuredShadow) { |
| 33 | + input->primaryValue()->setValue(compiler->engine()->findBroadcast(input->primaryValue()->value().toString())); |
| 34 | + compiler->addFunctionCall(&broadcastByIndex); |
| 35 | + } else |
| 36 | + compiler->addFunctionCall(&broadcast); |
| 37 | +} |
| 38 | + |
| 39 | +void EventBlocks::compileWhenBroadcastReceived(Compiler *compiler) |
| 40 | +{ |
| 41 | + auto broadcast = std::static_pointer_cast<Broadcast>(compiler->field(BROADCAST_OPTION)->valuePtr()); |
| 42 | + compiler->engine()->addBroadcastScript(compiler->block(), broadcast); |
| 43 | +} |
| 44 | + |
| 45 | +unsigned int EventBlocks::broadcast(VirtualMachine *vm) |
| 46 | +{ |
| 47 | + vm->engine()->broadcast(vm->engine()->findBroadcast(vm->getInput(0, 1)->toString())); |
| 48 | + return 1; |
| 49 | +} |
| 50 | + |
| 51 | +unsigned int EventBlocks::broadcastByIndex(VirtualMachine *vm) |
| 52 | +{ |
| 53 | + vm->engine()->broadcast(vm->getInput(0, 1)->toLong()); |
| 54 | + return 1; |
| 55 | +} |
0 commit comments