private void treeView_MouseUp(object sender, MouseButtonEventArgs e)
        {
            // NEW WAY TO DETECT SELECTIONS:
            if (sender != null && (sender as TreeView).SelectedItem != null)
            {
                List <TreeViewItem> it = TreeViewExtension.GetSelectedTreeViewItems(treeView);
                EditorHandler.SelectedGameObjects = new List <GameObject>();
                foreach (var i in it)
                {
                    //Console.WriteLine(i.Header);
                    //object tag = ((sender as TreeView).SelectedItem as DragDropTreeViewItem).Tag; // old
                    object tag = (i as DragDropTreeViewItem).Tag;

                    if (tag is GameObject)
                    {
                        // TODO : multiple selection
                        EditorHandler.SelectedGameObjects.Add((GameObject)tag);
                    }
                }
                EditorHandler.ChangeSelectedObjects();

                lastSelectedItem = (sender as TreeView).SelectedItem as DragDropTreeViewItem;
            }
            else
            {
                EditorHandler.SelectedGameObjects.Clear();
                EditorHandler.ChangeSelectedObjects();
            }
        }
        void cutItem_Click(object sender, RoutedEventArgs e)
        {
            List <GameObject> toCopy = new List <GameObject>();

            foreach (TreeViewItem ti in TreeViewExtension.GetSelectedTreeViewItems(treeView))
            {
                var gameObject = (ti.Tag as GameObject);
                gameObject.SaveComponentValues();
                toCopy.Add(gameObject);

                DragDropTreeViewItem parentNode = ti.Parent as DragDropTreeViewItem;
                if (parentNode == null)
                {
                    SceneManager.ActiveScene.GameObjects.Remove(ti.Tag as GameObject);
                    treeView.Items.Remove(ti);
                }
                else
                {
                    GameObject objParent = (GameObject)parentNode.Tag;
                    objParent.Children.Remove(ti.Tag as GameObject);
                    parentNode.Items.Remove(ti);
                }
            }
            Clipboard.SetData("GameObject", toCopy);
            TreeViewExtension.UnselectAll(treeView);
            EditorHandler.SelectedGameObjects.Clear();
            EditorHandler.ChangeSelectedObjects();
        }
        void deleteItem_Click(object sender, RoutedEventArgs e)
        {
            List <TreeViewItem> selected = TreeViewExtension.GetSelectedTreeViewItems(treeView);
            string message = "Are you sure you want to delete the selected game object?";

            if (selected.Count > 1)
            {
                message = "Are you sure you want to delete the selected game objects?";
            }

            if (System.Windows.Forms.MessageBox.Show(message, "Warning", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                TreeViewExtension.UnselectAll(treeView);
                foreach (var t in selected)
                {
                    GameObject           gameObject = (GameObject)(t as DragDropTreeViewItem).Tag; //(GameObject)(SelectedItem as DragDropTreeViewItem).Tag;
                    DragDropTreeViewItem parentNode = (t as DragDropTreeViewItem).Parent as DragDropTreeViewItem;
                    if (parentNode == null)
                    {
                        SceneManager.ActiveScene.GameObjects.Remove((t as DragDropTreeViewItem).Tag as GameObject);
                        treeView.Items.Remove(t);
                    }
                    else
                    {
                        GameObject objParent = (GameObject)parentNode.Tag;
                        objParent.Children.Remove((t as DragDropTreeViewItem).Tag as GameObject);
                        parentNode.Items.Remove(t);
                    }
                }
                EditorHandler.SelectedGameObjects.Clear();
                EditorHandler.ChangeSelectedObjects();
            }
        }
        void copyItem_Click(object sender, RoutedEventArgs e)
        {
            List <GameObject> toCopy = new List <GameObject>();

            foreach (TreeViewItem ti in TreeViewExtension.GetSelectedTreeViewItems(treeView))
            {
                var gameObject = (ti.Tag as GameObject);
                gameObject.SaveComponentValues();
                toCopy.Add(gameObject);
            }

            Clipboard.SetData("GameObject", toCopy);
        }
        void newItem_Click(object sender, RoutedEventArgs e)
        {
            GibboMenuItem _sender = (GibboMenuItem)sender;

            foreach (var ti in TreeViewExtension.GetSelectedTreeViewItems(treeView))
            {
                ObjectComponent component  = (ObjectComponent)Activator.CreateInstance((Type)_sender.Tag, new object[] { /* params here */ });
                GameObject      gameObject = (GameObject)ti.Tag;
                if (!gameObject.AddComponent(component))
                {
                    MessageBox.Show("The component was not added to " + gameObject.Name + ", the requirements are not met", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    EditorCommands.CreatePropertyGridView();
                }
            }
        }
        void treeView_OnDragDropSuccess(DragDropTreeViewItem source, DragDropTreeViewItem target, System.ComponentModel.CancelEventArgs e)
        {
            if (!(source.Tag is GameObject))
            {
                e.Cancel = true;
                return;
            }

            List <TreeViewItem> movedItems = TreeViewExtension.GetSelectedTreeViewItems(treeView);

            // verify if the moved items will not be placed on any of their children or in themselfs
            foreach (var ti in movedItems)
            {
                var tx = (DragDropTreeViewItem)ti;
                if (ti == target)
                {
                    // trying to drop on one of the moved items
                    e.Cancel = true;
                    return;
                }

                if (DragDropTreeView.TreeContainsNode(treeView, tx, target))
                {
                    e.Cancel = true;
                    return;
                }
            }

            if (target.Tag is GameObject)
            {
                foreach (var ti in movedItems)
                {
                    GameObject _source = ti.Tag as GameObject;

                    // no parent?
                    if (_source.Transform.Parent == null)
                    {
                        SceneManager.ActiveScene.GameObjects.Remove(_source, false);
                    }
                    else
                    {
                        GameObject parent = (ti.Parent as DragDropTreeViewItem).Tag as GameObject;
                        parent.Children.Remove(_source, false);
                    }

                    if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Center)
                    {
                        (target.Tag as GameObject).Children.Insert(0, _source);
                    }
                    else
                    {
                        int index = 0;

                        // no parent?
                        if ((target.Tag as GameObject).Transform.Parent == null)
                        {
                            index = SceneManager.ActiveScene.GameObjects.IndexOf(target.Tag as GameObject);
                        }
                        else
                        {
                            GameObject parent = (target.Parent as DragDropTreeViewItem).Tag as GameObject;
                            index = parent.Children.IndexOf(target.Tag as GameObject);
                        }

                        if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Top)
                        {
                            index++;
                        }

                        if ((target.Tag as GameObject).Transform.Parent == null)
                        {
                            SceneManager.ActiveScene.GameObjects.Insert(index, _source);
                        }
                        else
                        {
                            GameObject parent = (target.Parent as DragDropTreeViewItem).Tag as GameObject;
                            parent.Children.Insert(index, _source);
                        }
                    }
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        void contextMenu_Opened(object sender, RoutedEventArgs e)
        {
            // check disabled :
            if (TreeViewExtension.GetSelectedTreeViewItems(treeView).Count > 1)
            {
                // more than one selected:
                renameItem.IsEnabled    = false;
                moveUpItem.IsEnabled    = false;
                moveDownItem.IsEnabled  = false;
                saveStateItem.IsEnabled = false;
            }
            else
            {
                renameItem.IsEnabled    = true;
                moveUpItem.IsEnabled    = true;
                moveDownItem.IsEnabled  = true;
                saveStateItem.IsEnabled = true;
            }

            addComponentItem.Items.Clear();
            GibboMenuItem item = null;

            if (SceneManager.ScriptsAssembly == null)
            {
                addComponentItem.Visibility = System.Windows.Visibility.Collapsed;
                return;
            }
            else
            {
                addComponentItem.Visibility = System.Windows.Visibility.Visible;

                //ObjectComponent dummy = new ObjectComponent();
                foreach (Type type in SceneManager.ScriptsAssembly.GetTypes())
                {
                    // is a game object component?
                    if (type.IsSubclassOf(typeof(ObjectComponent)))
                    {
                        string        fullname = type.FullName;
                        GibboMenuItem lastItem = addComponentItem;

                        if (fullname != type.Name && fullname.Contains('.'))
                        {
                            string[] splitted = fullname.Split('.');
                            int      scount   = splitted.Count() - 1;

                            for (int i = 0; i < scount; i++)
                            {
                                item = null;
                                string camelCaseFix = GibboHelper.SplitCamelCase(splitted[i]);

                                for (int j = 0; j < lastItem.Items.Count; j++)
                                {
                                    if ((lastItem.Items[j] as GibboMenuItem).TagText.Equals(camelCaseFix))
                                    {
                                        item = lastItem.Items[j] as GibboMenuItem;
                                        break;
                                    }
                                }

                                if (item == null)
                                {
                                    item         = EditorUtils.CreateMenuItem(camelCaseFix, null);
                                    item.TagText = camelCaseFix;
                                    lastItem.Items.Insert(0, item);
                                }

                                lastItem = item;
                            }
                        }

                        string        camelCase = GibboHelper.SplitCamelCase(type.Name);
                        GibboMenuItem newItem   = EditorUtils.CreateMenuItem(camelCase, (ImageSource)FindResource("ComponentItemIcon"));
                        newItem.Tag     = type;
                        newItem.TagText = camelCase;
                        newItem.Click  += newItem_Click;

                        SearchAndAttachInfo(newItem, type);

                        lastItem.Items.Add(newItem);
                    }
                }
            }

            // System Components :
            GibboMenuItem subItem;

            addComponentItem.Items.Add(new Separator());
            item = EditorUtils.CreateMenuItem("Physics");

            subItem         = EditorUtils.CreateMenuItem("Physical Body", (ImageSource)FindResource("ComponentItemIcon"));
            subItem.Tag     = typeof(PhysicalBody);
            subItem.TagText = typeof(PhysicalBody).Name;
            subItem.Click  += newItem_Click;

            SearchAndAttachInfo(subItem, typeof(PhysicalBody));

            item.Items.Add(subItem);

            subItem         = EditorUtils.CreateMenuItem("Rectangle Body", (ImageSource)FindResource("ComponentItemIcon"));
            subItem.Tag     = typeof(RectangleBody);
            subItem.TagText = typeof(RectangleBody).Name;
            subItem.Click  += newItem_Click;

            SearchAndAttachInfo(subItem, typeof(RectangleBody));

            item.Items.Add(subItem);

            subItem         = EditorUtils.CreateMenuItem("Circle Body", (ImageSource)FindResource("ComponentItemIcon"));
            subItem.Tag     = typeof(CircleBody);
            subItem.TagText = typeof(CircleBody).Name;
            subItem.Click  += newItem_Click;

            SearchAndAttachInfo(subItem, typeof(CircleBody));

            item.Items.Add(subItem);

            subItem         = EditorUtils.CreateMenuItem("Texture Body", (ImageSource)FindResource("ComponentItemIcon"));
            subItem.Tag     = typeof(TextureBody);
            subItem.TagText = typeof(TextureBody).Name;
            subItem.Click  += newItem_Click;

            SearchAndAttachInfo(subItem, typeof(TextureBody));

            item.Items.Add(subItem);

            addComponentItem.Items.Add(item);
        }
Exemplo n.º 8
0
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);

            // Retrieve the client coordinates of the drop location.
            Point targetPoint = e.GetPosition(this);

            try
            {
                // Retrieve the node at the drop location.
                DragDropTreeViewItem targetNode = GetNearestContainer(e.Source as UIElement);

                DragDropHelper.RemoveInsertionAdorner();

                // Retrieve the node that was dragged.
                DragDropTreeViewItem draggedNode = (DragDropTreeViewItem)e.Data.GetData(typeof(DragDropTreeViewItem));

                if (draggedNode == null)
                {
                    draggedNode = (DragDropTreeViewItem)e.Data.GetData(typeof(ExplorerTreeViewItem));
                }

                if (targetNode == null || draggedNode == null)
                {
                    return;
                }

                // Confirm that the node at the drop location is not
                // the dragged node or a descendant of the dragged node.
                if (!draggedNode.Equals(targetNode) && !ContainsNode(draggedNode, targetNode))
                {
                    CancelEventArgs evt = new CancelEventArgs();
                    OnDragDropSuccess(draggedNode, targetNode, evt);

                    if (!evt.Cancel)
                    {
                        // If it is a move operation, remove the node from its current
                        // location and add it to the node at the drop location.
                        if (e.Effects == DragDropEffects.Move)
                        {
                            List <TreeViewItem> items = TreeViewExtension.GetSelectedTreeViewItems(TreeViewExtension.GetTree(targetNode));

                            if (items != null && items.Count > 0)
                            {
                                // multi selection drag:
                                foreach (var ti in items)
                                {
                                    (ti.Parent as ItemsControl).Items.Remove(ti);

                                    if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Center)
                                    {
                                        targetNode.Items.Add(ti);
                                    }
                                    else
                                    {
                                        int index = (targetNode.Parent as ItemsControl).ItemContainerGenerator.IndexFromContainer(targetNode);
                                        if (index < 0)
                                        {
                                            index = 0;
                                        }

                                        if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Bottom)
                                        {
                                            index++;
                                        }

                                        (targetNode.Parent as ItemsControl).Items.Insert(index, ti);
                                    }

                                    ti.IsSelected = true;

                                    ReApplyStyle(ti as DragDropTreeViewItem, "IgniteMultiTreeViewItem");
                                }
                            }
                            else
                            {
                                (draggedNode.Parent as ItemsControl).Items.Remove(draggedNode);

                                if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Center)
                                {
                                    targetNode.Items.Add(draggedNode);
                                }
                                else
                                {
                                    int index = (targetNode.Parent as ItemsControl).ItemContainerGenerator.IndexFromContainer(targetNode);
                                    if (index < 0)
                                    {
                                        index = 0;
                                    }

                                    if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Bottom)
                                    {
                                        index++;
                                    }

                                    (targetNode.Parent as ItemsControl).Items.Insert(index, draggedNode);
                                }

                                draggedNode.IsSelected = true;

                                ReApplyStyle(draggedNode, "IgniteTreeViewItem");
                            }
                        }
                        // OPTIONAL:
                        // If it is a copy operation, clone the dragged node
                        // and add it to the node at the drop location.
                        //else if (e.Effects == DragDropEffects.Copy)
                        //{
                        //    targetNode.Items.Add((DragDropTreeViewItem)draggedNode);
                        //}

                        // Expand the node at the location
                        // to show the dropped node.
                        targetNode.IsExpanded = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }