|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +#include <scratchcpp/dev/test/scriptbuilder.h> |
| 4 | +#include <scratchcpp/block.h> |
| 5 | +#include <scratchcpp/input.h> |
| 6 | +#include <scratchcpp/field.h> |
| 7 | +#include <scratchcpp/dev/compilerconstant.h> |
| 8 | +#include <scratchcpp/dev/executablecode.h> |
| 9 | +#include <scratchcpp/stage.h> |
| 10 | +#include <scratchcpp/iengine.h> |
| 11 | +#include <scratchcpp/list.h> |
| 12 | + |
| 13 | +#include "scriptbuilder_p.h" |
| 14 | + |
| 15 | +using namespace libscratchcpp; |
| 16 | +using namespace libscratchcpp::test; |
| 17 | + |
| 18 | +static std::unordered_map<IEngine *, std::shared_ptr<List>> captureLists; |
| 19 | + |
| 20 | +/*! Constructs ScriptBuilder. */ |
| 21 | +ScriptBuilder::ScriptBuilder(IExtension *extension, IEngine *engine, std::shared_ptr<Target> target) : |
| 22 | + impl(spimpl::make_unique_impl<ScriptBuilderPrivate>(engine, target)) |
| 23 | +{ |
| 24 | + // Create capture list |
| 25 | + if (captureLists.find(engine) != captureLists.cend()) { |
| 26 | + std::cerr << "error: only one ScriptBuilder can be created for each engine" << std::endl; |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + captureLists[engine] = std::make_shared<List>("", ""); |
| 31 | + |
| 32 | + // Add start hat block |
| 33 | + auto block = std::make_shared<Block>(std::to_string(impl->blockId++), "script_builder_init"); |
| 34 | + engine->addCompileFunction(extension, block->opcode(), [](Compiler *compiler) -> CompilerValue * { |
| 35 | + compiler->engine()->addGreenFlagScript(compiler->block()); |
| 36 | + return nullptr; |
| 37 | + }); |
| 38 | + addBlock(block); |
| 39 | + |
| 40 | + // Add compile function for return value capture block |
| 41 | + engine->addCompileFunction(extension, "script_builder_capture", [](Compiler *compiler) -> CompilerValue * { |
| 42 | + CompilerValue *input = compiler->addInput("VALUE"); |
| 43 | + compiler->createListAppend(captureLists[compiler->engine()].get(), input); |
| 44 | + return nullptr; |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +/*! Destroys ScriptBuilder. */ |
| 49 | +ScriptBuilder::~ScriptBuilder() |
| 50 | +{ |
| 51 | + captureLists.erase(impl->engine); |
| 52 | +} |
| 53 | + |
| 54 | +/*! Adds a block with the given opcode to the script. */ |
| 55 | +void ScriptBuilder::addBlock(const std::string &opcode) |
| 56 | +{ |
| 57 | + impl->lastBlock = std::make_shared<Block>(std::to_string(impl->blockId++), opcode); |
| 58 | + addBlock(impl->lastBlock); |
| 59 | +} |
| 60 | + |
| 61 | +/*! Creates a reporter block with the given opcode to be used with captureBlockReturnValue() later. */ |
| 62 | +void ScriptBuilder::addReporterBlock(const std::string &opcode) |
| 63 | +{ |
| 64 | + impl->lastBlock = std::make_shared<Block>(std::to_string(impl->blockId++), opcode); |
| 65 | +} |
| 66 | + |
| 67 | +/*! Captures the return value of the created reporter block. It can be retrieved using capturedValues() later. */ |
| 68 | +void ScriptBuilder::captureBlockReturnValue() |
| 69 | +{ |
| 70 | + if (!impl->lastBlock) |
| 71 | + return; |
| 72 | + |
| 73 | + auto valueBlock = impl->lastBlock; |
| 74 | + addBlock("script_builder_capture"); |
| 75 | + addObscuredInput("VALUE", valueBlock); |
| 76 | +} |
| 77 | + |
| 78 | +/*! Adds a simple input with a value to the current block. */ |
| 79 | +void ScriptBuilder::addValueInput(const std::string &name, const Value &value) |
| 80 | +{ |
| 81 | + if (!impl->lastBlock) |
| 82 | + return; |
| 83 | + |
| 84 | + auto input = std::make_shared<Input>(name, Input::Type::Shadow); |
| 85 | + input->setPrimaryValue(value); |
| 86 | + impl->lastBlock->addInput(input); |
| 87 | +} |
| 88 | + |
| 89 | +/*! Adds a null input (zero) to the current block. */ |
| 90 | +void ScriptBuilder::addNullInput(const std::string &name) |
| 91 | +{ |
| 92 | + if (!impl->lastBlock) |
| 93 | + return; |
| 94 | + |
| 95 | + auto input = std::make_shared<Input>(name, Input::Type::Shadow); |
| 96 | + impl->lastBlock->addInput(input); |
| 97 | +} |
| 98 | + |
| 99 | +/*! Adds an input obscured by the given block to the current block. */ |
| 100 | +void ScriptBuilder::addObscuredInput(const std::string &name, std::shared_ptr<Block> valueBlock) |
| 101 | +{ |
| 102 | + if (!impl->lastBlock) |
| 103 | + return; |
| 104 | + |
| 105 | + valueBlock->setId(std::to_string(impl->blockId++)); |
| 106 | + impl->inputBlocks.push_back(valueBlock); |
| 107 | + |
| 108 | + auto input = std::make_shared<Input>(name, Input::Type::ObscuredShadow); |
| 109 | + input->setValueBlock(valueBlock); |
| 110 | + impl->lastBlock->addInput(input); |
| 111 | +} |
| 112 | + |
| 113 | +/*! Adds an input obscured by a block which returns zero to the current block. */ |
| 114 | +void ScriptBuilder::addNullObscuredInput(const std::string &name) |
| 115 | +{ |
| 116 | + if (!impl->lastBlock) |
| 117 | + return; |
| 118 | + |
| 119 | + auto input = std::make_shared<Input>(name, Input::Type::ObscuredShadow); |
| 120 | + auto block = std::make_shared<Block>(std::to_string(impl->blockId++), ""); |
| 121 | + block->setCompileFunction([](Compiler *compiler) -> CompilerValue * { return compiler->addConstValue(Value()); }); |
| 122 | + input->setValueBlock(block); |
| 123 | + impl->inputBlocks.push_back(block); |
| 124 | + impl->blocks.back()->addInput(input); |
| 125 | +} |
| 126 | + |
| 127 | +/*! Adds a dropdown menu input to the current block. */ |
| 128 | +void ScriptBuilder::addDropdownInput(const std::string &name, const std::string &selectedValue) |
| 129 | +{ |
| 130 | + if (!impl->lastBlock) |
| 131 | + return; |
| 132 | + |
| 133 | + auto block = impl->blocks.back(); |
| 134 | + auto input = std::make_shared<Input>(name, Input::Type::Shadow); |
| 135 | + block->addInput(input); |
| 136 | + |
| 137 | + auto menu = std::make_shared<Block>(std::to_string(impl->blockId++), block->opcode() + "_menu"); |
| 138 | + menu->setShadow(true); |
| 139 | + impl->inputBlocks.push_back(menu); |
| 140 | + input->setValueBlock(menu); |
| 141 | + |
| 142 | + auto field = std::make_shared<Field>(name, selectedValue); |
| 143 | + menu->addField(field); |
| 144 | +} |
| 145 | + |
| 146 | +/*! Adds a dropdown field to the current block. */ |
| 147 | +void ScriptBuilder::addDropdownField(const std::string &name, const std::string &selectedValue) |
| 148 | +{ |
| 149 | + if (!impl->lastBlock) |
| 150 | + return; |
| 151 | + |
| 152 | + auto field = std::make_shared<Field>(name, selectedValue); |
| 153 | + impl->blocks.back()->addField(field); |
| 154 | +} |
| 155 | + |
| 156 | +/*! Builds and compiles the script. */ |
| 157 | +void ScriptBuilder::build() |
| 158 | +{ |
| 159 | + if (impl->target->blocks().empty()) { |
| 160 | + for (auto block : impl->blocks) |
| 161 | + impl->target->addBlock(block); |
| 162 | + |
| 163 | + for (auto block : impl->inputBlocks) |
| 164 | + impl->target->addBlock(block); |
| 165 | + } |
| 166 | + |
| 167 | + std::vector<std::shared_ptr<Target>> targets = impl->engine->targets(); |
| 168 | + |
| 169 | + if (std::find(targets.begin(), targets.end(), impl->target) == targets.end()) { |
| 170 | + targets.push_back(impl->target); |
| 171 | + impl->engine->setTargets({ impl->target }); |
| 172 | + } |
| 173 | + |
| 174 | + impl->engine->compile(); |
| 175 | +} |
| 176 | + |
| 177 | +/*! Runs the built script. */ |
| 178 | +void ScriptBuilder::run() |
| 179 | +{ |
| 180 | + impl->engine->run(); |
| 181 | +} |
| 182 | + |
| 183 | +/*! Returns the list of captured block return values. */ |
| 184 | +List *ScriptBuilder::capturedValues() const |
| 185 | +{ |
| 186 | + return captureLists[impl->engine].get(); |
| 187 | +} |
| 188 | + |
| 189 | +void ScriptBuilder::addBlock(std::shared_ptr<Block> block) |
| 190 | +{ |
| 191 | + if (!impl->blocks.empty()) { |
| 192 | + auto lastBlock = impl->blocks.back(); |
| 193 | + lastBlock->setNext(block); |
| 194 | + block->setParent(lastBlock); |
| 195 | + } |
| 196 | + |
| 197 | + impl->blocks.push_back(block); |
| 198 | +} |
0 commit comments