From 73938fd8eac71d719f0184ecb350cdc9fd850b65 Mon Sep 17 00:00:00 2001 From: Fauth Victor Date: Wed, 7 Feb 2018 02:31:02 +0100 Subject: [PATCH 1/2] Fixed moving component when selecting and deselecting it multiple times. --- PPcurry/BoardGrid.cs | 2 +- PPcurry/Component.cs | 65 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 14 deletions(-) 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..03f9e39 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) { @@ -66,16 +88,16 @@ public void SetIsSelected(bool isSelected) if (isSelected != this.IsSelected) { this.IsSelected = isSelected; + double thickness = Properties.Settings.Default.ComponentBorderThickness; if (isSelected) { 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 { @@ -83,13 +105,15 @@ public void SetIsSelected(bool isSelected) 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(); + UpdatePosition(); } } + public void SwitchIsSelected() { SetIsSelected(!this.IsSelected); @@ -117,6 +141,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,7 +154,8 @@ 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 @@ -225,6 +251,19 @@ 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.ImageSize.X = this.Width - this.BorderThickness.Left - this.BorderThickness.Right; + this.ImageSize.Y = this.Height - this.BorderThickness.Top - this.BorderThickness.Bottom; + this.ComponentSize.X = this.Width; + this.ComponentSize.Y = this.Height; } /// @@ -254,8 +293,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 From 475000cdbb8da5010223a2e8fcab23f37ba70d1a Mon Sep 17 00:00:00 2001 From: Fauth Victor Date: Wed, 7 Feb 2018 03:19:47 +0100 Subject: [PATCH 2/2] Fixed wrong component position when deselecting it after a rotation. --- PPcurry/Component.cs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/PPcurry/Component.cs b/PPcurry/Component.cs index 03f9e39..911843b 100644 --- a/PPcurry/Component.cs +++ b/PPcurry/Component.cs @@ -85,11 +85,11 @@ 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; double thickness = Properties.Settings.Default.ComponentBorderThickness; - if (isSelected) + if (isSelected) // The component is selected { this.BoardGrid.SetSelectedComponent(this); this.BorderThickness = new Thickness(thickness); @@ -99,7 +99,7 @@ public void SetIsSelected(bool isSelected) this.Height += thickness * 2; 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); @@ -110,7 +110,6 @@ public void SetIsSelected(bool isSelected) this.SetComponentPosition(new Point(ComponentPosition.X + thickness, ComponentPosition.Y + thickness)); } UpdateSize(); - UpdatePosition(); } } @@ -160,12 +159,7 @@ public Component(Point position, BoardGrid boardGrid, XElement xmlElement) 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 @@ -201,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 @@ -216,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 @@ -260,10 +258,13 @@ public void UpdatePosition() /// public void UpdateSize() { - this.ImageSize.X = this.Width - this.BorderThickness.Left - this.BorderThickness.Right; - this.ImageSize.Y = this.Height - this.BorderThickness.Top - this.BorderThickness.Bottom; 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; } ///