Advertisement
7heSama

XNA Input/Drawing class

Jun 9th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.85 KB | None | 0 0
  1. // Use however you want.
  2.  
  3. using System;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8.  
  9. namespace GoesHere
  10. {
  11.     public class UtilClass
  12.     {
  13.         private KeyboardState FormerKeyboard, CurrentKeyboard;
  14.         private MouseState FormerMouse, CurrentMouse;
  15.        
  16.         public const float TargetCycle = 1.0f / 60.0f;
  17.         public Random RNG;
  18.  
  19.         GraphicsDevice Graphics;
  20.         ContentManager Content;
  21.  
  22.         public Texture2D Blank;
  23.         public SpriteFont Font;
  24.  
  25.         public UtilClass(ContentManager content, GraphicsDevice graphics)
  26.         {
  27.             CurrentKeyboard = Keyboard.GetState();
  28.             CurrentMouse = Mouse.GetState();
  29.  
  30.             RNG = new Random();
  31.  
  32.             Graphics = graphics;
  33.             Content = content;
  34.  
  35.             Blank = new Texture2D(graphics, 1, 1, false, SurfaceFormat.Color);
  36.             Blank.SetData(new[] { Color.White });
  37.             Font = Content.Load<SpriteFont>("SpriteFont1");
  38.         }
  39.  
  40.         public void Update()
  41.         {
  42.             FormerKeyboard = CurrentKeyboard;
  43.             CurrentKeyboard = Keyboard.GetState();
  44.  
  45.             FormerMouse = CurrentMouse;
  46.             CurrentMouse = Mouse.GetState();
  47.         }
  48.  
  49.         #region keyboard
  50.         /// <summary>
  51.         /// Returns true if a key is held down.
  52.         /// </summary>
  53.         public bool KeyDown(Keys key)
  54.         {
  55.             if (CurrentKeyboard.IsKeyDown(key))
  56.                 return true;
  57.             else
  58.                 return false;
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Returns true if a key was JUST released.
  63.         /// </summary>
  64.         public bool KeyReleased(Keys key)
  65.         {
  66.             if (CurrentKeyboard.IsKeyUp(key) && FormerKeyboard.IsKeyDown(key))
  67.                 return true;
  68.             else
  69.                 return false;
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Returns true if a key was JUST pressed.
  74.         /// </summary>
  75.         public bool KeyPressed(Keys key)
  76.         {
  77.             if (CurrentKeyboard.IsKeyDown(key) && FormerKeyboard.IsKeyUp(key))
  78.                 return true;
  79.             else
  80.                 return false;
  81.         }
  82.         #endregion
  83.  
  84.         #region mouse
  85.         public bool LeftDown()
  86.         {
  87.             if (CurrentMouse.LeftButton == ButtonState.Pressed)
  88.                 return true;
  89.             else
  90.                 return false;
  91.         }
  92.  
  93.         public bool RightDown()
  94.         {
  95.             if (CurrentMouse.RightButton == ButtonState.Pressed)
  96.                 return true;
  97.             else
  98.                 return false;
  99.         }
  100.  
  101.         public bool LeftReleased()
  102.         {
  103.             if (CurrentMouse.LeftButton == ButtonState.Released &&
  104.                 FormerMouse.LeftButton == ButtonState.Pressed)
  105.                 return true;
  106.             else
  107.                 return false;
  108.         }
  109.  
  110.         public bool RightReleased()
  111.         {
  112.             if (CurrentMouse.RightButton == ButtonState.Released &&
  113.                 FormerMouse.RightButton == ButtonState.Pressed)
  114.                 return true;
  115.             else
  116.                 return false;
  117.         }
  118.  
  119.         public bool LeftPressed()
  120.         {
  121.             if (CurrentMouse.LeftButton == ButtonState.Pressed &&
  122.                 FormerMouse.LeftButton == ButtonState.Released)
  123.                 return true;
  124.             else
  125.                 return false;
  126.         }
  127.  
  128.         public bool RightPressed()
  129.         {
  130.             if (CurrentMouse.RightButton == ButtonState.Pressed &&
  131.                 FormerMouse.RightButton == ButtonState.Released)
  132.                 return true;
  133.             else
  134.                 return false;
  135.         }
  136.  
  137.         public Vector2 MousePos
  138.         {
  139.             get { return new Vector2(CurrentMouse.X, CurrentMouse.Y); }
  140.         }
  141.         #endregion
  142.  
  143.         #region drawing
  144.         public void DrawQuad(SpriteBatch spriteBatch, Vector2 position, Color color, float rotation,
  145.             Vector2 scale, float depth, bool centered)
  146.         {
  147.             Vector2 origin;
  148.             if (centered)
  149.                 origin = new Vector2(0.5f);
  150.             else
  151.                 origin = Vector2.Zero;
  152.             spriteBatch.Draw(Blank, position, null, color, rotation, origin, scale, SpriteEffects.None, depth);
  153.         }
  154.  
  155.         public void CenterLine(SpriteBatch spriteBatch, float width, Color color, Vector2 p1, Vector2 p2, float depth)
  156.         {
  157.             DrawLine(spriteBatch, width / 2, color, p1, p2, depth);
  158.             DrawLine(spriteBatch, width / 2, color, p2, p1, depth);
  159.         }
  160.  
  161.         public void DrawLine(SpriteBatch spriteBatch, float width, Color color, Vector2 p1, Vector2 p2, float depth)
  162.         {
  163.             float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
  164.             float length = Vector2.Distance(p1, p2);
  165.  
  166.             spriteBatch.Draw(Blank, p1, null, color,
  167.                 angle, Vector2.Zero, new Vector2(length, width),
  168.                 SpriteEffects.None, depth);
  169.         }
  170.  
  171.         public void DrawCircle(SpriteBatch spriteBatch, float r, float cutr, Vector2 position, float depth, Color color)
  172.         {
  173.             float width = r - cutr;
  174.             float segments = (float)Math.Floor(r / MathHelper.Pi * 2.0f);
  175.             for (float i = 0; i < segments; i++)
  176.             {
  177.                 float sA = (float)(i / segments) * MathHelper.TwoPi;
  178.                 Vector2 start = new Vector2((float)Math.Cos(sA) * r, (float)Math.Sin(sA) * r);
  179.                 float eA = (float)((i + 1) / segments) * MathHelper.TwoPi;
  180.                 Vector2 end = new Vector2((float)Math.Cos(eA) * r, (float)Math.Sin(eA) * r);
  181.                 DrawLine(spriteBatch, width, color, start + position, end + position, depth);
  182.             }
  183.         }
  184.         #endregion
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement