Skip to content

Commit 8cecd7b

Browse files
committed
Add BlockPrototype class
1 parent d309623 commit 8cecd7b

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/scratch/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ target_sources(scratchcpp
55
value.cpp
66
list.cpp
77
block.cpp
8+
blockprototype.cpp
89
input.cpp
910
inputvalue.cpp
1011
field.cpp
@@ -21,6 +22,7 @@ target_sources(scratchcpp
2122
value.h
2223
list.h
2324
block.h
25+
blockprototype.h
2426
input.h
2527
inputvalue.h
2628
field.h

src/scratch/blockprototype.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "blockprototype.h"
4+
5+
using namespace libscratchcpp;
6+
7+
/*! Constructs BlockPrototype. */
8+
BlockPrototype::BlockPrototype()
9+
{
10+
}
11+
12+
/*! Constructs BlockPrototype. */
13+
BlockPrototype::BlockPrototype(const std::string &procCode)
14+
{
15+
setProcCode(procCode);
16+
}
17+
18+
/*! Returns the name of the custom block, including inputs. */
19+
const std::string &BlockPrototype::procCode() const
20+
{
21+
return m_procCode;
22+
}
23+
24+
/*! Sets the name of the custom block, including inputs. */
25+
void BlockPrototype::setProcCode(const std::string &newProcCode)
26+
{
27+
m_procCode = newProcCode;
28+
m_argumentDefaults.clear();
29+
m_argumentTypes.clear();
30+
bool arg = false;
31+
32+
for (auto c : m_procCode) {
33+
if (c == '%')
34+
arg = true;
35+
else if (arg) {
36+
switch (c) {
37+
case 's':
38+
m_argumentDefaults.push_back("");
39+
m_argumentTypes.push_back(ArgType::StringNum);
40+
break;
41+
42+
case 'b':
43+
m_argumentDefaults.push_back(false);
44+
m_argumentTypes.push_back(ArgType::Bool);
45+
break;
46+
47+
default:
48+
break;
49+
}
50+
}
51+
}
52+
}
53+
54+
/*! Returns the list of argument IDs. */
55+
const std::vector<std::string> &BlockPrototype::argumentIds() const
56+
{
57+
return m_argumentIds;
58+
}
59+
60+
/*! Sets the list of argument IDs. */
61+
void BlockPrototype::setArgumentIds(const std::vector<std::string> &newArgumentIds)
62+
{
63+
m_argumentIds = newArgumentIds;
64+
}
65+
66+
/*! Returns the list of argument names. */
67+
const std::vector<std::string> &BlockPrototype::argumentNames() const
68+
{
69+
return m_argumentNames;
70+
}
71+
72+
/*! Sets the list of argument names. */
73+
void BlockPrototype::setArgumentNames(const std::vector<std::string> &newArgumentNames)
74+
{
75+
m_argumentNames = newArgumentNames;
76+
}
77+
78+
/*! Returns the list of argument default values. */
79+
const std::vector<Value> &BlockPrototype::argumentDefaults() const
80+
{
81+
return m_argumentDefaults;
82+
}
83+
84+
/*! Returns the list of argument types. */
85+
const std::vector<BlockPrototype::ArgType> &BlockPrototype::argumentTypes() const
86+
{
87+
return m_argumentTypes;
88+
}
89+
90+
/*! Returns true if the block is set to run without screen refresh. */
91+
bool BlockPrototype::warp() const
92+
{
93+
return m_warp;
94+
}
95+
96+
/*! Sets whether to run the block without screen refresh. */
97+
void BlockPrototype::setWarp(bool newWarp)
98+
{
99+
m_warp = newWarp;
100+
}

src/scratch/blockprototype.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "value.h"
6+
#include <vector>
7+
8+
namespace libscratchcpp
9+
{
10+
11+
/*! \brief The BlockPrototype class represents the prototype of a custom block. */
12+
class LIBSCRATCHCPP_EXPORT BlockPrototype
13+
{
14+
public:
15+
enum class ArgType
16+
{
17+
StringNum,
18+
Bool
19+
};
20+
21+
BlockPrototype();
22+
BlockPrototype(const std::string &procCode);
23+
24+
const std::string &procCode() const;
25+
void setProcCode(const std::string &newProcCode);
26+
27+
const std::vector<std::string> &argumentIds() const;
28+
void setArgumentIds(const std::vector<std::string> &newArgumentIds);
29+
30+
const std::vector<std::string> &argumentNames() const;
31+
void setArgumentNames(const std::vector<std::string> &newArgumentNames);
32+
33+
const std::vector<Value> &argumentDefaults() const;
34+
35+
const std::vector<ArgType> &argumentTypes() const;
36+
37+
bool warp() const;
38+
void setWarp(bool newWarp);
39+
40+
private:
41+
std::string m_procCode;
42+
std::vector<std::string> m_argumentIds;
43+
std::vector<std::string> m_argumentNames;
44+
std::vector<Value> m_argumentDefaults;
45+
std::vector<ArgType> m_argumentTypes;
46+
bool m_warp = false;
47+
};
48+
49+
} // namespace libscratchcpp

0 commit comments

Comments
 (0)