Skip to content

Commit f2f304d

Browse files
committed
Yeah
1 parent 1f9084f commit f2f304d

1 file changed

Lines changed: 65 additions & 22 deletions

File tree

include/scratch/block.h

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,71 @@
44
#include <memory>
55
#include <string>
66
#include <vector>
7+
#include <optional>
78

89
#include "field.h"
910

10-
//
11-
namespace libscratch
12-
{
13-
/*! \brief The Block class represents a Scratch block. */
14-
class Block {
15-
// Monitor blocks are sensor blocks.
16-
// Opcodes are like the unique identifier for blocks: move () steps might have something like move_steps
17-
Block(const std::string &id, const std::string &opcode, bool is_sensor_block = false);
18-
19-
Block *next() const;
20-
const std::string &next_id() const;
21-
void set_next(Block *block);
22-
void set_next_id(const std::string &next_id);
23-
24-
Block *parent() const;
25-
const std::string &parent_id();
26-
void set_parent(Block *block);
27-
void set_parent_id(const std::string &parent_id);
28-
29-
const std::vector<std::shared_ptr<Field>> &get_fields() const;
30-
}
31-
}
11+
namespace scratch {
12+
/**
13+
* @brief Represents a block in a Scratch project.
14+
*
15+
* A block is a fundamental unit in Scratch, representing a command or control structure.
16+
* It can have fields and may be connected to other blocks.
17+
*/
18+
class Block {
19+
protected:
20+
/**
21+
* @brief The block's opcode. This is a string that tells Scratch what kind of block it is.
22+
* This is different from an ID, which is really just Scratch's way of keeping count of what block we're at!
23+
*/
24+
std::string opcode;
25+
/**
26+
* @brief The block's ID. This is a unique identifier for the block within the project.
27+
*/
28+
std::string id;
29+
/**
30+
* @brief The block's parent ID. This is the ID of the block that contains this block, if any.
31+
* For example, if blocks are nested, the parent ID would be the ID of the outer block.
32+
* This field is optional, as not all blocks have a parent.
33+
*/
34+
std::optional<std::string> parent_id;
35+
36+
std::vector<std::shared_ptr<Field>> fields; ///< The fields associated with this block.
37+
std::vector<std::shared_ptr<Block>> inputs; ///< The input blocks connected to this block.
38+
39+
public:
40+
/**
41+
* @brief Constructs a Block with specified opcode and id.
42+
* @param opcode The opcode of the block.
43+
* @param id The ID of the block.
44+
* @param parent_id The parent ID of the block, if any.
45+
*/
46+
Block(const std::string& opcode, const std::string& id, const std::optional<std::string>& parent_id = std::nullopt)
47+
: opcode(opcode), id(id), parent_id(parent_id) {}
48+
49+
/**
50+
* @brief Constructs a Block with specified opcode, id, fields, and inputs.
51+
* @param opcode The opcode of the block.
52+
* @param id The ID of the block.
53+
* @param fields The fields associated with the block.
54+
* @param inputs The input blocks connected to this block.
55+
* @param parent_id The parent ID of the block, if any.
56+
*/
57+
Block(const std::string &opcode, const std::string &id, const std::vector<std::shared_ptr<Field>>& fields,
58+
const std::vector<std::shared_ptr<Block>>& inputs,
59+
const std::optional<std::string>& parent_id = std::nullopt)
60+
: opcode(opcode), id(id), fields(fields), inputs(inputs), parent_id(parent_id) {}
61+
62+
const std::string &get_opcode() const;
63+
64+
const std::vector<std::shared_ptr<Field>>& get_fields() const;
65+
const std::vector<std::shared_ptr<Block>>& get_inputs() const;
66+
67+
inline void add_field(const std::shared_ptr<Field>& field) {
68+
fields.push_back(field);
69+
}
70+
inline void add_input(const std::shared_ptr<Block>& input) {
71+
inputs.push_back(input);
72+
}
73+
};
74+
};

0 commit comments

Comments
 (0)