/// <summary> /// Move a node upwards in the list. /// </summary> /// <param name="node">The node to move.</param> /// <returns>Whether the operation was succesful or not. For instance if the node already is at the top, this method will return false.</returns> public bool MoveNodeUp(TreeNode node) { //First see if the child node actually exists directly beneath this node. if (_Nodes.Exists(n => (n.Equals(node)))) { //Get the index position of the child node. int index = _Nodes.FindIndex(n => (n.Equals(node))); //If the child node can climb in the list. if (index != 0) { //Remove the node from the list. _Nodes.Remove(node); //Insert the node at one step up from its past position in the list. _Nodes.Insert((index - 1), node); //Wrap it all up by returning true. return true; } //Otherwise return false. else { return false; } } //Otherwise return false. else { return false; } }
/// <summary> /// A node has been clicked on by a mouse. /// </summary> /// <param name="obj">The object that fired the event.</param> /// <param name="e">The event arguments.</param> private void OnChildNodeMouseClick(object obj, MouseClickEventArgs e) { //Save the clicked node as the selected node. _SelectedNode = (obj as TreeNode); }
/// <summary> /// Get the index of a node. /// </summary> /// <param name="node">The node in question.</param> /// <returns>The index of the node.</returns> public int GetNodeIndex(TreeNode node) { //Return the index. return (_Nodes.IndexOf(node)); }
/// <summary> /// Insert a node. /// </summary> /// <param name="index">The index of where to insert the node.</param> /// <param name="node">The node to insert.</param> public void InsertNode(int index, TreeNode node) { //Add the node to the list of other items. Add(node); //Insert the child node to the list of other nodes. _Nodes.Insert(index, node); //Hook up some events. node.ChildNodeAdded += OnChildNodeAdded; node.NodeStateChanged += OnChildNodeStateChanged; node.MouseClick += OnChildNodeMouseClick; node.Ticked += OnChildNodeTicked; }
/// <summary> /// Add a treeview node to the list. /// </summary> /// <returns>The added node.</returns> public TreeNode AddNode() { //The new tree view node. TreeNode node = new TreeNode(GUI, null, _ChildPosition, _ChildWidth, _ChildHeight); //Add the child node to the list of other nodes, but also the list of other items. Add(node); _Nodes.Add(node); //Load the node's content. if (GUI.ContentManager != null) { node.LoadContent(); } //Hook up some events. node.ChildNodeAdded += OnChildNodeAdded; node.NodeStateChanged += OnChildNodeStateChanged; node.MouseClick += OnChildNodeMouseClick; node.Ticked += OnChildNodeTicked; //Return the node. return node; }
/// <summary> /// See if a certain node exists in the tree. /// </summary> /// <param name="node">The node in question.</param> /// <param name="surfaceScratchOnly">Whether to only look at the first layer of nodes or all the way down.</param> /// <returns>The index of the node.</returns> public bool Contains(TreeNode node, bool surfaceScratchOnly) { //If the specified node exists directly underneath this one. if (_Nodes.Contains(node)) { return true; } //If allowed to go deep. if (!surfaceScratchOnly) { foreach (TreeNode n in _Nodes) { if (n.Contains(node, false)) { return true; } } } //Return false, no one was found. I bet your face looks appropriately sunken and puffy at this time, complete with eyes red from crying. return false; }
/// <summary> /// If the node has had a child node added to it, see if it has to expand itself and then fire the event. /// </summary> private void ChildNodeAddedInvoke(TreeNode child) { //If this was the first child node added, expand the node and then fire the event. if (_Nodes.Count == 1) { _NodeState = TreeNodeState.Expanded; } //If someone has hooked up a delegate to the event, fire it. if (ChildNodeAdded != null) { ChildNodeAdded(this, new ChildNodeAddedEventArgs(this, child)); } }
/// <summary> /// Create a treeview node. /// </summary> /// <param name="gui">The GUI that this node will be a part of.</param> /// <param name="parent">The node's parent node.</param> /// <param name="position">The position of this node.</param> /// <param name="height">The height of this node.</param> /// <param name="width">The width of this node.</param> public TreeNode(GraphicalUserInterface gui, TreeNode parent, Vector2 position, float width, float height) { //Initialize some variables. _ParentNode = parent; Initialize(gui, position, width, height); }
/// <summary> /// Move a child node downwards in the list. /// </summary> /// <param name="childNode">The child node to move.</param> /// <returns>Whether the operation was succesful or not. For instance if the node already is at the bottom, this method will return false.</returns> public bool MoveChildNodeDown(TreeNode childNode) { //First see if the child node actually exists directly beneath this node. if (_Nodes.Exists(node => (node.Equals(childNode)))) { //Get the index position of the child node. int childIndex = _Nodes.FindIndex(node => (node.Equals(childNode))); //If the child node can descend in the list. if (childIndex != (_Nodes.Count - 1)) { //Remove the node from the list. _Nodes.Remove(childNode); //Insert the node at one step down from its past position in the list. _Nodes.Insert((childIndex + 1), childNode); //Wrap it all up by returning true. return true; } //Otherwise return false. else { return false; } } //Otherwise return false. else { return false; } }
/// <summary> /// Insert a child node. /// </summary> /// <param name="index">The index of where to insert the child node.</param> /// <param name="childNode">The child node to insert.</param> public void InsertNode(int index, TreeNode childNode) { //Insert the child node to the list of other nodes. _Nodes.Insert(index, childNode); Add(childNode); //Let the world now you just added a child node. ChildNodeAddedInvoke(_Nodes[index]); }
/// <summary> /// Get the index of a node. /// </summary> /// <param name="node">The node in question.</param> /// <returns>The index of the node.</returns> public int GetNodeIndex(TreeNode node) { return _Nodes.IndexOf(node); }
/// <summary> /// See if a certain node exists underneath this one. /// </summary> /// <param name="node">The node in question.</param> /// <param name="surfaceScratchcOnly">Whether to only look at the nodes directly under this one or all the way down.</param> /// <returns>The index of the node.</returns> public bool Contains(TreeNode node, bool surfaceScratchcOnly) { //If the specified node exists directly underneath this one. if (_Nodes.Contains(node)) { return true; } //If allowed to go deep. if (!surfaceScratchcOnly) { foreach (TreeNode n in _Nodes) { if (n.Contains(node, false)) { return true; } } } //Return false, no one was found. return false; }
/// <summary> /// Add a child node. /// </summary> /// <param name="width">The width of the child node.</param> /// <param name="height">The height of the child node.</param> /// <returns>The added node.</returns> public TreeNode AddNode(float width, float height) { //Add the child node to the list of other nodes. TreeNode node = new TreeNode(GUI, this, new Vector2(Position.X + 15, Position.Y + 15), width, height); Add(node); _Nodes.Add(node); //Let the world now you just added a child node. ChildNodeAddedInvoke(node); //Return the node. return node; }