public void CreateRect(string rectname, Brush rectcolor, double rectopacity, double width, double height, double x, double y)
        {
            List <RectangleNamed> rctngl = SomeUtilities.GetLogicalChildCollection <RectangleNamed>(canvas1);

            for (int i = 0; i < rctngl.Count; i++)
            {
                if (rctngl[i].Name == rectname)
                {
                    return;
                }
            }
            RectangleNamed rect;

            rect             = new RectangleNamed();
            rect.description = rectname;

            rect.Stroke  = new SolidColorBrush(Colors.Black);
            rect.Fill    = rectcolor;
            rect.Width   = width;
            rect.Height  = height;
            rect.Opacity = rectopacity;
            Canvas.SetLeft(rect, x);
            Canvas.SetTop(rect, y);
            canvas1.Children.Add(rect);
            SaveItem(rect);
        }
        public void CreateRect(Element item)
        {
            List <RectangleNamed> rctngl = SomeUtilities.GetLogicalChildCollection <RectangleNamed>(canvas1);

            for (int i = 0; i < rctngl.Count; i++)
            {
                if (rctngl[i].Name == item.name)
                {
                    return;
                }
            }
            RectangleNamed rect;

            rect             = new RectangleNamed();
            rect.description = item.name;

            rect.Stroke  = new SolidColorBrush(Colors.Black);
            rect.Fill    = item.item_fill;
            rect.Opacity = item.opacity;
            rect.Width   = item.size_width;
            rect.Height  = item.size_height;
            Canvas.SetLeft(rect, item.pos_x);
            Canvas.SetTop(rect, item.pos_y);
            canvas1.Children.Add(rect);
        }
 public void SelectRect(string rectname)
 {
     if (selected_rectangle != null)
     {
         selected_rectangle.Unselect();
     }
     selected_rectangle = SomeUtilities.FindChild <RectangleNamed>(canvas1, rectname);
     selected_rectangle.Select();
 }
 //We track that the mouse when going abroad our rectengla did not activate the rectengle over which it may be at this moment
 //отслеживаем чтобы мышь когда выходит за границу нашего ректенгла не активировала ректенгл над которым может оказаться в этот момент
 private void RectangleNamed_MouseMove(object sender, MouseEventArgs e)
 {
     if (!DragInProgress)
     {
         if (sender is RectangleNamed)
         {
             rectangle = (RectangleNamed)sender;
         }
     }
 }
 private void SaveItem(RectangleNamed rect)
 {
     if (rect == null)
     {
         return;
     }
     if (toolbox.currentView.elements.IndexOfKey(rect.description) >= 0)
     {
         SaveItem(rect, toolbox.currentView.elements[rect.description].typeOfElement);
     }
 }
        public void CreateRect(Element element)
        {
            RectangleNamed rect = new RectangleNamed(element);

            rect.MouseMove  += RectangleNamed_MouseMove;
            rect.MouseLeave += RectangleNamed_MouseLeave;
            rect.MouseDown  += RectangleNamed_MouseDown;
            rect.onRectangleNamedSelectedChange += rectangleSelectChanget;
            canvas1.Children.Add(rect);
            SaveItem(rect, element.typeOfElement);
        }
        private void SaveItem(RectangleNamed rect, Element.types typeOfElement)
        {
            Element item = toolbox.currentView.getItem(rect.description);

            if (item == null)
            {
                item = new Element(rect.description, typeOfElement, null, rect.Width, rect.Height, rect.Fill);
            }
            item.applyChanges(rect.Opacity, rect.Fill, rect.Width, rect.Height, Canvas.GetLeft(rect), Canvas.GetTop(rect), rect.getExtra);

            toolbox.currentView.addItem = item;
        }
 public void RectangleNamed_MouseDown(object sender, MouseButtonEventArgs e)
 {
     DragInProgress = true;
     if (sender is RectangleNamed)
     {
         if (selected_rectangle != null)
         {
             SaveItem(rectangle);
             selected_rectangle.Unselect();
             selected_rectangle = null;
         }
         selected_rectangle = (RectangleNamed)sender;
         selected_rectangle.Select();
     }
 }
 private void btn_BringToFront_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (selected_rectangle != null)
         {
             RectangleNamed pToMove = SomeUtilities.FindChild <RectangleNamed>(canvas1, selected_rectangle.Name);
             canvas1.Children.Remove(pToMove);
             canvas1.Children.Add(pToMove);
         }
     }
     catch (Exception)
     {
     }
 }
        private void SaveItem(RectangleNamed rect)
        {
            Element item = new Element();

            item.name                   = rect.description;
            item.opacity                = rect.Opacity;
            item.item_fill              = rect.Fill;
            item.pos_x                  = Canvas.GetLeft(rect);
            item.pos_y                  = Canvas.GetTop(rect);
            item.pos_x_NORMALIZED       = Canvas.GetLeft(rect) / canvas1.Width;
            item.pos_y_NORMALIZED       = Canvas.GetTop(rect) / canvas1.Height;
            item.size_height_NORMALIZED = rect.Height / canvas1.Height;
            item.size_width_NORMALIZED  = rect.Width / canvas1.Width;
            item.size_height            = rect.Height;
            item.size_width             = rect.Width;
            view.addItem                = item;
        }
        // Start dragging.
        public void canvas1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            MouseHitType = SetHitType(rectangle, Mouse.GetPosition(canvas1));
            SetMouseCursor();
            if (selected_rectangle != null)
            {
                if (MouseHitType == HitType.None)
                {
                    if (selected_rectangle != null)
                    {
                        selected_rectangle.Unselect();
                        selected_rectangle = null;
                    }
                    return;
                }
            }

            LastPoint      = Mouse.GetPosition(canvas1);
            DragInProgress = true;
        }
        private RectangleNamed selectRectangleNamed(object e)
        {
            RectangleNamed rectangle = new RectangleNamed();

            try
            {
                if (e is Rectangle)
                {
                    rectangle = (RectangleNamed)((Rectangle)e).Parent;
                }
                if (e is TextBlock)
                {
                    rectangle = (RectangleNamed)((TextBlock)e).Parent;
                }
            }
            catch (Exception err)
            {
                return(null);
            }
            return(rectangle);
        }
        // Start dragging.
        private void canvas1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            MouseHitType = SetHitType(rectangle, Mouse.GetPosition(canvas1));
            SetMouseCursor();

            if (selected_rectangle != null && e.OriginalSource is Canvas)
            {
                selected_rectangle.Unselect();
                NotifySelectionChanged(selected_rectangle.description, selected_rectangle.isSelected);
                selected_rectangle = null;
                return;
            }
            if (MouseHitType == HitType.None)
            {
                return;
            }

            LastPoint      = Mouse.GetPosition(canvas1);
            DragInProgress = true;

            try
            {
                if (selected_rectangle != null)
                {
                    selected_rectangle.Unselect();
                    NotifySelectionChanged(selected_rectangle.description, selected_rectangle.isSelected);
                }
                //Проверяем чтобы был нажат один из элементов на RectangleNamed
                //if (e.OriginalSource is Rectangle)
                //    selected_rectangle = (RectangleNamed)((Rectangle)e.OriginalSource).Parent;
                //if (e.OriginalSource is TextBlock)
                //    selected_rectangle = (RectangleNamed)((TextBlock)e.OriginalSource).Parent;
                selected_rectangle = selectRectangleNamed(e.OriginalSource);
                selected_rectangle.Select();
                NotifySelectionChanged(selected_rectangle.description, selected_rectangle.isSelected);
            }
            catch (Exception err) { }
        }
        // Stop dragging.
        private void canvas1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            DragInProgress = false;

            if (e.OriginalSource is RectangleNamed)
            {
                NotifyPositionChanged(rectangle.description);
                if (selected_rectangle != null)
                {
                    //if (e.OriginalSource is Rectangle)
                    //    rectangle = (RectangleNamed)((Rectangle)e.OriginalSource).Parent;
                    //if (e.OriginalSource is TextBlock)
                    //    rectangle = (RectangleNamed)((TextBlock)e.OriginalSource).Parent;
                    selected_rectangle = selectRectangleNamed(e.OriginalSource);
                    selected_rectangle.Select();
                    NotifySelectionChanged(selected_rectangle.description, selected_rectangle.isSelected);
                }
                else
                {
                    NotifySelectionChanged("None", false);
                }
            }
        }
        public void CreateRect(string rectname, Element.types typeOfElement, Brush rectcolor, double rectopacity, double width, double height, double x, double y, bool horizontalResizing = true, bool verticallResizing = true)
        {
            List <RectangleNamed> rctngl = SomeUtilities.GetLogicalChildCollection <RectangleNamed>(canvas1);

            for (int i = 0; i < rctngl.Count; i++)
            {
                if (rctngl[i].Name == rectname)
                {
                    return;
                }
            }
            RectangleNamed rect;

            rect             = new RectangleNamed(rectname, typeOfElement, rectcolor, rectopacity, width, height, x, y);
            rect.MouseMove  += RectangleNamed_MouseMove;
            rect.MouseLeave += RectangleNamed_MouseLeave;
            rect.MouseDown  += RectangleNamed_MouseDown;
            rect.onRectangleNamedSelectedChange += rectangleSelectChanget;
            rect.horizontalResizing              = horizontalResizing;
            rect.verticallResizing = verticallResizing;
            SaveItem(rect, typeOfElement);
            rect.fillProperties(propertiesbox.getPropertiesByType(typeOfElement));
            canvas1.Children.Add(rect);
        }
        // If a drag is in progress, continue the drag.
        // Otherwise display the correct cursor.
        private void canvas1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!DragInProgress)
            {
                MouseHitType = SetHitType(rectangle, Mouse.GetPosition(canvas1));
                SetMouseCursor();
                //try
                //{
                //    if (e.OriginalSource is Rectangle)
                //        rectangle = (RectangleNamed)((Rectangle)e.OriginalSource).Parent;
                //    if (e.OriginalSource is TextBlock)
                //        rectangle = (RectangleNamed)((TextBlock)e.OriginalSource).Parent;
                //}
                //catch (Exception err) { }
                rectangle = selectRectangleNamed(e.OriginalSource);
            }
            else
            {
                // See how much the mouse has moved.
                Point  point    = Mouse.GetPosition(canvas1);
                double offset_x = point.X - LastPoint.X;
                double offset_y = point.Y - LastPoint.Y;

                // Get the rectangle's current position.
                double new_x      = Canvas.GetLeft(rectangle);
                double new_y      = Canvas.GetTop(rectangle);
                double new_width  = rectangle.Width;
                double new_height = rectangle.Height;

                // Update the rectangle.
                switch (MouseHitType)
                {
                case HitType.Body:
                    new_x += offset_x;
                    new_y += offset_y;
                    break;

                case HitType.UL:
                    new_x      += offset_x;
                    new_y      += offset_y;
                    new_width  -= offset_x;
                    new_height -= offset_y;
                    break;

                case HitType.UR:
                    new_y      += offset_y;
                    new_width  += offset_x;
                    new_height -= offset_y;
                    break;

                case HitType.LR:
                    new_width  += offset_x;
                    new_height += offset_y;
                    break;

                case HitType.LL:
                    new_x      += offset_x;
                    new_width  -= offset_x;
                    new_height += offset_y;
                    break;

                case HitType.L:
                    new_x     += offset_x;
                    new_width -= offset_x;
                    break;

                case HitType.R:
                    new_width += offset_x;
                    break;

                case HitType.B:
                    new_height += offset_y;
                    break;

                case HitType.T:
                    new_y      += offset_y;
                    new_height -= offset_y;
                    break;
                }

                // Don't use negative width or height.
                if ((new_width > 0) && (new_height > 0))
                {
                    // Update the rectangle.

                    //Check that the new coordinates do not go beyond our Canvas.
                    double xc = Canvas.GetLeft(canvas1);
                    if (double.IsNaN(Canvas.GetLeft(canvas1)))
                    {
                        xc = 0;
                    }
                    else
                    {
                        xc = Canvas.GetLeft(canvas1);
                    }

                    double yc = Canvas.GetTop(canvas1);
                    if (double.IsNaN(Canvas.GetTop(canvas1)))
                    {
                        yc = 0;
                    }
                    else
                    {
                        yc = Canvas.GetTop(canvas1);
                    }

                    if (new_x < xc)
                    {
                        new_x = xc;
                    }
                    if (new_y < yc)
                    {
                        new_y = yc;
                    }
                    if (new_x + new_width > canvas1.ActualWidth)
                    {
                        new_x = canvas1.ActualWidth - new_width;
                    }
                    if (new_y + new_height > canvas1.ActualHeight)
                    {
                        new_y = canvas1.ActualHeight - new_height;
                    }

                    Canvas.SetLeft(rectangle, new_x);
                    Canvas.SetTop(rectangle, new_y);
                    if (new_width > canvas1.ActualWidth)
                    {
                        rectangle.Width = canvas1.ActualWidth;
                    }
                    else
                    {
                        rectangle.Width = new_width;
                    }
                    if (new_height > canvas1.ActualHeight)
                    {
                        rectangle.Height = canvas1.ActualHeight;
                    }
                    else
                    {
                        rectangle.Height = new_height;
                    }
                    // Save the mouse's new location.
                    LastPoint = point;
                }
            }
        }
        // Return a HitType value to indicate what is at the point.
        private HitType SetHitType(RectangleNamed rect, Point point)
        {
            double left   = Canvas.GetLeft(rect);
            double top    = Canvas.GetTop(rect);
            double right  = left + rect.Width;
            double bottom = top + rect.Height;

            if (double.IsNaN(left) || double.IsNaN(top) || double.IsNaN(right) || double.IsNaN(bottom))
            {
                return(HitType.None);
            }
            if (point.X < left)
            {
                return(HitType.None);
            }
            if (point.X > right)
            {
                return(HitType.None);
            }
            if (point.Y < top)
            {
                return(HitType.None);
            }
            if (point.Y > bottom)
            {
                return(HitType.None);
            }

            const double GAP = 10;

            if (point.X - left < GAP)
            {
                // Left edge.
                if (point.Y - top < GAP)
                {
                    return(HitType.UL);
                }
                if (bottom - point.Y < GAP)
                {
                    return(HitType.LL);
                }
                return(HitType.L);
            }
            if (right - point.X < GAP)
            {
                // Right edge.
                if (point.Y - top < GAP)
                {
                    return(HitType.UR);
                }
                if (bottom - point.Y < GAP)
                {
                    return(HitType.LR);
                }
                return(HitType.R);
            }
            if (point.Y - top < GAP)
            {
                return(HitType.T);
            }
            if (bottom - point.Y < GAP)
            {
                return(HitType.B);
            }
            return(HitType.Body);
        }
        // Return a HitType value to indicate what is at the point.
        private HitType SetHitType(RectangleNamed rect, Point point)
        {
            if (rect == null)
            {
                return(HitType.None);
            }
            double left   = Canvas.GetLeft(rect);
            double top    = Canvas.GetTop(rect);
            double right  = left + rect.Width;
            double bottom = top + rect.Height;

            if (double.IsNaN(left) || double.IsNaN(top) || double.IsNaN(right) || double.IsNaN(bottom))
            {
                return(HitType.None);
            }
            if (point.X < left)
            {
                return(HitType.None);
            }
            if (point.X > right)
            {
                return(HitType.None);
            }
            if (point.Y < top)
            {
                return(HitType.None);
            }
            if (point.Y > bottom)
            {
                return(HitType.None);
            }

            const double GAP = 10;

            if (point.X - left < GAP && rectangle.horizontalResizing)
            {
                // Left edge.
                if (point.Y - top < GAP && rectangle.verticallResizing)
                {
                    return(HitType.UL);
                }
                if (bottom - point.Y < GAP && rectangle.verticallResizing)
                {
                    return(HitType.LL);
                }
                return(HitType.L);
            }
            if (right - point.X < GAP && rectangle.horizontalResizing)
            {
                // Right edge.
                if (point.Y - top < GAP && rectangle.verticallResizing)
                {
                    return(HitType.UR);
                }
                if (bottom - point.Y < GAP && rectangle.verticallResizing)
                {
                    return(HitType.LR);
                }
                return(HitType.R);
            }
            //if (rectangle.verticallResizing)
            //{
            if (point.Y - top < GAP && rectangle.verticallResizing)
            {
                return(HitType.T);
            }
            if (bottom - point.Y < GAP && rectangle.verticallResizing)
            {
                return(HitType.B);
            }
            return(HitType.Body);
            //}

            //return HitType.None;
        }