
I really should be working on Saucer Pilots, but as I've said before, I'm much better at starting projects than finishing them...

public class Vibration
{
float leftMotor;
float rightMotor;
float msLeft;
public float LeftMotor
{
get { return leftMotor; }
set { leftMotor = value; }
}
public float RightMotor
{
get { return rightMotor; }
set { rightMotor = value; }
}
public float MSLeft
{
get { return msLeft; }
set { msLeft = value; }
}
public Vibration(float leftMotor, float rightMotor, float durationMS)
{
this.leftMotor = leftMotor;
this.rightMotor = rightMotor;
this.msLeft = durationMS;
}
}
public class ForceFeedbackManager
{
private PlayerIndex playerIndex;
List<Vibration> vibrations = new List<Vibration>();
public ForceFeedbackManager(PlayerIndex playerIndex)
{
this.playerIndex = playerIndex;
}
public void AddVibration(float leftMotor, float rightMotor, float durationMS)
{
vibrations.Add(new Vibration(leftMotor, rightMotor, durationMS));
}
public void Update(float msElapsed)
{
List<Vibration> toDelete = new List<Vibration>();
foreach (Vibration vibration in vibrations)
{
vibration.MSLeft -= msElapsed;
if (vibration.MSLeft < 0.0f)
{
toDelete.Add(vibration);
}
}
foreach (Vibration vibration in toDelete)
{
vibrations.Remove(vibration);
}
float leftMotor;
float rightMotor;
GetVibration(out leftMotor, out rightMotor);
GamePad.SetVibration(playerIndex, leftMotor, rightMotor);
}
public void GetVibration(out float leftMotor, out float rightMotor)
{
leftMotor = 0.0f;
rightMotor = 0.0f;
foreach (Vibration vibration in vibrations)
{
leftMotor = Math.Max(leftMotor, vibration.LeftMotor);
rightMotor = Math.Max(rightMotor, vibration.RightMotor);
}
}
}


public delegate void ProgressWorkerDelegate(IProgressContext progressContext); public partial class ProgressDialog : Window, IProgressContext { private bool canceled = false; private ProgressWorkerDelegate workDelegate = null; public bool Canceled { get { return canceled; } } public ProgressDialog() : this(null) { } public ProgressDialog(ProgressWorkerDelegate workDelegate) : this(workDelegate, false) { } public ProgressDialog(ProgressWorkerDelegate workDelegate, bool startInBackground) { this.workDelegate = workDelegate; InitializeComponent(); CancelButton.Click += new RoutedEventHandler(CancelButton_Click); if (workDelegate != null) { if (startInBackground) { new Thread(new ThreadStart(StartWork)).Start(); } else { StartWork(); } } } private void StartWork() { workDelegate(this); } void CancelButton_Click(object sender, RoutedEventArgs e) { canceled = true; CancelButton.IsEnabled = false; } public void UpdateProgress(double progress) { Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { Progress.SetValue(ProgressBar.ValueProperty, progress); }, null); } public void UpdateStatus(string status) { Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { StatusText.SetValue(TextBlock.TextProperty, status); }, null); } public void Finish() { Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate { Close(); }, null); } }
ProgressDialog progressDialog = new ProgressDialog(MyWorkFunction, true); progressDialog.ShowDialog();
void MyWorkFunction(IProgressContext progressContext) { for (int i = 0; i < 100; i++) { if (myProgressContext.Canceled) break; progressContext.UpdateProgress((double)i / 100.0); progressContext.UpdateStatus("Doing Step " + i); } progressContext.Finish(); }

