#Region "File Description"
'----------------------------------------------------------------------------- 
' MainForm.cs 
' 
' Microsoft XNA Community Game Platform 
' Copyright (C) Microsoft Corporation. All rights reserved. 
'----------------------------------------------------------------------------- 
#End Region

#Region "Using Statements"
Imports System
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Graphics
#End Region

''' <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> 
Partial Public Class MainForm
    Inherits Form
    Private contentBuilder As ContentBuilder
    Private contentManager As ContentManager


    ''' <summary> 
    ''' Constructs the main form. 
    ''' </summary> 
    Public Sub New()
        InitializeComponent()

        contentBuilder = New ContentBuilder()

        contentManager = New ContentManager(ModelViewerControl.Services, contentBuilder.OutputDirectory)

        ' Automatically bring up the "Load Model" dialog when we are first shown. 
        AddHandler Me.Shown, AddressOf OpenMenuClicked
    End Sub


    ''' <summary> 
    ''' Event handler for the Exit menu option. 
    ''' </summary> 
    Private Sub ExitMenuClicked(ByVal sender As Object, ByVal e As EventArgs)
        Close()
    End Sub


    ''' <summary> 
    ''' Event handler for the Open menu option. 
    ''' </summary> 
    Private Sub OpenMenuClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim fileDialog As New OpenFileDialog()

        ' Default to the directory which contains our content files. 
        Dim assemblyLocation As String = Assembly.GetExecutingAssembly().Location
        Dim relativePath As String = Path.Combine(assemblyLocation, "../../../../Content")
        Dim contentPath As String = 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 Then
            LoadModel(fileDialog.FileName)
        End If
    End Sub


    ''' <summary> 
    ''' Loads a new 3D model file into the ModelViewerControl. 
    ''' </summary> 
    Private Sub LoadModel(ByVal fileName As String)
        Cursor = Cursors.WaitCursor

        ' Unload any existing model. 
        ModelViewerControl.Model = Nothing
        contentManager.Unload()

        ' Tell the ContentBuilder what to build. 
        contentBuilder.Clear()
        contentBuilder.Add(fileName, "Model", Nothing, "ModelProcessor")

        ' Build this new model data. 
        Dim buildError As String = contentBuilder.Build()

        If String.IsNullOrEmpty(buildError) Then
            ' If the build succeeded, use the ContentManager to 
            ' load the temporary .xnb file that we just created. 
            ModelViewerControl.Model = contentManager.Load(Of Model)("Model")
        Else
            ' If the build failed, display an error message. 
            MessageBox.Show(buildError, "Error")
        End If

        Cursor = Cursors.Arrow
    End Sub
End Class
