Skip to content

Add KeyEvent overload for IEngine::setKeyState() #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Variable;
class List;
class Script;
class ITimer;
class KeyEvent;

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

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

/*! Sets the state of the given key. */
virtual void setKeyState(const KeyEvent &event, bool pressed) = 0;

/*! Sets whether any key is pressed (use this for any key, even for unsupported keys). */
virtual void setAnyKeyPressed(bool pressed) = 0;

Expand Down
5 changes: 5 additions & 0 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,11 @@ bool Engine::keyPressed(const std::string &name) const
void Engine::setKeyState(const std::string &name, bool pressed)
{
KeyEvent event(name);
setKeyState(event, pressed);
}

void Engine::setKeyState(const KeyEvent &event, bool pressed)
{
m_keyMap[event.name()] = pressed;

// Start "when key pressed" scripts
Expand Down
1 change: 1 addition & 0 deletions src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Engine : public IEngine

bool keyPressed(const std::string &name) const override;
void setKeyState(const std::string &name, bool pressed) override;
void setKeyState(const KeyEvent &event, bool pressed) override;
void setAnyKeyPressed(bool pressed) override;

double mouseX() const override;
Expand Down
60 changes: 49 additions & 11 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <scratchcpp/stage.h>
#include <scratchcpp/variable.h>
#include <scratchcpp/list.h>
#include <scratchcpp/keyevent.h>
#include <timermock.h>
#include <clockmock.h>
#include <thread>
Expand Down Expand Up @@ -192,6 +193,7 @@ TEST(EngineTest, KeyState)
ASSERT_FALSE(engine.keyPressed("up arrow"));
ASSERT_FALSE(engine.keyPressed("any"));

// Key name
engine.setKeyState("A", true);
ASSERT_TRUE(engine.keyPressed("a"));
ASSERT_FALSE(engine.keyPressed("b"));
Expand Down Expand Up @@ -263,6 +265,20 @@ TEST(EngineTest, KeyState)

engine.setAnyKeyPressed(false);
ASSERT_FALSE(engine.keyPressed("any"));

// Key object
engine.setKeyState(KeyEvent("A"), true);
ASSERT_TRUE(engine.keyPressed("a"));
ASSERT_FALSE(engine.keyPressed("b"));
ASSERT_FALSE(engine.keyPressed("up arrow"));
ASSERT_TRUE(engine.keyPressed("any"));

engine.setKeyState(KeyEvent("up arrow"), true);
ASSERT_TRUE(engine.keyPressed("a"));
ASSERT_FALSE(engine.keyPressed("b"));
ASSERT_TRUE(engine.keyPressed("up arrow"));
ASSERT_FALSE(engine.keyPressed("U"));
ASSERT_TRUE(engine.keyPressed("any"));
}

TEST(EngineTest, WhenKeyPressed)
Expand Down Expand Up @@ -335,21 +351,43 @@ TEST(EngineTest, WhenKeyPressed)
ASSERT_VAR(stage, "4_pressed");
ASSERT_EQ(GET_VAR(stage, "4_pressed")->value().toInt(), 0);

// right arrow - key object
engine->setKeyState(KeyEvent("right arrow"), true);
p.run();

ASSERT_VAR(stage, "right_arrow_pressed");
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
engine->setKeyState("right arrow", false);
p.run();

ASSERT_VAR(stage, "space_pressed");
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "right_arrow_pressed");
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
ASSERT_VAR(stage, "any_key_pressed");
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 3);
ASSERT_VAR(stage, "a_pressed");
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 0);
ASSERT_VAR(stage, "x_pressed");
ASSERT_EQ(GET_VAR(stage, "x_pressed")->value().toInt(), 0);
ASSERT_VAR(stage, "4_pressed");
ASSERT_EQ(GET_VAR(stage, "4_pressed")->value().toInt(), 0);

// any key
engine->setAnyKeyPressed(true);
p.run();

ASSERT_VAR(stage, "any_key_pressed");
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 3);
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 4);
engine->setAnyKeyPressed(false);
p.run();

ASSERT_VAR(stage, "space_pressed");
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "right_arrow_pressed");
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
ASSERT_VAR(stage, "any_key_pressed");
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 3);
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 4);
ASSERT_VAR(stage, "a_pressed");
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 0);
ASSERT_VAR(stage, "x_pressed");
Expand All @@ -369,9 +407,9 @@ TEST(EngineTest, WhenKeyPressed)
ASSERT_VAR(stage, "space_pressed");
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "right_arrow_pressed");
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
ASSERT_VAR(stage, "any_key_pressed");
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 4);
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 5);
ASSERT_VAR(stage, "a_pressed");
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "x_pressed");
Expand All @@ -391,9 +429,9 @@ TEST(EngineTest, WhenKeyPressed)
ASSERT_VAR(stage, "space_pressed");
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "right_arrow_pressed");
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
ASSERT_VAR(stage, "any_key_pressed");
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 5);
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 6);
ASSERT_VAR(stage, "a_pressed");
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "x_pressed");
Expand All @@ -413,9 +451,9 @@ TEST(EngineTest, WhenKeyPressed)
ASSERT_VAR(stage, "space_pressed");
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "right_arrow_pressed");
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
ASSERT_VAR(stage, "any_key_pressed");
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 6);
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 7);
ASSERT_VAR(stage, "a_pressed");
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "x_pressed");
Expand All @@ -439,9 +477,9 @@ TEST(EngineTest, WhenKeyPressed)
ASSERT_VAR(stage, "space_pressed");
ASSERT_EQ(GET_VAR(stage, "space_pressed")->value().toInt(), 2);
ASSERT_VAR(stage, "right_arrow_pressed");
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 1);
ASSERT_EQ(GET_VAR(stage, "right_arrow_pressed")->value().toInt(), 2);
ASSERT_VAR(stage, "any_key_pressed");
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 8);
ASSERT_EQ(GET_VAR(stage, "any_key_pressed")->value().toInt(), 9);
ASSERT_VAR(stage, "a_pressed");
ASSERT_EQ(GET_VAR(stage, "a_pressed")->value().toInt(), 1);
ASSERT_VAR(stage, "x_pressed");
Expand Down
1 change: 1 addition & 0 deletions test/mocks/enginemock.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class EngineMock : public IEngine

MOCK_METHOD(bool, keyPressed, (const std::string &), (const, override));
MOCK_METHOD(void, setKeyState, (const std::string &, bool), (override));
MOCK_METHOD(void, setKeyState, (const KeyEvent &, bool), (override));
MOCK_METHOD(void, setAnyKeyPressed, (bool), (override));

MOCK_METHOD(double, mouseX, (), (const, override));
Expand Down