Skip to content

Commit 7901c1a

Browse files
authored
Merge pull request #376 from scratchcpp/setkeystate_keyevent
Add KeyEvent overload for IEngine::setKeyState()
2 parents ed26594 + 83eea77 commit 7901c1a

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

include/scratchcpp/iengine.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Variable;
2222
class List;
2323
class Script;
2424
class ITimer;
25+
class KeyEvent;
2526

2627
/*!
2728
* \brief The IEngine interface provides an API for running Scratch projects.
@@ -115,9 +116,12 @@ class LIBSCRATCHCPP_EXPORT IEngine
115116
/*! Returns true if the given key is pressed. */
116117
virtual bool keyPressed(const std::string &name) const = 0;
117118

118-
/*! Sets the state of the given key. */
119+
/*! Sets the state of the key with the given name. */
119120
virtual void setKeyState(const std::string &name, bool pressed) = 0;
120121

122+
/*! Sets the state of the given key. */
123+
virtual void setKeyState(const KeyEvent &event, bool pressed) = 0;
124+
121125
/*! Sets whether any key is pressed (use this for any key, even for unsupported keys). */
122126
virtual void setAnyKeyPressed(bool pressed) = 0;
123127

src/engine/internal/engine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ bool Engine::keyPressed(const std::string &name) const
546546
void Engine::setKeyState(const std::string &name, bool pressed)
547547
{
548548
KeyEvent event(name);
549+
setKeyState(event, pressed);
550+
}
551+
552+
void Engine::setKeyState(const KeyEvent &event, bool pressed)
553+
{
549554
m_keyMap[event.name()] = pressed;
550555

551556
// Start "when key pressed" scripts

src/engine/internal/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Engine : public IEngine
5555

5656
bool keyPressed(const std::string &name) const override;
5757
void setKeyState(const std::string &name, bool pressed) override;
58+
void setKeyState(const KeyEvent &event, bool pressed) override;
5859
void setAnyKeyPressed(bool pressed) override;
5960

6061
double mouseX() const override;

test/engine/engine_test.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <scratchcpp/stage.h>
66
#include <scratchcpp/variable.h>
77
#include <scratchcpp/list.h>
8+
#include <scratchcpp/keyevent.h>
89
#include <timermock.h>
910
#include <clockmock.h>
1011
#include <thread>
@@ -192,6 +193,7 @@ TEST(EngineTest, KeyState)
192193
ASSERT_FALSE(engine.keyPressed("up arrow"));
193194
ASSERT_FALSE(engine.keyPressed("any"));
194195

196+
// Key name
195197
engine.setKeyState("A", true);
196198
ASSERT_TRUE(engine.keyPressed("a"));
197199
ASSERT_FALSE(engine.keyPressed("b"));
@@ -263,6 +265,20 @@ TEST(EngineTest, KeyState)
263265

264266
engine.setAnyKeyPressed(false);
265267
ASSERT_FALSE(engine.keyPressed("any"));
268+
269+
// Key object
270+
engine.setKeyState(KeyEvent("A"), true);
271+
ASSERT_TRUE(engine.keyPressed("a"));
272+
ASSERT_FALSE(engine.keyPressed("b"));
273+
ASSERT_FALSE(engine.keyPressed("up arrow"));
274+
ASSERT_TRUE(engine.keyPressed("any"));
275+
276+
engine.setKeyState(KeyEvent("up arrow"), true);
277+
ASSERT_TRUE(engine.keyPressed("a"));
278+
ASSERT_FALSE(engine.keyPressed("b"));
279+
ASSERT_TRUE(engine.keyPressed("up arrow"));
280+
ASSERT_FALSE(engine.keyPressed("U"));
281+
ASSERT_TRUE(engine.keyPressed("any"));
266282
}
267283

268284
TEST(EngineTest, WhenKeyPressed)
@@ -335,21 +351,43 @@ TEST(EngineTest, WhenKeyPressed)
335351
ASSERT_VAR(stage, "4_pressed");
336352
ASSERT_EQ(GET_VAR(stage, "4_pressed")->value().toInt(), 0);
337353

354+
// right arrow - key object
355+
engine->setKeyState(KeyEvent("right arrow"), true);
356+
p.run();
357+
358+
ASSERT_VAR(stage, "right_arrow_pressed");
359+
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
360+
engine->setKeyState("right arrow", false);
361+
p.run();
362+
363+
ASSERT_VAR(stage, "space_pressed");
364+
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
365+
ASSERT_VAR(stage, "right_arrow_pressed");
366+
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
367+
ASSERT_VAR(stage, "any_key_pressed");
368+
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 3);
369+
ASSERT_VAR(stage, "a_pressed");
370+
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 0);
371+
ASSERT_VAR(stage, "x_pressed");
372+
ASSERT_EQ(GET_VAR(stage, "x_pressed")->value().toInt(), 0);
373+
ASSERT_VAR(stage, "4_pressed");
374+
ASSERT_EQ(GET_VAR(stage, "4_pressed")->value().toInt(), 0);
375+
338376
// any key
339377
engine->setAnyKeyPressed(true);
340378
p.run();
341379

342380
ASSERT_VAR(stage, "any_key_pressed");
343-
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 3);
381+
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 4);
344382
engine->setAnyKeyPressed(false);
345383
p.run();
346384

347385
ASSERT_VAR(stage, "space_pressed");
348386
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
349387
ASSERT_VAR(stage, "right_arrow_pressed");
350-
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
388+
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
351389
ASSERT_VAR(stage, "any_key_pressed");
352-
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 3);
390+
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 4);
353391
ASSERT_VAR(stage, "a_pressed");
354392
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 0);
355393
ASSERT_VAR(stage, "x_pressed");
@@ -369,9 +407,9 @@ TEST(EngineTest, WhenKeyPressed)
369407
ASSERT_VAR(stage, "space_pressed");
370408
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
371409
ASSERT_VAR(stage, "right_arrow_pressed");
372-
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
410+
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
373411
ASSERT_VAR(stage, "any_key_pressed");
374-
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 4);
412+
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 5);
375413
ASSERT_VAR(stage, "a_pressed");
376414
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
377415
ASSERT_VAR(stage, "x_pressed");
@@ -391,9 +429,9 @@ TEST(EngineTest, WhenKeyPressed)
391429
ASSERT_VAR(stage, "space_pressed");
392430
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
393431
ASSERT_VAR(stage, "right_arrow_pressed");
394-
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
432+
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
395433
ASSERT_VAR(stage, "any_key_pressed");
396-
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 5);
434+
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 6);
397435
ASSERT_VAR(stage, "a_pressed");
398436
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
399437
ASSERT_VAR(stage, "x_pressed");
@@ -413,9 +451,9 @@ TEST(EngineTest, WhenKeyPressed)
413451
ASSERT_VAR(stage, "space_pressed");
414452
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
415453
ASSERT_VAR(stage, "right_arrow_pressed");
416-
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
454+
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
417455
ASSERT_VAR(stage, "any_key_pressed");
418-
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 6);
456+
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 7);
419457
ASSERT_VAR(stage, "a_pressed");
420458
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
421459
ASSERT_VAR(stage, "x_pressed");
@@ -439,9 +477,9 @@ TEST(EngineTest, WhenKeyPressed)
439477
ASSERT_VAR(stage, "space_pressed");
440478
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 2);
441479
ASSERT_VAR(stage, "right_arrow_pressed");
442-
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
480+
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
443481
ASSERT_VAR(stage, "any_key_pressed");
444-
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 8);
482+
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 9);
445483
ASSERT_VAR(stage, "a_pressed");
446484
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
447485
ASSERT_VAR(stage, "x_pressed");

test/mocks/enginemock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class EngineMock : public IEngine
4040

4141
MOCK_METHOD(bool, keyPressed, (const std::string &), (const, override));
4242
MOCK_METHOD(void, setKeyState, (const std::string &, bool), (override));
43+
MOCK_METHOD(void, setKeyState, (const KeyEvent &, bool), (override));
4344
MOCK_METHOD(void, setAnyKeyPressed, (bool), (override));
4445

4546
MOCK_METHOD(double, mouseX, (), (const, override));

0 commit comments

Comments
 (0)