private void DrawArea_LeftMouseDown(object sender, MouseButtonEventArgs e)
        {
            var vm       = DataContext as PolygonsViewModel;
            var drawArea = sender as ItemsControl;
            var pos      = e.GetPosition(drawArea);
            int x        = (int)(pos.X / drawArea.ActualWidth * vm.BitmapWidth);
            int y        = (int)(pos.Y / drawArea.ActualHeight * vm.BitmapHeight);

            if (vm.IsCreating)
            {
                if (vm.SelectedPolygon.Points.Count > 3)
                {
                    var dist = PointsHelpers.Distance(vm.SelectedPolygon.Points[0].X, vm.SelectedPolygon.Points[0].Y, x, y);
                    Debug.WriteLine("Distance: " + dist.ToString());
                }

                if (vm.SelectedPolygon.Points.Count >= 3 && PointsHelpers.Distance(vm.SelectedPolygon.Points[0].X, vm.SelectedPolygon.Points[0].Y, x, y) <= 15)
                {
                    vm.IsCreating = false;
                    vm.SelectedPolygon.RenderBitmap();
                }
                else
                {
                    vm.SelectedPolygon.AddPoint(new DragablePoint(x, y));
                    vm.SelectedPolygon.RenderBitmap(true);
                }
            }
            else
            {
                Polygon clicked = null;
                foreach (var poly in vm.Polygons.Reverse())
                {
                    if (poly.IsHit(new Point(x, y)))
                    {
                        clicked = poly;
                        break;
                    }
                }
                vm.SelectedPolygon = clicked;
                if (clicked == null)
                {
                    Debug.WriteLine("Selected: null");
                }
                else
                {
                    Debug.WriteLine("Selected:" + clicked.Name);
                }
                if (vm.SelectedPolygon != null)
                {
                    if (e.LeftButton == MouseButtonState.Pressed)
                    {
                        if (!vm.IsAddingRelation)
                        {
                            foreach (var pnt in vm.SelectedPolygon.Points)
                            {
                                if (pnt.IsHit(x, y, 6))
                                {
                                    if (!pnt.IsBlocked)
                                    {
                                        selectedPoint     = pnt;
                                        dragStartPoint    = new Point(x, y);
                                        vm.IsPointDragged = true;
                                        return;
                                    }
                                }
                            }
                        }

                        foreach (var line in vm.SelectedPolygon.Lines)
                        {
                            if (line.IsHit(x, y, 6))
                            {
                                if (vm.IsAddingRelation)
                                {
                                    if (line.Relation == Relation.NONE)
                                    {
                                        vm.AddRelationEnd(line);

                                        vm.SelectedPolygon.FixRelations(line.Second.IsBlocked ? line.First : line.Second);
                                        vm.SelectedPolygon.RenderBitmap();
                                    }
                                }
                                else
                                {
                                    if (!(line.First.IsBlocked || line.Second.IsBlocked))
                                    {
                                        dragStartPoint   = new Point(x, y);
                                        selectedLine     = line;
                                        lineInitial      = (new DragablePoint(line.First.X, line.First.Y), new DragablePoint(line.Second.X, line.Second.Y));
                                        vm.IsLineDragged = true;
                                        return;
                                    }
                                }
                            }
                        }

                        if (!vm.IsAddingRelation && !vm.SelectedPolygon.IsAnyPointBlocked)
                        {
                            dragStartPoint = new Point(x, y);
                            vm.IsMoving    = true;
                        }
                    }
                }
            }
        }
Example #2
0
 public bool IsHit(int x1, int y1, int margin)
 {
     return(PointsHelpers.Distance(X, Y, x1, y1) <= margin);
 }
Example #3
0
 public bool IsHit(int x, int y, int margin)
 {
     return(PointsHelpers.DistanceToLine(First.X, First.Y,
                                         Second.X, Second.Y, x, y) <= margin);
 }