Ejemplo n.º 1
0
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if (this.DataContext is PageItem)
            {
                PageItem Page = this.DataContext as PageItem;
                Point    p    = SnapGrid.Snap(e.GetPosition(this), GridSize, GridOffsetLeft, GridOffsetTop);

                if (this.IsMouseCaptured)
                {
                    if ((currentLineItem.X1 == p.X && currentLineItem.Y1 == p.Y) ||
                        (currentLineItem.X1 == currentLineItem.X2 && currentLineItem.Y1 == currentLineItem.Y2))
                    {
                        Page.Items.Remove(currentLineItem);
                        currentLineItem = null;
                        this.ReleaseMouseCapture();
                    }
                    else
                    {
                        currentLineItem.X2 = p.X;
                        currentLineItem.Y2 = p.Y;
                        this.ReleaseMouseCapture();
                    }
                }
            }
        }
        void LineAdorner_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!IsControlModeOn)
            {
                if (this.IsMouseCaptured)
                {
                    CustomLineElement line     = this.AdornedElement as CustomLineElement;
                    LineLogicItem     lineItem = line.DataContext as LineLogicItem;
                    Point             p        = SnapGrid.Snap(e.GetPosition(line), gridSize, GridOffsetLeft, GridOffsetTop);

                    if (IsStartPoint)
                    {
                        lineItem.X1 = p.X;
                        lineItem.Y1 = p.Y;
                    }
                    else
                    {
                        lineItem.X2 = p.X;
                        lineItem.Y2 = p.Y;
                    }

                    // update line item modification flag
                    //if (originalLineItem.CheckForModification(lineItem))
                    //    lineItem.IsModified = true;

                    this.InvalidateVisual();
                    this.ReleaseMouseCapture();
                    this.Cursor = Cursors.Arrow;
                }
            }
        }
Ejemplo n.º 3
0
        //private bool IsControlModeOn = false;

        Point GetClickLocation(Point p, double offsetLeft, double offsetTop)
        {
            return(new Point()
            {
                X = SnapGrid.Snap(p.X, GridSizeCreate, offsetLeft),
                Y = SnapGrid.Snap(p.Y, GridSizeCreate, offsetTop)
            });
        }
        void LineAdorner_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.IsMouseCaptured)
            {
                if (this.AdornedElement.GetType() == typeof(CustomLineElement))
                {
                    CustomLineElement line     = this.AdornedElement as CustomLineElement;
                    LineLogicItem     lineItem = line.DataContext as LineLogicItem;

                    Point p = SnapGrid.Snap(e.GetPosition(line), gridSize, GridOffsetLeft, GridOffsetTop);

                    // edit mode: move line start point OR move line end point
                    if (mode == LineAdornerMode.Edit)
                    {
                        if (IsStartPoint)
                        {
                            lineItem.X1 = p.X;
                            lineItem.Y1 = p.Y;
                        }
                        else
                        {
                            lineItem.X2 = p.X;
                            lineItem.Y2 = p.Y;
                        }
                    }

                    // move mode: move line start point AND move line end point

                    if (mode == LineAdornerMode.Move)
                    {
                        if (IsStartPoint)
                        {
                            double dX   = lineItem.X1 - p.X;
                            double dY   = lineItem.Y1 - p.Y;
                            Point  pEnd = new Point(lineItem.X2 - dX, lineItem.Y2 - dY);

                            lineItem.X1 = p.X;
                            lineItem.Y1 = p.Y;
                            lineItem.X2 = pEnd.X;
                            lineItem.Y2 = pEnd.Y;
                        }
                        else
                        {
                            double dX     = lineItem.X2 - p.X;
                            double dY     = lineItem.Y2 - p.Y;
                            Point  pStart = new Point(lineItem.X1 - dX, lineItem.Y1 - dY);

                            lineItem.X2 = p.X;
                            lineItem.Y2 = p.Y;
                            lineItem.X1 = pStart.X;
                            lineItem.Y1 = pStart.Y;
                        }
                    }

                    this.InvalidateVisual();
                }
            }
        }
Ejemplo n.º 5
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.DataContext is PageItem)
            {
                PageItem Page = this.DataContext as PageItem;
                Point    p    = SnapGrid.Snap(e.GetPosition(this), GridSize, GridOffsetLeft, GridOffsetTop);

                if (this.IsMouseCaptured)
                {
                    currentLineItem.X2 = p.X;
                    currentLineItem.Y2 = p.Y;
                }
            }
        }
        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            var item = (sender as FrameworkElement).DataContext as Item;

            if (item is LineLogicItem)
            {
                // ILine line = item as ILine;
            }
            else if (item is DataBlockLogicItem)
            {
                SnapDataBlockLogicItem(item, e.HorizontalChange, e.VerticalChange, DataBlockSnapMode.Auto);
                //item.IsModified = true;
            }
            else
            {
                double left = item.X + e.HorizontalChange;
                double top  = item.Y + e.VerticalChange;

                item.X = SnapGrid.Snap(left, GridSize, GridOffsetLeft);
                item.Y = SnapGrid.Snap(top, GridSize, GridOffsetTop);

                //item.IsModified = true;
            }
        }
        void LineAdorner_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                IsControlModeOn = true;
            }

            CustomLineElement line     = this.AdornedElement as CustomLineElement;
            LineLogicItem     lineItem = line.DataContext as LineLogicItem;
            Point             p        = SnapGrid.Snap(e.GetPosition(line), GridSize, GridOffsetLeft, GridOffsetTop);

            double dStart = 0.0;
            double dEnd   = 0.0;

            if (!this.IsMouseCaptured)
            {
                // copy original coordinates
                originalLineItem = new LineLogicItem()
                {
                    X1 = lineItem.X1,
                    Y1 = lineItem.Y1,
                    X2 = lineItem.X2,
                    Y2 = lineItem.Y2
                };

                // calculate start/end point coordinates
                dStart = Math.Sqrt(Math.Pow(lineItem.X1 - p.X, 2) + Math.Pow(lineItem.Y1 - p.Y, 2));
                dEnd   = Math.Sqrt(Math.Pow(lineItem.X2 - p.X, 2) + Math.Pow(lineItem.Y2 - p.Y, 2));
            }

            if (IsControlModeOn)
            {
                if (this.IsMouseCaptured)
                {
                    if (IsStartPoint)
                    {
                        lineItem.X1 = p.X;
                        lineItem.Y1 = p.Y;
                    }
                    else
                    {
                        lineItem.X2 = p.X;
                        lineItem.Y2 = p.Y;
                    }

                    this.InvalidateVisual();
                    this.ReleaseMouseCapture();

                    IsControlModeOn = false;
                }
                else
                {
                    IsStartPoint = dStart < dEnd ? true : false;
                    this.Cursor  = Cursors.Hand;
                    this.InvalidateVisual();
                    this.CaptureMouse();
                }
            }
            else
            {
                if (!this.IsMouseCaptured)
                {
                    IsStartPoint = dStart < dEnd ? true : false;
                    this.Cursor  = Cursors.Hand;
                    this.InvalidateVisual();
                    this.CaptureMouse();
                }
            }
        }
Ejemplo n.º 8
0
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (this.DataContext is PageItem)
            {
                //if (Keyboard.Modifiers == ModifierKeys.Control)
                //    IsControlModeOn = true;

                /*
                 * PageItem Page = this.DataContext as PageItem;
                 * Point p = GetClickLocation(e.GetPosition(this), GridOffsetLeft, GridOffsetTop);
                 * Page.Items.Add(new AndGateLogicItem()
                 * {
                 *  IsNew = true,
                 *  IsModified = false,
                 *  IsDeleted = false,
                 *  X = p.X,
                 *  Y = p.Y,
                 *  Z = 1
                 * });
                 */

                PageItem Page = this.DataContext as PageItem;
                Page.SelectedItem = null;

                Point p = SnapGrid.Snap(e.GetPosition(this), GridSize, GridOffsetLeft, GridOffsetTop);
                if (this.IsMouseCaptured)
                {
                    //currentLineItem.X2 = p.X;
                    //currentLineItem.Y2 = p.Y;
                    //c.ReleaseMouseCapture();
                }
                else
                {
                    Point pHit = e.GetPosition(this);

                    CustomLineElement lineHit = null;
                    HitTestResult     result  = VisualTreeHelper.HitTest(this, pHit);
                    if (result != null)
                    {
                        if (result.VisualHit.GetType() == typeof(CustomLineElement))
                        {
                            lineHit = result.VisualHit as CustomLineElement;
                        }
                    }

                    if (lineHit == null && selectedLine == null)
                    {
                        currentLineItem = new LineLogicItem()
                        {
                            IsNew      = false,
                            IsModified = false,
                            IsDeleted  = false,
                            X1         = p.X,
                            Y1         = p.Y,
                            X2         = p.X,
                            Y2         = p.Y,
                            Z          = 0
                        };

                        Page.Items.Add(currentLineItem);
                        this.CaptureMouse();
                    }

                    if (lineHit != selectedLine && selectedLine != null)
                    {
                        if (Keyboard.Modifiers != ModifierKeys.Control)
                        {
                            var LineAdornerLayer = AdornerLayer.GetAdornerLayer(selectedLine);
                            if (LineAdornerLayer != null)
                            {
                                LineAdornerLayer.Remove(LineAdornerLayer.GetAdorners(selectedLine)[0]);
                            }

                            selectedLine = null;
                        }
                    }

                    if (lineHit != null && lineHit != selectedLine)
                    {
                        selectedLine = lineHit;
                        var LineAdornerLayer = AdornerLayer.GetAdornerLayer(lineHit);

                        if (LineAdornerLayer != null)
                        {
                            LineAdornerLayer.Add(new LineAdorner(lineHit));
                        }
                    }

                    //currentLineItem = new LineLogicItem() { X1 = p.X, Y1 = p.Y, X2 = p.X, Y2 = p.Y, Z = 0 };
                    //Page.Items.Add(currentLineItem);
                    //c.CaptureMouse();
                }
            }
        }
Ejemplo n.º 9
0
        protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
        {
            if (this.DataContext is PageItem)
            {
                PageItem Page = this.DataContext as PageItem;
                Point    p    = SnapGrid.Snap(e.GetPosition(this), GridSize, GridOffsetLeft, GridOffsetTop);

                if (this.IsMouseCaptured)
                {
                    Page.Items.Remove(currentLineItem);
                    currentLineItem = null;
                    this.ReleaseMouseCapture();
                }
                else
                {
                    // delete line element

                    /*
                     * Point pHit = e.GetPosition(this);
                     *
                     * CustomLineElement lineHit = null;
                     * HitTestResult result = VisualTreeHelper.HitTest(this, pHit);
                     * if (result != null)
                     * {
                     *  if (result.VisualHit.GetType() == typeof(CustomLineElement))
                     *  {
                     *      lineHit = result.VisualHit as CustomLineElement;
                     *      Page.Items.Remove(lineHit.DataContext as LineLogicItem);
                     *  }
                     * }
                     */
                }

                /*
                 * Canvas c = sender as Canvas;
                 * Point p = SnapGrid.Snap(e.GetPosition(this), GridSize, GridOffsetLeft, GridOffsetTop);
                 * CustomLineElement lineHit = null;
                 *
                 * HitTestResult result = VisualTreeHelper.HitTest(c, p);
                 * if (result != null)
                 * {
                 *  if (result.VisualHit.GetType() == typeof(CustomLineElement))
                 *  {
                 *      lineHit = result.VisualHit as CustomLineElement;
                 *  }
                 * }
                 *
                 * if (selectedLine != null && lineHit != selectedLine)
                 * {
                 *  var LineAdornerLayer = AdornerLayer.GetAdornerLayer(selectedLine);
                 *  LineAdornerLayer.Remove(LineAdornerLayer.GetAdorners(selectedLine)[0]);
                 *  selectedLine = null;
                 * }
                 *
                 * if (lineHit != null && lineHit != selectedLine)
                 * {
                 *  selectedLine = lineHit;
                 *  var LineAdornerLayer = AdornerLayer.GetAdornerLayer(lineHit);
                 *  LineAdornerLayer.Add(new LineAdorner(lineHit));
                 * }
                 */
            }
        }
        void SnapDataBlockLogicItem(ILocation locacion, double dX, double dY, DataBlockSnapMode mode)
        {
            double left = SnapGrid.Snap(locacion.X + dX, UnitConverter.CmToDip(0.1));
            //double top = SnapGrid.Snap(locacion.Y + dY, UnitConverter.CmToDip(1.0), UnitConverter.CmToDip(0.1));
            double top = UnitConverter.CmToDip(Math.Floor(UnitConverter.DipToCm(locacion.Y + dY)) + 0.1);

            switch (mode)
            {
            case DataBlockSnapMode.Input:
            {
                left = UnitConverter.CmToDip(1.3);
            }
            break;

            case DataBlockSnapMode.Output:
            {
                left = UnitConverter.CmToDip(31.2);
            }
            break;

            case DataBlockSnapMode.Auto:
            {
                if (left < UnitConverter.CmToDip(1.3))
                {
                    // X < 1.3cm
                    left = UnitConverter.CmToDip(1.3);
                }
                else if (left >= UnitConverter.CmToDip(1.3) && left < UnitConverter.CmToDip(11.2))
                {
                    // 1.3cm >= X > 11.2cm
                    left = UnitConverter.CmToDip(1.3);
                }
                else if (left >= UnitConverter.CmToDip(11.2) && left < UnitConverter.CmToDip(31.2))
                {
                    // 11.2cm >= X > 31.2cm
                    if (left <= UnitConverter.CmToDip(21.2))
                    {
                        // move to inputs
                        left = UnitConverter.CmToDip(1.3);
                    }
                    else
                    {
                        // move to outputs
                        left = UnitConverter.CmToDip(31.2);
                    }
                }
                else
                {
                    // X >= 31.2cm
                    left = UnitConverter.CmToDip(31.2);
                }
            }
            break;
            }

            if (top <= UnitConverter.CmToDip(2.1))
            {
                // Y <= 2.1cm
                top = UnitConverter.CmToDip(2.1);
            }
            else if (top >= UnitConverter.CmToDip(25.1))
            {
                // Y >= 25.1
                top = UnitConverter.CmToDip(25.1);
            }
            else
            {
                // 2.1cm < Y < 25.1cm
                //top = top + UnitConverter.CmToDip(0.1);
            }

            locacion.X = left;
            locacion.Y = top;
        }