Skip to content

Commit 83e45f2

Browse files
committed
Add classes for different input types
1 parent e74ebba commit 83e45f2

File tree

13 files changed

+197
-49
lines changed

13 files changed

+197
-49
lines changed

src/internal/scratch3reader.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include "scratch3reader.h"
44
#include "../scratch/sprite.h"
55
#include "../scratch/stage.h"
6+
#include "../scratch/internal/shadowinput.h"
7+
#include "../scratch/internal/variableinput.h"
8+
#include "../scratch/internal/listinput.h"
9+
#include "../scratch/internal/blockinput.h"
610
#include "reader_common.h"
711

812
using namespace libscratchcpp;
@@ -79,8 +83,29 @@ bool Scratch3Reader::load()
7983
auto inputs = blockInfo["inputs"];
8084
for (json::iterator it = inputs.begin(); it != inputs.end(); ++it) {
8185
auto inputInfo = it.value();
82-
auto input = std::make_shared<Input>(it.key(), static_cast<Input::Type>(inputInfo[0]));
86+
auto type = static_cast<Input::Type>(inputInfo[0]);
8387
auto primary = inputInfo[1];
88+
std::shared_ptr<Input> input;
89+
if (type == Input::Type::Shadow)
90+
input = std::make_shared<ShadowInput>(it.key(), type);
91+
else {
92+
if (primary.is_array()) {
93+
auto valueType = static_cast<InputValue::Type>(primary[0]);
94+
switch (valueType) {
95+
case InputValue::Type::Variable:
96+
input = std::make_shared<VariableInput>(it.key(), type);
97+
break;
98+
case InputValue::Type::List:
99+
input = std::make_shared<ListInput>(it.key(), type);
100+
break;
101+
default:
102+
assert(false); // a block can only be obscured by a variable or a list in this case
103+
break;
104+
}
105+
} else
106+
input = std::make_shared<BlockInput>(it.key(), type);
107+
}
108+
84109
if (primary.is_array()) {
85110
input->setPrimaryValue(jsonToValue(primary[1]));
86111
input->primaryValue()->setType(static_cast<InputValue::Type>(primary[0]));

src/scratch/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ target_sources(scratchcpp
3333
broadcast.h
3434
runningscript.h
3535
)
36+
37+
add_subdirectory(internal)

src/scratch/input.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,51 +51,6 @@ InputValue *Input::secondaryValue()
5151
return &m_secondaryValue;
5252
}
5353

54-
/*!
55-
* Returns the value of the input.\n
56-
* If there's an obscured shadow, the value will
57-
* be retrieved from the block that obscures the shadow.\n
58-
* Use secondaryValue() to get the value of the shadow.
59-
*/
60-
Value Input::value() const
61-
{
62-
switch (m_type) {
63-
case Type::Shadow:
64-
return m_primaryValue.value();
65-
case Type::NoShadow:
66-
case Type::ObscuredShadow: {
67-
switch (m_primaryValue.type()) {
68-
case InputValue::Type::Variable: {
69-
auto variable = std::static_pointer_cast<Variable>(m_primaryValue.valuePtr());
70-
if (!variable) {
71-
std::cout << "warning: attempted to read a null variable" << std::endl;
72-
return Value();
73-
}
74-
return variable->value();
75-
}
76-
case InputValue::Type::List: {
77-
auto list = std::static_pointer_cast<List>(m_primaryValue.valuePtr());
78-
if (!list) {
79-
std::cout << "warning: attempted to read a null list" << std::endl;
80-
return Value();
81-
}
82-
return list->toString();
83-
}
84-
default: {
85-
auto block = m_primaryValue.valueBlock();
86-
if (!block) {
87-
if (m_type == Type::ObscuredShadow)
88-
std::cout << "warning: attempted to get value of an input shadow obscured by a null block" << std::endl;
89-
return Value();
90-
}
91-
return block->run();
92-
}
93-
}
94-
}
95-
}
96-
return Value();
97-
}
98-
9954
/*! Sets the primary value. */
10055
void Input::setPrimaryValue(Value value)
10156
{

src/scratch/input.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ class LIBSCRATCHCPP_EXPORT Input
3131
InputValue *primaryValue();
3232
InputValue *secondaryValue();
3333

34-
Value value() const;
34+
/*!
35+
* Returns the value of the input.\n
36+
* If there's an obscured shadow, the value will
37+
* be retrieved from the block that obscures the shadow.\n
38+
* Use secondaryValue() to get the value of the shadow.
39+
*/
40+
virtual Value value() const = 0;
3541

3642
void setPrimaryValue(Value value);
3743
void setSecondaryValue(Value value);
@@ -41,12 +47,14 @@ class LIBSCRATCHCPP_EXPORT Input
4147
void setValueBlock(std::shared_ptr<Block> block);
4248
void setValueBlockId(std::string id);
4349

50+
protected:
51+
InputValue m_primaryValue;
52+
InputValue m_secondaryValue;
53+
4454
private:
4555
std::string m_name;
4656
int m_inputId = -1;
4757
Type m_type;
48-
InputValue m_primaryValue;
49-
InputValue m_secondaryValue;
5058
};
5159

5260
} // namespace libscratchcpp
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
target_sources(scratchcpp
2+
PRIVATE
3+
shadowinput.cpp
4+
shadowinput.h
5+
blockinput.cpp
6+
blockinput.h
7+
variableinput.cpp
8+
variableinput.h
9+
listinput.cpp
10+
listinput.h
11+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "blockinput.h"
4+
#include "../block.h"
5+
6+
namespace libscratchcpp
7+
{
8+
9+
BlockInput::BlockInput(std::string name, Type type) :
10+
Input(name, type)
11+
{
12+
}
13+
14+
Value BlockInput::value() const
15+
{
16+
return m_primaryValue.valueBlock()->run();
17+
}
18+
19+
} // namespace libscratchcpp

src/scratch/internal/blockinput.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "../input.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class BlockInput : public Input
11+
{
12+
public:
13+
BlockInput(std::string name, Type type);
14+
15+
Value value() const override;
16+
};
17+
18+
} // namespace libscratchcpp

src/scratch/internal/listinput.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "listinput.h"
4+
#include "../list.h"
5+
6+
namespace libscratchcpp
7+
{
8+
9+
ListInput::ListInput(std::string name, Type type) :
10+
Input(name, type)
11+
{
12+
}
13+
14+
Value ListInput::value() const
15+
{
16+
return reinterpret_cast<List *>(m_primaryValue.valuePtr().get())->toString();
17+
}
18+
19+
} // namespace libscratchcpp

src/scratch/internal/listinput.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "../input.h"
6+
7+
namespace libscratchcpp
8+
{
9+
10+
class ListInput : public Input
11+
{
12+
public:
13+
ListInput(std::string name, Type type);
14+
15+
Value value() const override;
16+
};
17+
18+
} // namespace libscratchcpp
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "shadowinput.h"
4+
5+
namespace libscratchcpp
6+
{
7+
8+
ShadowInput::ShadowInput(std::string name, Type type) :
9+
Input(name, type)
10+
{
11+
}
12+
13+
Value ShadowInput::value() const
14+
{
15+
return m_primaryValue.value();
16+
}
17+
18+
} // namespace libscratchcpp

0 commit comments

Comments
 (0)