Beispiel #1
0
        private bool CollisionDetection(ElementContentItem dragItem, ElementContentItem otherItem)
        {
            Rect dragRect  = GetItemRect(dragItem);
            Rect otherRect = GetItemRect(otherItem);

            return(dragRect.IntersectsWith(otherRect));
        }
Beispiel #2
0
        private void Workbench_MouseMove(object sender, MouseEventArgs e)
        {
            Point  mousePos = e.GetPosition(null);
            Vector diff     = mousePos - _MouseStartPoint;

            if (_DragStartedItem != null && e.LeftButton == MouseButtonState.Pressed)
            {
                double newX  = _ItemStartPoint.X + diff.X;
                double newY  = _ItemStartPoint.Y + diff.Y;
                Point  newPt = ClipPoint(new Point(newX, newY));
                Canvas.SetLeft(_DragStartedItem, newPt.X);
                Canvas.SetTop(_DragStartedItem, newPt.Y);

                foreach (UIElement uiElement in Workbench.Children)
                {
                    if (uiElement is ElementContentItem && uiElement != _DragStartedItem)
                    {
                        ElementContentItem elementItem = uiElement as ElementContentItem;
                        elementItem.Collision = CollisionDetection(elementItem, mousePos);
                    }
                }

                SolidColorBrush brush = new SolidColorBrush();
                brush.Color           = InRecycleBin(mousePos) ? Color.FromArgb(0x55, 0xFF, 0, 0) : Colors.Transparent;
                RecycleBin.Background = brush;
            }
        }
Beispiel #3
0
 private void BringToFront(ElementContentItem item)
 {
     foreach (UIElement control in Workbench.Children)
     {
         Canvas.SetZIndex(control, 0);
     }
     Canvas.SetZIndex(item, 1);
 }
Beispiel #4
0
        private void Element_MouseDown(object sender, MouseEventArgs e)
        {
            var item = e.Source as ElementContentItem;

            if (item != null)
            {
                _MouseStartPoint = e.GetPosition(null);
                _ItemStartPoint  = new Point(Canvas.GetLeft(item), Canvas.GetTop(item));
                BringToFront(item);
                _DragStartedItem = item;
            }
        }
Beispiel #5
0
        private ElementContentItem CreateItem(double x, double y, Element element)
        {
            ElementContentItem item = new ElementContentItem();

            Canvas.SetLeft(item, x);
            Canvas.SetTop(item, y);
            item.Template          = (ControlTemplate)Workbench.Resources["ElementItemTemplate"];
            item.Element           = element;
            item.MouseDown        += Element_MouseDown;
            item.MouseDoubleClick += Element_MouseDoubleClick;
            item.AllowDrop         = true;

            return(item);
        }
Beispiel #6
0
        private void ElementContentItem_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var item = sender as ElementContentItem;

            if (item != null && e.ChangedButton == MouseButton.Left)
            {
                Point p = item.TranslatePoint(new Point(0, 0), Workbench);
                _DragNew         = true;
                _DragStartedItem = CreateItem(p.X, p.Y, item.Element);
                Workbench.Children.Add(_DragStartedItem);
                _MouseStartPoint = e.GetPosition(null);
                _ItemStartPoint  = new Point(Canvas.GetLeft(_DragStartedItem), Canvas.GetTop(_DragStartedItem));
                BringToFront(_DragStartedItem);
            }
        }
 private void BringToFront(ElementContentItem item)
 {
     foreach (UIElement control in Workbench.Children)
     {
         Canvas.SetZIndex(control, 0);
     }
     Canvas.SetZIndex(item, 1);
 }
 private bool CollisionDetection(ElementContentItem dragItem, ElementContentItem otherItem)
 {
     Rect dragRect = GetItemRect(dragItem);
     Rect otherRect = GetItemRect(otherItem);
     return dragRect.IntersectsWith(otherRect);
 }
Beispiel #9
0
 private Rect GetItemRect(ElementContentItem item)
 {
     return(new Rect(Canvas.GetLeft(item), Canvas.GetTop(item), item.ActualWidth, item.ActualHeight));
 }
        private ElementContentItem CreateItem( double x, double y, Element element)
        {
            ElementContentItem item = new ElementContentItem();
            Canvas.SetLeft(item, x);
            Canvas.SetTop(item, y);
            item.Template = (ControlTemplate) Workbench.Resources["ElementItemTemplate"];
            item.Element = element;
            item.MouseDown += Element_MouseDown;
            item.MouseDoubleClick += Element_MouseDoubleClick;
            item.AllowDrop = true;

            return item;
        }
Beispiel #11
0
        private bool CollisionDetection(ElementContentItem item, Point point)
        {
            Rect itemRect = GetItemRect(item);

            return(itemRect.Contains(point));
        }
 private void ElementContentItem_MouseDown(object sender, MouseButtonEventArgs e)
 {
     var item = sender as ElementContentItem;
     if (item != null && e.ChangedButton == MouseButton.Left)
     {
         Point p = item.TranslatePoint(new Point(0, 0), Workbench);
         _DragNew = true;
         _DragStartedItem = CreateItem(p.X, p.Y, item.Element);
         Workbench.Children.Add(_DragStartedItem);
         _MouseStartPoint = e.GetPosition(null);
         _ItemStartPoint = new Point(Canvas.GetLeft(_DragStartedItem), Canvas.GetTop(_DragStartedItem));
         BringToFront(_DragStartedItem);
     }
 }
Beispiel #13
0
        private void Workbench_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Workbench_MouseUp called");
            if (_DragStartedItem != null)
            {
                var   item     = _DragStartedItem;
                Point mousePos = e.GetPosition(null);
                if (InRecycleBin(mousePos))
                {
                    // Recycle element
                    var anim = new DoubleAnimation
                    {
                        To           = 0,
                        Duration     = TimeSpan.FromMilliseconds(100),
                        FillBehavior = FillBehavior.Stop
                    };

                    anim.Completed += (s, a) =>
                    {
                        Workbench.Children.Remove(item);
                    };
                    item.BeginAnimation(UIElement.OpacityProperty, anim);
                }
                else
                {
                    foreach (UIElement uiElement in Workbench.Children)
                    {
                        if (uiElement is ElementContentItem && uiElement != item)
                        {
                            ElementContentItem elementItem = uiElement as ElementContentItem;
                            if (CollisionDetection(elementItem, mousePos))
                            {
                                // Find center point between two elements
                                Rect  dragItemRect    = GetItemRect(item);
                                Rect  otherItemRect   = GetItemRect(elementItem);
                                Point dragItemCenter  = GetCenterPoint(dragItemRect);
                                Point otherItemCenter = GetCenterPoint(otherItemRect);
                                Rect  bothItemsRect   = new Rect(dragItemCenter, otherItemCenter);
                                Point centerPoint     = GetCenterPoint(bothItemsRect);

                                // Do expirement
                                ElementDatabase.ExperimentResult result = _ElementDatabase.DoExperiment(item.Element, elementItem.Element);

                                // Provide feedback for success
                                if (result.Success)
                                {
                                    // Remove items from workbench
                                    Workbench.Children.Remove(elementItem);
                                    Workbench.Children.Remove(item);

                                    // Add results back
                                    AddElements(centerPoint.X, centerPoint.Y, result.ElementsCreated);
                                }

                                // Break out of the foreach (...) so we don't process
                                break;
                            }
                        }
                    }
                }

                // Reset borders
                foreach (UIElement uiElement in Workbench.Children)
                {
                    if (uiElement is ElementContentItem)
                    {
                        (uiElement as ElementContentItem).Collision = false;
                    }
                }

                // Reset RecycleBin background
                RecycleBin.Background = new SolidColorBrush(Colors.Transparent);

                // Done dragging
                Point toolboxOrigin = Toolbox.TranslatePoint(new Point(0, 0), Workbench);
                Rect  toolboxBounds = new Rect(toolboxOrigin.X, toolboxOrigin.Y, Toolbox.ActualWidth, Toolbox.ActualHeight);
                if (_DragNew && !!toolboxBounds.Contains(mousePos))
                {
                    Workbench.Children.Remove(_DragStartedItem);
                }
                _DragNew         = false;
                _DragStartedItem = null;
            }
        }
 private bool CollisionDetection(ElementContentItem item, Point point)
 {
     Rect itemRect = GetItemRect(item);
     return itemRect.Contains(point);
 }
        private void Workbench_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine("Workbench_MouseUp called");
            if (_DragStartedItem != null)
            {
                var item = _DragStartedItem;
                Point mousePos = e.GetPosition(null);
                if (InRecycleBin(mousePos))
                {
                    // Recycle element
                    var anim = new DoubleAnimation
                    {
                        To = 0,
                        Duration = TimeSpan.FromMilliseconds(100),
                        FillBehavior = FillBehavior.Stop
                    };

                    anim.Completed += (s, a) =>
                    {
                        Workbench.Children.Remove(item);
                    };
                    item.BeginAnimation(UIElement.OpacityProperty, anim);
                }
                else
                {
                    foreach (UIElement uiElement in Workbench.Children)
                    {
                        if (uiElement is ElementContentItem && uiElement != item)
                        {
                            ElementContentItem elementItem = uiElement as ElementContentItem;
                            if (CollisionDetection(elementItem, mousePos))
                            {
                                // Find center point between two elements
                                Rect dragItemRect = GetItemRect(item);
                                Rect otherItemRect = GetItemRect(elementItem);
                                Point dragItemCenter = GetCenterPoint(dragItemRect);
                                Point otherItemCenter = GetCenterPoint(otherItemRect);
                                Rect bothItemsRect = new Rect(dragItemCenter, otherItemCenter);
                                Point centerPoint = GetCenterPoint(bothItemsRect);

                                // Do expirement
                                ElementDatabase.ExperimentResult result = _ElementDatabase.DoExperiment(item.Element, elementItem.Element);

                                // Provide feedback for success
                                if (result.Success)
                                {
                                    // Remove items from workbench
                                    Workbench.Children.Remove(elementItem);
                                    Workbench.Children.Remove(item);

                                    // Add results back
                                    AddElements(centerPoint.X, centerPoint.Y, result.ElementsCreated);
                                }

                                // Break out of the foreach (...) so we don't process
                                break;
                            }
                        }
                    }
                }

                // Reset borders
                foreach (UIElement uiElement in Workbench.Children)
                {
                    if (uiElement is ElementContentItem)
                    {
                        (uiElement as ElementContentItem).Collision = false;
                    }
                }

                // Reset RecycleBin background
                RecycleBin.Background = new SolidColorBrush(Colors.Transparent);

                // Done dragging
                Point toolboxOrigin = Toolbox.TranslatePoint(new Point(0, 0), Workbench);
                Rect toolboxBounds = new Rect(toolboxOrigin.X, toolboxOrigin.Y, Toolbox.ActualWidth, Toolbox.ActualHeight);
                if (_DragNew && !!toolboxBounds.Contains(mousePos))
                {
                    Workbench.Children.Remove(_DragStartedItem);
                }
                _DragNew = false;
                _DragStartedItem = null;
            }
        }
 private Rect GetItemRect(ElementContentItem item)
 {
     return new Rect(Canvas.GetLeft(item), Canvas.GetTop(item), item.ActualWidth, item.ActualHeight);
 }
 private void Element_MouseDown(object sender, MouseEventArgs e)
 {
     var item = e.Source as ElementContentItem;
     if (item != null)
     {
         _MouseStartPoint = e.GetPosition(null);
         _ItemStartPoint = new Point(Canvas.GetLeft(item), Canvas.GetTop(item));
         BringToFront(item);
         _DragStartedItem = item;
     }
 }