I have a dilemma with my program. My program is like a specialized calculator, probability, dice roller type thing - think automated RPG rolling. And the way it's set up at the moment it on one window it has all the data and does all the calculations and such, BUT I also have a second, optional window that's purely for animations, just so it's not just text output. Now the animation window works fine, and as does my "data window" for lack of a better term, the issue becomes when I want to update my animation window from my data window and make the data window wait until the animation has completed.
Here's the set-up: Data window runs a method, within that method it tells animation window to update the images and run a storyboard animation method. That works. Now data window runs a method that has a foreach loop, and in that foreach loop it tells the animation window to update their image and run a animation for the current foreach item. Simple right? Well the problem is the data window is impatient and continues to tell the animation window to run it's animations - and since the animation is supposed to recycle a image object, just with a new source, with that animation, the animation window forgets all the orders from the data window and only runs the last one, rather than run each one one-by-one.
So basically my question is - how can I get my data window to wait for the animation window to finish their storyboard animation before continuing the foreach loop?
A simplified version of my code is this -
//In my data window
foreach (var item in Weapons)
{
try
{
string weapons = "\\" + weapon.Name + ".png";
AnimationWindow.img_Pojectile.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "\\Images" + weapons));
}
catch
{
try
{
string weapons = "\\" + weapon.Type + ".png";
AnimationWindow.img_Pojectile.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "\\Images" + weapons));
}
catch
{
AnimationWindow.img_Pojectile.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "\\Images\\Default.png"));
}
}
AnimationWindow.Fire(item.Name);
//More code stuff I have to repeat here, all the calculations and such...
}
//In my AnimationWindow
public void Fire(string name)
{
img_Pojectile.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory() + "\\" + name + ".png"));
Storyboard sb = new Storyboard();
DoubleAnimation anim = new DoubleAnimation(0, 120, TimeSpan.FromSeconds(seconds));
TranslateTransform trans = new TranslateTransform();
target.RenderTransform = trans;
anim.AutoReverse = true;
anim.RepeatBehavior = RepeatBehavior.Forever;
anim.BeginTime = TimeSpan.FromSeconds(offset);
Storyboard.SetTarget(anim, target);
Storyboard.SetTargetProperty(anim, new PropertyPath("(FrameworkElement.RenderTransform).(TranslateTransform.Y)"));
sb.Completed += (o, s) => {
//Figured a completed event might help, so far no luck
};
sb.Children.Add(anim);
sb.Begin();
}
So in this simpler version, what I'd like is for the animation finishes before my foreach loop moves on.