|
12 | 12 | #include "GDCore/Project/ObjectsContainersList.h"
|
13 | 13 | #include "GDCore/Project/Variable.h"
|
14 | 14 | #include "GDCore/Project/VariablesContainer.h"
|
| 15 | +#include "GDCore/Project/EventsBasedObject.h" |
15 | 16 | #include "GDCore/String.h"
|
16 | 17 |
|
17 | 18 | namespace gd {
|
@@ -158,38 +159,67 @@ void GroupVariableHelper::ApplyChangesToObjects(
|
158 | 159 | }
|
159 | 160 | auto &object = hasObject ? objectsContainer.GetObject(objectName)
|
160 | 161 | : globalObjectsContainer.GetObject(objectName);
|
161 |
| - auto &variablesContainer = object.GetVariables(); |
162 |
| - for (const gd::String &variableName : changeset.removedVariableNames) { |
163 |
| - variablesContainer.Remove(variableName); |
| 162 | + gd::GroupVariableHelper::ApplyChangesToVariableContainer( |
| 163 | + groupVariablesContainer, object.GetVariables(), changeset, true); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +void GroupVariableHelper::ApplyChangesToVariants( |
| 168 | + gd::EventsBasedObject &eventsBasedObject, const gd::String &objectName, |
| 169 | + const gd::VariablesChangeset &changeset) { |
| 170 | + auto &defaultVariablesContainer = eventsBasedObject.GetDefaultVariant() |
| 171 | + .GetObjects() |
| 172 | + .GetObject(objectName) |
| 173 | + .GetVariables(); |
| 174 | + for (auto &variant : eventsBasedObject.GetVariants().GetInternalVector()) { |
| 175 | + if (!variant->GetObjects().HasObjectNamed(objectName)) { |
| 176 | + continue; |
164 | 177 | }
|
165 |
| - for (const gd::String &variableName : changeset.addedVariableNames) { |
166 |
| - if (variablesContainer.Has(variableName)) { |
167 |
| - // It can happens if an object already had the variable but it was not |
168 |
| - // shared by other object of the group. |
169 |
| - continue; |
170 |
| - } |
171 |
| - variablesContainer.Insert(variableName, |
172 |
| - groupVariablesContainer.Get(variableName), |
173 |
| - variablesContainer.Count()); |
| 178 | + auto &object = variant->GetObjects().GetObject(objectName); |
| 179 | + gd::GroupVariableHelper::ApplyChangesToVariableContainer( |
| 180 | + defaultVariablesContainer, object.GetVariables(), changeset, false); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +void GroupVariableHelper::ApplyChangesToVariableContainer( |
| 185 | + const gd::VariablesContainer &originalVariablesContainer, |
| 186 | + gd::VariablesContainer &destinationVariablesContainer, |
| 187 | + const gd::VariablesChangeset &changeset, bool shouldApplyValueChanges) { |
| 188 | + for (const gd::String &variableName : changeset.removedVariableNames) { |
| 189 | + destinationVariablesContainer.Remove(variableName); |
| 190 | + } |
| 191 | + for (const gd::String &variableName : changeset.addedVariableNames) { |
| 192 | + if (destinationVariablesContainer.Has(variableName)) { |
| 193 | + // It can happens if an object already had the variable but it was not |
| 194 | + // shared by other object of the group. |
| 195 | + continue; |
174 | 196 | }
|
175 |
| - for (const auto &pair : changeset.oldToNewVariableNames) { |
176 |
| - const gd::String &oldVariableName = pair.first; |
177 |
| - const gd::String &newVariableName = pair.second; |
178 |
| - if (variablesContainer.Has(newVariableName)) { |
179 |
| - // It can happens if an object already had the variable but it was not |
180 |
| - // shared by other object of the group. |
181 |
| - variablesContainer.Remove(oldVariableName); |
182 |
| - } else { |
183 |
| - variablesContainer.Rename(oldVariableName, newVariableName); |
184 |
| - } |
| 197 | + destinationVariablesContainer.Insert( |
| 198 | + variableName, originalVariablesContainer.Get(variableName), |
| 199 | + destinationVariablesContainer.Count()); |
| 200 | + } |
| 201 | + for (const auto &pair : changeset.oldToNewVariableNames) { |
| 202 | + const gd::String &oldVariableName = pair.first; |
| 203 | + const gd::String &newVariableName = pair.second; |
| 204 | + if (destinationVariablesContainer.Has(newVariableName)) { |
| 205 | + // It can happens if an object already had the variable but it was not |
| 206 | + // shared by other object of the group. |
| 207 | + destinationVariablesContainer.Remove(oldVariableName); |
| 208 | + } else { |
| 209 | + destinationVariablesContainer.Rename(oldVariableName, newVariableName); |
185 | 210 | }
|
186 |
| - // Apply type and value changes |
187 |
| - for (const gd::String &variableName : changeset.valueChangedVariableNames) { |
188 |
| - size_t index = variablesContainer.GetPosition(variableName); |
189 |
| - variablesContainer.Remove(variableName); |
190 |
| - variablesContainer.Insert( |
191 |
| - variableName, groupVariablesContainer.Get(variableName), index); |
| 211 | + } |
| 212 | + // Apply type and value changes |
| 213 | + for (const gd::String &variableName : changeset.valueChangedVariableNames) { |
| 214 | + if (!shouldApplyValueChanges && |
| 215 | + destinationVariablesContainer.Get(variableName).GetType() == |
| 216 | + originalVariablesContainer.Get(variableName).GetType()) { |
| 217 | + continue; |
192 | 218 | }
|
| 219 | + size_t index = destinationVariablesContainer.GetPosition(variableName); |
| 220 | + destinationVariablesContainer.Remove(variableName); |
| 221 | + destinationVariablesContainer.Insert( |
| 222 | + variableName, originalVariablesContainer.Get(variableName), index); |
193 | 223 | }
|
194 | 224 | }
|
195 | 225 |
|
|
0 commit comments