// Creates a component, adds it to the context menu and to the entity component list
        public void CreateComponent(string componentName)
        {
            IComponent testControl = null;

            if (componentName.Equals("SpriteComponent"))
            {
                testControl = new SpriteComponentControl(selectedEntity);
            }
            else if (componentName.Equals("CollisionComponent"))
            {
                testControl = new CollisionComponentControl(selectedEntity);
            }
            else if (componentName.Equals("MovementComponent"))
            {
                testControl = new MovementComponentControl(selectedEntity);
            }
            else if (componentName.Equals("RuleComponent"))
            {
                testControl = new RuleComponentControl(selectedEntity);
            }
            else if (componentName.Equals("AudioComponent"))
            {
                testControl = new AudioComponentControl(selectedEntity);
            }
            else if (componentName.Equals("InputComponent"))
            {
                testControl = new InputComponentControl(selectedEntity);
            }

            if (selectedEntity.GetComponents().Any(x => x.GetType().FullName == testControl.GetType().FullName))
            {
                MessageBox.Show("Impossible d'avoir plusieurs fois le même composant au sein de la même entité.", "Composant en double");
            }
            else if (testControl != null)
            {
                menuPanel.Children.Add((UserControl)testControl);
                selectedEntity.AddComponent(testControl);
            }
        }
Beispiel #2
0
        private void CanvasDrop(object sender, DragEventArgs e)
        {
            if (e.Handled == false)
            {
                Canvas       canvas       = (Canvas)sender;
                ImageSource  imgsource    = e.Data.GetData(typeof(ImageSource)) as ImageSource;
                BitmapSource bitmapSource = (BitmapSource)imgsource;

                Point          mousePos = Mouse.GetPosition(null);
                FS.VirtualFile file     = e.Data.GetData(typeof(FS.VirtualFile)) as FS.VirtualFile;

                if (canvas != null)
                {
                    if (imgsource != null)
                    {
                        if (e.AllowedEffects.HasFlag(DragDropEffects.Copy))
                        {
                            Image img      = new Image();
                            var   position = e.GetPosition(canvas);
                            var   offset   = position - mousePos;
                            img.Source = imgsource;

                            img.Width  = bitmapSource.PixelWidth;
                            img.Height = bitmapSource.PixelHeight;

                            Entity canvasImage = new Entity(imgsource.ToString(), "Image" + entitiesController.CountCanvasEntities())
                            {
                                originalImage = img,
                                entity_id     = entitiesController.generator.NewID()
                            };
                            canvasImage.originalImage.Name = "_" + canvasImage.entity_id.ToString();
                            img.Source = imgsource;

                            canvasImage.canvas_image_data.position.x = offset.X - imgsource.Width / 2;
                            canvasImage.canvas_image_data.position.y = offset.Y - imgsource.Height / 2;

                            canvasImage.canvas_image_data.source_hashcode = img.Source.GetHashCode();
                            canvasImage.canvas_image_data.image_id        = canvasImage.originalImage.Name;

                            canvasImage.Name = "Image" + canvasImage.entity_id.ToString();

                            SpriteComponentControl spriteComponent = new SpriteComponentControl(canvasImage);
                            spriteComponent.ChangeSpriteName(file.FileDisplayName);
                            canvasImage.AddComponent(spriteComponent);

                            entitiesController.AddCanvasEntity(canvasImage.Name, canvasImage);

                            Canvas.SetLeft(img, offset.X - imgsource.Width / 2);
                            Canvas.SetTop(img, offset.Y - imgsource.Height / 2);
                            canvasImage.Transform.x = (int)(offset.X - imgsource.Width / 2);
                            canvasImage.Transform.y = (int)(offset.Y - imgsource.Height / 2);

                            ((Canvas)sender).Children.Add(img);

                            canvasController.InitializeCanvasSizeIfNecessary(canvasController.GetCanvas());

                            // set the value to return to the DoDragDrop call
                            e.Effects = DragDropEffects.Copy;
                            AddEntityToList(canvasImage);
                        }
                    }
                }
            }
        }