Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Use however you want.
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace GoesHere
- {
- public class UtilClass
- {
- private KeyboardState FormerKeyboard, CurrentKeyboard;
- private MouseState FormerMouse, CurrentMouse;
- public const float TargetCycle = 1.0f / 60.0f;
- public Random RNG;
- GraphicsDevice Graphics;
- ContentManager Content;
- public Texture2D Blank;
- public SpriteFont Font;
- public UtilClass(ContentManager content, GraphicsDevice graphics)
- {
- CurrentKeyboard = Keyboard.GetState();
- CurrentMouse = Mouse.GetState();
- RNG = new Random();
- Graphics = graphics;
- Content = content;
- Blank = new Texture2D(graphics, 1, 1, false, SurfaceFormat.Color);
- Blank.SetData(new[] { Color.White });
- Font = Content.Load<SpriteFont>("SpriteFont1");
- }
- public void Update()
- {
- FormerKeyboard = CurrentKeyboard;
- CurrentKeyboard = Keyboard.GetState();
- FormerMouse = CurrentMouse;
- CurrentMouse = Mouse.GetState();
- }
- #region keyboard
- /// <summary>
- /// Returns true if a key is held down.
- /// </summary>
- public bool KeyDown(Keys key)
- {
- if (CurrentKeyboard.IsKeyDown(key))
- return true;
- else
- return false;
- }
- /// <summary>
- /// Returns true if a key was JUST released.
- /// </summary>
- public bool KeyReleased(Keys key)
- {
- if (CurrentKeyboard.IsKeyUp(key) && FormerKeyboard.IsKeyDown(key))
- return true;
- else
- return false;
- }
- /// <summary>
- /// Returns true if a key was JUST pressed.
- /// </summary>
- public bool KeyPressed(Keys key)
- {
- if (CurrentKeyboard.IsKeyDown(key) && FormerKeyboard.IsKeyUp(key))
- return true;
- else
- return false;
- }
- #endregion
- #region mouse
- public bool LeftDown()
- {
- if (CurrentMouse.LeftButton == ButtonState.Pressed)
- return true;
- else
- return false;
- }
- public bool RightDown()
- {
- if (CurrentMouse.RightButton == ButtonState.Pressed)
- return true;
- else
- return false;
- }
- public bool LeftReleased()
- {
- if (CurrentMouse.LeftButton == ButtonState.Released &&
- FormerMouse.LeftButton == ButtonState.Pressed)
- return true;
- else
- return false;
- }
- public bool RightReleased()
- {
- if (CurrentMouse.RightButton == ButtonState.Released &&
- FormerMouse.RightButton == ButtonState.Pressed)
- return true;
- else
- return false;
- }
- public bool LeftPressed()
- {
- if (CurrentMouse.LeftButton == ButtonState.Pressed &&
- FormerMouse.LeftButton == ButtonState.Released)
- return true;
- else
- return false;
- }
- public bool RightPressed()
- {
- if (CurrentMouse.RightButton == ButtonState.Pressed &&
- FormerMouse.RightButton == ButtonState.Released)
- return true;
- else
- return false;
- }
- public Vector2 MousePos
- {
- get { return new Vector2(CurrentMouse.X, CurrentMouse.Y); }
- }
- #endregion
- #region drawing
- public void DrawQuad(SpriteBatch spriteBatch, Vector2 position, Color color, float rotation,
- Vector2 scale, float depth, bool centered)
- {
- Vector2 origin;
- if (centered)
- origin = new Vector2(0.5f);
- else
- origin = Vector2.Zero;
- spriteBatch.Draw(Blank, position, null, color, rotation, origin, scale, SpriteEffects.None, depth);
- }
- public void CenterLine(SpriteBatch spriteBatch, float width, Color color, Vector2 p1, Vector2 p2, float depth)
- {
- DrawLine(spriteBatch, width / 2, color, p1, p2, depth);
- DrawLine(spriteBatch, width / 2, color, p2, p1, depth);
- }
- public void DrawLine(SpriteBatch spriteBatch, float width, Color color, Vector2 p1, Vector2 p2, float depth)
- {
- float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
- float length = Vector2.Distance(p1, p2);
- spriteBatch.Draw(Blank, p1, null, color,
- angle, Vector2.Zero, new Vector2(length, width),
- SpriteEffects.None, depth);
- }
- public void DrawCircle(SpriteBatch spriteBatch, float r, float cutr, Vector2 position, float depth, Color color)
- {
- float width = r - cutr;
- float segments = (float)Math.Floor(r / MathHelper.Pi * 2.0f);
- for (float i = 0; i < segments; i++)
- {
- float sA = (float)(i / segments) * MathHelper.TwoPi;
- Vector2 start = new Vector2((float)Math.Cos(sA) * r, (float)Math.Sin(sA) * r);
- float eA = (float)((i + 1) / segments) * MathHelper.TwoPi;
- Vector2 end = new Vector2((float)Math.Cos(eA) * r, (float)Math.Sin(eA) * r);
- DrawLine(spriteBatch, width, color, start + position, end + position, depth);
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement