Skip to content

Commit 1a78e0d

Browse files
committed
Add addKeyPressScript method to IEngine
1 parent 9b33a2a commit 1a78e0d

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

include/scratchcpp/iengine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
210210
/* Registers the given "when I start as clone" script. */
211211
virtual void addCloneInitScript(std::shared_ptr<Block> hatBlock) = 0;
212212

213+
/* Registers the given "when key pressed" script. */
214+
virtual void addKeyPressScript(std::shared_ptr<Block> hatBlock, std::string keyName) = 0;
215+
213216
/*! Returns the list of targets. */
214217
virtual const std::vector<std::shared_ptr<Target>> &targets() const = 0;
215218

src/engine/internal/engine.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,22 @@ void Engine::addCloneInitScript(std::shared_ptr<Block> hatBlock)
620620
}
621621
}
622622

623+
void Engine::addKeyPressScript(std::shared_ptr<Block> hatBlock, std::string keyName)
624+
{
625+
std::transform(keyName.begin(), keyName.end(), keyName.begin(), ::tolower);
626+
Script *script = m_scripts[hatBlock].get();
627+
auto it = m_whenKeyPressedScripts.find(keyName);
628+
629+
if (it == m_whenKeyPressedScripts.cend())
630+
m_whenKeyPressedScripts[keyName] = { script };
631+
else {
632+
auto &scripts = it->second;
633+
634+
if (std::find(scripts.begin(), scripts.end(), script) == scripts.cend())
635+
scripts.push_back(script);
636+
}
637+
}
638+
623639
const std::vector<std::shared_ptr<Target>> &Engine::targets() const
624640
{
625641
return m_targets;

src/engine/internal/engine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class Engine : public IEngine
8383
int findBroadcastById(const std::string &broadcastId) const override;
8484

8585
void addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, Broadcast *broadcast) override;
86-
8786
void addCloneInitScript(std::shared_ptr<Block> hatBlock) override;
87+
void addKeyPressScript(std::shared_ptr<Block> hatBlock, std::string keyName) override;
8888

8989
const std::vector<std::shared_ptr<Target>> &targets() const override;
9090
void setTargets(const std::vector<std::shared_ptr<Target>> &newTargets) override;
@@ -120,6 +120,7 @@ class Engine : public IEngine
120120
std::unordered_map<Broadcast *, std::vector<Script *>> m_broadcastMap;
121121
std::unordered_map<Broadcast *, std::vector<std::pair<VirtualMachine *, VirtualMachine *>>> m_runningBroadcastMap; // source script, "when received" script
122122
std::unordered_map<Target *, std::vector<Script *>> m_cloneInitScriptsMap; // target (no clones), "when I start as a clone" scripts
123+
std::unordered_map<std::string, std::vector<Script *>> m_whenKeyPressedScripts; // key name, "when key pressed" scripts
123124
std::vector<std::string> m_extensions;
124125
std::vector<std::shared_ptr<VirtualMachine>> m_runningScripts;
125126
std::vector<VirtualMachine *> m_scriptsToRemove;

test/mocks/enginemock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class EngineMock : public IEngine
6969
MOCK_METHOD(int, findBroadcastById, (const std::string &), (const, override));
7070

7171
MOCK_METHOD(void, addBroadcastScript, (std::shared_ptr<Block>, Broadcast *), (override));
72-
7372
MOCK_METHOD(void, addCloneInitScript, (std::shared_ptr<Block>), (override));
73+
MOCK_METHOD(void, addKeyPressScript, (std::shared_ptr<Block>, std::string), (override));
7474

7575
MOCK_METHOD(const std::vector<std::shared_ptr<Target>> &, targets, (), (const, override));
7676
MOCK_METHOD(void, setTargets, (const std::vector<std::shared_ptr<Target>> &), (override));

0 commit comments

Comments
 (0)