/// <summary> /// Rename a variable by udpate its name in the variable map.Identify the /// variable to rename with the given variable. /// </summary> /// <param name="variable">Variable to rename</param> /// <param name="newName">New variable name</param> public void RenameVariableInternal(VariableModel variable, string newName) { if (newName == "") { return; } var newVariable = this.GetVariable(newName); // If they are different types, throw an error. if (null != variable && null != newVariable && !string.Equals(variable.Type, newVariable.Type)) { throw new Exception("Variable " + variable.Name + " is type " + variable.Type + " and variable " + newName + " is type " + newVariable.Type + ".Both must be the same type."); } string oldName = variable != null ? variable.Name : null; string oldCase = newVariable != null ? newVariable.Name : null; this.VariableMap.RenameVariable(variable, newName); // Iterate through every block and update name. var blocks = this.GetAllBlocks(); foreach (var block in blocks) { block.RenameVar(oldName, newName); // newVariable's name maybe changed after renaming, because of the case insensative if (!string.IsNullOrEmpty(oldCase) && !oldCase.Equals(newName)) { block.RenameVar(oldCase, newName); } } }
/// <summary> /// Deletes a variable and all of its uses from this workspace without asking the /// user for confirmation. /// </summary> /// <param name="variable"> Variable to delete</param> public void DeleteVariableInternal(VariableModel variable) { var uses = GetVariableUses(variable.Name); foreach (var block in uses) { block.Dispose(true); } VariableMap.DeleteVariable(variable); }
/// <summary> /// Delete a variable /// </summary> /// <param name="variable"> Variable to delete.</param> public void DeleteVariable(VariableModel variable) { var variableList = mVariableMap[variable.Type]; foreach (var tempVar in variableList) { if (string.Equals(tempVar.ID, variable.ID)) { variableList.Remove(tempVar); FireUpdate(new VariableUpdateData(VariableUpdateData.Delete, variable.Name)); return; } } }
/// <summary> /// Deletes a variable and all of its uses from this workspace without asking the /// user for confirmation. /// </summary> /// <param name="variable"> Variable to delete</param> public void DeleteVariableInternal(VariableModel variable) { var uses = GetVariableUses(variable.Name); foreach (var block in uses) { TrackerAsset.Instance.setVar("block_type", block.Type); TrackerAsset.Instance.setVar("action", "remove"); TrackerAsset.Instance.GameObject.Interacted(GameManager.Instance.GetBlockId(block)); block.Dispose(true); } VariableMap.DeleteVariable(variable); }
/// <summary> /// Rename the given variable by updating its name in the variable map. /// </summary> public void RenameVariable(VariableModel variable, string newName) { VariableModel newVariable = this.GetVariable(newName); int variableIndex = -1; int newVariableIndex = -1; string type = ""; if (variable != null) { type = variable.Type; } else if (newVariable != null) { type = newVariable.Type; } List <VariableModel> varList = this.GetVariablesOfType(type); if (variable != null) { variableIndex = varList.IndexOf(variable); } if (newVariable != null) { newVariableIndex = varList.IndexOf(newVariable); } if (variableIndex == -1 && newVariableIndex == -1) { CreateVariable(newName, ""); } else if (variableIndex == newVariableIndex || variableIndex != -1 && newVariableIndex == -1) { var variableToRename = mVariableMap[type][variableIndex]; FireUpdate(new VariableUpdateData(VariableUpdateData.Rename, variableToRename.Name, newName)); variableToRename.Name = newName; } else if (variableIndex != -1 && newVariableIndex != -1) { var variableToRename = mVariableMap[type][newVariableIndex]; FireUpdate(new VariableUpdateData(VariableUpdateData.Rename, variableToRename.Name, newName)); var variableToDelete = mVariableMap[type][variableIndex]; FireUpdate(new VariableUpdateData(VariableUpdateData.Delete, variableToDelete.Name)); variableToRename.Name = newName; mVariableMap[type].RemoveAt(variableIndex); } }
/// <summary> /// Create a variable with a given name, optional type, and optional id. /// </summary> public VariableModel CreateVariable(string name, string optType = null, string optId = null) { var variable = this.GetVariable(name); if (null != variable) { if (!string.IsNullOrEmpty(optType) && variable.Type != optType) { throw new Exception("Variable " + name + " is already in use and its type is " + variable.Type + " which conflicts with the passed in " + "type, " + optType + "."); } if (!string.IsNullOrEmpty(optId) && !string.Equals(variable.ID, optId)) { throw new Exception("Variable " + name + " is already in use and its id is " + variable.ID + " which conflicts with the passed in " + "id, " + optId + "."); } // The variable already exists and has the same id and type. return(variable); } if (!string.IsNullOrEmpty(optId) && null != this.GetVariableById(optId)) { throw new Exception("Variable " + optId + ", is already in use."); } optId = string.IsNullOrEmpty(optId) ? Utils.GenUid() : optId; optType = string.IsNullOrEmpty(optType) ? "" : optType; variable = new VariableModel(this.mWorkspace, name, optType, optId); // If optType is not a key,create a new list. if (!mVariableMap.ContainsKey(optType)) { this.mVariableMap.Add(optType, new List <VariableModel>() { variable }); } else { // Else append the variable to the preexisting list. this.mVariableMap[optType].Add(variable); } FireUpdate(new VariableUpdateData(VariableUpdateData.Create, variable.Name)); return(variable); }
public override void SetValue(string newValue) { string value = newValue; string text = newValue; if (SourceBlock != null) { VariableModel variable = SourceBlock.Workspace.GetVariableById(newValue); if (variable != null) { text = variable.Name; } else { variable = SourceBlock.Workspace.GetVariable(newValue); if (variable != null) value = variable.ID; } } mValue = value; SetText(text); }
/// <summary> /// A custom compare function for the VariableModel objects. /// </summary> public static int CompareByName(VariableModel var1, VariableModel var2) { return(String.CompareOrdinal(var1.Name.ToLower(), var2.Name.ToLower())); }