Esempio n. 1
0
        public override void MouseDown(object sender, MouseButtonEventArgs e)
        {
#if !SILVERLIGHT
            if (e.ClickCount == 2)
            {
                Drawing.CoordinateSystem.ZoomExtend();
                return;
            }
#endif
            offsetFromFigureLeftTopCorner = Coordinates(e, false, false, false);
            oldCoordinates         = offsetFromFigureLeftTopCorner;
            coordinatesOnMouseDown = offsetFromFigureLeftTopCorner;
            startedMoving          = false;

            moving = new List <IMovable>();
            IEnumerable <IFigure> roots = null;
            bool isLocked = false;

            found = Drawing.Figures.HitTest(offsetFromFigureLeftTopCorner);

            IMovable oneMovable = found as IMovable;
            if (oneMovable != null)
            {
                if (!found.Locked)
                {
                    if (oneMovable is IPoint)
                    {
                        // when we drag a point, we want it to snap to the cursor
                        // so that the point center is directly under the tip of the mouse
                        offsetFromFigureLeftTopCorner = new Point();
                        oldCoordinates = oneMovable.Coordinates;
                    }
                    else
                    {
                        // however when we drag other stuff (such as text labels)
                        // we want the mouse to always touch the part of the draggable
                        // where it first touched during MouseDown
                        // we don't want the draggable to "snap" to the cursor like points do
                        offsetFromFigureLeftTopCorner = offsetFromFigureLeftTopCorner.Minus(oneMovable.Coordinates);
                    }
                    roots = DependencyAlgorithms.FindRoots(f => f.Dependents, found);
                    if (roots.All(root => (!root.Locked)))
                    {
                        moving.Add(oneMovable);
                        roots = found.AsEnumerable();
                    }
                    else
                    {
                        isLocked = true;
                    }
                }
                else
                {
                    isLocked = true;
                }
            }
            else if (found != null && !found.Locked)
            {
                if (!found.Locked)
                {
                    roots = DependencyAlgorithms.FindRoots(f => f.Dependencies, found);
                    if (roots.All(root => root is IMovable))
                    {
                        if (roots.All(root => ((IMovable)root).AllowMove()))
                        {
                            moving.AddRange(roots.OfType <IMovable>());
                        }
                        else
                        {
                            isLocked = true;
                        }
                    }
                }
                else
                {
                    isLocked = true;
                }
            }

            if (roots != null)
            {
                toRecalculate = DependencyAlgorithms.FindDescendants(f => f.Dependents, roots);
                toRecalculate.Reverse();
            }
            else
            {
                toRecalculate = null;
            }

            if (moving.IsEmpty() && !isLocked && !Drawing.CoordinateGrid.Locked)
            {
                moving.Add(Drawing.CoordinateSystem);
                //var allFigures = Drawing.Figures.GetAllFiguresRecursive();
                //roots = DependencyAlgorithms.FindRoots(f => f.Dependencies, allFigures);
                //moving.AddRange(roots.OfType<IMovable>());
                //roots = null;
                toRecalculate = null; // Figures;
            }
        }
Esempio n. 2
0
 public override void MoveToCore(Point newPosition)
 {
     Offset = newPosition.Minus(Origin);
     base.MoveToCore(newPosition);
 }
Esempio n. 3
0
        bool mousePressed = false; //CC
        //MouseButton pressedButton = MouseButton.Left; //CC

        public override void MouseDown(object sender, MouseButtonEventArgs e)
        {
            var  canvas   = this.ParentCanvas as Canvas;
            bool isLocked = false;

            moving = new List <IMovable>();
            IEnumerable <IFigure> roots = null;

#if !SILVERLIGHT
            if (e.ChangedButton == MouseButton.Left)
            {
                offsetFromFigureLeftTopCorner = Coordinates(e, false, false, false);
                oldCoordinates         = offsetFromFigureLeftTopCorner;
                coordinatesOnMouseDown = offsetFromFigureLeftTopCorner;
                startedMoving          = false;

                //found = Drawing.Figures.MouseHover(offsetFromFigureLeftTopCorner);

                found = Drawing.Figures.HitTest(offsetFromFigureLeftTopCorner);
                IMovable oneMovable = found as IMovable;
                //canvas.Cursor = Cursors.Arrow;
                if (oneMovable != null)
                {
                    if (!found.Locked)
                    {
                        if (oneMovable is IPoint)
                        {
                            // when we drag a point, we want it to snap to the cursor
                            // so that the point center is directly under the tip of the mouse
                            offsetFromFigureLeftTopCorner = new Point();
                            oldCoordinates = oneMovable.Coordinates;
                        }
                        else
                        {
                            // however when we drag other stuff (such as text labels)
                            // we want the mouse to always touch the part of the draggable
                            // where it first touched during MouseDown
                            // we don't want the draggable to "snap" to the cursor like points do
                            offsetFromFigureLeftTopCorner = offsetFromFigureLeftTopCorner.Minus(oneMovable.Coordinates);
                        }
                        roots = DependencyAlgorithms.FindRoots(f => f.Dependents, found);
                        if (roots.All(root => (!root.Locked)))
                        {
                            moving.Add(oneMovable);
                            roots = found.AsEnumerable();
                        }
                        else
                        {
                            isLocked = true;
                        }
                    }
                    else
                    {
                        isLocked = true;
                    }
                }
                else if (found != null && !found.Locked)
                {
                    if (!found.Locked)
                    {
                        roots = DependencyAlgorithms.FindRoots(f => f.Dependencies, found);
                        if (roots.All(root => root is IMovable))
                        {
                            if (roots.All(root => ((IMovable)root).AllowMove()))
                            {
                                moving.AddRange(roots.OfType <IMovable>());
                            }
                            else
                            {
                                isLocked = true;
                            }
                        }
                    }
                    else
                    {
                        isLocked = true;
                    }
                }

                if (roots != null)
                {
                    toRecalculate = DependencyAlgorithms.FindDescendants(f => f.Dependents, roots);
                    toRecalculate.Reverse();
                }
                else
                {
                    toRecalculate = null;
                }
            }
            // Chuột giữa
            else if (e.ChangedButton == MouseButton.Middle)
            {
                canvas.Cursor = Cursors.Hand;

                // zoom extend
                if (e.ClickCount == 2)
                {
                    Drawing.CoordinateSystem.ZoomExtend();
                    return;
                }
                // Di chuyển lưới
                if (moving.IsEmpty() && !isLocked && !Drawing.CoordinateGrid.Locked)
                {
                    moving.Add(Drawing.CoordinateSystem);
                    //var allFigures = Drawing.Figures.GetAllFiguresRecursive();
                    //roots = DependencyAlgorithms.FindRoots(f => f.Dependencies, allFigures);
                    //moving.AddRange(roots.OfType<IMovable>());
                    //roots = null;
                    //toRecalculate = null; // Figures;
                }
            }
#endif

            // Chuột phải hiển thị context menu
            else if (e.ChangedButton == MouseButton.Right)
            {
                ContextMenu menu = new ContextMenu();
                MenuItem    file = new MenuItem()
                {
                    Header = "Reactor"
                };
                MenuItem options = new MenuItem()
                {
                    Header = "Options", InputGestureText = "Ctrl+O"
                };
                options.Click += options_Click;

                menu.Items.Add(file);
                menu.Items.Add(new Separator());
                menu.Items.Add(options);

                var open = new MenuItem()
                {
                    Header = "_Open", InputGestureText = "Ctrl+O"
                };
                open.Click += Open_Click;
                var save = new MenuItem()
                {
                    Header = "_Save...", InputGestureText = "Ctrl+S"
                };
                //save.Click += Save_Click;
                var print = new MenuItem()
                {
                    Header = "Print"
                };
                //print.Click += Print_Click;
                var exit = new MenuItem()
                {
                    Header = "Exit"
                };
                //exit.Click += Exit_Click;

                var items = new UIElement[] {
                    open,
                    save,
                    print,
                    new Separator(),
                    exit
                };

                file.ItemsSource = items;

                canvas.ContextMenu = menu;
            }
        }