home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectInput / ReadFFE / Main.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  9.4 KB  |  256 lines

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using Microsoft.DirectX;
  6. using Microsoft.DirectX.DirectInput;
  7. using Microsoft.Samples.DirectX.UtilityToolkit;
  8. using System.Collections;
  9.  
  10. public class MainForm : Form
  11. {
  12.     private System.ComponentModel.Container components = null;
  13.     private Label lblStatic;
  14.     private Label lblStatic1;
  15.     private Label lblStatic2;
  16.     
  17.     string strPath = string.Empty;
  18.     private Device applicationDevice = null;
  19.     private System.Windows.Forms.Button btnReadFile;
  20.     private System.Windows.Forms.Button btnPlayEffects;
  21.     private System.Windows.Forms.Button btnExit;
  22.     private ArrayList applicationEffects = new ArrayList();
  23.  
  24.     public static int Main(string[] Args)
  25.     {
  26.         Application.Run(new MainForm());
  27.         return 0;
  28.     }
  29.     
  30.     protected override void Dispose( bool disposing )
  31.     {
  32.         if(disposing)
  33.         {
  34.             if (null != components)
  35.             {
  36.                 components.Dispose();
  37.             }
  38.         }
  39.         base.Dispose(disposing);    }
  40.     public MainForm()
  41.     {
  42.         //
  43.         // Req  uired for Windows Form Designer support
  44.         //
  45.         InitializeComponent();
  46.     }
  47.     #region InitializeComponent code
  48.     private void InitializeComponent()
  49.     {
  50.         this.lblStatic = new System.Windows.Forms.Label();
  51.         this.lblStatic1 = new System.Windows.Forms.Label();
  52.         this.lblStatic2 = new System.Windows.Forms.Label();
  53.         this.btnReadFile = new System.Windows.Forms.Button();
  54.         this.btnPlayEffects = new System.Windows.Forms.Button();
  55.         this.btnExit = new System.Windows.Forms.Button();
  56.         this.SuspendLayout();
  57.         // 
  58.         // lblStatic
  59.         // 
  60.         this.lblStatic.Location = new System.Drawing.Point(10, 9);
  61.         this.lblStatic.Name = "lblStatic";
  62.         this.lblStatic.Size = new System.Drawing.Size(312, 13);
  63.         this.lblStatic.TabIndex = 3;
  64.         this.lblStatic.Text = "This sample reads a DirectInput force feedback effects file.  After";
  65.         // 
  66.         // lblStatic1
  67.         // 
  68.         this.lblStatic1.Location = new System.Drawing.Point(10, 24);
  69.         this.lblStatic1.Name = "lblStatic1";
  70.         this.lblStatic1.Size = new System.Drawing.Size(283, 13);
  71.         this.lblStatic1.TabIndex = 4;
  72.         this.lblStatic1.Text = "reading the file the effects can be played back on the force ";
  73.         // 
  74.         // lblStatic2
  75.         // 
  76.         this.lblStatic2.Location = new System.Drawing.Point(10, 38);
  77.         this.lblStatic2.Name = "lblStatic2";
  78.         this.lblStatic2.Size = new System.Drawing.Size(84, 13);
  79.         this.lblStatic2.TabIndex = 5;
  80.         this.lblStatic2.Text = "feedback device.";
  81.         // 
  82.         // btnReadFile
  83.         // 
  84.         this.btnReadFile.Location = new System.Drawing.Point(8, 72);
  85.         this.btnReadFile.Name = "btnReadFile";
  86.         this.btnReadFile.Size = new System.Drawing.Size(80, 24);
  87.         this.btnReadFile.TabIndex = 6;
  88.         this.btnReadFile.Text = "&Read File";
  89.         this.btnReadFile.Click += new System.EventHandler(this.btnReadFile_Click);
  90.         // 
  91.         // btnPlayEffects
  92.         // 
  93.         this.btnPlayEffects.Enabled = false;
  94.         this.btnPlayEffects.Location = new System.Drawing.Point(96, 72);
  95.         this.btnPlayEffects.Name = "btnPlayEffects";
  96.         this.btnPlayEffects.Size = new System.Drawing.Size(80, 24);
  97.         this.btnPlayEffects.TabIndex = 7;
  98.         this.btnPlayEffects.Text = "&Play Effects";
  99.         this.btnPlayEffects.Click += new System.EventHandler(this.btnPlayEffects_Click);
  100.         // 
  101.         // btnExit
  102.         // 
  103.         this.btnExit.Location = new System.Drawing.Point(256, 72);
  104.         this.btnExit.Name = "btnExit";
  105.         this.btnExit.Size = new System.Drawing.Size(80, 24);
  106.         this.btnExit.TabIndex = 8;
  107.         this.btnExit.Text = "&Exit";
  108.         this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
  109.         // 
  110.         // MainForm
  111.         // 
  112.         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  113.         this.ClientSize = new System.Drawing.Size(344, 106);
  114.         this.Controls.AddRange(new System.Windows.Forms.Control[] {
  115.                                                                       this.btnExit,
  116.                                                                       this.btnPlayEffects,
  117.                                                                       this.btnReadFile,
  118.                                                                       this.lblStatic,
  119.                                                                       this.lblStatic1,
  120.                                                                       this.lblStatic2});
  121.         this.Name = "MainForm";
  122.         this.Text = "DirectInput FFE File Reader";
  123.         this.Load += new System.EventHandler(this.MainForm_Load);
  124.         this.Activated += new System.EventHandler(this.MainForm_Activated);
  125.         this.ResumeLayout(false);
  126.  
  127.     }
  128.     #endregion
  129.  
  130.     private void btnReadFile_Click(object sender, System.EventArgs e)
  131.     {
  132.         //-----------------------------------------------------------------------------
  133.         // Name: OnReadFile()
  134.         // Desc: Reads a file contain a collection of DirectInput force feedback 
  135.         //       effects.  It creates each of effect read in and stores it 
  136.         //       in the linked list, g_EffectsList.
  137.         //-----------------------------------------------------------------------------
  138.         
  139.         OpenFileDialog ofd = new OpenFileDialog();
  140.         EffectList effects = null;        
  141.     
  142.         if(strPath == string.Empty)
  143.         {
  144.             // See if we can find 'bazooka.ffe'
  145.             try
  146.             {
  147.                 string file = Utility.FindMediaFile("bazooka.ffe");
  148.                 System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
  149.                 ofd.FileName = fileInfo.Name;
  150.                 strPath = fileInfo.Directory.FullName;
  151.             }
  152.             catch (FileNotFoundException)
  153.             {
  154.                 strPath = Utility.SdkMediaPath;
  155.             }
  156.         }
  157.  
  158.         // Setup the OpenFileDialog structure.
  159.         ofd.Filter = "FEdit Files|*.ffe|All Files|*.*";
  160.         ofd.InitialDirectory = strPath;
  161.         ofd.Title = "Open FEdit File";
  162.         ofd.ShowReadOnly = false;
  163.         
  164.         // Display the OpenFileName dialog. Then, try to load the specified file.
  165.         if( DialogResult.Cancel == ofd.ShowDialog() )
  166.             return;
  167.  
  168.         // Store the path.
  169.         strPath = Path.GetDirectoryName(ofd.FileName);
  170.  
  171.         // Get the effects in the file selected.
  172.         effects = applicationDevice.GetEffects(ofd.FileName, FileEffectsFlags.ModifyIfNeeded);
  173.  
  174.         EmptyEffectList();
  175.  
  176.         foreach(FileEffect f in effects)
  177.         {
  178.             EffectObject eo = new EffectObject(f.EffectGuid, f.EffectStruct, applicationDevice);
  179.             applicationEffects.Add(eo);
  180.         }
  181.  
  182.         // If list of effects is empty, then there are no effects created.
  183.         if( 0 == applicationEffects.Count )
  184.         {
  185.             // Pop up a box informing the user.
  186.             MessageBox.Show("Unable to create any effects.");
  187.             btnPlayEffects.Enabled = false;
  188.         }   
  189.         else
  190.         {
  191.             // There are effects, so enable the 'play effects' button.
  192.             btnPlayEffects.Enabled = true;
  193.         }   
  194.     }
  195.  
  196.     private void MainForm_Load(object sender, System.EventArgs e)
  197.     {
  198.         DeviceList list = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly | EnumDevicesFlags.ForceFeeback);
  199.         if(0 == list.Count)
  200.         {
  201.             MessageBox.Show("No force feedback devices found attached to the system. Sample will now exit.", "No suitable device", 
  202.                 MessageBoxButtons.OK, MessageBoxIcon.Error);
  203.             Close();
  204.         }
  205.  
  206.         list.MoveNext();
  207.         applicationDevice = new Device(((DeviceInstance)list.Current).InstanceGuid);
  208.         applicationDevice.SetDataFormat(DeviceDataFormat.Joystick);
  209.         applicationDevice.SetCooperativeLevel(this, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Background);
  210.         
  211.         try{applicationDevice.Acquire();}
  212.         catch(InputException){}
  213.  
  214.     }
  215.     private void OnPlayEffects()
  216.     {
  217.         //-----------------------------------------------------------------------------
  218.         // Name: OnPlayEffects()
  219.         // Desc: Plays all of the effects enumerated in the file 
  220.         //-----------------------------------------------------------------------------
  221.  
  222.         // Stop all previous forces.
  223.         applicationDevice.SendForceFeedbackCommand(ForceFeedbackCommand.StopAll);
  224.  
  225.         foreach (EffectObject eo in  applicationEffects)
  226.         {
  227.             // Play all of the effects enumerated in the file .
  228.             eo.Start( 1, EffectStartFlags.NoDownload);
  229.         }
  230.     }
  231.  
  232.     private void btnPlayEffects_Click(object sender, System.EventArgs e)
  233.     {
  234.         OnPlayEffects();
  235.     }
  236.  
  237.     private void MainForm_Activated(object sender, System.EventArgs e)
  238.     {
  239.         if(null!= applicationDevice)
  240.         {
  241.             try{applicationDevice.Acquire();}
  242.             catch(InputException){}
  243.         }
  244.     }
  245.  
  246.     private void btnExit_Click(object sender, System.EventArgs e)
  247.     {
  248.         Close();
  249.     }
  250.  
  251.     private void EmptyEffectList()
  252.     {
  253.         applicationEffects.Clear();
  254.     }
  255. }
  256.