private static void MoveCustomVariable(TreeNode nodeMoving, TreeNode targetNode) { CustomVariable customVariable = nodeMoving.Tag as CustomVariable; if (targetNode.IsRootEventsNode()) { // The user dragged a variable onto the events node, so they want to make // an event for this. We'll assume an "after" event since I think no one makes // before events if (customVariable != null) { customVariable.CreatesEvent = true; FlatRedBall.Glue.Events.EventResponseSave eventResponseSave = new Events.EventResponseSave(); eventResponseSave.EventName = "After" + customVariable.Name + "Set"; eventResponseSave.SourceObject = null; eventResponseSave.SourceObjectEvent = null; eventResponseSave.SourceVariable = customVariable.Name; eventResponseSave.BeforeOrAfter = BeforeOrAfter.After; eventResponseSave.DelegateType = null; RightClickHelper.AddEventToElementAndSave(EditorLogic.CurrentElement, eventResponseSave); } } else if (targetNode.IsRootCustomVariablesNode()) { // let's see if the user is moving a variable from one element to another IElement sourceElement = nodeMoving.GetContainingElementTreeNode().Tag as IElement; IElement targetElement = targetNode.GetContainingElementTreeNode().Tag as IElement; if (sourceElement != targetElement) { // copying a variable from one element to another // eventually we need to add some error checking here. CustomVariable newVariable = customVariable.Clone(); targetElement.CustomVariables.Add(newVariable); GlueCommands.Self.GenerateCodeCommands.GenerateElementCode(targetElement); GlueCommands.Self.RefreshCommands.RefreshUi(targetElement); } } }
public static CustomVariable GetCustomVariableFromNosOrElement(ElementRuntime elementRuntime, string variableName) { CustomVariable variable = elementRuntime.AssociatedIElement.GetCustomVariableRecursively(variableName); if (variable != null) { // The NOS may be overwriting the value from the element, so we need to set that if so: if (elementRuntime.AssociatedNamedObjectSave != null) { NamedObjectSave nos = elementRuntime.AssociatedNamedObjectSave; InstructionSave instruction = nos.GetInstructionFromMember(variable.Name); if (instruction != null && instruction.Value != null) { variable = variable.Clone(); variable.DefaultValue = instruction.Value; } } } return(variable); }
public CustomVariable CreateCustomVariableFor(string newVariableDeclarationType, string leftOfEquals, string rightOfEquals, ElementRuntime elementRuntime, CodeContext codeContext) { IElement element = null; CustomVariable toReturn = null; if (elementRuntime != null) { element = elementRuntime.AssociatedIElement; toReturn = element.GetCustomVariableRecursively(leftOfEquals); } // See if there is already a CustomVariable for this: if (toReturn != null) { toReturn = toReturn.Clone(); } if (toReturn == null) { // If there's no event, we gotta create one toReturn = new CustomVariable(); toReturn.Name = leftOfEquals; // If the left side has a period, that means the user is setting a variable on a contained object (because this. will already have been stripped) if (leftOfEquals.Contains('.')) { int indexOfDot = leftOfEquals.IndexOf('.'); toReturn.SourceObject = leftOfEquals.Substring(0, indexOfDot); toReturn.SourceObjectProperty = leftOfEquals.Substring(indexOfDot + 1, leftOfEquals.Length - (indexOfDot + 1)); } if (newVariableDeclarationType == null) { Type throwaway = null; toReturn.Type = GetTypeStringForValue(leftOfEquals, elementRuntime, codeContext, out throwaway); } else { toReturn.Type = newVariableDeclarationType; } } object valueToAssign = null; if (!string.IsNullOrEmpty(rightOfEquals)) { valueToAssign = mExpressionParser.EvaluateExpression(rightOfEquals, codeContext); } if (toReturn.Type == null && valueToAssign != null) { toReturn.Type = valueToAssign.GetType().FullName; } if (toReturn.Type != null) { if (toReturn.GetRuntimeType() == typeof(float)) { if (valueToAssign is double) { valueToAssign = (float)((double)valueToAssign); } else if (valueToAssign is int) { valueToAssign = (float)((int)valueToAssign); } } if (valueToAssign is float && float.IsNaN((float)valueToAssign)) { int m = 3; } toReturn.DefaultValue = valueToAssign; } return(toReturn); }