Skip to content

Commit 5bdc44b

Browse files
committed
Engine: Find broadcasts in getEntity()
1 parent 9e74242 commit 5bdc44b

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/engine/engine.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ void Engine::setBroadcasts(const std::vector<std::shared_ptr<Broadcast>> &broadc
305305
}
306306

307307
/*! Returns the broadcast at index. */
308-
Broadcast *Engine::broadcastAt(int index) const
308+
std::shared_ptr<Broadcast> Engine::broadcastAt(int index) const
309309
{
310-
return m_broadcasts[index].get();
310+
return m_broadcasts[index];
311311
}
312312

313313
/*! Returns the index of the broadcast with the given name. */
@@ -322,6 +322,29 @@ int Engine::findBroadcast(const std::string &broadcastName) const
322322
return -1;
323323
}
324324

325+
/*! Returns the index of the broadcast with the given ID. */
326+
int Engine::findBroadcastById(const std::string &broadcastId) const
327+
{
328+
int i = 0;
329+
for (auto broadcast : m_broadcasts) {
330+
if (broadcast->id() == broadcastId)
331+
return i;
332+
i++;
333+
}
334+
return -1;
335+
}
336+
337+
/*! Registers the broadcast script. */
338+
void libscratchcpp::Engine::addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, std::shared_ptr<Broadcast> broadcast)
339+
{
340+
auto id = findBroadcast(broadcast->name());
341+
if (m_broadcastMap.count(id) == 1) {
342+
std::vector<VirtualMachine *> &scripts = m_broadcastMap[id];
343+
scripts.push_back(m_scripts[whenReceivedBlock].get());
344+
} else
345+
m_broadcastMap[id] = { m_scripts[whenReceivedBlock].get() };
346+
}
347+
325348
/*! Returns the list of targets. */
326349
std::vector<std::shared_ptr<Target>> Engine::targets() const
327350
{
@@ -431,6 +454,19 @@ std::shared_ptr<List> Engine::getList(std::string id)
431454
return nullptr;
432455
}
433456

457+
/*! Returns the broadcast with the given ID. */
458+
std::shared_ptr<Broadcast> Engine::getBroadcast(std::string id)
459+
{
460+
if (id.empty())
461+
return nullptr;
462+
463+
int index = findBroadcastById(id);
464+
if (index != -1)
465+
return broadcastAt(index);
466+
467+
return nullptr;
468+
}
469+
434470
/*! Returns the entity with the given ID. \see IEntity */
435471
std::shared_ptr<IEntity> Engine::getEntity(std::string id)
436472
{
@@ -449,5 +485,10 @@ std::shared_ptr<IEntity> Engine::getEntity(std::string id)
449485
if (list)
450486
return std::static_pointer_cast<IEntity>(list);
451487

488+
// Broadcasts
489+
auto broadcast = getBroadcast(id);
490+
if (broadcast)
491+
return std::static_pointer_cast<IEntity>(broadcast);
492+
452493
return nullptr;
453494
}

src/engine/engine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ class LIBSCRATCHCPP_EXPORT Engine
5050

5151
std::vector<std::shared_ptr<Broadcast>> broadcasts() const;
5252
void setBroadcasts(const std::vector<std::shared_ptr<Broadcast>> &broadcasts);
53-
Broadcast *broadcastAt(int index) const;
53+
std::shared_ptr<Broadcast> broadcastAt(int index) const;
5454
int findBroadcast(const std::string &broadcastName) const;
55+
int findBroadcastById(const std::string &broadcastId) const;
5556

5657
std::vector<std::shared_ptr<Target>> targets() const;
5758
void setTargets(const std::vector<std::shared_ptr<Target>> &newTargets);
@@ -65,6 +66,7 @@ class LIBSCRATCHCPP_EXPORT Engine
6566
std::shared_ptr<Block> getBlock(std::string id);
6667
std::shared_ptr<Variable> getVariable(std::string id);
6768
std::shared_ptr<List> getList(std::string id);
69+
std::shared_ptr<Broadcast> getBroadcast(std::string id);
6870
std::shared_ptr<IEntity> getEntity(std::string id);
6971
std::vector<std::shared_ptr<IBlockSection>> m_sections;
7072
std::vector<std::shared_ptr<Target>> m_targets;

0 commit comments

Comments
 (0)