Advertisement
Guest User

Control Base Class XNA

a guest
Oct 22nd, 2011
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Input;
  6. using Microsoft.Xna.Framework.Design;
  7. using Microsoft.Xna.Framework.Graphics;
  8. using Microsoft.Xna.Framework;
  9. using System.Runtime.Serialization;
  10.  
  11. namespace Redux.GUI.Controls
  12. {
  13.     public class Control : DrawableGameComponent
  14.     {
  15.         //public vars
  16.         public bool Hovering { get; set; }
  17.         public bool Clicked { get; set; }
  18.         public bool JustReleased { get; set; }
  19.         public Vector2 Location { get; set; }
  20.         public Vector2 Size { get; set; }
  21.         public bool HasFocus { get; set; }
  22.         public virtual Texture2D MainTexture { get; set; }
  23.         public virtual Texture2D HoverTexture { get; set; }
  24.         public virtual Texture2D ClickedTexture { get; set; }
  25.         public object Parent { get; set; }
  26.         public MState CurrentMouseState { get; set; }
  27.         public MState PreviousMouseState { get; set; }
  28.  
  29.         //private and internal
  30.         internal MouseState mState;
  31.         internal MouseState prevMState;
  32.         DateTime time = DateTime.Now;
  33.         Rectangle cont; //the apps rectangle
  34.  
  35.         public Control(Game1 game, Vector2 location, Vector2 size, object parent)
  36.             : base(game)
  37.         {
  38.             Location = location;
  39.             Size = size;
  40.             cont = new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y);
  41.             Parent = parent;
  42.         }
  43.  
  44.         public override void Update(GameTime gameTime)
  45.         {
  46.             mState = Mouse.GetState();
  47.  
  48.             Hovering = isHovering();
  49.             Clicked = isClicked();
  50.             JustReleased = isReleased();
  51.             CurrentMouseState = new MState(mState, Clicked, Hovering);
  52.  
  53.             base.Update(gameTime);
  54.         }
  55.  
  56.         public override void Draw(GameTime gameTime)
  57.         {
  58.             SpriteBatch bat = Game1.Instance.spriteBatch;
  59.             DateTime now = DateTime.Now;
  60.             TimeSpan sinceLastClickedDraw = now - time;
  61.             cont = new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y);
  62.  
  63.             //the reason we have so many parameters for bat.draw in the texture drawing is so that they are drawn in the back
  64.             if (this is CheckBox || this is RadioButton)
  65.             {
  66.                 if (true)
  67.                     bat.Draw(((BoolControl)this).DrawTexture, new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1);
  68.                 //else
  69.                   //  bat.Draw(HoverTexture, new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1);
  70.             }
  71.             else
  72.             {
  73.                 if (!Hovering && MainTexture != null) //draw main texture
  74.                     bat.Draw(MainTexture, new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1);
  75.                 if (Hovering && !Clicked && HoverTexture != null)
  76.                     bat.Draw(HoverTexture, new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1);
  77.                 else if (Clicked && ClickedTexture != null)
  78.                 {
  79.                     bat.Draw(ClickedTexture, new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1);
  80.                     time = DateTime.Now;
  81.                 }
  82.             }
  83.  
  84.             base.Draw(gameTime);
  85.             prevMState = mState;
  86.             PreviousMouseState = CurrentMouseState;
  87.         }
  88.  
  89.         #region Helper Functions
  90.  
  91.         bool isHovering()
  92.         {
  93.             if (cont.Intersects(new Rectangle(mState.X, mState.Y, 1, 1)))
  94.             {
  95.                 //System.Windows.Forms.MessageBox.Show("lol");
  96.                 return true;
  97.             }
  98.             else
  99.                 return false;
  100.         }
  101.  
  102.         bool isClicked()
  103.         {
  104.             if (mState.LeftButton == ButtonState.Pressed && isHovering())
  105.             {
  106.                 foreach (DrawableGameComponent comp in Game1.Instance.Controls)
  107.                 {
  108.                     ((Control)comp).HasFocus = false;
  109.                 }
  110.                 HasFocus = true;
  111.                 //System.Windows.Forms.MessageBox.Show("lol");
  112.                 return true;
  113.             }
  114.             else
  115.                 return false;
  116.         }
  117.  
  118.         bool isReleased()
  119.         {
  120.             if (prevMState.LeftButton == ButtonState.Pressed &&
  121.                 new Rectangle(prevMState.X, prevMState.Y, 1, 1).Intersects(cont) && //make sure that the prev mouse state was in fact clicked on this
  122.                 !isClicked())
  123.                 return true;
  124.             else
  125.                 return false;
  126.         }
  127.  
  128.         #endregion
  129.     }
  130.  
  131.     /// <summary>
  132.     /// this is mainly for other classes checking if the mouse was previously clicked
  133.     /// </summary>
  134.     public class MState
  135.     {
  136.         public MouseState state;
  137.         public bool wasClicked; //on this control
  138.         public bool wasHovering; //on this control
  139.  
  140.         public MState(MouseState stat, bool clicked, bool hover)
  141.         {
  142.             state = stat;
  143.             wasClicked = clicked;
  144.             wasHovering = hover;
  145.         }
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement