Skip to content
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
41 changes: 20 additions & 21 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,24 +755,22 @@ std::shared_ptr<Broadcast> Engine::broadcastAt(int index) const

int Engine::findBroadcast(const std::string &broadcastName) const
{
int i = 0;
for (auto broadcast : m_broadcasts) {
if (broadcast->name() == broadcastName)
return i;
i++;
}
return -1;
auto it = std::find_if(m_broadcasts.begin(), m_broadcasts.end(), [broadcastName](std::shared_ptr<Broadcast> broadcast) { return broadcast->name() == broadcastName; });

if (it == m_broadcasts.end())
return -1;
else
return it - m_broadcasts.begin();
}

int Engine::findBroadcastById(const std::string &broadcastId) const
{
int i = 0;
for (auto broadcast : m_broadcasts) {
if (broadcast->id() == broadcastId)
return i;
i++;
}
return -1;
auto it = std::find_if(m_broadcasts.begin(), m_broadcasts.end(), [broadcastId](std::shared_ptr<Broadcast> broadcast) { return broadcast->id() == broadcastId; });

if (it == m_broadcasts.end())
return -1;
else
return it - m_broadcasts.begin();
}

void Engine::addGreenFlagScript(std::shared_ptr<Block> hatBlock)
Expand Down Expand Up @@ -856,13 +854,14 @@ Target *Engine::targetAt(int index) const

int Engine::findTarget(const std::string &targetName) const
{
int i = 0;
for (auto target : m_targets) {
if ((target->isStage() && targetName == "_stage_") || (!target->isStage() && target->name() == targetName))
return i;
i++;
}
return -1;
auto it = std::find_if(m_targets.begin(), m_targets.end(), [targetName](std::shared_ptr<Target> target) {
return ((target->isStage() && targetName == "_stage_") || (!target->isStage() && target->name() == targetName));
});

if (it == m_targets.end())
return -1;
else
return it - m_targets.begin();
}

void Engine::moveSpriteToFront(Sprite *sprite)
Expand Down
26 changes: 12 additions & 14 deletions src/scratch/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,12 @@ std::shared_ptr<Input> Block::inputAt(int index) const
/*! Returns the index of the input with the given name. */
int Block::findInput(const std::string &inputName) const
{
int i = 0;
for (auto input : impl->inputs) {
if (input->name() == inputName)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->inputs.begin(), impl->inputs.end(), [inputName](std::shared_ptr<Input> input) { return input->name() == inputName; });

if (it == impl->inputs.end())
return -1;
else
return it - impl->inputs.begin();
}

/*! Returns the input with the given ID. */
Expand Down Expand Up @@ -248,13 +247,12 @@ std::shared_ptr<Field> Block::fieldAt(int index) const
/*! Returns the index of the field with the given name. */
int Block::findField(const std::string &fieldName) const
{
int i = 0;
for (auto field : impl->fields) {
if (field->name() == fieldName)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->fields.begin(), impl->fields.end(), [fieldName](std::shared_ptr<Field> field) { return field->name() == fieldName; });

if (it == impl->fields.end())
return -1;
else
return it - impl->fields.begin();
}

/*! Returns the index of the field with the given ID. */
Expand Down
105 changes: 47 additions & 58 deletions src/scratch/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,23 @@ std::shared_ptr<Variable> Target::variableAt(int index) const
/*! Returns the index of the variable with the given name. */
int Target::findVariable(const std::string &variableName) const
{
int i = 0;
for (auto var : impl->variables) {
if (var->name() == variableName)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->variables.begin(), impl->variables.end(), [variableName](std::shared_ptr<Variable> variable) { return variable->name() == variableName; });

if (it == impl->variables.end())
return -1;
else
return it - impl->variables.begin();
}

/*! Returns the index of the variable with the given ID. */
int Target::findVariableById(const std::string &id) const
{
int i = 0;
for (auto var : impl->variables) {
if (var->id() == id)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->variables.begin(), impl->variables.end(), [id](std::shared_ptr<Variable> variable) { return variable->id() == id; });

if (it == impl->variables.end())
return -1;
else
return it - impl->variables.begin();
}

/*! Returns the list of Scratch lists. */
Expand Down Expand Up @@ -114,25 +112,23 @@ std::shared_ptr<List> Target::listAt(int index) const
/*! Returns the index of the list with the given name. */
int Target::findList(const std::string &listName) const
{
int i = 0;
for (auto list : impl->lists) {
if (list->name() == listName)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->lists.begin(), impl->lists.end(), [listName](std::shared_ptr<List> list) { return list->name() == listName; });

if (it == impl->lists.end())
return -1;
else
return it - impl->lists.begin();
}

/*! Returns the index of the list with the given ID. */
int Target::findListById(const std::string &id) const
{
int i = 0;
for (auto list : impl->lists) {
if (list->id() == id)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->lists.begin(), impl->lists.end(), [id](std::shared_ptr<List> list) { return list->id() == id; });

if (it == impl->lists.end())
return -1;
else
return it - impl->lists.begin();
}

/*! Returns the list of blocks. */
Expand Down Expand Up @@ -177,13 +173,12 @@ int Target::findBlock(const std::string &id) const
if (Target *source = dataSource())
return source->findBlock(id);

int i = 0;
for (auto block : impl->blocks) {
if (block->id() == id)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->blocks.begin(), impl->blocks.end(), [id](std::shared_ptr<Block> block) { return block->id() == id; });

if (it == impl->blocks.end())
return -1;
else
return it - impl->blocks.begin();
}

/*! Returns list of all "when green flag clicked" blocks. */
Expand Down Expand Up @@ -242,16 +237,12 @@ int Target::findComment(const std::string &id) const
if (Target *source = dataSource())
return source->findComment(id);

int i = 0;
auto it = std::find_if(impl->comments.begin(), impl->comments.end(), [id](std::shared_ptr<Comment> comment) { return comment->id() == id; });

for (auto comment : impl->comments) {
if (comment->id() == id)
return i;

i++;
}

return -1;
if (it == impl->comments.end())
return -1;
else
return it - impl->comments.begin();
}

/*! Returns the index of the current costume. */
Expand Down Expand Up @@ -320,13 +311,12 @@ int Target::findCostume(const std::string &costumeName) const
if (Target *source = dataSource())
return source->findCostume(costumeName);

int i = 0;
for (auto costume : impl->costumes) {
if (costume->name() == costumeName)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->costumes.begin(), impl->costumes.end(), [costumeName](std::shared_ptr<Costume> costume) { return costume->name() == costumeName; });

if (it == impl->costumes.end())
return -1;
else
return it - impl->costumes.begin();
}

/*! Returns the list of sounds. */
Expand Down Expand Up @@ -376,13 +366,12 @@ int Target::findSound(const std::string &soundName) const
if (Target *source = dataSource())
return source->findSound(soundName);

int i = 0;
for (auto sound : impl->sounds) {
if (sound->name() == soundName)
return i;
i++;
}
return -1;
auto it = std::find_if(impl->sounds.begin(), impl->sounds.end(), [soundName](std::shared_ptr<Sound> sound) { return sound->name() == soundName; });

if (it == impl->sounds.end())
return -1;
else
return it - impl->sounds.begin();
}

/*! Returns the layer number. */
Expand Down