diff --git a/PPcurry/BoardGrid.cs b/PPcurry/BoardGrid.cs index bef387d..b87206b 100644 --- a/PPcurry/BoardGrid.cs +++ b/PPcurry/BoardGrid.cs @@ -254,7 +254,7 @@ private void BoardGrid_Drop(object sender, DragEventArgs e) Vector anchor = component.GetAnchors()[0]; // One anchor must be superposed with a node Node gridNode = Magnetize(relativePosition - component.GetImageSize() / 2 + anchor); // The nearest grid node from the anchor Vector thickness = new Vector(component.BorderThickness.Left, component.BorderThickness.Top); - component.SetPosition(gridNode.GetPosition() - anchor - thickness); + component.SetComponentPosition(gridNode.GetPosition() - anchor - thickness); component.UpdatePosition(); // Update the component position attribute component.ConnectAnchors(); // Connect anchors to nodes } diff --git a/PPcurry/Component.cs b/PPcurry/Component.cs index b01214c..911843b 100644 --- a/PPcurry/Component.cs +++ b/PPcurry/Component.cs @@ -25,8 +25,10 @@ public class Component : Border private Image ComponentImage; private BoardGrid BoardGrid; // The board on which is this component - private Point ImagePosition; // The position of the component on the grid // WARNING: Position of the image, border excluded - private Vector ImageSize; // The component displayed size as a Vector + private Point ImagePosition; // The position of the image on the grid + private Point ComponentPosition; // The position of the component with its border on the grid + private Vector ImageSize; // The displayed image size as a Vector + private Vector ComponentSize; // The size of the component and its border as a Vector private List Anchors = new List(); // The vectors between the image origin and the component anchors private double Scale; // The scaling factor applied to the image private string ComponentName; // The component name @@ -43,10 +45,24 @@ public class Component : Border public List GetAnchors() => this.Anchors; - public Vector GetImageSize() => this.ImageSize; + public Vector GetImageSize() + { + UpdateSize(); + return this.ImageSize; + } + + public Vector GetComponentSize() + { + UpdateSize(); + return this.ComponentSize; + } - public Point GetImagePosition() => this.ImagePosition; - public void SetPosition(Point position) + public Point GetImagePosition() + { + UpdatePosition(); + return this.ImagePosition; + } + public void SetComponentPosition(Point position) { Canvas.SetLeft(this, position.X); // Position Canvas.SetTop(this, position.Y); @@ -54,6 +70,12 @@ public void SetPosition(Point position) UpdatePosition(); // Update the image position } + public Point GetComponentPosition() + { + UpdatePosition(); + return this.ComponentPosition; + } + public string GetName() => this.ComponentName; public void SetName(string name) { @@ -63,33 +85,34 @@ public void SetName(string name) public void SetIsSelected(bool isSelected) { - if (isSelected != this.IsSelected) + if (isSelected != this.IsSelected) // Check whether the selected state has changed { this.IsSelected = isSelected; - if (isSelected) + double thickness = Properties.Settings.Default.ComponentBorderThickness; + if (isSelected) // The component is selected { this.BoardGrid.SetSelectedComponent(this); - this.BorderThickness = new Thickness(Properties.Settings.Default.ComponentBorderThickness); + this.BorderThickness = new Thickness(thickness); // Adjust the component size and position to avoid image resizing - double thickness = Properties.Settings.Default.ComponentBorderThickness; this.Width += thickness * 2; this.Height += thickness * 2; - this.SetPosition(new Point(Canvas.GetLeft(this) - thickness, Canvas.GetTop(this) - thickness)); + this.SetComponentPosition(new Point(ComponentPosition.X - thickness, ComponentPosition.Y - thickness)); } - else + else // The component is unselected { this.BoardGrid.SetSelectedComponent(null); this.BorderThickness = new Thickness(0); // Adjust the component size and position to avoid image resizing - double thickness = Properties.Settings.Default.ComponentBorderThickness; this.Width -= thickness * 2; this.Height -= thickness * 2; - this.SetPosition(new Point(Canvas.GetLeft(this) - thickness, Canvas.GetTop(this) + thickness)); + this.SetComponentPosition(new Point(ComponentPosition.X + thickness, ComponentPosition.Y + thickness)); } + UpdateSize(); } } + public void SwitchIsSelected() { SetIsSelected(!this.IsSelected); @@ -117,6 +140,7 @@ public Component(Point position, BoardGrid boardGrid, XElement xmlElement) this.Width = 2 * BoardGrid.GetGridSpacing() + 3 * BoardGrid.GetGridThickness(); // The component covers 2 grid cells this.Height = 2 * BoardGrid.GetGridSpacing() + 3 * BoardGrid.GetGridThickness(); // The component covers 2 grid cells this.ImageSize = new Vector(this.Width, this.Height); + this.ComponentSize = new Vector(this.Width, this.Height); this.Scale = this.Width / (double)xmlElement.Element("width"); // Display the image @@ -129,17 +153,13 @@ public Component(Point position, BoardGrid boardGrid, XElement xmlElement) this.BorderBrush = new SolidColorBrush(Colors.Black); this.BorderThickness = new Thickness(0); - this.SetPosition(position); + this.ImagePosition = position; + this.ComponentPosition = position; this.ToolTip = this.ComponentName; // Tooltip boardGrid.AddComponent(this); // Add the component // Rotation - Rotation = new RotateTransform(0) - { - CenterX = this.Width / 2 + Properties.Settings.Default.ComponentBorderThickness, // To make the component rotate around its center - CenterY = this.Height / 2 + Properties.Settings.Default.ComponentBorderThickness - }; - this.RenderTransform = Rotation; + this.Rotation = new RotateTransform(0); // Anchors IEnumerable xmlAnchors = xmlElement.Element("anchors").Elements("anchor"); // Get all the anchors present in the XML @@ -175,6 +195,8 @@ public void RotateLeft() { this.Rotation.Angle += 360; } + this.Rotation.CenterX = this.ComponentSize.X / 2; // To make the component rotate around its center + this.Rotation.CenterY = this.ComponentSize.Y / 2; this.RenderTransform = this.Rotation; TransformAnchorsAfterRotation(-90); ConnectAnchors(); // Reconnect anchors @@ -190,6 +212,8 @@ public void RotateRight() { this.Rotation.Angle -= 360; } + this.Rotation.CenterX = this.ComponentSize.X / 2; // To make the component rotate around its center + this.Rotation.CenterY = this.ComponentSize.Y / 2; this.RenderTransform = this.Rotation; TransformAnchorsAfterRotation(90); ConnectAnchors(); // Reconnect anchors @@ -225,6 +249,22 @@ public void UpdatePosition() { this.ImagePosition.X = (double)Canvas.GetLeft(this) + this.BorderThickness.Left; this.ImagePosition.Y = (double)Canvas.GetTop(this) + this.BorderThickness.Top; + this.ComponentPosition.X = (double)Canvas.GetLeft(this); + this.ComponentPosition.Y = (double)Canvas.GetTop(this); + } + + /// + /// Updates the size attribute after the component has been resized + /// + public void UpdateSize() + { + this.ComponentSize.X = this.Width; + this.ComponentSize.Y = this.Height; + + // Reapply the rotation with the new center coordinates + this.Rotation.CenterX = this.ComponentSize.X / 2; + this.Rotation.CenterY = this.ComponentSize.Y / 2; + this.RenderTransform = this.Rotation; } /// @@ -254,8 +294,8 @@ public void ConnectAnchors() Node node = this.BoardGrid.Magnetize(this.ImagePosition + anchor); // The nearest node Vector nodeRelativePosition = node.GetPosition() - this.ImagePosition; // Node position relative to the image - Directions direction = new Directions(); // Direction of the component relative to the node - + Directions direction = new Directions(); // Direction of the component relative to the node + try { if (Math.Abs(this.ImageSize.X - nodeRelativePosition.X) < Properties.Settings.Default.GridSpacing / 10) // The grid spacing is used as an error threshold