Skip to content

Commit

Permalink
Add a refactoring operation for property type change.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Nov 18, 2024
1 parent 3d9efb9 commit 22c9ead
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 4 deletions.
31 changes: 27 additions & 4 deletions Core/GDCore/IDE/ProjectBrowserHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,26 @@ void ProjectBrowserHelper::ExposeEventsBasedBehaviorEvents(
gd::Project &project, const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedBehavior &eventsBasedBehavior,
gd::ArbitraryEventsWorkerWithContext &worker) {
gd::VariablesContainer propertyVariablesContainer(
gd::VariablesContainer::SourceType::Properties);
gd::ProjectBrowserHelper::ExposeEventsBasedBehaviorEvents(
project, eventsFunctionsExtension,
eventsBasedBehavior,
propertyVariablesContainer,
worker);
}

void ProjectBrowserHelper::ExposeEventsBasedBehaviorEvents(
gd::Project &project, const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedBehavior &eventsBasedBehavior,
gd::VariablesContainer &propertyVariablesContainer,
gd::ArbitraryEventsWorkerWithContext &worker) {
auto &behaviorEventsFunctions = eventsBasedBehavior.GetEventsFunctions();
for (auto &&eventsFunction : behaviorEventsFunctions.GetInternalVector()) {

gd::ObjectsContainer parameterObjectsContainers;
gd::VariablesContainer parameterVariablesContainer(
gd::VariablesContainer::SourceType::Parameters);
gd::VariablesContainer propertyVariablesContainer(
gd::VariablesContainer::SourceType::Properties);
auto projectScopedContainers = gd::ProjectScopedContainers::
MakeNewProjectScopedContainersForBehaviorEventsFunction(
project, eventsFunctionsExtension, eventsBasedBehavior,
Expand All @@ -242,14 +254,25 @@ void ProjectBrowserHelper::ExposeEventsBasedObjectEvents(
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedObject &eventsBasedObject,
gd::ArbitraryEventsWorkerWithContext &worker) {
gd::VariablesContainer propertyVariablesContainer(
gd::VariablesContainer::SourceType::Properties);
gd::ProjectBrowserHelper::ExposeEventsBasedObjectEvents(
project, eventsFunctionsExtension, eventsBasedObject,
propertyVariablesContainer, worker);
}

void ProjectBrowserHelper::ExposeEventsBasedObjectEvents(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedObject &eventsBasedObject,
gd::VariablesContainer &propertyVariablesContainer,
gd::ArbitraryEventsWorkerWithContext &worker) {
auto &objectEventsFunctions = eventsBasedObject.GetEventsFunctions();
for (auto &&eventsFunction : objectEventsFunctions.GetInternalVector()) {

gd::ObjectsContainer parameterObjectsContainers;
gd::VariablesContainer parameterVariablesContainer(
gd::VariablesContainer::SourceType::Parameters);
gd::VariablesContainer propertyVariablesContainer(
gd::VariablesContainer::SourceType::Properties);
auto projectScopedContainers = gd::ProjectScopedContainers::
MakeNewProjectScopedContainersForObjectEventsFunction(
project, eventsFunctionsExtension, eventsBasedObject,
Expand Down
29 changes: 29 additions & 0 deletions Core/GDCore/IDE/ProjectBrowserHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ArbitraryEventsFunctionsWorker;
class ArbitraryObjectsWorker;
class ArbitraryEventBasedBehaviorsWorker;
class ArbitraryBehaviorSharedDataWorker;
class VariablesContainer;
} // namespace gd

namespace gd {
Expand Down Expand Up @@ -127,6 +128,20 @@ class GD_CORE_API ProjectBrowserHelper {
const gd::EventsBasedBehavior &eventsBasedBehavior,
gd::ArbitraryEventsWorkerWithContext &worker);

/**
* \brief Call the specified worker on all events of the event-based
* behavior.
*
* This should be the preferred way to traverse all the events of an
* event-based behavior.
*/
static void ExposeEventsBasedBehaviorEvents(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedBehavior &eventsBasedBehavior,
gd::VariablesContainer &propertyVariablesContainer,
gd::ArbitraryEventsWorkerWithContext &worker);

/**
* \brief Call the specified worker on all events of the event-based
* object.
Expand All @@ -152,6 +167,20 @@ class GD_CORE_API ProjectBrowserHelper {
const gd::EventsBasedObject &eventsBasedObject,
gd::ArbitraryEventsWorkerWithContext &worker);

/**
* \brief Call the specified worker on all events of the event-based
* object.
*
* This should be the preferred way to traverse all the events of an
* event-based object.
*/
static void ExposeEventsBasedObjectEvents(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedObject &eventsBasedObject,
gd::VariablesContainer &propertyVariablesContainer,
gd::ArbitraryEventsWorkerWithContext &worker);

/**
* \brief Call the specified worker on all ObjectContainers of the project
* (global, layouts...)
Expand Down
36 changes: 36 additions & 0 deletions Core/GDCore/IDE/WholeProjectRefactorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,42 @@ void WholeProjectRefactorer::RenameEventsBasedObjectProperty(
gd::ProjectBrowserHelper::ExposeProjectEvents(project, conditionRenamer);
}

void WholeProjectRefactorer::ChangeEventsBasedBehaviorPropertyType(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedBehavior &eventsBasedBehavior,
const gd::String &propertyName) {
std::unordered_set<gd::String> typeChangedPropertyNames;
typeChangedPropertyNames.insert(propertyName);
gd::VariablesContainer propertyVariablesContainer(
gd::VariablesContainer::SourceType::Properties);
gd::EventsVariableInstructionTypeSwitcher
eventsVariableInstructionTypeSwitcher(project.GetCurrentPlatform(),
typeChangedPropertyNames,
propertyVariablesContainer);
gd::ProjectBrowserHelper::ExposeEventsBasedBehaviorEvents(
project, eventsFunctionsExtension, eventsBasedBehavior,
propertyVariablesContainer, eventsVariableInstructionTypeSwitcher);
}

void WholeProjectRefactorer::ChangeEventsBasedObjectPropertyType(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedObject &eventsBasedObject,
const gd::String &propertyName) {
std::unordered_set<gd::String> typeChangedPropertyNames;
typeChangedPropertyNames.insert(propertyName);
gd::VariablesContainer propertyVariablesContainer(
gd::VariablesContainer::SourceType::Properties);
gd::EventsVariableInstructionTypeSwitcher
eventsVariableInstructionTypeSwitcher(project.GetCurrentPlatform(),
typeChangedPropertyNames,
propertyVariablesContainer);
gd::ProjectBrowserHelper::ExposeEventsBasedObjectEvents(
project, eventsFunctionsExtension, eventsBasedObject,
propertyVariablesContainer, eventsVariableInstructionTypeSwitcher);
}

void WholeProjectRefactorer::AddBehaviorAndRequiredBehaviors(
gd::Project &project, gd::Object &object, const gd::String &behaviorType,
const gd::String &behaviorName) {
Expand Down
20 changes: 20 additions & 0 deletions Core/GDCore/IDE/WholeProjectRefactorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,26 @@ class GD_CORE_API WholeProjectRefactorer {
const gd::String& oldPropertyName,
const gd::String& newPropertyName);

/**
* \brief Refactor the project **after** a property of a behavior has
* changed of type.
*/
static void ChangeEventsBasedBehaviorPropertyType(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedBehavior &eventsBasedBehavior,
const gd::String &propertyName);

/**
* \brief Refactor the project **after** a property of an object has
* changed of type.
*/
static void ChangeEventsBasedObjectPropertyType(
gd::Project &project,
const gd::EventsFunctionsExtension &eventsFunctionsExtension,
const gd::EventsBasedObject &eventsBasedObject,
const gd::String &propertyName);

/**
* \brief Add a behavior to an object and add required behaviors if necessary
* to fill every behavior properties of the added behaviors.
Expand Down
10 changes: 10 additions & 0 deletions GDevelop.js/Bindings/Bindings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2541,12 +2541,22 @@ interface WholeProjectRefactorer {
[Const, Ref] EventsBasedBehavior eventsBasedBehavior,
[Const] DOMString oldName,
[Const] DOMString newName);
void STATIC_ChangeEventsBasedBehaviorPropertyType(
[Ref] Project project,
[Const, Ref] EventsFunctionsExtension eventsFunctionsExtension,
[Const, Ref] EventsBasedBehavior eventsBasedBehavior,
[Const] DOMString propertyName);
void STATIC_RenameEventsBasedObjectProperty(
[Ref] Project project,
[Const, Ref] EventsFunctionsExtension eventsFunctionsExtension,
[Const, Ref] EventsBasedObject eventsBasedObject,
[Const] DOMString oldName,
[Const] DOMString newName);
void STATIC_ChangeEventsBasedObjectPropertyType(
[Ref] Project project,
[Const, Ref] EventsFunctionsExtension eventsFunctionsExtension,
[Const, Ref] EventsBasedObject eventsBasedObject,
[Const] DOMString propertyName);
void STATIC_RenameEventsBasedBehavior(
[Ref] Project project,
[Const, Ref] EventsFunctionsExtension eventsFunctionsExtension,
Expand Down
2 changes: 2 additions & 0 deletions GDevelop.js/Bindings/Wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,8 @@ typedef ExtensionAndMetadata<ExpressionMetadata> ExtensionAndExpressionMetadata;
#define STATIC_RenameEventsBasedBehaviorSharedProperty \
RenameEventsBasedBehaviorSharedProperty
#define STATIC_RenameEventsBasedObjectProperty RenameEventsBasedObjectProperty
#define STATIC_ChangeEventsBasedBehaviorPropertyType ChangeEventsBasedBehaviorPropertyType
#define STATIC_ChangeEventsBasedObjectPropertyType ChangeEventsBasedObjectPropertyType
#define STATIC_RenameEventsBasedBehavior RenameEventsBasedBehavior
#define STATIC_UpdateBehaviorNameInEventsBasedBehavior UpdateBehaviorNameInEventsBasedBehavior
#define STATIC_RenameEventsBasedObject RenameEventsBasedObject
Expand Down
2 changes: 2 additions & 0 deletions GDevelop.js/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,9 @@ export class WholeProjectRefactorer extends EmscriptenObject {
static moveObjectEventsFunctionParameter(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, eventsBasedObject: EventsBasedObject, functionName: string, oldIndex: number, newIndex: number): void;
static renameEventsBasedBehaviorProperty(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, eventsBasedBehavior: EventsBasedBehavior, oldName: string, newName: string): void;
static renameEventsBasedBehaviorSharedProperty(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, eventsBasedBehavior: EventsBasedBehavior, oldName: string, newName: string): void;
static changeEventsBasedBehaviorPropertyType(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, eventsBasedBehavior: EventsBasedBehavior, propertyName: string): void;
static renameEventsBasedObjectProperty(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, eventsBasedObject: EventsBasedObject, oldName: string, newName: string): void;
static changeEventsBasedObjectPropertyType(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, eventsBasedObject: EventsBasedObject, propertyName: string): void;
static renameEventsBasedBehavior(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, oldName: string, newName: string): void;
static updateBehaviorNameInEventsBasedBehavior(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, eventsBasedBehavior: EventsBasedBehavior, sourceBehaviorName: string): void;
static renameEventsBasedObject(project: Project, eventsFunctionsExtension: EventsFunctionsExtension, oldName: string, newName: string): void;
Expand Down
2 changes: 2 additions & 0 deletions GDevelop.js/types/gdwholeprojectrefactorer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Props = {|
eventsBasedBehavior: gdEventsBasedBehavior,
onRenameProperty: (oldName: string, newName: string) => void,
onRenameSharedProperty: (oldName: string, newName: string) => void,
onPropertyTypeChanged: (propertyName: string) => void,
onEventsFunctionsAdded: () => void,
unsavedChanges?: ?UnsavedChanges,
onConfigurationUpdated?: (?ExtensionItemConfigurationAttribute) => void,
Expand All @@ -31,6 +32,7 @@ export default function EventsBasedBehaviorEditorPanel({
projectScopedContainersAccessor,
onRenameProperty,
onRenameSharedProperty,
onPropertyTypeChanged,
unsavedChanges,
onEventsFunctionsAdded,
onConfigurationUpdated,
Expand Down Expand Up @@ -90,6 +92,7 @@ export default function EventsBasedBehaviorEditorPanel({
onRenameProperty={onRenameProperty}
behaviorObjectType={eventsBasedBehavior.getObjectType()}
onPropertiesUpdated={onPropertiesUpdated}
onPropertyTypeChanged={onPropertyTypeChanged}
onEventsFunctionsAdded={onEventsFunctionsAdded}
/>
)}
Expand All @@ -103,6 +106,7 @@ export default function EventsBasedBehaviorEditorPanel({
properties={eventsBasedBehavior.getSharedPropertyDescriptors()}
onRenameProperty={onRenameSharedProperty}
onPropertiesUpdated={onPropertiesUpdated}
onPropertyTypeChanged={onPropertyTypeChanged}
onEventsFunctionsAdded={onEventsFunctionsAdded}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Props = {|
isSceneProperties?: boolean,
onPropertiesUpdated?: () => void,
onRenameProperty: (oldName: string, newName: string) => void,
onPropertyTypeChanged: (propertyName: string) => void,
onEventsFunctionsAdded: () => void,
behaviorObjectType?: string,
|};
Expand Down Expand Up @@ -136,6 +137,7 @@ export default function EventsBasedBehaviorPropertiesEditor({
isSceneProperties,
onPropertiesUpdated,
onRenameProperty,
onPropertyTypeChanged,
onEventsFunctionsAdded,
behaviorObjectType,
}: Props) {
Expand Down Expand Up @@ -709,6 +711,9 @@ export default function EventsBasedBehaviorPropertiesEditor({
);
}
forceUpdate();
onPropertyTypeChanged(
property.getName()
);
onPropertiesUpdated &&
onPropertiesUpdated();
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = {|
eventsFunctionsExtension: gdEventsFunctionsExtension,
eventsBasedObject: gdEventsBasedObject,
onRenameProperty: (oldName: string, newName: string) => void,
onPropertyTypeChanged: (propertyName: string) => void,
onEventsFunctionsAdded: () => void,
onOpenCustomObjectEditor: () => void,
unsavedChanges?: ?UnsavedChanges,
Expand All @@ -29,6 +30,7 @@ export default function EventsBasedObjectEditorPanel({
eventsFunctionsExtension,
eventsBasedObject,
onRenameProperty,
onPropertyTypeChanged,
onEventsFunctionsAdded,
onOpenCustomObjectEditor,
unsavedChanges,
Expand Down Expand Up @@ -84,6 +86,7 @@ export default function EventsBasedObjectEditorPanel({
eventsBasedObject={eventsBasedObject}
onRenameProperty={onRenameProperty}
onPropertiesUpdated={onPropertiesUpdated}
onPropertyTypeChanged={onPropertyTypeChanged}
onEventsFunctionsAdded={onEventsFunctionsAdded}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type Props = {|
eventsBasedObject: gdEventsBasedObject,
onPropertiesUpdated?: () => void,
onRenameProperty: (oldName: string, newName: string) => void,
onPropertyTypeChanged: (propertyName: string) => void,
onEventsFunctionsAdded: () => void,
|};

Expand Down Expand Up @@ -129,6 +130,7 @@ export default function EventsBasedObjectPropertiesEditor({
eventsBasedObject,
onPropertiesUpdated,
onRenameProperty,
onPropertyTypeChanged,
onEventsFunctionsAdded,
}: Props) {
const scrollView = React.useRef<?ScrollViewInterface>(null);
Expand Down Expand Up @@ -700,6 +702,9 @@ export default function EventsBasedObjectPropertiesEditor({
);
}
forceUpdate();
onPropertyTypeChanged(
property.getName()
);
onPropertiesUpdated &&
onPropertiesUpdated();
}}
Expand Down
16 changes: 16 additions & 0 deletions newIDE/app/src/EventsFunctionsExtensionEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,14 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
newName
)
}
onPropertyTypeChanged={propertyName => {
gd.WholeProjectRefactorer.changeEventsBasedBehaviorPropertyType(
project,
eventsFunctionsExtension,
selectedEventsBasedBehavior,
propertyName
);
}}
onEventsFunctionsAdded={() => {
if (this.eventsFunctionList) {
this.eventsFunctionList.forceUpdateList();
Expand All @@ -1447,6 +1455,14 @@ export default class EventsFunctionsExtensionEditor extends React.Component<
newName
)
}
onPropertyTypeChanged={propertyName => {
gd.WholeProjectRefactorer.changeEventsBasedObjectPropertyType(
project,
eventsFunctionsExtension,
selectedEventsBasedObject,
propertyName
);
}}
onEventsFunctionsAdded={() => {
if (this.eventsFunctionList) {
this.eventsFunctionList.forceUpdateList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const Default = () => (
eventsFunctionsExtension={testProject.testEventsFunctionsExtension}
eventsBasedBehavior={testProject.testEventsBasedBehavior}
onRenameProperty={action('property rename')}
onPropertyTypeChanged={action('onPropertyTypeChanged')}
onRenameSharedProperty={action('shared property rename')}
onEventsFunctionsAdded={action('functions added')}
/>
Expand All @@ -40,6 +41,7 @@ export const WithoutFunction = () => (
eventsFunctionsExtension={testProject.testEventsFunctionsExtension}
eventsBasedBehavior={testProject.testEmptyEventsBasedBehavior}
onRenameProperty={action('property rename')}
onPropertyTypeChanged={action('onPropertyTypeChanged')}
onRenameSharedProperty={action('shared property rename')}
onEventsFunctionsAdded={action('functions added')}
/>
Expand Down
Loading

0 comments on commit 22c9ead

Please sign in to comment.