Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Design;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework;
- using System.Runtime.Serialization;
- namespace Redux.GUI.Controls
- {
- public class Control : DrawableGameComponent
- {
- //public vars
- public bool Hovering { get; set; }
- public bool Clicked { get; set; }
- public bool JustReleased { get; set; }
- public Vector2 Location { get; set; }
- public Vector2 Size { get; set; }
- public bool HasFocus { get; set; }
- public virtual Texture2D MainTexture { get; set; }
- public virtual Texture2D HoverTexture { get; set; }
- public virtual Texture2D ClickedTexture { get; set; }
- public object Parent { get; set; }
- public MState CurrentMouseState { get; set; }
- public MState PreviousMouseState { get; set; }
- //private and internal
- internal MouseState mState;
- internal MouseState prevMState;
- DateTime time = DateTime.Now;
- Rectangle cont; //the apps rectangle
- public Control(Game1 game, Vector2 location, Vector2 size, object parent)
- : base(game)
- {
- Location = location;
- Size = size;
- cont = new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y);
- Parent = parent;
- }
- public override void Update(GameTime gameTime)
- {
- mState = Mouse.GetState();
- Hovering = isHovering();
- Clicked = isClicked();
- JustReleased = isReleased();
- CurrentMouseState = new MState(mState, Clicked, Hovering);
- base.Update(gameTime);
- }
- public override void Draw(GameTime gameTime)
- {
- SpriteBatch bat = Game1.Instance.spriteBatch;
- DateTime now = DateTime.Now;
- TimeSpan sinceLastClickedDraw = now - time;
- cont = new Rectangle((int)Location.X, (int)Location.Y, (int)Size.X, (int)Size.Y);
- //the reason we have so many parameters for bat.draw in the texture drawing is so that they are drawn in the back
- if (this is CheckBox || this is RadioButton)
- {
- if (true)
- 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);
- //else
- // 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);
- }
- else
- {
- if (!Hovering && MainTexture != null) //draw main texture
- 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);
- if (Hovering && !Clicked && HoverTexture != null)
- 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);
- else if (Clicked && ClickedTexture != null)
- {
- 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);
- time = DateTime.Now;
- }
- }
- base.Draw(gameTime);
- prevMState = mState;
- PreviousMouseState = CurrentMouseState;
- }
- #region Helper Functions
- bool isHovering()
- {
- if (cont.Intersects(new Rectangle(mState.X, mState.Y, 1, 1)))
- {
- //System.Windows.Forms.MessageBox.Show("lol");
- return true;
- }
- else
- return false;
- }
- bool isClicked()
- {
- if (mState.LeftButton == ButtonState.Pressed && isHovering())
- {
- foreach (DrawableGameComponent comp in Game1.Instance.Controls)
- {
- ((Control)comp).HasFocus = false;
- }
- HasFocus = true;
- //System.Windows.Forms.MessageBox.Show("lol");
- return true;
- }
- else
- return false;
- }
- bool isReleased()
- {
- if (prevMState.LeftButton == ButtonState.Pressed &&
- new Rectangle(prevMState.X, prevMState.Y, 1, 1).Intersects(cont) && //make sure that the prev mouse state was in fact clicked on this
- !isClicked())
- return true;
- else
- return false;
- }
- #endregion
- }
- /// <summary>
- /// this is mainly for other classes checking if the mouse was previously clicked
- /// </summary>
- public class MState
- {
- public MouseState state;
- public bool wasClicked; //on this control
- public bool wasHovering; //on this control
- public MState(MouseState stat, bool clicked, bool hover)
- {
- state = stat;
- wasClicked = clicked;
- wasHovering = hover;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement