Skip to content

Commit

Permalink
Merge branch 'hotfix-rotation_bug' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
vfauth committed Feb 7, 2018
2 parents c1c5131 + 475000c commit aaf48b6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
2 changes: 1 addition & 1 deletion PPcurry/BoardGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
84 changes: 62 additions & 22 deletions PPcurry/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector> Anchors = new List<Vector>(); // 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
Expand All @@ -43,17 +45,37 @@ public class Component : Border

public List<Vector> 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);

UpdatePosition(); // Update the image position
}

public Point GetComponentPosition()
{
UpdatePosition();
return this.ComponentPosition;
}

public string GetName() => this.ComponentName;
public void SetName(string name)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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<XElement> xmlAnchors = xmlElement.Element("anchors").Elements("anchor"); // Get all the anchors present in the XML
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/// <summary>
/// Updates the size attribute after the component has been resized
/// </summary>
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;
}

/// <summary>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aaf48b6

Please sign in to comment.