Skip to content

Commit

Permalink
Enable search and replace in For each object events and Javascript ev…
Browse files Browse the repository at this point in the history
…ents (#5477)
  • Loading branch information
AlexandreSi authored Jul 13, 2023
1 parent 656255a commit 3286722
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Core/GDCore/Events/Builtin/ForEachEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace std;
namespace gd {

ForEachEvent::ForEachEvent()
: BaseEvent(), objectsToPick(""), objectsToPickSelected(false) {}
: BaseEvent(), objectsToPick("") {}

vector<gd::InstructionsList*> ForEachEvent::GetAllConditionsVectors() {
vector<gd::InstructionsList*> allConditions;
Expand Down
10 changes: 8 additions & 2 deletions Core/GDCore/Events/Builtin/ForEachEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#ifndef FOREACHEVENT_H
#define FOREACHEVENT_H
#include <vector>

#include "GDCore/Events/Event.h"
#include "GDCore/Events/EventsList.h"
namespace gd {
Expand Down Expand Up @@ -62,13 +64,17 @@ class GD_CORE_API ForEachEvent : public gd::BaseEvent {
virtual void UnserializeFrom(gd::Project& project,
const SerializerElement& element);

std::vector<gd::Expression*> GetAllObjectExpressions() {
std::vector<gd::Expression*> allObjectExpressions;
allObjectExpressions.push_back(&objectsToPick);
return allObjectExpressions;
}

private:
gd::Expression objectsToPick;
gd::InstructionsList conditions;
gd::InstructionsList actions;
gd::EventsList events;

bool objectsToPickSelected;
};

} // namespace gd
Expand Down
8 changes: 8 additions & 0 deletions Core/GDCore/Events/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ class GD_CORE_API BaseEvent {
*/
bool IsFolded() const { return folded; }

/**
* \brief Return a list of all objects linked to the event.
*/
virtual std::vector<gd::Expression*> GetAllObjectExpressions() {
std::vector<gd::Expression*> allObjectExpressions;
return allObjectExpressions;
}

///@}

std::weak_ptr<gd::BaseEvent>
Expand Down
78 changes: 62 additions & 16 deletions Core/GDCore/IDE/Events/EventsRefactorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,22 @@ void EventsRefactorer::RemoveObjectInEvents(const gd::Platform& platform,
}
}

gd::String ReplaceAllOccurrencesCaseInsensitive(gd::String context,
const gd::String& from,
const gd::String& to) {
size_t lookHere = 0;
size_t foundHere;
size_t fromSize = from.size();
size_t toSize = to.size();
while ((foundHere = context.FindCaseInsensitive(from, lookHere)) !=
gd::String::npos) {
context.replace(foundHere, fromSize, to);
lookHere = foundHere + toSize;
}

return context;
}

std::vector<EventsSearchResult> EventsRefactorer::ReplaceStringInEvents(
gd::ObjectsContainer& project,
gd::ObjectsContainer& layout,
Expand All @@ -570,6 +586,32 @@ std::vector<EventsSearchResult> EventsRefactorer::ReplaceStringInEvents(

for (std::size_t i = 0; i < events.size(); ++i) {
bool eventModified = false;

std::vector<gd::Expression*> allObjectExpressions =
events[i].GetAllObjectExpressions();
for (std::size_t j = 0; j < allObjectExpressions.size(); ++j) {
gd::String newExpressionPlainString =
matchCase ? allObjectExpressions[j]->GetPlainString().FindAndReplace(
toReplace, newString, true)
: ReplaceAllOccurrencesCaseInsensitive(
allObjectExpressions[j]->GetPlainString(),
toReplace,
newString);

if (newExpressionPlainString !=
allObjectExpressions[j]->GetPlainString()) {
*allObjectExpressions[j] = gd::Expression(newExpressionPlainString);

if (!eventModified) {
modifiedEvents.push_back(EventsSearchResult(
std::weak_ptr<gd::BaseEvent>(events.GetEventSmartPtr(i)),
&events,
i));
eventModified = true;
}
}
}

if (inConditions) {
vector<gd::InstructionsList*> conditionsVectors =
events[i].GetAllConditionsVectors();
Expand Down Expand Up @@ -642,22 +684,6 @@ std::vector<EventsSearchResult> EventsRefactorer::ReplaceStringInEvents(
return modifiedEvents;
}

gd::String ReplaceAllOccurrencesCaseInsensitive(gd::String context,
gd::String from,
const gd::String& to) {
size_t lookHere = 0;
size_t foundHere;
size_t fromSize = from.size();
size_t toSize = to.size();
while ((foundHere = context.FindCaseInsensitive(from, lookHere)) !=
gd::String::npos) {
context.replace(foundHere, fromSize, to);
lookHere = foundHere + toSize;
}

return context;
}

bool EventsRefactorer::ReplaceStringInActions(gd::ObjectsContainer& project,
gd::ObjectsContainer& layout,
gd::InstructionsList& actions,
Expand Down Expand Up @@ -789,6 +815,24 @@ vector<EventsSearchResult> EventsRefactorer::SearchInEvents(
for (std::size_t i = 0; i < events.size(); ++i) {
bool eventAddedInResults = false;

std::vector<gd::Expression*> allObjectExpressions =
events[i].GetAllObjectExpressions();
for (std::size_t j = 0; j < allObjectExpressions.size(); ++j) {
size_t foundPosition =
matchCase
? allObjectExpressions[j]->GetPlainString().find(search)
: allObjectExpressions[j]->GetPlainString().FindCaseInsensitive(
search);

if (foundPosition != gd::String::npos && !eventAddedInResults) {
results.push_back(EventsSearchResult(
std::weak_ptr<gd::BaseEvent>(events.GetEventSmartPtr(i)),
&events,
i));
eventAddedInResults = true;
}
}

if (inConditions) {
vector<gd::InstructionsList*> conditionsVectors =
events[i].GetAllConditionsVectors();
Expand All @@ -803,6 +847,7 @@ vector<EventsSearchResult> EventsRefactorer::SearchInEvents(
std::weak_ptr<gd::BaseEvent>(events.GetEventSmartPtr(i)),
&events,
i));
eventAddedInResults = true;
}
}
}
Expand All @@ -820,6 +865,7 @@ vector<EventsSearchResult> EventsRefactorer::SearchInEvents(
std::weak_ptr<gd::BaseEvent>(events.GetEventSmartPtr(i)),
&events,
i));
eventAddedInResults = true;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions GDJS/GDJS/Events/Builtin/JsCodeEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class JsCodeEvent : public gd::BaseEvent {
bool IsEventsSheetExpanded() const { return eventsSheetExpanded; }
void SetEventsSheetExpanded(bool enable) { eventsSheetExpanded = enable; };

std::vector<gd::Expression*> GetAllObjectExpressions() {
std::vector<gd::Expression*> allObjectExpressions;
allObjectExpressions.push_back(&parameterObjects);
return allObjectExpressions;
}

private:
void Init(const JsCodeEvent& event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ const styles = {
},
};

export default class ForEachEvent extends React.Component<
EventRendererProps,
*
> {
export default class WhileEvent extends React.Component<EventRendererProps, *> {
render() {
var whileEvent = gd.asWhileEvent(this.props.event);

Expand Down

0 comments on commit 3286722

Please sign in to comment.