private void FindChildDepth(ref int depth, HItem parent)
 {
     if (parent.HierarchyParent != null)
     {
         depth++;
         FindChildDepth(ref depth, parent.HierarchyParent);
     }
 }
        private void ItemDoubleClicked(object sender, MouseEventArgs e)
        {
            HItem selectedItem = hierarchyItems[displayedHierarchyIDs[_listView.FocusedItem.Index]];

            // If the item has children
            if (selectedItem.HierarchyChildren.Count != 0)
            {
                if (!selectedItem.Expanded) // If not expanded we will need to make visible
                {
                    // First of all switch the icon around on this item
                    int removeIndex = _listView.FocusedItem.Index;
                    _listView.Items.RemoveAt(removeIndex);

                    ListViewItem newItem = new ListViewItem(selectedItem.GameObjectName);
                    newItem.IndentCount = selectedItem.ChildDepthLevel;
                    newItem.ImageIndex  = 1;
                    _listView.Items.Insert(removeIndex, newItem);

                    for (int i = 0; i < selectedItem.HierarchyChildren.Count; i++)
                    {
                        ListViewItem newChildItem = new ListViewItem(selectedItem.HierarchyChildren[i].GameObjectName);
                        newChildItem.IndentCount = selectedItem.HierarchyChildren[i].ChildDepthLevel;

                        if (selectedItem.HierarchyChildren[i].HierarchyChildren.Count != 0) // If the child has children add icon
                        {
                            newChildItem.ImageIndex = 0;
                        }

                        _listView.Items.Insert(removeIndex + 1, (newChildItem));
                        displayedHierarchyIDs.Insert(removeIndex + 1, removeIndex + 1 + i);
                    }
                }
                else
                {
                    // First of all switch the icon around on this item
                    int removeIndex = _listView.FocusedItem.Index;
                    _listView.Items.RemoveAt(removeIndex);

                    ListViewItem newItem = new ListViewItem(selectedItem.GameObjectName);
                    newItem.IndentCount = selectedItem.ChildDepthLevel;
                    newItem.ImageIndex  = 0;
                    _listView.Items.Insert(removeIndex, newItem);

                    // Remove each child from the displayed hierarchy
                    for (int i = selectedItem.HierarchyChildren.Count; i > 0; i--)
                    {
                        _listView.Items.RemoveAt(removeIndex + i);
                        displayedHierarchyIDs.RemoveAt(removeIndex + i);
                    }
                }

                selectedItem.Expanded = !selectedItem.Expanded;
            }
        }
        private void NewGameObjectClicked(object sender, EventArgs e)
        {
            IntPtr    sceneItem = SceneInterface.CreateGameObject(_sceneManager);
            IntPtr    data      = new IntPtr(sceneItem.ToInt64());
            SceneItem hItem     = (SceneItem)Marshal.PtrToStructure(data, typeof(SceneItem));

            displayedHierarchyIDs.Add(hierarchyItems.Count);

            HItem item = new HItem(hItem.GameObjectName, (int)hItem.GameObjectID, (int)hItem.ComponentCount);

            item.ChildDepthLevel = 0;
            hierarchyItems.Add(hierarchyItems.Count, item);

            ListViewItem lvItem = new ListViewItem(hierarchyItems[displayedHierarchyIDs[hierarchyItems.Count - 1]].GameObjectName);

            _listView.Items.Add(lvItem);
            lvItem.Focused = true;

            SceneInterface.FreeMemory(sceneItem);
            _scene.HasChanged = true;

            _listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            RenameClicked(null, null);
        }
        public void CreateHierachyList(IntPtr sceneManager)
        {
            _sceneManager = sceneManager;

            int    numberOfGameObjects = SceneInterface.GetGameObjectCount(sceneManager);
            IntPtr hierarchy           = SceneInterface.PopulateHierarchyItems(sceneManager, numberOfGameObjects);
            int    structSize          = Marshal.SizeOf(typeof(SceneItem));

            hierarchyItems.Clear();
            displayedHierarchyIDs.Clear();

            _listView.Items.Clear();

            for (int i = 0; i < numberOfGameObjects; i++)
            {
                // Parse the data recieved from the engine
                IntPtr    data  = new IntPtr(hierarchy.ToInt64() + structSize * i);
                SceneItem hItem = (SceneItem)Marshal.PtrToStructure(data, typeof(SceneItem));

                // Create the item that will be stored in the hierarchy
                HItem item = new HItem(hItem.GameObjectName, (int)hItem.GameObjectID, (int)hItem.ComponentCount);

                // Check if the item has a parent
                HItem itemParent = FindParent((int)hItem.GameObjectParentID);
                if (itemParent != null)
                {
                    // If the item has a parent, setup the parent and child data
                    item.HierarchyParent = itemParent;
                    itemParent.HierarchyChildren.Add(item);
                }
                else // Else add it's index to be rendered in the list
                {
                    displayedHierarchyIDs.Add(i);
                }

                // Cache the item
                hierarchyItems.Add(i, item);
            }

            // Determine every items depth
            for (int i = 0; i < hierarchyItems.Count; i++)
            {
                int depth = 0;
                FindChildDepth(ref depth, hierarchyItems[i]);
                hierarchyItems[i].ChildDepthLevel = depth;
            }

            // Add the items that are visible to the actual listview
            for (int i = 0; i < displayedHierarchyIDs.Count; i++)
            {
                // If this item does not have any children display without an expand icon
                if (hierarchyItems[displayedHierarchyIDs[i]].HierarchyChildren.Count == 0)
                {
                    _listView.Items.Add(new ListViewItem(hierarchyItems[displayedHierarchyIDs[i]].GameObjectName));
                }
                else // If this icon has children display with an expand icon
                {
                    _listView.Items.Add(new ListViewItem(hierarchyItems[displayedHierarchyIDs[i]].GameObjectName, 0));
                }
            }

            SceneInterface.FreeMemory(hierarchy);

            _listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }