示例#1
0
        private void DragTop(double scale, DesignerItem item, SelectionService selectionService)
        {
            IEnumerable <DesignerItem> groupItems = selectionService.GetGroupMembers(item).Cast <DesignerItem>();
            double groupBottom = Canvas.GetTop(item) + item.Height;

            foreach (DesignerItem groupItem in groupItems)
            {
                double groupItemTop = Canvas.GetTop(groupItem);
                double delta        = (groupBottom - groupItemTop) * (scale - 1);
                Canvas.SetTop(groupItem, groupItemTop - delta);
                groupItem.Height = groupItem.ActualHeight * scale;
            }
        }
示例#2
0
        private void DragLeft(double scale, DesignerItem item, SelectionService selectionService)
        {
            IEnumerable <DesignerItem> groupItems = selectionService.GetGroupMembers(item).Cast <DesignerItem>();

            double groupLeft = Canvas.GetLeft(item) + item.Width;

            foreach (DesignerItem groupItem in groupItems)
            {
                double groupItemLeft = Canvas.GetLeft(groupItem);
                double delta         = (groupLeft - groupItemLeft) * (scale - 1);
                Canvas.SetLeft(groupItem, groupItemLeft - delta);
                groupItem.Width = groupItem.ActualWidth * scale;
            }
        }
        private static DesignerItem DeserializeDesignerItem(XElement itemXML, Guid id, double OffsetX, double OffsetY)
        {
            DesignerItem item = new DesignerItem(id);

            item.Width    = Double.Parse(itemXML.Element("Width").Value, CultureInfo.InvariantCulture);
            item.Height   = Double.Parse(itemXML.Element("Height").Value, CultureInfo.InvariantCulture);
            item.ParentID = new Guid(itemXML.Element("ParentID").Value);
            item.IsGroup  = Boolean.Parse(itemXML.Element("IsGroup").Value);
            Canvas.SetLeft(item, Double.Parse(itemXML.Element("Left").Value, CultureInfo.InvariantCulture) + OffsetX);
            Canvas.SetTop(item, Double.Parse(itemXML.Element("Top").Value, CultureInfo.InvariantCulture) + OffsetY);
            Canvas.SetZIndex(item, Int32.Parse(itemXML.Element("zIndex").Value));
            Object content = XamlReader.Load(XmlReader.Create(new StringReader(itemXML.Element("Content").Value)));

            item.Content = content;
            return(item);
        }
示例#4
0
        void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem   designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer     = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft = double.MaxValue;
                double minTop  = double.MaxValue;

                // we only move DesignerItems
                var designerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>();

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
                    minTop  = double.IsNaN(top) ? 0 : Math.Min(top, minTop);
                }

                double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                double deltaVertical   = Math.Max(-minTop, e.VerticalChange);

                foreach (DesignerItem item in designerItems)
                {
                    double left = Canvas.GetLeft(item);
                    double top  = Canvas.GetTop(item);

                    if (double.IsNaN(left))
                    {
                        left = 0;
                    }
                    if (double.IsNaN(top))
                    {
                        top = 0;
                    }

                    Canvas.SetLeft(item, left + deltaHorizontal);
                    Canvas.SetTop(item, top + deltaVertical);
                }

                designer.InvalidateMeasure();
                e.Handled = true;
            }
        }
示例#5
0
        protected override void OnDrop(DragEventArgs e)
        {
            base.OnDrop(e);
            DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;

            if (dragObject != null && !String.IsNullOrEmpty(dragObject.Xaml))
            {
                DesignerItem newItem = null;
                Object       content = XamlReader.Load(XmlReader.Create(new StringReader(dragObject.Xaml)));

                if (content != null)
                {
                    newItem         = new DesignerItem();
                    newItem.Content = content;

                    Point position = e.GetPosition(this);

                    //if (dragObject.DesiredSize.HasValue)
                    if (!double.IsNaN(dragObject.DesiredSize.Value.Width) && !double.IsNaN(dragObject.DesiredSize.Value.Height))
                    {
                        Size desiredSize = dragObject.DesiredSize.Value;
                        newItem.Width  = desiredSize.Width;
                        newItem.Height = desiredSize.Height;

                        DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X - newItem.Width / 2));
                        DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y - newItem.Height / 2));
                    }
                    else
                    {
                        DesignerCanvas.SetLeft(newItem, Math.Max(0, position.X));
                        DesignerCanvas.SetTop(newItem, Math.Max(0, position.Y));
                    }

                    Canvas.SetZIndex(newItem, this.Children.Count);
                    this.Children.Add(newItem);
                    //update selection
                    this.SelectionService.SelectItem(newItem);
                    newItem.Focus();
                }

                e.Handled = true;
            }
        }
        private void UpdateSelection()
        {
            designerCanvas.SelectionService.ClearSelection();

            Rect rubberBand = new Rect(startPoint.Value, endPoint.Value);

            foreach (Control item in designerCanvas.Children)
            {
                Rect itemRect = VisualTreeHelper.GetDescendantBounds(item);
                //itemRect.Width = itemRect.Width / 2;
                //itemRect.Height = itemRect.Height / 2;
                Rect itemBounds = item.TransformToAncestor(designerCanvas).TransformBounds(itemRect);

                if (rubberBand.Contains(itemBounds))
                {
                    DesignerItem di = item as DesignerItem;
                    if (di.ParentID == Guid.Empty)
                    {
                        designerCanvas.SelectionService.AddToSelection(di);
                    }
                }
            }
        }
        private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            XElement root = LoadSerializedDataFromFile();

            if (root == null)
            {
                return;
            }

            this.Children.Clear();
            this.SelectionService.ClearSelection();

            IEnumerable <XElement> itemsXML = root.Elements("DesignerItems").Elements("DesignerItem");

            foreach (XElement itemXML in itemsXML)
            {
                Guid         id   = new Guid(itemXML.Element("ID").Value);
                DesignerItem item = DeserializeDesignerItem(itemXML, id, 0, 0);
                this.Children.Add(item);
            }

            this.InvalidateVisual();
        }
示例#8
0
        void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            DesignerItem   designerItem = this.DataContext as DesignerItem;
            DesignerCanvas designer     = VisualTreeHelper.GetParent(designerItem) as DesignerCanvas;

            if (designerItem != null && designer != null && designerItem.IsSelected)
            {
                double minLeft, minTop, minDeltaHorizontal, minDeltaVertical;
                double dragDeltaVertical, dragDeltaHorizontal, scale;

                IEnumerable <DesignerItem> selectedDesignerItems = designer.SelectionService.CurrentSelection.OfType <DesignerItem>();

                CalculateDragLimits(selectedDesignerItems, out minLeft, out minTop,
                                    out minDeltaHorizontal, out minDeltaVertical);

                foreach (DesignerItem item in selectedDesignerItems)
                {
                    if (item != null && item.ParentID == Guid.Empty)
                    {
                        switch (base.VerticalAlignment)
                        {
                        case VerticalAlignment.Bottom:
                            dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
                            scale             = (item.ActualHeight - dragDeltaVertical) / item.ActualHeight;
                            DragBottom(scale, item, designer.SelectionService);
                            break;

                        case VerticalAlignment.Top:
                            double top = Canvas.GetTop(item);
                            dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
                            scale             = (item.ActualHeight - dragDeltaVertical) / item.ActualHeight;
                            DragTop(scale, item, designer.SelectionService);
                            break;

                        default:
                            break;
                        }

                        switch (base.HorizontalAlignment)
                        {
                        case HorizontalAlignment.Left:
                            double left = Canvas.GetLeft(item);
                            dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
                            scale = (item.ActualWidth - dragDeltaHorizontal) / item.ActualWidth;
                            DragLeft(scale, item, designer.SelectionService);
                            break;

                        case HorizontalAlignment.Right:
                            dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
                            scale = (item.ActualWidth - dragDeltaHorizontal) / item.ActualWidth;
                            DragRight(scale, item, designer.SelectionService);
                            break;

                        default:
                            break;
                        }
                    }
                }
                e.Handled = true;
            }
        }