MainForm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // MainForm.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Windows.Forms;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. #endregion
  17. namespace WinFormsContentLoading
  18. {
  19. /// <summary>
  20. /// Custom form provides the main user interface for the program.
  21. /// In this sample we used the designer to fill the entire form with a
  22. /// ModelViewerControl, except for the menu bar which provides the
  23. /// "File / Open..." option.
  24. /// </summary>
  25. public partial class MainForm : Form
  26. {
  27. ContentBuilder contentBuilder;
  28. ContentManager contentManager;
  29. /// <summary>
  30. /// Constructs the main form.
  31. /// </summary>
  32. public MainForm()
  33. {
  34. InitializeComponent();
  35. contentBuilder = new ContentBuilder();
  36. contentManager = new ContentManager(modelViewerControl.Services,
  37. contentBuilder.OutputDirectory);
  38. /// Automatically bring up the "Load Model" dialog when we are first shown.
  39. this.Shown += OpenMenuClicked;
  40. }
  41. /// <summary>
  42. /// Event handler for the Exit menu option.
  43. /// </summary>
  44. void ExitMenuClicked(object sender, EventArgs e)
  45. {
  46. Close();
  47. }
  48. /// <summary>
  49. /// Event handler for the Open menu option.
  50. /// </summary>
  51. void OpenMenuClicked(object sender, EventArgs e)
  52. {
  53. OpenFileDialog fileDialog = new OpenFileDialog();
  54. // Default to the directory which contains our content files.
  55. string assemblyLocation = Assembly.GetExecutingAssembly().Location;
  56. string relativePath = Path.Combine(assemblyLocation, "../../../../Content");
  57. string contentPath = Path.GetFullPath(relativePath);
  58. fileDialog.InitialDirectory = contentPath;
  59. fileDialog.Title = "Load Model";
  60. fileDialog.Filter = "Model Files (*.fbx;*.x)|*.fbx;*.x|" +
  61. "FBX Files (*.fbx)|*.fbx|" +
  62. "X Files (*.x)|*.x|" +
  63. "All Files (*.*)|*.*";
  64. if (fileDialog.ShowDialog() == DialogResult.OK)
  65. {
  66. LoadModel(fileDialog.FileName);
  67. }
  68. }
  69. /// <summary>
  70. /// Loads a new 3D model file into the ModelViewerControl.
  71. /// </summary>
  72. void LoadModel(string fileName)
  73. {
  74. Cursor = Cursors.WaitCursor;
  75. // Unload any existing model.
  76. modelViewerControl.Model = null;
  77. contentManager.Unload();
  78. // Tell the ContentBuilder what to build.
  79. contentBuilder.Clear();
  80. contentBuilder.Add(fileName, "Model", null, "ModelProcessor");
  81. // Build this new model data.
  82. string buildError = contentBuilder.Build();
  83. if (string.IsNullOrEmpty(buildError))
  84. {
  85. // If the build succeeded, use the ContentManager to
  86. // load the temporary .xnb file that we just created.
  87. modelViewerControl.Model = contentManager.Load<Model>("Model");
  88. }
  89. else
  90. {
  91. // If the build failed, display an error message.
  92. MessageBox.Show(buildError, "Error");
  93. }
  94. Cursor = Cursors.Arrow;
  95. }
  96. }
  97. }