1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
public partial class MainPage : UserControl
{
double centerX;
double centerY;
Duration duration;
rectangle current;
Point offset;
Point center;
string upSuffixe = "{0}_UP";
string backSuffixe = "{0}_BACK";
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
centerX = (double)(LayoutRoot.Width / 2);
centerY = (double)(LayoutRoot.Height / 2);
center = new Point(centerX, centerY);
duration = new Duration(TimeSpan.FromSeconds(1));
foreach (rectangle r in LayoutRoot.Children)
{
r.MouseLeftButtonUp += new MouseButtonEventHandler(r_MouseLeftButtonUp);
InitAnimation(r, true);
InitAnimation(r, false);
}
}
void r_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Storyboard playing;
rectangle r = (sender as rectangle);
// the user has clicked on the highlighted rectangle. we put it back to original place
if (r != null && r.Equals(current) && r.IsUp)
{
playing = (this.Resources[GetSBKey(r.Name, false)] as Storyboard);
if (playing != null)
{
playing.Begin();
r.IsUp = false;
return;
}
}
// otherwise the user has clicked on a NOT highlighted rectangle
else
{
// first check if a rectangle is highlighted -> then reduce it
if (current != null && r != null && current != r)
{
playing = (this.Resources[GetSBKey(current.Name, false)] as Storyboard);
if (playing != null)
{
playing.Begin();
current.IsUp = false;
}
}
if (r != null)
{
if (r.IsUp == false)
{
playing = (this.Resources[GetSBKey(r.Name, true)] as Storyboard);
if (playing != null)
{
r.IsUp = true;
current = r;
playing.Begin();
}
}
}
}
}
//App.Current.RootVisual.Clip.Bounds.Y;
private void InitAnimation(rectangle r, bool up)
{
Storyboard sb = new Storyboard();
this.Resources.Add(GetSBKey(r.Name, up), sb);
GeneralTransform gt = r.TransformToVisual(LayoutRoot);
Point currentPosition = gt.Transform(new Point(0, 0));
offset = new Point(center.X - currentPosition.X, centerY - currentPosition.Y);
DoubleAnimation X = new DoubleAnimation();
sb.Children.Add(X);
X.To = up ? offset.X : offset.X * -1;
X.Duration = duration;
X.EasingFunction = new ElasticEase();
DoubleAnimation Y = new DoubleAnimation();
sb.Children.Add(Y);
Y.To = up ? offset.Y : offset.Y *-1;
Y.Duration = duration;
Y.EasingFunction = new ElasticEase();
Storyboard.SetTarget(X, r);
Storyboard.SetTargetProperty(X, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"));
Storyboard.SetTarget(Y, r);
Storyboard.SetTargetProperty(Y, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"));
}
private TransformGroup GetTransFormGroup(int x, int y, double angle, double scale)
{
TransformGroup tg = new TransformGroup();
TranslateTransform tt = new TranslateTransform();
tt.X = x;
tt.Y = y;
tg.Children.Add(tt);
ScaleTransform st = new ScaleTransform();
st.ScaleX = scale;
st.ScaleY = scale;
tg.Children.Add(st);
return tg;
}
private string GetSBKey(string rectangleName, bool up)
{
if (up)
{
return String.Format(upSuffixe, rectangleName);
}
else
{
return String.Format(backSuffixe, rectangleName);
}
}
} |
Partager