| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #region File Description
- //-----------------------------------------------------------------------------
- // MainForm.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.IO;
- using System.Reflection;
- using System.Windows.Forms;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- #endregion
- namespace WinFormsContentLoading
- {
- /// <summary>
- /// Custom form provides the main user interface for the program.
- /// In this sample we used the designer to fill the entire form with a
- /// ModelViewerControl, except for the menu bar which provides the
- /// "File / Open..." option.
- /// </summary>
- public partial class MainForm : Form
- {
- ContentBuilder contentBuilder;
- ContentManager contentManager;
- /// <summary>
- /// Constructs the main form.
- /// </summary>
- public MainForm()
- {
- InitializeComponent();
- contentBuilder = new ContentBuilder();
- contentManager = new ContentManager(modelViewerControl.Services,
- contentBuilder.OutputDirectory);
- /// Automatically bring up the "Load Model" dialog when we are first shown.
- this.Shown += OpenMenuClicked;
- }
- /// <summary>
- /// Event handler for the Exit menu option.
- /// </summary>
- void ExitMenuClicked(object sender, EventArgs e)
- {
- Close();
- }
- /// <summary>
- /// Event handler for the Open menu option.
- /// </summary>
- void OpenMenuClicked(object sender, EventArgs e)
- {
- OpenFileDialog fileDialog = new OpenFileDialog();
- // Default to the directory which contains our content files.
- string assemblyLocation = Assembly.GetExecutingAssembly().Location;
- string relativePath = Path.Combine(assemblyLocation, "../../../../Content");
- string contentPath = Path.GetFullPath(relativePath);
- fileDialog.InitialDirectory = contentPath;
- fileDialog.Title = "Load Model";
- fileDialog.Filter = "Model Files (*.fbx;*.x)|*.fbx;*.x|" +
- "FBX Files (*.fbx)|*.fbx|" +
- "X Files (*.x)|*.x|" +
- "All Files (*.*)|*.*";
- if (fileDialog.ShowDialog() == DialogResult.OK)
- {
- LoadModel(fileDialog.FileName);
- }
- }
- /// <summary>
- /// Loads a new 3D model file into the ModelViewerControl.
- /// </summary>
- void LoadModel(string fileName)
- {
- Cursor = Cursors.WaitCursor;
- // Unload any existing model.
- modelViewerControl.Model = null;
- contentManager.Unload();
- // Tell the ContentBuilder what to build.
- contentBuilder.Clear();
- contentBuilder.Add(fileName, "Model", null, "ModelProcessor");
- // Build this new model data.
- string buildError = contentBuilder.Build();
- if (string.IsNullOrEmpty(buildError))
- {
- // If the build succeeded, use the ContentManager to
- // load the temporary .xnb file that we just created.
- modelViewerControl.Model = contentManager.Load<Model>("Model");
- }
- else
- {
- // If the build failed, display an error message.
- MessageBox.Show(buildError, "Error");
- }
- Cursor = Cursors.Arrow;
- }
- }
- }
|