Skip to content

Commit 3ced3d2

Browse files
committed
Add API for dropdown menus
1 parent 66f8b8e commit 3ced3d2

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

include/scratchcpp/input.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class LIBSCRATCHCPP_EXPORT Input
4545
void setValueBlock(std::shared_ptr<Block> block);
4646
void setValueBlockId(const std::string &id);
4747

48+
bool pointsToDropdownMenu() const;
49+
std::string selectedMenuItem() const;
50+
4851
private:
4952
spimpl::unique_impl_ptr<InputPrivate> impl;
5053
};

src/scratch/input.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <iostream>
44
#include <scratchcpp/input.h>
5+
#include <scratchcpp/field.h>
56
#include <scratchcpp/variable.h>
67
#include <scratchcpp/block.h>
78

@@ -94,3 +95,35 @@ void Input::setValueBlockId(const std::string &id)
9495
impl->primaryValue.setValueBlock(nullptr);
9596
impl->primaryValue.setValueBlockId(id);
9697
}
98+
99+
/*!
100+
* Returns true if the input points to a dropdown menu.\n
101+
* (if type() == Type::Shadow and valueBlock() points to a block with a single field which does not point to an entity)
102+
*/
103+
bool Input::pointsToDropdownMenu() const
104+
{
105+
auto block = valueBlock();
106+
107+
if ((impl->type != Type::Shadow) || !block)
108+
return false;
109+
110+
const auto &fields = block->fields();
111+
112+
if (fields.size() != 1)
113+
return false;
114+
115+
auto field = fields[0];
116+
return (field && !field->valuePtr() && (field->valueId() == ""));
117+
}
118+
119+
/*!
120+
* Returns the selected item in the dropdown menu.\n
121+
* Works only pointsToDropdownMenu() is true.
122+
*/
123+
std::string Input::selectedMenuItem() const
124+
{
125+
if (!pointsToDropdownMenu())
126+
return "";
127+
128+
return valueBlock()->fieldAt(0)->value().toString();
129+
}

test/scratch_classes/input_test.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <scratchcpp/input.h>
22
#include <scratchcpp/inputvalue.h>
3+
#include <scratchcpp/field.h>
34
#include <scratchcpp/block.h>
5+
#include <scratchcpp/variable.h>
46

57
#include "../common.h"
68

@@ -64,3 +66,75 @@ TEST(InputTest, ValueBlock)
6466
ASSERT_EQ(input.valueBlock(), nullptr);
6567
ASSERT_EQ(input.valueBlockId(), "");
6668
}
69+
70+
TEST(InputTest, SelectedMenuItem)
71+
{
72+
// Test with Shadow type
73+
Input input1("", Input::Type::Shadow);
74+
ASSERT_FALSE(input1.pointsToDropdownMenu());
75+
ASSERT_TRUE(input1.selectedMenuItem().empty());
76+
77+
auto block1 = std::make_shared<Block>("abc", "");
78+
input1.setValueBlock(block1);
79+
ASSERT_FALSE(input1.pointsToDropdownMenu());
80+
ASSERT_TRUE(input1.selectedMenuItem().empty());
81+
82+
auto field1 = std::make_shared<Field>("OPTION1", "");
83+
block1->addField(field1);
84+
ASSERT_TRUE(input1.pointsToDropdownMenu());
85+
ASSERT_TRUE(input1.selectedMenuItem().empty());
86+
87+
auto field2 = std::make_shared<Field>("OPTION2", "something");
88+
block1->addField(field2);
89+
ASSERT_FALSE(input1.pointsToDropdownMenu());
90+
ASSERT_TRUE(input1.selectedMenuItem().empty());
91+
92+
auto block2 = std::make_shared<Block>("def", "");
93+
input1.setValueBlock(block2);
94+
95+
field1 = std::make_shared<Field>("OPTION1", "something");
96+
block2->addField(field1);
97+
ASSERT_TRUE(input1.pointsToDropdownMenu());
98+
ASSERT_EQ(input1.selectedMenuItem(), "something");
99+
100+
auto variable = std::make_shared<Variable>("", "");
101+
field1->setValuePtr(variable);
102+
ASSERT_FALSE(input1.pointsToDropdownMenu());
103+
ASSERT_TRUE(input1.selectedMenuItem().empty());
104+
105+
field1->setValuePtr(nullptr);
106+
ASSERT_TRUE(input1.pointsToDropdownMenu());
107+
ASSERT_EQ(input1.selectedMenuItem(), "something");
108+
109+
field2 = std::make_shared<Field>("OPTION2", "hello");
110+
block2->addField(field2);
111+
ASSERT_FALSE(input1.pointsToDropdownMenu());
112+
ASSERT_TRUE(input1.selectedMenuItem().empty());
113+
114+
// Test with NoShadow type
115+
Input input2("", Input::Type::NoShadow);
116+
ASSERT_FALSE(input2.pointsToDropdownMenu());
117+
ASSERT_TRUE(input2.selectedMenuItem().empty());
118+
119+
auto block3 = std::make_shared<Block>("ghi", "");
120+
input2.setValueBlock(block3);
121+
ASSERT_FALSE(input2.pointsToDropdownMenu());
122+
ASSERT_TRUE(input2.selectedMenuItem().empty());
123+
124+
block3->addField(field1);
125+
ASSERT_FALSE(input2.pointsToDropdownMenu());
126+
ASSERT_TRUE(input2.selectedMenuItem().empty());
127+
128+
// Test with ObscuredShadow type
129+
Input input3("", Input::Type::ObscuredShadow);
130+
ASSERT_FALSE(input3.pointsToDropdownMenu());
131+
ASSERT_TRUE(input3.selectedMenuItem().empty());
132+
133+
input3.setValueBlock(block3);
134+
ASSERT_FALSE(input3.pointsToDropdownMenu());
135+
ASSERT_TRUE(input3.selectedMenuItem().empty());
136+
137+
block3->addField(field1);
138+
ASSERT_FALSE(input3.pointsToDropdownMenu());
139+
ASSERT_TRUE(input3.selectedMenuItem().empty());
140+
}

0 commit comments

Comments
 (0)