Hello,
I have problem with adornments after node flipping (GoSilverlight 2.0.1.5, Silverlight 5).
I’ve found the Topic: Flipping (Mirroring) Nodes
http://www.nwoods.com/forum/forum_posts.asp?TID=4624&KW=flip&title=flipping-mirroring-nodes
and implemented similar code.
Attached project uses the next node (initial state):
Red and blue triangles are ports.
I’ve tried 2 cases of vertical (along Y-axis) flip:
1. Node is flipped.
Node selection is valid,
Port adornment is wrong:
2. NodePanel is flipped.
Port adornment is valid,
Node adornments are wrong after reselecting:
I’ve tried to call InvalidateMeasure() for parent FrameworkElements of Node/NodePanel, but result is the same.
Where is my mistake?
Here is code
//=====================================================
using System;
using System.Windows;
using System.Windows.Controls;
using Northwoods.GoXam;
using System.Windows.Media;
namespace SLSchemaTest
{
public partial class MainPage : UserControl
{
//-----------------------------------------------------
public void FlipNode(Diagram dg, Node node, bool bFlipY, bool bFlipX) // bFlipY = true - flip along Y-axis; bFlipX = true - flip along X-axis
{
// DiagramPanel -> NodeLayer -> Node -> NodePanel -> STNode_Shape
NodePanel nodePanel = node.FindNamedDescendant("NodePanel") as NodePanel;
//FrameworkElement nodeShape = node.FindNamedDescendant("STNode_Shape") as FrameworkElement;
FrameworkElement elementToFlip = node; // Result: ports adornments bad, node adornments OK (after flip)
//FrameworkElement elementToFlip = nodePanel; // Result: node adornments bad, ports adornments OK (after flip)
FlipFrameworkElement(elementToFlip, bFlipY, bFlipX);
foreach (FrameworkElement port in node.Ports)
FlipPort(port, bFlipY, bFlipX);
InvalidateParents(elementToFlip);
}
//-----------------------------------------------------
private void FlipFrameworkElement(FrameworkElement el, bool bFlipY, bool bFlipX)
{
ScaleTransform scaleTransform = el.RenderTransform as ScaleTransform;
if (scaleTransform == null)
{
scaleTransform = new ScaleTransform();
scaleTransform.ScaleX = 1;
scaleTransform.ScaleY = 1;
el.RenderTransformOrigin = new Point(0.5, 0.5);
}
if (bFlipY)
scaleTransform.ScaleX *= -1; // vertical flip (along Y-axis)
if (bFlipX)
scaleTransform.ScaleY *= -1; // horizontal flip (along X-axis)
el.RenderTransform = scaleTransform;
}
//-----------------------------------------------------
private void FlipPort(FrameworkElement port, bool bFlipY, bool bFlipX)
{
Spot spot = SpotPanel.GetSpot(port);
if (bFlipY)
spot.X = (1 - spot.X);
if (bFlipX)
spot.Y = (1 - spot.Y);
SpotPanel.SetSpot(port, spot);
Spot linkSpot = Node.GetFromSpot(port);
if (bFlipY)
{// flip along Y-axis (vertical flip)
if (linkSpot == Spot.MiddleLeft)
linkSpot = Spot.MiddleRight;
else
if (linkSpot == Spot.MiddleRight)
linkSpot = Spot.MiddleLeft;
}
if (bFlipX)
{// flip along X-axis (horizontal flip)
if (linkSpot == Spot.MiddleBottom)
linkSpot = Spot.MiddleTop;
else
if (linkSpot == Spot.MiddleTop)
linkSpot = Spot.MiddleBottom;
}
Node.SetFromSpot(port, linkSpot);
Node.SetToSpot(port, linkSpot);
SpotPanel.SetAlignment(port, linkSpot);
}
//-----------------------------------------------------
internal void InvalidateParents(FrameworkElement elt)
{
if (elt != null)
{
FrameworkElement fe = elt;
while (fe != null)
{
//if (fe is DiagramPanel)
// break;
fe.InvalidateMeasure();
fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
}
}
}
}
}
Thanks