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 / ActionBasic / ActionBasicUI.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  11.6 KB  |  300 lines

  1. //-----------------------------------------------------------------------------
  2. // File: ActionBasicUI.cs
  3. //
  4. // Desc: The ActionBasic sample is intended to be an introduction to action mapping, 
  5. //       and illustrates a step by step approach to creating an action mapped 
  6. //       application.
  7. //
  8. // Copyright (c) Microsoft Corporation. All rights reserved
  9. //-----------------------------------------------------------------------------
  10. using System;
  11. using System.Text;
  12. using System.Drawing;
  13. using System.Collections;
  14. using System.ComponentModel;
  15. using System.Windows.Forms;
  16. using Microsoft.DirectX;
  17.  
  18. namespace ActionBasic
  19. {
  20.     /// <summary>
  21.     /// description.
  22.     /// </summary>
  23.     public class ActionBasicUI : System.Windows.Forms.Form
  24.     {
  25.         private ActionBasicApp app;
  26.         private System.Windows.Forms.Label label1;
  27.         private System.Windows.Forms.Label label2;
  28.         private System.Windows.Forms.Label label3;
  29.         private System.Windows.Forms.Label label4;
  30.         private System.Windows.Forms.GroupBox groupBox1;
  31.         private ActionBasic.Chart chart;
  32.         private System.Windows.Forms.Button ConfigButton;
  33.         private System.Windows.Forms.Button ExitButton;
  34.         /// <summary>
  35.         /// Required designer variable.
  36.         /// </summary>
  37.         private System.ComponentModel.Container components = null;
  38.  
  39.         /// <summary>
  40.         /// Constructor
  41.         /// </summary>
  42.         /// <param name="app">Reference to the application object</param>
  43.         /// <param name="ActionNames">List of game action strings</param>
  44.         /// <param name="deviceStates">Reference array for the device states</param>
  45.         public ActionBasicUI(ActionBasicApp app, String[] ActionNames, ArrayList deviceStates)
  46.         {
  47.             try
  48.             {
  49.                 // Load the icon from our resources
  50.                 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(this.GetType());
  51.                 this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  52.             }
  53.             catch
  54.             {
  55.                 // It's no big deal if we can't load our icons, but try to load the embedded one
  56.                 try { this.Icon = new System.Drawing.Icon(this.GetType(), "directx.ico"); } 
  57.                 catch {}
  58.             }
  59.  
  60.             this.app = app;
  61.             //
  62.             // Required for Windows Form Designer support
  63.             //
  64.             InitializeComponent();
  65.  
  66.             chart.ColumnTitles = ActionNames;
  67.             chart.RowData = deviceStates;
  68.         }
  69.  
  70.         
  71.         
  72.         
  73.         /// <summary>
  74.         /// Print information about the provided exception
  75.         /// </summary>
  76.         /// <param name="ex">Exception instance</param>
  77.         /// <param name="calling">Name of the method which returned the exception</param>
  78.         public void ShowException(Exception ex, string calling)
  79.         {
  80.             string output = ex.Message + "\n\n";
  81.             
  82.             if (ex.GetType().DeclaringType == Type.GetType("System.DirectX.DirectXException"))
  83.             {
  84.                 // DirectX-specific info
  85.                 DirectXException dex = (DirectXException) ex;
  86.                 output += "HRESULT: " + dex.ErrorString + " (" + dex.ErrorCode.ToString("X") + ")\n";
  87.             }
  88.            
  89.             output += "Calling: " + calling + "\n";
  90.             output += "Source: " + ex.Source + "\n";  
  91.             MessageBox.Show(this, output, "ActionBasic Sample Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  92.         }
  93.  
  94.         
  95.         
  96.         
  97.         /// <summary>
  98.         /// Force the chart object to repaint
  99.         /// </summary>
  100.         public void UpdateChart()
  101.         {
  102.             if (!chart.Created)
  103.                 return;
  104.             
  105.             try{chart.UpdateData();}
  106.             catch(Exception){}
  107.         }
  108.  
  109.         
  110.         
  111.         
  112.         /// <summary>
  113.         /// Clean up any resources being used.
  114.         /// </summary>
  115.         protected override void Dispose(bool disposing)
  116.         {
  117.             if (disposing)
  118.             {
  119.                 if (components != null) 
  120.                 {
  121.                     components.Dispose();
  122.                 }
  123.             }
  124.             base.Dispose(disposing);
  125.         }
  126.  
  127.         
  128.         
  129.         
  130.         /// <summary>
  131.         /// Display the user guide
  132.         /// </summary>
  133.         private void DisplayHelp()
  134.         {
  135.             StringBuilder message = new StringBuilder();
  136.  
  137.             message.Append("The chart shows the list of devices found ");
  138.             message.Append("attached to your computer, plotted against a\n");
  139.             message.Append("defined set of actions for an imaginary fighting ");
  140.             message.Append("game.\n\n");
  141.             message.Append("30 times per second, the program polls for new "); 
  142.             message.Append("input from the devices, and displays which actions\n");
  143.             message.Append("are currently being sent by each device. During ");
  144.             message.Append("initialization, the program attempts to establish\n");
  145.             message.Append("a mapping between actions and device objects for ");
  146.             message.Append("each attached device. Actions which were not\n");
  147.             message.Append("mapped to a device object are shown as a ");
  148.             message.Append("crosshatch-filled cell on the chart.\n\n");
  149.             message.Append("To view the current action mappings for all the ");
  150.             message.Append("devices, click the \"View Configuration\" button,\n");
  151.             message.Append("which will access the default configuration UI ");
  152.             message.Append("managed by DirectInput.");
  153.  
  154.             MessageBox.Show(this, message.ToString(), "ActionBasic Help", MessageBoxButtons.OK);
  155.         }
  156.  
  157.         
  158.         
  159.         
  160.         #region Windows Form Designer generated code
  161.         /// <summary>
  162.         /// Required method for Designer support - do not modify
  163.         /// the contents of this method with the code editor.
  164.         /// </summary>
  165.         private void InitializeComponent()
  166.         {
  167.             this.label1 = new System.Windows.Forms.Label();
  168.             this.label2 = new System.Windows.Forms.Label();
  169.             this.label3 = new System.Windows.Forms.Label();
  170.             this.label4 = new System.Windows.Forms.Label();
  171.             this.groupBox1 = new System.Windows.Forms.GroupBox();
  172.             this.chart = new ActionBasic.Chart();
  173.             this.ConfigButton = new System.Windows.Forms.Button();
  174.             this.ExitButton = new System.Windows.Forms.Button();
  175.             this.groupBox1.SuspendLayout();
  176.             this.SuspendLayout();
  177.             // 
  178.             // label1
  179.             // 
  180.             this.label1.Location = new System.Drawing.Point(16, 24);
  181.             this.label1.Name = "label1";
  182.             this.label1.Size = new System.Drawing.Size(344, 16);
  183.             this.label1.TabIndex = 0;
  184.             this.label1.Text = "This tutorial polls for action-mapped data from the attached devices,";
  185.             // 
  186.             // label2
  187.             // 
  188.             this.label2.Location = new System.Drawing.Point(16, 40);
  189.             this.label2.Name = "label2";
  190.             this.label2.Size = new System.Drawing.Size(344, 16);
  191.             this.label2.TabIndex = 1;
  192.             this.label2.Text = "and displays a chart showing triggered game actions plotted against";
  193.             // 
  194.             // label3
  195.             // 
  196.             this.label3.Location = new System.Drawing.Point(16, 56);
  197.             this.label3.Name = "label3";
  198.             this.label3.Size = new System.Drawing.Size(344, 16);
  199.             this.label3.TabIndex = 2;
  200.             this.label3.Text = "the corresponding input devices.";
  201.             // 
  202.             // label4
  203.             // 
  204.             this.label4.Location = new System.Drawing.Point(16, 80);
  205.             this.label4.Name = "label4";
  206.             this.label4.Size = new System.Drawing.Size(344, 16);
  207.             this.label4.TabIndex = 3;
  208.             this.label4.Text = "Press the F1 key for help.";
  209.             // 
  210.             // groupBox1
  211.             // 
  212.             this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
  213.                                                                                     this.chart,
  214.                                                                                     this.ConfigButton,
  215.                                                                                     this.label4,
  216.                                                                                     this.label3,
  217.                                                                                     this.label2,
  218.                                                                                     this.label1});
  219.             this.groupBox1.Location = new System.Drawing.Point(8, 8);
  220.             this.groupBox1.Name = "groupBox1";
  221.             this.groupBox1.Size = new System.Drawing.Size(408, 400);
  222.             this.groupBox1.TabIndex = 4;
  223.             this.groupBox1.TabStop = false;
  224.             // 
  225.             // chart
  226.             // 
  227.             this.chart.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  228.             this.chart.ColumnTitles = null;
  229.             this.chart.Location = new System.Drawing.Point(16, 112);
  230.             this.chart.Name = "chart";
  231.             this.chart.RowData = null;
  232.             this.chart.Size = new System.Drawing.Size(376, 240);
  233.             this.chart.TabIndex = 6;
  234.             this.chart.Text = "chart";
  235.             // 
  236.             // ConfigButton
  237.             // 
  238.             this.ConfigButton.Location = new System.Drawing.Point(272, 368);
  239.             this.ConfigButton.Name = "ConfigButton";
  240.             this.ConfigButton.Size = new System.Drawing.Size(120, 24);
  241.             this.ConfigButton.TabIndex = 5;
  242.             this.ConfigButton.Text = "&View Configuration";
  243.             this.ConfigButton.Click += new System.EventHandler(this.ConfigButton_Click);
  244.             // 
  245.             // ExitButton
  246.             // 
  247.             this.ExitButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  248.             this.ExitButton.Location = new System.Drawing.Point(344, 416);
  249.             this.ExitButton.Name = "ExitButton";
  250.             this.ExitButton.Size = new System.Drawing.Size(72, 24);
  251.             this.ExitButton.TabIndex = 5;
  252.             this.ExitButton.Text = "Exit";
  253.             this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click);
  254.             // 
  255.             // ActionBasicUI
  256.             // 
  257.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  258.             this.CancelButton = this.ExitButton;
  259.             this.ClientSize = new System.Drawing.Size(424, 446);
  260.             this.Controls.AddRange(new System.Windows.Forms.Control[] {
  261.                                                                           this.ExitButton,
  262.                                                                           this.groupBox1});
  263.             this.MaximizeBox = false;
  264.             this.Name = "ActionBasicUI";
  265.             this.Text = "ActionBasic Sample";
  266.             this.groupBox1.ResumeLayout(false);
  267.             this.ResumeLayout(false);
  268.  
  269.         }
  270.         #endregion
  271.  
  272.         
  273.         
  274.         
  275.         private void ExitButton_Click(object sender, System.EventArgs e)
  276.         {
  277.             ((Button)sender).DialogResult = DialogResult.Cancel;
  278.             this.Close();
  279.         }
  280.  
  281.         
  282.         
  283.         
  284.         protected override void OnHelpRequested(HelpEventArgs e)
  285.         {
  286.             e.Handled = true;
  287.             DisplayHelp();
  288.         }
  289.  
  290.  
  291.         
  292.         
  293.         
  294.         private void ConfigButton_Click(object sender, System.EventArgs e)
  295.         {
  296.             app.ConfigureDevices();
  297.         }            
  298.     }
  299. }
  300.