/// <summary> /// Release all used memory /// </summary> public void Dispose() { if (_listViews != null) { while (_listViews.Count > 0) { IView view = _listViews[0]; _listViews.RemoveAt(0); view.Dispose(); view = null; } _listViews.Clear(); _listViews = null; } if (_history != null) { _history.Clear(); _history = null; } if (_turtle != null) { _turtle.Dispose(); _turtle = null; } }
/// <summary> /// Performs the provided action against the turtle /// </summary> /// <param name="action">IAction - The action instance with which to perform on the turtle</param> public void Perform(IAction action) { if (action == null) return; if (_turtle == null) _turtle = new Turtle(); bool success = action.Perform(_turtle); if (success) { _history.Add(action); for (int index = 0; index < _listViews.Count; index ++) { _listViews[index].Draw(_turtle); _listViews[index].Print(action.ToString()); } } }
/// <summary> /// Default constructor. Must always instantiate object without exception /// </summary> private Conductor() { _listViews = new List<IView>(); _turtle = null; _history = new List<IAction>(25); }
/// <summary> /// Load an XML document from file /// </summary> /// <param name="filePath"></param> public void Load(string filePath) { if (!File.Exists(filePath)) throw new FileNotFoundException("File not found!", filePath); XmlDocument xmlDocument = null; try { xmlDocument = new XmlDocument(); xmlDocument.Load(filePath); } catch (Exception ex) { throw new Exception("Failed to load XML document!"); } XmlNode document = xmlDocument["Document"]; if (document == null) throw new Exception("Root document element not found in file!"); // clean up old memory if it exists if (_turtle != null) { _turtle.Dispose(); _turtle = null; } if (_history != null) { _history.Clear(); _history = null; } // create new turtle instance _turtle = new Turtle(); _turtle.Surface.ToString(); _turtle.Reset(); _history = new List<IAction>(); for (int index = 0; index < document.Attributes.Count; index++) { XmlAttribute attribute = document.Attributes[index]; switch (attribute.Name.ToLower()) { case "backcolor": _turtle.BackColor = Color.FromName(attribute.Value); break; case "forecolor": _turtle.ForeColor = Color.FromName(attribute.Value); break; case "penwidth": float penWidth = 0f; if (float.TryParse(attribute.Value, out penWidth)) _turtle.PenWidth = penWidth; break; case "width": int width = 0; if (int.TryParse(attribute.Value, out width)) _turtle.Size = new Size(width, _turtle.Size.Height); break; case "height": int height = 0; if (int.TryParse(attribute.Value, out height)) _turtle.Size = new Size(_turtle.Size.Width, height); break; } } for (int index = 0; index < document.ChildNodes.Count; index++) { XmlNode child = document.ChildNodes[index]; // simply pass to ActionFactory and try perform the result... IAction action = ActionFactory.Create(child); if (action != null) Perform(action); } document = null; xmlDocument = null; }