Skip to content

Commit 086ca12

Browse files
committed
Add answer getter to engine
1 parent 6f9ebdf commit 086ca12

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

include/scratchcpp/iengine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Stage;
2424
class Variable;
2525
class List;
2626
class Script;
27+
class StringPtr;
2728
class Thread;
2829
class ITimer;
2930
class KeyEvent;
@@ -182,6 +183,12 @@ class LIBSCRATCHCPP_EXPORT IEngine
182183
/*! Call this when a target is clicked. */
183184
virtual void clickTarget(Target *target) = 0;
184185

186+
/*!
187+
* Returns the answer received from the user.
188+
* \see questionAnswered()
189+
*/
190+
virtual const StringPtr *answer() const = 0;
191+
185192
/*! Returns the stage width. */
186193
virtual unsigned int stageWidth() const = 0;
187194

src/engine/internal/engine.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,20 @@ Engine::Engine() :
5050
m_clock(Clock::instance().get()),
5151
m_audioEngine(IAudioEngine::instance())
5252
{
53+
m_answer = string_pool_new();
54+
string_assign_cstring(m_answer, "");
55+
56+
m_questionAnswered.connect([this](const std::string &answer) {
57+
// Update answer
58+
string_assign_cstring(m_answer, answer.c_str());
59+
});
5360
}
5461

5562
Engine::~Engine()
5663
{
5764
m_clones.clear();
5865
m_sortedDrawables.clear();
66+
string_pool_free(m_answer);
5967
}
6068

6169
void Engine::clear()
@@ -848,6 +856,11 @@ void Engine::clickTarget(Target *target)
848856
}
849857
}
850858

859+
const StringPtr *Engine::answer() const
860+
{
861+
return m_answer;
862+
}
863+
851864
unsigned int Engine::stageWidth() const
852865
{
853866
return m_stageWidth;

src/engine/internal/engine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Engine : public IEngine
8484

8585
void clickTarget(Target *target) override;
8686

87+
const StringPtr *answer() const override;
88+
8789
unsigned int stageWidth() const override;
8890
void setStageWidth(unsigned int width) override;
8991

@@ -273,6 +275,7 @@ class Engine : public IEngine
273275
double m_mouseX = 0;
274276
double m_mouseY = 0;
275277
bool m_mousePressed = false;
278+
StringPtr *m_answer = nullptr;
276279
unsigned int m_stageWidth = 480;
277280
unsigned int m_stageHeight = 360;
278281
int m_cloneLimit = 300;

test/engine/engine_test.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,8 +1694,19 @@ TEST(EngineTest, CreateMissingMonitors)
16941694
}
16951695
}
16961696

1697-
void questionFunction(const std::string &)
1697+
TEST(EngineTest, Answer)
16981698
{
1699+
Engine engine;
1700+
const StringPtr empty("");
1701+
const StringPtr test("test");
1702+
const StringPtr *answer = engine.answer();
1703+
1704+
ASSERT_TRUE(answer);
1705+
ASSERT_EQ(string_compare_case_sensitive(answer, &empty), 0);
1706+
1707+
engine.questionAnswered()("test");
1708+
answer = engine.answer();
1709+
ASSERT_EQ(string_compare_case_sensitive(answer, &test), 0);
16991710
}
17001711

17011712
TEST(EngineTest, Clones)

test/mocks/enginemock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class EngineMock : public IEngine
6666

6767
MOCK_METHOD(void, clickTarget, (Target * target), (override));
6868

69+
MOCK_METHOD(const StringPtr *, answer, (), (const, override));
70+
6971
MOCK_METHOD(unsigned int, stageWidth, (), (const, override));
7072
MOCK_METHOD(void, setStageWidth, (unsigned int), (override));
7173

0 commit comments

Comments
 (0)