Skip to content

Commit

Permalink
Setting variable to private now asks user to remove variable on all i…
Browse files Browse the repository at this point in the history
…nstances.

fixes #1313
  • Loading branch information
vchelaru committed Jan 1, 2024
1 parent 7959a32 commit e739a07
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions FRBDK/Glue/Glue/SetProperty/CustomVariableSaveSetPropertyLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Threading.Tasks;
using FlatRedBall.Glue.Managers;
using System.Windows.Media.Animation;
using Gum.DataTypes;

namespace FlatRedBall.Glue.SetVariable;

Expand Down Expand Up @@ -493,6 +494,60 @@ private void HandleScopeSet(CustomVariable customVariable, object oldValue)
didErrorOccur = true;
}

if(!didErrorOccur)
{
if(newScope == Scope.Private || newScope == Scope.Protected)
{
// if any instances use this entity, and if those instances set those variables, we need to remove those variables:
var instances = ObjectFinder.Self.GetAllNamedObjectsThatUseElement(owner);
List<NamedObjectSave> namedObjectsUsingVariable = new List<NamedObjectSave>();
foreach(var nos in instances)
{
var variable = nos.GetCustomVariable(customVariable.Name);

if(variable?.Value != null)
{
namedObjectsUsingVariable.Add(nos);
}
}

if(namedObjectsUsingVariable.Count > 0)
{
var message = $"By setting the variable's scope to {newScope}, the following variables would get removed:\n";

foreach(var item in namedObjectsUsingVariable)
{
var variable = item.GetCustomVariable(customVariable.Name);
message += $"\n{item.InstanceName}.{variable?.ToString()}";
}

message += "\n\nWould you like to continue?";

var dialogResult = GlueCommands.Self.DialogCommands.ShowYesNoMessageBox(message);

if(dialogResult == System.Windows.MessageBoxResult.Yes)
{
var elementsToRegenerate = new HashSet<GlueElement>();

foreach(var nos in namedObjectsUsingVariable)
{
nos.InstructionSaves.RemoveAll(item => item.Member == customVariable.Name);
elementsToRegenerate.Add(ObjectFinder.Self.GetElementContaining(nos));
}

foreach(var element in elementsToRegenerate)
{
GlueCommands.Self.GenerateCodeCommands.GenerateElementCode(element, generateDerivedElements: false);
}
}
else
{
didErrorOccur = true;
}
}
}
}

if(didErrorOccur)
{
customVariable.Scope = (Scope)oldValue;
Expand Down

0 comments on commit e739a07

Please sign in to comment.