public void AddChild(TreeItem item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } if (item.Parent != null) { throw new InvalidOperationException(); } _items.Add(item); item.Parent = this; }
/// <summary> /// The draw tree item. /// </summary> /// <param name="context"> /// The context. /// </param> /// <param name="layout"> /// The layout. /// </param> /// <param name="treeItem"> /// The tree item. /// </param> public void DrawTreeItem(IRenderContext context, Rectangle layout, TreeItem treeItem) { if (treeItem.Parent is TreeView) { var view = treeItem.Parent as TreeView; if (view.SelectedItem == treeItem) { this.DrawRaised(context, layout); } } this.m_RenderUtilities.RenderText( context, new Vector2(layout.X + 5, layout.Center.Y), treeItem.Text, this.m_AssetManager.Get<FontAsset>("font.Default"), verticalAlignment: VerticalAlignment.Center); }
/// <summary> /// The find parent for item. /// </summary> /// <param name="current"> /// The current. /// </param> /// <param name="item"> /// The item. /// </param> /// <returns> /// The <see cref="TreeEntry"/>. /// </returns> private TreeEntry FindParentForItem(TreeEntry current, TreeItem item) { var segments = item.Text == null ? -1 : item.Text.Split('.').Length; if (current.SegmentCount == segments - 1 && item.Text.StartsWith(current.FullName)) { return current; } foreach (var child in current.Children) { var result = this.FindParentForItem(child, item); if (result != null) { return result; } } return null; }
/// <summary> /// The backfill parents for item. /// </summary> /// <param name="root"> /// The root. /// </param> /// <param name="item"> /// The item. /// </param> private void BackfillParentsForItem(TreeEntry root, TreeItem item) { var segments = item.Text.Split('.').Reverse().Where((x, id) => id >= 1).Reverse().ToArray(); var current = root; var i = 1; var built = string.Empty; foreach (var segment in segments) { built += "." + segment; built = built.Trim('.'); var next = current.Children.FirstOrDefault(x => x.Name == segment); if (next == null) { var newItem = new TreeItem { Text = built, Parent = this }; this.m_Items.Add(newItem); var created = new TreeEntry { Item = newItem, Layout = null, Name = segment, FullName = built, Children = new List<TreeEntry>(), SegmentCount = i }; current.Children.Add(created); current = created; } else { current = next; } i++; } }
/// <summary> /// The remove child. /// </summary> /// <param name="item"> /// The item. /// </param> public void RemoveChild(TreeItem item) { this.m_Items.Remove(item); item.Parent = null; }
/// <summary> /// Requests that the UI container handle the specified event or return false. /// </summary> /// <param name="skin"> /// The UI skin. /// </param> /// <param name="layout"> /// The layout for the element. /// </param> /// <param name="context"> /// The current game context. /// </param> /// <param name="event"> /// The event that was raised. /// </param> /// <returns> /// Whether or not this UI element handled the event. /// </returns> public bool HandleEvent(ISkin skin, Rectangle layout, IGameContext context, Event @event) { foreach (var kv in this.GetChildrenWithLayouts(skin, layout)) { if (kv.Item.HandleEvent(skin, kv.Layout.Value, context, @event)) { return true; } } var keyPressEvent = @event as KeyPressEvent; if (keyPressEvent != null) { var upPressed = keyPressEvent.Key == Keys.Up; var downPressed = keyPressEvent.Key == Keys.Down; if (this.SelectedItem != null && (upPressed || downPressed)) { var tree = this.BuildEntryGraph(layout); var list = this.NormalizeTree(tree, true); var current = list.IndexOf(list.First(x => this.SelectedItem == x.Item)); if (upPressed) { current -= 1; } else if (downPressed) { current += 1; } if (current >= 0 && current < list.Count) { this.SelectedItem = list[current].Item; return true; } } } return false; }
public void DrawTreeItem(IRenderContext context, Rectangle layout, TreeItem treeItem) { this.m_BasicSkin.DrawTreeItem(context, layout, treeItem); }