Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.69 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using TheMarket.Share;
  6.  
  7. namespace Entities
  8. {
  9.     internal class FirmaGraph : IEntity
  10.     {
  11.         private BasicEffect basicEffect;
  12.  
  13.         private Vector3 ct = Vector3.Zero;
  14.         private Vector3 ctpos = Vector3.Zero;
  15.         private Vector3 ctposWorld = Vector3.Zero;
  16.         private Vector3 ctrotWorld = Vector3.Zero;
  17.  
  18.         private int movmode;
  19.         private Matrix projection; //Matrix.CreateOrthographic(720f, 1280f, -2000f,2000f);
  20.         private TimeSpan ts = TimeSpan.Zero;
  21.         private string tx = "";
  22.         private VertexBuffer vertexBuffer;
  23.         private Matrix view;
  24.  
  25.         private VertexPositionColor[] vp =
  26.         {
  27.             new VertexPositionColor(new Vector3(0, 0, 0), Color.Black),
  28.             new VertexPositionColor(new Vector3(10, 10, 0), Color.Black),
  29.             new VertexPositionColor(new Vector3(10, 0, 0), Color.Black),
  30.             new VertexPositionColor(new Vector3(0, 10, 0), Color.Black),
  31.             new VertexPositionColor(new Vector3(10, 10, 0), Color.Black),
  32.             new VertexPositionColor(new Vector3(0, 0, 10), Color.Black)
  33.         };
  34.  
  35.         private Matrix world = Matrix.CreateTranslation(0, 0, 0);
  36.  
  37.         public Vector2 Draw(GameTime gameTime, SpriteBatch spriteBatch)
  38.         {
  39.             var cameraPosition = new Vector3(350, 350, 350);
  40.             var cameraLookAtVector = new Vector3(350, 350, 0);
  41.             var cameraUpVector = Vector3.UnitZ;
  42.  
  43.             basicEffect.View = Matrix.CreateLookAt(
  44.                 cameraPosition, cameraLookAtVector, cameraUpVector);
  45.             //+ Matrix.CreateTranslation(ctpos) + Matrix.CreateRotationX((float)MathHelper.Degree2Radian(ct.X)) + Matrix.CreateRotationY((float)MathHelper.Degree2Radian(ct.Y)) + Matrix.CreateRotationZ((float)MathHelper.Degree2Radian(ct.Z));
  46.  
  47.  
  48.             basicEffect.Projection = Matrix.CreateOrthographicOffCenter(new Rectangle(0, 0, 700, 700), 0.1f, 1000);
  49.             basicEffect.VertexColorEnabled = true;
  50.  
  51.             /*
  52.              * old
  53.             basicEffect.World = world + Matrix.CreateTranslation(ctposWorld) + Matrix.CreateRotationX((float)MathHelper.Degree2Radian(ctrotWorld.X)) + Matrix.CreateRotationY((float)MathHelper.Degree2Radian(ctrotWorld.Y)) + Matrix.CreateRotationZ((float)MathHelper.Degree2Radian(ctrotWorld.Z));
  54.             basicEffect.View = view +Matrix.CreateTranslation(ctpos)+ Matrix.CreateRotationX((float)MathHelper.Degree2Radian(ct.X)) + Matrix.CreateRotationY((float)MathHelper.Degree2Radian(ct.Y)) + Matrix.CreateRotationZ((float)MathHelper.Degree2Radian(ct.Z));
  55.             basicEffect.Projection = projection;
  56.             basicEffect.VertexColorEnabled = true;*/
  57.  
  58.             spriteBatch.Begin(); //Show information of positions on screen
  59.             InDrawFunctions.Set(spriteBatch, new Vector2(250, 675), tx, 0);
  60.             InDrawFunctions.Set(spriteBatch, new Vector2(0, 600), ct.ToString(), 0);
  61.             InDrawFunctions.Set(spriteBatch, new Vector2(0, 640), ctpos.ToString(), 0);
  62.             InDrawFunctions.Set(spriteBatch, new Vector2(350, 600), ctrotWorld.ToString(), 0);
  63.             InDrawFunctions.Set(spriteBatch, new Vector2(350, 640), ctposWorld.ToString(), 0);
  64.             spriteBatch.End();
  65.  
  66.             Manager.Game.GraphicsDevice.SetVertexBuffer(vertexBuffer);
  67.  
  68.             var rasterizerState = new RasterizerState();
  69.             rasterizerState.CullMode = CullMode.None;
  70.             Manager.Game.GraphicsDevice.RasterizerState = rasterizerState;
  71.  
  72.             foreach (var pass in basicEffect.CurrentTechnique.Passes)
  73.             {
  74.                 pass.Apply();
  75.                 Manager.Game.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 7);
  76.             }
  77.             return Vector2.One;
  78.         }
  79.  
  80.         public void Initialize()
  81.         {
  82.             var center = new Vector2(350, 350);
  83.             /*center.X = Manager.Game.GraphicsDevice.Viewport.Width * 0.5f;
  84.             center.Y = Manager.Game.GraphicsDevice.Viewport.Height * 0.5f;*/
  85.         }
  86.  
  87.         public void LoadContent()
  88.         {
  89.             basicEffect = new BasicEffect(Manager.Game.GraphicsDevice);
  90.  
  91.             var vertices = new VertexPositionColor[14];
  92.             vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0f), Color.Red);
  93.             vertices[1] = new VertexPositionColor(new Vector3(100, 0, 0), Color.Black); // not visible
  94.             vertices[2] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Blue);
  95.             vertices[3] = new VertexPositionColor(new Vector3(0, 0, 100), Color.Black);
  96.             vertices[4] = new VertexPositionColor(new Vector3(0, 0, 0), Color.Green);
  97.             vertices[5] = new VertexPositionColor(new Vector3(0, 100, 0), Color.Black);
  98.  
  99.             vertices[6] = new VertexPositionColor(new Vector3(0, 0, 0), Color.White);
  100.             vertices[7] = new VertexPositionColor(new Vector3(100, 100, 0), Color.Black);
  101.             vertices[8] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Chocolate);
  102.             vertices[9] = new VertexPositionColor(new Vector3(700, 701, 0), Color.Black);
  103.             vertices[10] = new VertexPositionColor(new Vector3(0, 100, 0), Color.Yellow);
  104.             vertices[11] = new VertexPositionColor(new Vector3(0, 100, 100), Color.Black);
  105.             vertices[12] = new VertexPositionColor(new Vector3(0, 0, 100), Color.Yellow);
  106.             vertices[13] = new VertexPositionColor(new Vector3(0, 100, 100), Color.Black);
  107.  
  108.             vertexBuffer = new VertexBuffer(Manager.Game.GraphicsDevice, typeof(VertexPositionColor), 14,
  109.                 BufferUsage.WriteOnly);
  110.             vertexBuffer.SetData(vertices);
  111.         }
  112.  
  113.         public void Unload()
  114.         {
  115.         }
  116.  
  117.         public void Update(GameTime gameTime)
  118.         {
  119.             var ks = Keyboard.GetState();
  120.             var repeat = 1;
  121.             ts += gameTime.ElapsedGameTime;
  122.             if (ts > TimeSpan.FromMilliseconds(ks.IsKeyDown(Keys.LeftShift) ? 50 : 500))
  123.             {
  124.                 if (ks.IsKeyDown(Keys.LeftControl)) repeat = 10;
  125.                 for (var i = 0; i < repeat; i++)
  126.                     switch (movmode)
  127.                     {
  128.                         case 0:
  129.                             if (ks.IsKeyDown(Keys.Up)) ct.Y++;
  130.                             if (ks.IsKeyDown(Keys.Down)) ct.Y--;
  131.                             if (ks.IsKeyDown(Keys.Left)) ct.X--;
  132.                             if (ks.IsKeyDown(Keys.Right)) ct.X++;
  133.                             if (ks.IsKeyDown(Keys.RightShift)) ct.Z++;
  134.                             if (ks.IsKeyDown(Keys.RightControl)) ct.Z--;
  135.                             tx = "Rotate Camera";
  136.                             break;
  137.                         case 1:
  138.                             if (ks.IsKeyDown(Keys.Up)) ctpos.Y++;
  139.                             if (ks.IsKeyDown(Keys.Down)) ctpos.Y--;
  140.                             if (ks.IsKeyDown(Keys.Left)) ctpos.X--;
  141.                             if (ks.IsKeyDown(Keys.Right)) ctpos.X++;
  142.                             if (ks.IsKeyDown(Keys.RightShift)) ctpos.Z++;
  143.                             if (ks.IsKeyDown(Keys.RightControl)) ctpos.Z--;
  144.                             tx = "Move Camera";
  145.                             break;
  146.                         case 2:
  147.                             if (ks.IsKeyDown(Keys.Up)) ctposWorld.Y++;
  148.                             if (ks.IsKeyDown(Keys.Down)) ctposWorld.Y--;
  149.                             if (ks.IsKeyDown(Keys.Left)) ctposWorld.X--;
  150.                             if (ks.IsKeyDown(Keys.Right)) ctposWorld.X++;
  151.                             if (ks.IsKeyDown(Keys.RightShift)) ctposWorld.Z++;
  152.                             if (ks.IsKeyDown(Keys.RightControl)) ctposWorld.Z--;
  153.                             tx = "Move World";
  154.                             break;
  155.                         case 3:
  156.                             if (ks.IsKeyDown(Keys.Up)) ctrotWorld.Y++;
  157.                             if (ks.IsKeyDown(Keys.Down)) ctrotWorld.Y--;
  158.                             if (ks.IsKeyDown(Keys.Left)) ctrotWorld.X--;
  159.                             if (ks.IsKeyDown(Keys.Right)) ctrotWorld.X++;
  160.                             if (ks.IsKeyDown(Keys.RightShift)) ctrotWorld.Z++;
  161.                             if (ks.IsKeyDown(Keys.RightControl)) ctrotWorld.Z--;
  162.                             tx = "Rotate World";
  163.                             break;
  164.                         default:
  165.                             tx = "Unknown";
  166.                             break;
  167.                     }
  168.                 if (ks.IsKeyDown(Keys.PageUp)) movmode++;
  169.                 else if (ks.IsKeyDown(Keys.PageDown)) movmode--;
  170.                 ts = TimeSpan.Zero;
  171.             }
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement