Advertisement
the_hornet

Model Manager & Folder & Basic Model Classes

Apr 2nd, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 47.79 KB | None | 0 0
  1. #region Using Statements
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Collections;
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Audio;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.GamerServices;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Media;
  13. #endregion
  14.  
  15. namespace _3D_Game
  16. {
  17.     /// <summary>
  18.     /// This is a game component that implements IUpdateable.
  19.     /// </summary>
  20.     public class ModelManager : DrawableGameComponent
  21.     {
  22.         #region Properties
  23.         List<Folder> myDocumentsfolderModels = new List<Folder>();
  24.         List<Folder> myMusicFolderModels = new List<Folder>();
  25.         List<Folder> myPicturesFolderModels = new List<Folder>();
  26.         BasicModel room;
  27.         Vector3 myDocumentsPosition = new Vector3(-35, 50, 25);
  28.         Vector3 myMusicPosition = new Vector3(-180,50,25);
  29.         Vector3 myPicturesPosition = new Vector3(100,50,25);
  30.         Vector3 myDocumentsfilePosition = new Vector3(-35, 15, 45);
  31.         Vector3 myMusicFilePosition = new Vector3(-180,15,45);
  32.         Vector3 myPicturesFilePosition = new Vector3(100, 15, 45);
  33.         BoundingSphere camSphere = new BoundingSphere();
  34.         Matrix camWorld = new Matrix();
  35.         MouseState prevMouseState;
  36.         SpriteBatch spriteBatch;
  37.         SpriteFont hudFont;
  38.         SpriteFont titleFont;
  39.         List<string> myDocumentsfolders;
  40.         List<string> myMusicFolders;
  41.         List<string> myPicturesFolders;
  42.         List<string> myDocumentsfolderFullPath;
  43.         List<string> myMusicFolderFullPath;
  44.         List<string> myPicturesFolderFullPath;
  45.         List<File> myDocumentsfileModels = new List<File>();
  46.         List<File> myMusicFileModels = new List<File>();
  47.         List<File> myPicturesFileModels = new List<File>();
  48.         int myDocumentsnumOfFiles;
  49.         int myMusicNumOfFiles;
  50.         int myPicturesNumOfFiles;
  51.         List<string> myDocumentsfiles;
  52.         List<string> myMusicFiles;
  53.         List<string> myPicturesFiles;
  54.         String myMusic = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
  55.         String myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  56.         String myPictures = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
  57.         int myDocumnetsUpperLimit = 25;
  58.         int myDocumentsLowerLimit = -35;
  59.         int myMusicUpperLimit = -100;
  60.         int myMusicLowerLimet = -180;
  61.         int myPicturesUpperLimit = 180;
  62.         int myPicturesLowerLimit = 100;
  63.         int myDocumentsfileRowSize, myMusicFileRowSize, myPicturesFileRowSize;
  64.         int myDocumentsfolderRowSise, myMusicFolderRowSize, myPicturesFolderRowSize;
  65.         String myDocumentsPrevFolder;
  66.         String myDocumentsCurrentFolder;
  67.         String myMusicPrevFolder;
  68.         String myMusicCurrentFolder;
  69.         String myPicturesPrevFolder;
  70.         String MyPicturesCurrentFolder;
  71.         String rightClickedFolder;
  72.         String selectedFolder = "My Documents";
  73.         Folder myDocumentsback;
  74.         Folder myMusicBack;
  75.         Folder myPicturesBack;
  76.         BasicModel musicRoom, pictureRoom;
  77.         bool firstime = true;
  78.         #endregion
  79.  
  80.         #region Constructor
  81.         public ModelManager(Game game)
  82.             : base(game)
  83.         {
  84.             // TODO: Construct any child components here                      
  85.         }
  86.         #endregion
  87.  
  88.         #region Initialisation
  89.         /// <summary>
  90.         /// Allows the game component to perform any initialization it needs to before starting
  91.         /// to run.  This is where it can query for any required services and load content.
  92.         /// </summary>
  93.         public override void Initialize()
  94.         {
  95.             // TODO: Add your initialization code here
  96.             MyDocumentsAssignFoldersAndFiles(myDocuments);
  97.             MyMusicAssignFoldersAndFiles(myMusic);
  98.             MyPicturesAssignFoldersAndFiles(myPictures);
  99.             myDocumentsCurrentFolder = myDocuments;
  100.             base.Initialize();
  101.         }
  102. #endregion
  103.  
  104.         #region Load Content
  105.         /// <summary>
  106.         /// Load content
  107.         /// </summary>
  108.        
  109.         protected override void LoadContent()
  110.         {
  111.             spriteBatch = new SpriteBatch(GraphicsDevice);
  112.             room = new BasicModel(Game.Content.Load<Model>(@"Models\room2"));
  113.             hudFont = Game.Content.Load<SpriteFont>(@"Fonts\hudFont");
  114.             titleFont = Game.Content.Load<SpriteFont>(@"Fonts\titleFont");
  115.             myDocumentsback = new Folder(Game.Content.Load<Model>(@"Models\folder7"), 0);
  116.             myMusicBack = new Folder(Game.Content.Load<Model>(@"Models\folder7"), 0);
  117.             myPicturesBack = new Folder(Game.Content.Load<Model>(@"Models\folder7"), 0);
  118.             musicRoom = new BasicModel(Game.Content.Load<Model>(@"Models\room2"));
  119.             pictureRoom = new BasicModel(Game.Content.Load<Model>(@"Models\room2"));
  120.             //PositionFileViewer();
  121.             for (int i = 0; i < myDocumentsfolders.Count; i++)
  122.             {
  123.                 myDocumentsfolderModels.Add(new Folder(Game.Content.Load<Model>(@"Models\folder7"), i));
  124.             }
  125.  
  126.             for (int i = 0; i < myDocumentsfiles.Count; i++)
  127.             {
  128.                 myDocumentsfileModels.Add(new File(Game.Content.Load<Model>(@"Models\folder7")));
  129.             }
  130.  
  131.             for (int i = 0; i < myMusicFolders.Count; i++)
  132.             {
  133.                 myMusicFolderModels.Add(new Folder(Game.Content.Load<Model>(@"Models\folder7"), i));
  134.             }
  135.  
  136.             for (int i = 0; i < myMusicFiles.Count; i++)
  137.             {
  138.                 myMusicFileModels.Add(new File(Game.Content.Load<Model>(@"Models\folder7")));
  139.             }
  140.  
  141.             for (int i = 0; i < myPicturesFolders.Count; i++)
  142.             {
  143.                 myPicturesFolderModels.Add(new Folder(Game.Content.Load<Model>(@"Models\folder7"), i));
  144.             }
  145.  
  146.             for (int i = 0; i < myPicturesFiles.Count; i++)
  147.             {
  148.                 myPicturesFileModels.Add(new File(Game.Content.Load<Model>(@"Models\folder7")));
  149.             }
  150.  
  151.             AssignFolderData();
  152.             AssignFileData();
  153.             PositionFolders();
  154.             PositionFiles();
  155.             myDocumentsback.moveFolder(new Vector3(-35, 60, 25));
  156.             myMusicBack.moveFolder(new Vector3(-180, 60, 25));
  157.             myPicturesBack.moveFolder(new Vector3(100, 60, 25));
  158.             musicRoom.MoveModel(new Vector3(-150, 0, 0));
  159.             pictureRoom.MoveModel(new Vector3(150, 0, 0));
  160.             BoundingSphereRenderer.Initialize(GraphicsDevice, 45);
  161.             base.LoadContent();
  162.         }
  163.         #endregion
  164.  
  165.         #region Update
  166.         /// <summary>
  167.         /// Allows the game component to update itself.
  168.         /// </summary>
  169.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  170.         public override void Update(GameTime gameTime)
  171.         {
  172.             Ray mouseRay = ((Game1)Game).cursor.CalculateCursorRay(((Game1)Game).camera.projection, ((Game1)Game).camera.view);
  173.             camSphere = ((Game1)Game).camera.sphere;
  174.             camWorld = ((Game1)Game).camera.world;
  175.  
  176.            
  177.  
  178.             // Loop through all models and call Update
  179.  
  180.             for (int i = 0; i < myDocumentsfolderModels.Count; ++i)
  181.             {
  182.                 if (myDocumentsfolderModels[i].CollidesWith(camSphere, camWorld))
  183.                 {
  184.                     ((Game1)Game).backgrnColour = Color.Red;
  185.                 }
  186.  
  187.                 if (myDocumentsfolderModels[i].CheckRayIntersection(mouseRay))
  188.                 {
  189.                     myDocumentsfolderModels[i].ScaleFolder(2.0f);
  190.                     selectedFolder = myDocumentsfolderModels[i].FolderName;
  191.                 }
  192.  
  193.                 if (!myDocumentsfolderModels[i].CheckRayIntersection(mouseRay))
  194.                 {
  195.                     myDocumentsfolderModels[i].ScaleFolder(1.0f);
  196.                 }
  197.  
  198.                 if (myDocumentsfolderModels[i].CheckRayIntersection(mouseRay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
  199.                 {
  200.  
  201.                     myDocumentsCurrentFolder = myDocumentsfolderModels[i].FolderFullPath;
  202.  
  203.                     ShowNewFolders(myDocumentsCurrentFolder);
  204.                     break;
  205.                 }
  206.  
  207.                 if (myDocumentsfolderModels[i].IsSelected == true)
  208.                 {
  209.                     myDocumentsfolderModels[i].ScaleFolder(2.0f);
  210.                 }
  211.  
  212.                 if (myDocumentsfolderModels[i].CheckRayIntersection(mouseRay) && Mouse.GetState().RightButton == ButtonState.Pressed)
  213.                 {
  214.                     prevMouseState = Mouse.GetState();
  215.                     rightClickedFolder = myDocumentsfolderModels[i].FolderFullPath;
  216.  
  217.                     ((Game1)Game).hud.Enabled = true;
  218.                     ((Game1)Game).hud.Visible = true;
  219.                     ((Game1)Game).hud.MyPosition = myDocumentsfolderModels[i].MyPosition;
  220.                     ((Game1)Game).camera.Enabled = false;
  221.                 }
  222.  
  223.                 if (((Game1)Game).hud.Enabled && Mouse.GetState().LeftButton == ButtonState.Pressed)
  224.                 {
  225.                     myDocumentsfolderModels[i].CheckRayIntersection(mouseRay);
  226.  
  227.                 }
  228.  
  229.                 myDocumentsfolderModels[i].Update();
  230.             }
  231.  
  232.             for (int i = 0; i < myMusicFolderModels.Count; ++i)
  233.             {
  234.                 if (myMusicFolderModels[i].CollidesWith(camSphere, camWorld))
  235.                 {
  236.                     ((Game1)Game).backgrnColour = Color.Red;
  237.                 }
  238.  
  239.                 if (myMusicFolderModels[i].CheckRayIntersection(mouseRay))
  240.                 {
  241.                     myMusicFolderModels[i].ScaleFolder(2.0f);
  242.                     selectedFolder = myMusicFolderModels[i].FolderName;
  243.                 }
  244.  
  245.                 if (!myMusicFolderModels[i].CheckRayIntersection(mouseRay))
  246.                 {
  247.                     myMusicFolderModels[i].ScaleFolder(1.0f);
  248.                 }
  249.  
  250.                 if (myMusicFolderModels[i].CheckRayIntersection(mouseRay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
  251.                 {
  252.  
  253.                     myMusicCurrentFolder = myMusicFolderModels[i].FolderFullPath;
  254.  
  255.                     MusicShowNewFolders(myMusicCurrentFolder);
  256.                     break;
  257.                 }
  258.  
  259.                 if (myMusicFolderModels[i].IsSelected == true)
  260.                 {
  261.                     myMusicFolderModels[i].ScaleFolder(2.0f);
  262.                 }
  263.  
  264.  
  265.  
  266.                 if (myMusicFolderModels[i].CheckRayIntersection(mouseRay) && Mouse.GetState().RightButton == ButtonState.Pressed)
  267.                 {
  268.                     prevMouseState = Mouse.GetState();
  269.                     rightClickedFolder = myMusicFolderModels[i].FolderFullPath;
  270.  
  271.                     ((Game1)Game).hud.Enabled = true;
  272.                     ((Game1)Game).hud.Visible = true;
  273.                     ((Game1)Game).hud.MyPosition = myMusicFolderModels[i].MyPosition;
  274.                     ((Game1)Game).camera.Enabled = false;
  275.                 }
  276.  
  277.                 if (((Game1)Game).hud.Enabled && Mouse.GetState().LeftButton == ButtonState.Pressed)
  278.                 {
  279.                     myMusicFolderModels[i].CheckRayIntersection(mouseRay);
  280.  
  281.                 }
  282.  
  283.                 myMusicFolderModels[i].Update();
  284.             }
  285.  
  286.             for (int i = 0; i < myPicturesFolderModels.Count; ++i)
  287.             {
  288.                 if (myPicturesFolderModels[i].CollidesWith(camSphere, camWorld))
  289.                 {
  290.                     ((Game1)Game).backgrnColour = Color.Red;
  291.                 }
  292.  
  293.                 if (myPicturesFolderModels[i].CheckRayIntersection(mouseRay))
  294.                 {
  295.                     myPicturesFolderModels[i].ScaleFolder(2.0f);
  296.                     selectedFolder = myPicturesFolderModels[i].FolderName;
  297.                 }
  298.  
  299.                 if (!myPicturesFolderModels[i].CheckRayIntersection(mouseRay))
  300.                 {
  301.                     myPicturesFolderModels[i].ScaleFolder(1.0f);
  302.                 }
  303.  
  304.                 if (myPicturesFolderModels[i].CheckRayIntersection(mouseRay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
  305.                 {
  306.  
  307.                     MyPicturesCurrentFolder = myPicturesFolderModels[i].FolderFullPath;
  308.  
  309.                     PicturesShowNewFolders(MyPicturesCurrentFolder);
  310.                     break;
  311.                 }
  312.  
  313.                 if (myPicturesFolderModels[i].IsSelected == true)
  314.                 {
  315.                     myPicturesFolderModels[i].ScaleFolder(2.0f);
  316.                 }
  317.  
  318.  
  319.  
  320.                 if (myPicturesFolderModels[i].CheckRayIntersection(mouseRay) && Mouse.GetState().RightButton == ButtonState.Pressed)
  321.                 {
  322.                     prevMouseState = Mouse.GetState();
  323.                     rightClickedFolder = myPicturesFolderModels[i].FolderFullPath;
  324.  
  325.                     ((Game1)Game).hud.Enabled = true;
  326.                     ((Game1)Game).hud.Visible = true;
  327.                     ((Game1)Game).hud.MyPosition = myPicturesFolderModels[i].MyPosition;
  328.                     ((Game1)Game).camera.Enabled = false;
  329.                 }
  330.  
  331.                 if (((Game1)Game).hud.Enabled && Mouse.GetState().LeftButton == ButtonState.Pressed)
  332.                 {
  333.                     myPicturesFolderModels[i].CheckRayIntersection(mouseRay);
  334.  
  335.                 }
  336.  
  337.                 myPicturesFileModels[i].Update();
  338.             }
  339.  
  340.  
  341.             if (myDocumentsback.CheckRayIntersection(mouseRay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
  342.             {
  343.  
  344.                 myDocumentsPrevFolder = FileOperations.UpDirectory(myDocumentsCurrentFolder);
  345.                 myDocumentsCurrentFolder = myDocumentsPrevFolder;
  346.                 ShowNewFolders(myDocumentsPrevFolder);
  347.             }
  348.  
  349.             if (myMusicBack.CheckRayIntersection(mouseRay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
  350.             {
  351.  
  352.                 myMusicPrevFolder = FileOperations.UpDirectory(myMusicCurrentFolder);
  353.                 myMusicCurrentFolder = myMusicPrevFolder;
  354.                 MusicShowNewFolders(myMusicPrevFolder);
  355.             }
  356.            
  357.             if (myPicturesBack.CheckRayIntersection(mouseRay) && Mouse.GetState().LeftButton == ButtonState.Pressed)
  358.             {
  359.  
  360.                 myPicturesPrevFolder = FileOperations.UpDirectory(MyPicturesCurrentFolder);
  361.                 MyPicturesCurrentFolder = myPicturesPrevFolder;
  362.                 PicturesShowNewFolders(myPicturesPrevFolder);
  363.             }
  364.  
  365.             if (Keyboard.GetState().IsKeyDown(Keys.R))
  366.             {
  367.                 ((Game1)Game).backgrnColour = Color.CornflowerBlue;
  368.             }
  369.  
  370.             if (Keyboard.GetState().IsKeyDown(Keys.Right))
  371.             {
  372.                 moveMyDocumentsFoldersRight();
  373.             }
  374.  
  375.             if (Keyboard.GetState().IsKeyDown(Keys.Left))
  376.             {
  377.                 moveMyDocumentFoldersLeft();
  378.             }
  379.  
  380.             if (Keyboard.GetState().IsKeyDown(Keys.Z))
  381.             {
  382.                 MoveMyDocumentsFileLeft();
  383.             }
  384.  
  385.             if (Keyboard.GetState().IsKeyDown(Keys.X))
  386.             {
  387.                 MoveMyDocumentsFileRight();
  388.             }
  389.  
  390.             if (((Game1)Game).hud.Enabled && Mouse.GetState().LeftButton == ButtonState.Pressed)
  391.             {
  392.                 ((Game1)Game).hud.Enabled = false;
  393.                 ((Game1)Game).hud.Visible = false;
  394.                 ((Game1)Game).camera.Enabled = true;
  395.                 Mouse.SetPosition(prevMouseState.X, prevMouseState.Y);
  396.             }
  397.  
  398.             base.Update(gameTime);
  399.         }
  400. #endregion
  401.  
  402.         #region Draw
  403.         /// <summary>
  404.         /// Allows the game component to draw itself
  405.         /// </summary>
  406.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  407.         public override void Draw(GameTime gameTime)
  408.         {
  409.             // Draw room
  410.             room.Draw(((Game1)Game).camera);
  411.             myDocumentsback.Draw(((Game1)Game).camera);
  412.             myMusicBack.Draw(((Game1)Game).camera);
  413.             myPicturesBack.Draw(((Game1)Game).camera);
  414.             musicRoom.Draw(((Game1)Game).camera);
  415.             pictureRoom.Draw(((Game1)Game).camera);
  416.  
  417.             // Loop through and draw each model
  418.             foreach (Folder bm in myDocumentsfolderModels)
  419.             {
  420.                 if ((bm.MyPosition.X >= myDocumentsLowerLimit) && (bm.MyPosition.X <= myDocumnetsUpperLimit))
  421.                 {
  422.                     bm.Draw(((Game1)Game).camera);
  423.                     DrawModelNames(bm);
  424.                     if (selectedFolder != null)
  425.                     {
  426.                         DrawTitle(selectedFolder);
  427.                     }
  428.                 }
  429.             }
  430.  
  431.             foreach (File f in myDocumentsfileModels)
  432.             {
  433.                 if ((f.MyPosition.X >= myDocumentsLowerLimit) && (f.MyPosition.X <= myDocumnetsUpperLimit))
  434.                 {
  435.                     f.Draw(((Game1)Game).camera);
  436.                     DrawFileNames(f);
  437.                 }
  438.             }
  439.  
  440.             foreach (Folder mf in myMusicFolderModels)
  441.             {
  442.                 if ((mf.MyPosition.X >= myMusicLowerLimet) && (mf.MyPosition.X <= myMusicUpperLimit))
  443.                 {
  444.                     mf.Draw(((Game1)Game).camera);
  445.                     DrawModelNames(mf);
  446.                     if (selectedFolder != null)
  447.                     {
  448.                         DrawTitle(selectedFolder);
  449.                     }
  450.                 }
  451.             }
  452.  
  453.             foreach (Folder pf in myPicturesFolderModels)
  454.             {
  455.                 if ((pf.MyPosition.X >= myPicturesLowerLimit) && (pf.MyPosition.X <= myPicturesUpperLimit))
  456.                 {
  457.                     pf.Draw(((Game1)Game).camera);
  458.                     DrawModelNames(pf);
  459.                     if (selectedFolder != null)
  460.                     {
  461.                         DrawTitle(selectedFolder);
  462.                     }
  463.                 }
  464.             }
  465.  
  466.             foreach (File mf in myMusicFileModels)
  467.             {
  468.                 if ((mf.MyPosition.X >= myMusicLowerLimet) && (mf.MyPosition.X <= myMusicUpperLimit))
  469.                 {
  470.                     mf.Draw(((Game1)Game).camera);
  471.                     DrawFileNames(mf);
  472.                 }
  473.             }
  474.  
  475.             foreach (File pf in myPicturesFileModels)
  476.             {
  477.                 if ((pf.MyPosition.X >= myPicturesLowerLimit) && (pf.MyPosition.X <= myPicturesUpperLimit))
  478.                 {
  479.                     pf.Draw(((Game1)Game).camera);
  480.                     DrawFileNames(pf);
  481.                 }
  482.             }
  483.  
  484.  
  485.             base.Draw(gameTime);
  486.         }
  487.         #endregion
  488.  
  489.         #region Draw Folder and File Names
  490.  
  491.         private void DrawTitle(string title)
  492.         {
  493.             if (title != null)
  494.             {
  495.                 spriteBatch.Begin();
  496.                 spriteBatch.DrawString(titleFont, title, new Vector2(GraphicsDevice.DisplayMode.Width / 2 + 1, 51), Color.Black, 0.0f, titleFont.MeasureString(title), 1.0f, SpriteEffects.None, 0.0f);
  497.                 spriteBatch.DrawString(titleFont, title, new Vector2(GraphicsDevice.DisplayMode.Width / 2, 50), Color.White, 0.0f, titleFont.MeasureString(title), 1.0f, SpriteEffects.None, 0.0f);
  498.                 spriteBatch.End();
  499.             }
  500.  
  501.         }
  502.  
  503.         private void DrawModelNames(Folder bm)
  504.         {
  505.             Ray mouseRay = ((Game1)Game).cursor.CalculateCursorRay(((Game1)Game).camera.projection, ((Game1)Game).camera.view);
  506.             if (bm.CheckRayIntersection(mouseRay))
  507.             {
  508.                 // begin on the spritebatch, because we're going to be drawing some text.
  509.                 spriteBatch.Begin();
  510.  
  511.                 Vector3 screenSpace = GraphicsDevice.Viewport.Project(
  512.                     Vector3.Zero, ((Game1)Game).camera.projection, ((Game1)Game).camera.view, bm.GetWorld());
  513.  
  514.                 Vector2 textPosition =
  515.                     new Vector2(screenSpace.X, screenSpace.Y - 60);
  516.  
  517.                 Vector2 stringCenter =
  518.                     hudFont.MeasureString(bm.FolderName + " " + bm.NumOfFiles) / 2;
  519.  
  520.                 Vector2 shadowOffset = new Vector2(1, 1);
  521.                 if (screenSpace.Z > 0)
  522.                 {
  523.                     spriteBatch.DrawString(hudFont, bm.FolderName + " " + bm.NumOfFiles,
  524.                     textPosition + shadowOffset, Color.Black, 0.0f,
  525.                     stringCenter, 1.0f, SpriteEffects.None, 0.0f);
  526.  
  527.                     spriteBatch.DrawString(hudFont, bm.FolderName + " " + bm.NumOfFiles,
  528.                         textPosition, Color.White, 0.0f,
  529.                         stringCenter, 1.0f, SpriteEffects.None, 0.0f);
  530.                 }
  531.  
  532.  
  533.  
  534.                 spriteBatch.End();
  535.             }
  536.         }
  537.  
  538.         private void DrawFileNames(File f)
  539.         {
  540.             Ray mouseRay = ((Game1)Game).cursor.CalculateCursorRay(((Game1)Game).camera.projection, ((Game1)Game).camera.view);
  541.             if (f.CheckRayIntersection(mouseRay))
  542.             {
  543.                 Vector2 stringCenter = Vector2.Zero;
  544.                 String text;
  545.                 spriteBatch.Begin();
  546.  
  547.                 Vector3 screenSpace = GraphicsDevice.Viewport.Project(
  548.                     Vector3.Zero, ((Game1)Game).camera.projection, ((Game1)Game).camera.view, f.GetWorld());
  549.  
  550.                 Vector2 textPosition =
  551.                     new Vector2(screenSpace.X, screenSpace.Y - 60);
  552.  
  553.                 if (f.FileName != null)
  554.                 {
  555.                     stringCenter =
  556.                         hudFont.MeasureString(f.FileName) / 2;
  557.                     text = f.FileName;
  558.                 }
  559.                 else
  560.                 {
  561.                     stringCenter = hudFont.MeasureString("null") / 2;
  562.                     text = "null";
  563.                 }
  564.  
  565.  
  566.                 Vector2 shadowOffset = new Vector2(1, 1);
  567.                 spriteBatch.DrawString(hudFont, text,
  568.                     textPosition + shadowOffset, Color.Black, 0.0f,
  569.                     stringCenter, 1.0f, SpriteEffects.None, 0.0f);
  570.  
  571.                 spriteBatch.DrawString(hudFont, text,
  572.                     textPosition, Color.White, 0.0f,
  573.                     stringCenter, 1.0f, SpriteEffects.None, 0.0f);
  574.  
  575.                 spriteBatch.End();
  576.             }
  577.         }
  578.         #endregion
  579.  
  580.         #region Folder & File Data Assigning
  581.  
  582.         private void AssignFolderData()
  583.         {
  584.             for (int i = 0; i < myDocumentsfolders.Count; i++)
  585.             {
  586.                 myDocumentsfolderModels[i].FolderName = myDocumentsfolders[i];
  587.                 myDocumentsfolderModels[i].FolderFullPath = myDocumentsfolderFullPath[i];
  588.             }
  589.  
  590.             for (int i = 0; i < myMusicFolders.Count; i++)
  591.             {
  592.                 myMusicFolderModels[i].FolderName = myMusicFolders[i];
  593.                 myMusicFolderModels[i].FolderFullPath = myMusicFolderFullPath[i];
  594.             }
  595.  
  596.             for (int i = 0; i < myPicturesFolders.Count; i++)
  597.             {
  598.                 myPicturesFolderModels[i].FolderName = myPicturesFolders[i];
  599.                 myPicturesFolderModels[i].FolderFullPath = myPicturesFolderFullPath[i];
  600.             }
  601.         }
  602.  
  603.         private void AssignFileData()
  604.         {
  605.             for (int i = 0; i < myDocumentsfiles.Count; i++)
  606.             {
  607.                 myDocumentsfileModels[i].FileName = myDocumentsfiles[i];
  608.             }
  609.  
  610.             for (int i = 0; i < myMusicFiles.Count; i++)
  611.             {
  612.                 myMusicFileModels[i].FileName = myMusicFiles[i];
  613.             }
  614.  
  615.             for (int i = 0; i < myPicturesFiles.Count; i++)
  616.             {
  617.                 myPicturesFileModels[i].FileName = myPicturesFiles[i];
  618.             }
  619.         }
  620.  
  621.         private void MyDocumentsAssignFoldersAndFiles(string folder)
  622.         {
  623.             // Assign Files and Folders for My Documents folder
  624.             myDocumentsfolders = FileOperations.showFolders(folder);
  625.             myDocumentsfolderFullPath = FileOperations.showFolderFullPath(folder);
  626.             myDocumentsnumOfFiles = FileOperations.NoOfFiles(folder);
  627.             myDocumentsfiles = FileOperations.showFiles(folder);
  628.         }
  629.  
  630.         private void MyMusicAssignFoldersAndFiles(string folder)
  631.         {
  632.             // Assign Files and Folders for My Music Folder
  633.             myMusicFolders = FileOperations.showFolders(folder);
  634.             myMusicFolderFullPath = FileOperations.showFolderFullPath(folder);
  635.             myMusicNumOfFiles = FileOperations.NoOfFiles(folder);
  636.             myMusicFiles = FileOperations.showFiles(folder);
  637.         }
  638.  
  639.         private void MyPicturesAssignFoldersAndFiles(string folder)
  640.         {
  641.             // Assign Files and Folders for My Pictures Folder
  642.             myPicturesFolders = FileOperations.showFolders(folder);
  643.             myPicturesFolderFullPath = FileOperations.showFolderFullPath(folder);
  644.             myPicturesNumOfFiles = FileOperations.NoOfFiles(folder);
  645.             myPicturesFiles = FileOperations.showFiles(folder);
  646.         }
  647. #endregion
  648.        
  649.         #region Folder & File Movement
  650.         private void MoveMyDocumentsFileRight()
  651.         {
  652.             if (myDocumentsfileModels[0].MyPosition.X < myDocumnetsUpperLimit)
  653.             {
  654.                 for (int i = 0; i < myDocumentsfileModels.Count; i++)
  655.                 {
  656.                     Vector3 newPosition = myDocumentsfileModels[i].MyPosition;
  657.                     newPosition.X += 1;
  658.                     myDocumentsfileModels[i].MoveFile(newPosition);
  659.                 }
  660.             }
  661.         }
  662.  
  663.         private void MoveMyMusicFileRight()
  664.         {
  665.             if (myMusicFileModels[0].MyPosition.X < myMusicUpperLimit)
  666.             {
  667.                 for (int i = 0; i < myMusicFileModels.Count; i++)
  668.                 {
  669.                     Vector3 newPosition = myMusicFileModels[i].MyPosition;
  670.                     newPosition.X += 1;
  671.                     myMusicFileModels[i].MoveFile(newPosition);
  672.                 }
  673.             }
  674.         }
  675.  
  676.         private void MoveMyPicturesFileRight()
  677.         {
  678.             if (myPicturesFileModels[0].MyPosition.X < myPicturesUpperLimit)
  679.             {
  680.                 for (int i = 0; i < myPicturesFileModels.Count; i++)
  681.                 {
  682.                     Vector3 newPosition = myPicturesFileModels[i].MyPosition;
  683.                     newPosition.X += 1;
  684.                     myPicturesFileModels[i].MoveFile(newPosition);
  685.                 }
  686.             }
  687.         }
  688.  
  689.         private void MoveMyDocumentsFileLeft()
  690.         {
  691.             if (myDocumentsfileModels[myDocumentsfileRowSize - 1].MyPosition.X > myDocumentsLowerLimit)
  692.             {
  693.                 for (int i = 0; i < myDocumentsfileModels.Count; i++)
  694.                 {
  695.                     Vector3 newPosition = myDocumentsfileModels[i].MyPosition;
  696.                     newPosition.X -= 1;
  697.                     myDocumentsfileModels[i].MoveFile(newPosition);
  698.                 }
  699.             }
  700.         }
  701.  
  702.         private void MoveMyMusicFileLeft()
  703.         {
  704.             if (myMusicFileModels[myMusicFileRowSize - 1].MyPosition.X > myMusicLowerLimet)
  705.             {
  706.                 for (int i = 0; i < myMusicFileModels.Count; i++)
  707.                 {
  708.                     Vector3 newPosition = myMusicFileModels[i].MyPosition;
  709.                     newPosition.X -= 1;
  710.                     myMusicFileModels[i].MoveFile(newPosition);
  711.                 }
  712.             }
  713.         }
  714.  
  715.         private void MoveMyPicturesFileLeft()
  716.         {
  717.             if (myPicturesFileModels[myPicturesFileRowSize - 1].MyPosition.X > myPicturesLowerLimit)
  718.             {
  719.                 for (int i = 0; i < myPicturesFileModels.Count; i++)
  720.                 {
  721.                     Vector3 newPosition = myPicturesFileModels[i].MyPosition;
  722.                     newPosition.X -= 1;
  723.                     myPicturesFileModels[i].MoveFile(newPosition);
  724.                 }
  725.             }
  726.         }
  727.  
  728.         private void moveMyDocumentsFoldersRight()
  729.         {
  730.             if (myDocumentsfolderModels[0].MyPosition.X < myDocumnetsUpperLimit)
  731.             {
  732.                 for (int i = 0; i < myDocumentsfolders.Count; i++)
  733.                 {
  734.                     Vector3 newPosition = myDocumentsfolderModels[i].MyPosition;
  735.                     newPosition.X += 1;
  736.                     myDocumentsfolderModels[i].moveFolder(newPosition);
  737.                 }
  738.             }
  739.         }
  740.  
  741.         private void moveMyMusicFoldersRight()
  742.         {
  743.             if (myMusicFolderModels[0].MyPosition.X < myMusicUpperLimit)
  744.             {
  745.                 for (int i = 0; i < myMusicFolderModels.Count; i++)
  746.                 {
  747.                     Vector3 newPosition = myMusicFolderModels[i].MyPosition;
  748.                     newPosition.X += 1;
  749.                     myMusicFolderModels[i].moveFolder(newPosition);
  750.                 }
  751.             }
  752.         }
  753.  
  754.         private void moveMyPicturesFoldersRight()
  755.         {
  756.             if (myPicturesFolderModels[0].MyPosition.X < myPicturesUpperLimit)
  757.             {
  758.                 for (int i = 0; i < myPicturesFolderModels.Count; i++)
  759.                 {
  760.                     Vector3 newPosition = myPicturesFolderModels[i].MyPosition;
  761.                     newPosition.X += 1;
  762.                     myPicturesFolderModels[i].moveFolder(newPosition);
  763.                 }
  764.             }
  765.         }
  766.  
  767.         private void moveMyDocumentFoldersLeft()
  768.         {
  769.             if (myDocumentsfolderModels[myDocumentsfolderRowSise - 1].MyPosition.X > myDocumentsLowerLimit)
  770.             {
  771.                 for (int i = 0; i < myDocumentsfolders.Count; i++)
  772.                 {
  773.                     Vector3 newPosition = myDocumentsfolderModels[i].MyPosition;
  774.                     newPosition.X -= 1;
  775.                     myDocumentsfolderModels[i].moveFolder(newPosition);
  776.                 }
  777.             }
  778.         }
  779.  
  780.         private void moveMyMusicFoldersLeft()
  781.         {
  782.             if (myMusicFolderModels[myMusicFolderRowSize - 1].MyPosition.X > myMusicLowerLimet)
  783.             {
  784.                 for (int i = 0; i < myMusicFolderModels.Count; i++)
  785.                 {
  786.                     Vector3 newPosition = myMusicFolderModels[i].MyPosition;
  787.                     newPosition.X -= 1;
  788.                     myMusicFolderModels[i].moveFolder(newPosition);
  789.                 }
  790.             }
  791.         }
  792.  
  793.         private void moveMyPicturesFoldersLeft()
  794.         {
  795.             if (myPicturesFolderModels[myPicturesFolderRowSize - 1].MyPosition.X > myPicturesLowerLimit)
  796.             {
  797.                 for (int i = 0; i < myPicturesFolderModels.Count; i++)
  798.                 {
  799.                     Vector3 newPosition = myPicturesFolderModels[i].MyPosition;
  800.                     newPosition.X -= 1;
  801.                     myPicturesFolderModels[i].moveFolder(newPosition);
  802.                 }
  803.             }
  804.         }
  805.         #endregion
  806.  
  807.         #region Folder and File Positioning
  808.  
  809.         private void PositionFolders()
  810.         {
  811.             int myDocumentsModulus = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(myDocumentsfolders.Count) / Convert.ToDecimal(6.0)));
  812.             int myMusicModulus = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(myMusicFolders.Count) / Convert.ToDecimal(6.0)));
  813.             int myPicturesModulus = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(myPicturesFolders.Count) / Convert.ToDecimal(6.0)));
  814.             myDocumentsfolderRowSise = myDocumentsModulus;
  815.             myMusicFolderRowSize = myMusicModulus;
  816.             myPicturesFolderRowSize = myPicturesModulus;
  817.  
  818.             for (int i = 0; i < myDocumentsfolders.Count; i++)
  819.             {
  820.                 myDocumentsfolderModels[i].moveFolder(myDocumentsPosition);
  821.                 myDocumentsPosition.X += 5;
  822.                 if (((i + 1) % myDocumentsModulus == 0) && (i != 0))
  823.                 {
  824.                     myDocumentsPosition.Y -= 5;
  825.                     myDocumentsPosition.X -= myDocumentsModulus * 5;
  826.                 }
  827.             }
  828.  
  829.             for (int i = 0; i < myMusicFolders.Count; i++)
  830.             {
  831.                 myMusicFolderModels[i].moveFolder(myMusicPosition);
  832.                 myMusicPosition.X += 5;
  833.                 if (((i + 1) % myMusicModulus == 0) && (i != 0))
  834.                 {
  835.                     myMusicPosition.Y -= 5;
  836.                     myMusicPosition.X -= myMusicModulus * 5;
  837.                 }
  838.             }
  839.  
  840.             for (int i = 0; i < myPicturesFolders.Count; i++)
  841.             {
  842.                 myPicturesFolderModels[i].moveFolder(myPicturesPosition);
  843.                 myPicturesPosition.X += 5;
  844.                 if (((i + 1) % myPicturesModulus == 0) && (i != 0))
  845.                 {
  846.                     myPicturesPosition.Y -= 5;
  847.                     myPicturesPosition.X -= myPicturesModulus * 5;
  848.                 }
  849.             }
  850.         }
  851.  
  852.         private void PositionFiles()
  853.         {
  854.             int myDocumentsmodulus = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(myDocumentsfileModels.Count) / Convert.ToDecimal(3)));
  855.             int myMusicmodulus = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(myMusicFileModels.Count) / Convert.ToDecimal(3)));
  856.             int myPicturesmodulus = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(myPicturesFileModels.Count) / Convert.ToDecimal(3)));
  857.  
  858.             myDocumentsfileRowSize = myDocumentsmodulus;
  859.             myMusicFileRowSize = myMusicmodulus;
  860.             myPicturesFileRowSize = myPicturesmodulus;
  861.  
  862.             for (int i = 0; i < myDocumentsfiles.Count; i++)
  863.             {
  864.  
  865.                 myDocumentsfileModels[i].MoveFile(myDocumentsfilePosition);
  866.                 myDocumentsfileModels[i].RotateFile(MathHelper.ToRadians(-45.0f), myDocumentsfilePosition);
  867.                 myDocumentsfilePosition.X += 5;
  868.                 if (((i + 1) % myDocumentsmodulus == 0) && (i != 0))
  869.                 {
  870.                     myDocumentsfilePosition.Y -= 5;
  871.                     myDocumentsfilePosition.X -= myDocumentsmodulus * 5;
  872.                     myDocumentsfilePosition.Z += 5;
  873.                 }
  874.             }
  875.  
  876.             for (int i = 0; i < myMusicFiles.Count; i++)
  877.             {
  878.  
  879.                 myMusicFileModels[i].MoveFile(myMusicFilePosition);
  880.                 myMusicFileModels[i].RotateFile(MathHelper.ToRadians(-45.0f), myMusicFilePosition);
  881.                 myMusicFilePosition.X += 5;
  882.                 if (((i + 1) % myMusicmodulus == 0) && (i != 0))
  883.                 {
  884.                     myMusicFilePosition.Y -= 5;
  885.                     myMusicFilePosition.X -= myMusicmodulus * 5;
  886.                     myMusicFilePosition.Z += 5;
  887.                 }
  888.             }
  889.  
  890.             for (int i = 0; i < myPicturesFiles.Count; i++)
  891.             {
  892.  
  893.                 myPicturesFileModels[i].MoveFile(myPicturesFilePosition);
  894.                 myPicturesFileModels[i].RotateFile(MathHelper.ToRadians(-45.0f), myPicturesFilePosition);
  895.                 myPicturesFilePosition.X += 5;
  896.                 if (((i + 1) % myPicturesmodulus == 0) && (i != 0))
  897.                 {
  898.                     myPicturesFilePosition.Y -= 5;
  899.                     myPicturesFilePosition.X -= myPicturesmodulus * 5;
  900.                     myPicturesFilePosition.Z += 5;
  901.                 }
  902.             }
  903.         }
  904.         #endregion
  905.  
  906.         #region Show New Folders & Files & Reset Folder Positions
  907.         private void ShowNewFolders(string folder)
  908.         {
  909.            
  910.                     myDocumentsfolderModels.Clear();
  911.                     myDocumentsfileModels.Clear();
  912.                     MyDocumentsAssignFoldersAndFiles(folder);
  913.                     ResetPositions();
  914.                     this.LoadContent();
  915.         }
  916.  
  917.         private void MusicShowNewFolders(string folder)
  918.         {
  919.                     myMusicFolderModels.Clear();
  920.                     myMusicFileModels.Clear();
  921.                     MyMusicAssignFoldersAndFiles(folder);
  922.                     ResetPositions();
  923.                     this.LoadContent();
  924.         }
  925.  
  926.         private void PicturesShowNewFolders(string folder)
  927.         {
  928.                     myPicturesFolderModels.Clear();
  929.                     myPicturesFileModels.Clear();
  930.                     MyPicturesAssignFoldersAndFiles(folder);
  931.                     ResetPositions();
  932.                     this.LoadContent();
  933.         }
  934.  
  935.         private void ResetPositions()
  936.         {
  937.             myDocumentsPosition = new Vector3(-35, 50, 25);
  938.             myDocumentsfilePosition = new Vector3(-35, 15, 45);
  939.             myMusicPosition = new Vector3(-180, 50, 25);
  940.             myMusicFilePosition = new Vector3(-180, 15, 45);
  941.             myPicturesPosition = new Vector3(100, 50, 25);
  942.             myPicturesFilePosition = new Vector3(100, 15, 45);
  943.  
  944.         }
  945.         #endregion      
  946.     }
  947. }
  948.  
  949. using System;
  950. using System.Collections.Generic;
  951. using System.Linq;
  952. using System.Text;
  953. using Microsoft.Xna.Framework;
  954. using Microsoft.Xna.Framework.Graphics;
  955.  
  956. namespace _3D_Game
  957. {
  958.     class Folder : BasicModel
  959.     {
  960.         private string folderName;
  961.         private string folderFullPath;
  962.         private int noOfFiles;
  963.         private bool isSelected;
  964.         private Vector3 myPosition = Vector3.Zero;
  965.         private float myScale = 1.0f;
  966.         private Matrix myWorld = Matrix.Identity;
  967.         private int number;
  968.         private List<BoundingBox> box = new List<BoundingBox>();
  969.         private BasicEffect boxEffect;
  970.         private GraphicsDevice graphics;
  971.        
  972.  
  973.         public Vector3 MyPosition
  974.         {
  975.             get { return myPosition; }
  976.             set { myPosition = value; }
  977.         }
  978.  
  979.         public Matrix MyWorld
  980.         {
  981.             get { return myWorld; }
  982.             set { myWorld = value; }
  983.         }
  984.         public float MyScale
  985.         {
  986.             get { return myScale; }
  987.             set { myScale = value; }
  988.         }
  989.  
  990.         public bool IsSelected
  991.         {
  992.             get { return isSelected; }
  993.             set { isSelected = value; }
  994.         }
  995.  
  996.         public int NumOfFiles
  997.         {
  998.             get { return noOfFiles; }
  999.         }
  1000.  
  1001.         public String FolderName
  1002.         {
  1003.             get { return folderName; }
  1004.             set
  1005.             {
  1006.                 folderName = value;
  1007.             }
  1008.         }
  1009.  
  1010.         public string FolderFullPath
  1011.         {
  1012.             get { return folderFullPath; }
  1013.             set
  1014.             {
  1015.                 folderFullPath = value;
  1016.                 NoOfFiles(value);
  1017.             }
  1018.         }
  1019.  
  1020.         public Folder(Model m, int folderNumber, GraphicsDevice g, Vector3 pos)
  1021.             : base(m,g)
  1022.         {
  1023.             graphics = g;
  1024.             number = folderNumber;
  1025.             myPosition = pos;
  1026.             boxEffect = new BasicEffect(g);
  1027.         }
  1028.  
  1029.         public override void Update()
  1030.         {
  1031.  
  1032.             base.Update();
  1033.         }
  1034.  
  1035.         public override Matrix GetWorld()
  1036.         {
  1037.             return myWorld;
  1038.         }        
  1039.  
  1040.         public void moveFolder(Vector3 pos)
  1041.         {
  1042.             myPosition = pos;
  1043.             myWorld = Matrix.CreateScale(myScale) * Matrix.CreateTranslation(pos);
  1044.         }
  1045.  
  1046.  
  1047.         private void NoOfFiles(string folder)
  1048.         {
  1049.             noOfFiles = FileOperations.NoOfFiles(folderFullPath);
  1050.         }
  1051.  
  1052.         public void ScaleFolder(float scale)
  1053.         {
  1054.             myScale = scale;
  1055.             myWorld = Matrix.CreateScale(scale) * Matrix.CreateTranslation(myPosition);
  1056.         }        
  1057.     }
  1058. }
  1059.  
  1060.  
  1061. using System;
  1062. using System.Collections.Generic;
  1063. using System.Linq;
  1064. using System.Text;
  1065. using Microsoft.Xna.Framework;
  1066. using Microsoft.Xna.Framework.Graphics;
  1067.  
  1068. namespace _3D_Game
  1069. {
  1070.     class BasicModel
  1071.     {
  1072.         public Model model { get; protected set; }
  1073.         protected Matrix world = Matrix.Identity;
  1074.         public List<BoundingBox> boundingBoxes = new List<BoundingBox>();
  1075.         short[] bBoxIndices =
  1076.         {
  1077.                 0, 1, 1, 2, 2, 3, 3, 0, // Front edges
  1078.                 4, 5, 5, 6, 6, 7, 7, 4, // Back edges
  1079.                 0, 4, 1, 5, 2, 6, 3, 7 // Side edges connecting front and back
  1080.         };
  1081.         BasicEffect boxEffect;
  1082.         GraphicsDevice graphics;
  1083.  
  1084.         public BasicModel(Model m, GraphicsDevice g)
  1085.         {
  1086.             model = m;
  1087.             graphics = g;
  1088.             MeshModel();
  1089.             boxEffect = new BasicEffect(g);                    
  1090.         }
  1091.  
  1092.        
  1093.  
  1094.         public virtual void Update()
  1095.         {
  1096.              
  1097.         }
  1098.  
  1099.         public virtual  void Draw(Camera camera)
  1100.         {
  1101.  
  1102.             Matrix[] transforms = new Matrix[model.Bones.Count];
  1103.             model.CopyAbsoluteBoneTransformsTo(transforms);
  1104.  
  1105.             foreach (ModelMesh mesh in model.Meshes)
  1106.             {
  1107.  
  1108.                 foreach (BasicEffect be in mesh.Effects)
  1109.                 {
  1110.                     be.EnableDefaultLighting();
  1111.                     be.Projection = camera.projection;
  1112.                     be.View = camera.view;
  1113.                     be.World = mesh.ParentBone.Transform * GetWorld();
  1114.                 }
  1115.  
  1116.                 mesh.Draw();
  1117.  
  1118.                 // ... and then transform the BoundingSphere using that matrix.
  1119.                 BoundingSphere sphere = TransformBoundingSphere(mesh.BoundingSphere, world);
  1120.  
  1121.                 foreach (BoundingBox box in boundingBoxes)
  1122.                 {
  1123.                     Vector3[] corners = box.GetCorners();
  1124.                     VertexPositionColor[] primitiveList = new VertexPositionColor[corners.Length];
  1125.  
  1126.                     // Assign the 8 box vertices
  1127.                     for (int i = 0; i < corners.Length; i++)
  1128.                     {
  1129.                         primitiveList[i] = new VertexPositionColor(corners[i], Color.White);
  1130.                     }
  1131.  
  1132.                     /* Set your own effect parameters here */
  1133.  
  1134.                     boxEffect.World = GetWorld();
  1135.                     boxEffect.View = camera.view;
  1136.                     boxEffect.Projection = camera.projection;
  1137.                     boxEffect.TextureEnabled = false;
  1138.  
  1139.                     // Draw the box with a LineList
  1140.                     foreach (EffectPass pass in boxEffect.CurrentTechnique.Passes)
  1141.                     {
  1142.                         pass.Apply();
  1143.                         graphics.DrawUserIndexedPrimitives(PrimitiveType.LineList, primitiveList, 0, 8, bBoxIndices, 0, 12);
  1144.  
  1145.                     }
  1146.                 }
  1147.  
  1148.  
  1149.  
  1150.                 // now draw the sphere with our renderer
  1151.                 BoundingSphereRenderer.Draw(sphere, camera.view, camera.projection);
  1152.  
  1153.             }
  1154.         }
  1155.  
  1156.         public void RotateFile(float rotation, Vector3 position)
  1157.         {
  1158.             world = Matrix.CreateRotationX(rotation) * Matrix.CreateTranslation(position);
  1159.         }
  1160.  
  1161.         public virtual Matrix GetWorld()
  1162.         {
  1163.             return world;
  1164.         }
  1165.  
  1166.         //public bool CollidesWith(BoundingBox otherbox, Matrix otherWorld)
  1167.         //{
  1168.         //    foreach (ModelMesh myModelMeshes in model.Meshes)
  1169.         //    {
  1170.  
  1171.         //        if (myModelMeshes.BoundingSphere.Transform(
  1172.         //            GetWorld()).Intersects(
  1173.         //            otherbox.Transform(otherWorld)))
  1174.         //        {
  1175.         //            return true;
  1176.         //        }
  1177.         //    }
  1178.         //    return false;
  1179.         //}
  1180.  
  1181.         public bool CheckRayIntersection(Ray ray)
  1182.         {
  1183.             foreach (BoundingBox bb in boundingBoxes)
  1184.             {
  1185.                 if (bb.Intersects(ray) != null)
  1186.                 {
  1187.                     return true;
  1188.                 }
  1189.             }
  1190.  
  1191.             return false;
  1192.         }
  1193.  
  1194.         /// <summary>
  1195.         /// This helper function takes a BoundingSphere and a transform matrix, and
  1196.         /// returns a transformed version of that BoundingSphere.
  1197.         /// </summary>
  1198.         /// <param name="sphere">the BoundingSphere to transform</param>
  1199.         /// <param name="world">how to transform the BoundingSphere.</param>
  1200.         /// <returns>the transformed BoundingSphere/</returns>
  1201.         private static BoundingSphere TransformBoundingSphere(BoundingSphere sphere, Matrix transform)
  1202.         {
  1203.             BoundingSphere transformedSphere;
  1204.  
  1205.             // the transform can contain different scales on the x, y, and z components.
  1206.             // this has the effect of stretching and squishing our bounding sphere along
  1207.             // different axes. Obviously, this is no good: a bounding sphere has to be a
  1208.             // SPHERE. so, the transformed sphere's radius must be the maximum of the
  1209.             // scaled x, y, and z radii.
  1210.  
  1211.             // to calculate how the transform matrix will affect the x, y, and z
  1212.             // components of the sphere, we'll create a vector3 with x y and z equal
  1213.             // to the sphere's radius...
  1214.             Vector3 scale3 = new Vector3(sphere.Radius, sphere.Radius, sphere.Radius);
  1215.  
  1216.             // then transform that vector using the transform matrix. we use
  1217.             // TransformNormal because we don't want to take translation into account.
  1218.             scale3 = Vector3.TransformNormal(scale3, transform);
  1219.  
  1220.             // scale3 contains the x, y, and z radii of a squished and stretched sphere.
  1221.             // we'll set the finished sphere's radius to the maximum of the x y and z
  1222.             // radii, creating a sphere that is large enough to contain the original
  1223.             // squished sphere.
  1224.             transformedSphere.Radius = Math.Max(scale3.X, Math.Max(scale3.Y, scale3.Z));
  1225.  
  1226.             // transforming the center of the sphere is much easier. we can just use
  1227.             // Vector3.Transform to transform the center vector. notice that we're using
  1228.             // Transform instead of TransformNormal because in this case we DO want to
  1229.             // take translation into account.
  1230.             transformedSphere.Center = Vector3.Transform(sphere.Center, transform);
  1231.  
  1232.             return transformedSphere;
  1233.         }
  1234.  
  1235.  
  1236.         protected virtual void MeshModel()
  1237.         {
  1238.             Matrix[] transforms = new Matrix[model.Bones.Count];
  1239.             model.CopyAbsoluteBoneTransformsTo(transforms);
  1240.  
  1241.             foreach (ModelMesh mesh in model.Meshes)
  1242.             {
  1243.                 Matrix meshTransform = transforms[mesh.ParentBone.Index];
  1244.                 boundingBoxes.Add(BuildBoundingBox(mesh, meshTransform));
  1245.  
  1246.             }
  1247.         }
  1248.  
  1249.         private BoundingBox BuildBoundingBox(ModelMesh mesh, Matrix meshTransform)
  1250.         {
  1251.             // Create initial variables to hold min and max xyz values for the mesh
  1252.             Vector3 meshMax = new Vector3(float.MinValue);
  1253.             Vector3 meshMin = new Vector3(float.MaxValue);
  1254.  
  1255.             foreach (ModelMeshPart part in mesh.MeshParts)
  1256.             {
  1257.                 // The stride is how big, in bytes, one vertex is in the vertex buffer
  1258.                 // We have to use this as we do not know the make up of the vertex
  1259.                 int stride = part.VertexBuffer.VertexDeclaration.VertexStride;
  1260.  
  1261.                 VertexPositionNormalTexture[] vertexData = new VertexPositionNormalTexture[part.NumVertices];
  1262.                 part.VertexBuffer.GetData(part.VertexOffset * stride, vertexData, 0, part.NumVertices, stride);
  1263.  
  1264.                 // Find minimum and maximum xyz values for this mesh part
  1265.                 Vector3 vertPosition = new Vector3();
  1266.  
  1267.                 for (int i = 0; i < vertexData.Length; i++)
  1268.                 {
  1269.                     vertPosition = vertexData[i].Position;
  1270.  
  1271.                     // update our values from this vertex
  1272.                     meshMin = Vector3.Min(meshMin, vertPosition);
  1273.                     meshMax = Vector3.Max(meshMax, vertPosition);
  1274.                 }
  1275.             }
  1276.  
  1277.             // transform by mesh bone matrix
  1278.             meshMin = Vector3.Transform(meshMin, meshTransform);
  1279.             meshMax = Vector3.Transform(meshMax, meshTransform);
  1280.  
  1281.             // Create the bounding box
  1282.             BoundingBox box = new BoundingBox(meshMin, meshMax);
  1283.  
  1284.             return box;
  1285.         }      
  1286.      
  1287.     }
  1288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement