Skip to content

Commit 54b5a4d

Browse files
committed
Add API for dropdown menus
1 parent 66f8b8e commit 54b5a4d

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <scratchcpp/input.h>
22
#include <scratchcpp/inputvalue.h>
3+
#include <scratchcpp/field.h>
34
#include <scratchcpp/block.h>
45

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

0 commit comments

Comments
 (0)