#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
{
///
/// 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.
///
public partial class MainForm : Form
{
ContentBuilder contentBuilder;
ContentManager contentManager;
///
/// Constructs the main form.
///
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;
}
///
/// Event handler for the Exit menu option.
///
void ExitMenuClicked(object sender, EventArgs e)
{
Close();
}
///
/// Event handler for the Open menu option.
///
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);
}
}
///
/// Loads a new 3D model file into the ModelViewerControl.
///
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");
}
else
{
// If the build failed, display an error message.
MessageBox.Show(buildError, "Error");
}
Cursor = Cursors.Arrow;
}
}
}