/* * Entity.cs * Authors: August Zinsser * * Copyright Matthew Belmonte 2007 */ using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Graphics; namespace tAC_Engine { /// /// An entity that responds to click events /// class Button : Entity { protected bool mLeftClickedLastCycle = false; // Remember when this button is clicked on protected bool mRightClickedLastCycle = false; // '' protected bool mLeftClickedThisCycle = false; // '' protected bool mRightClickedThisCycle = false; // '' public bool IsLeftClicked { get { return mLeftClickedThisCycle; } } public bool IsRightClicked { get { return mRightClickedThisCycle; } } public bool WasLeftClicked { get { return mLeftClickedLastCycle; } } public bool WasRightClicked { get { return mRightClickedLastCycle; } } /// /// Creates a new button with the dimensions of the off texture /// /// The standard idle texture of the button /// The texture to display on mouseover public Button(Texture2D offTexture, Texture2D onTexture) : base(offTexture) { mSprite.AddClip("On", onTexture, 1, 0f); } /// /// Creates a new button with the dimensions of the texture /// /// The standard idle texture of the button public Button(Texture2D offTexture) : base(offTexture) { mSprite.AddClip("On", offTexture, 1, 0f); } /// /// Check if the mouse was clicked /// public override void Update() { base.Update(); MouseState mouse = Mouse.GetState(); mLeftClickedLastCycle = mLeftClickedThisCycle; mRightClickedLastCycle = mRightClickedThisCycle; mLeftClickedThisCycle = false; mRightClickedThisCycle = false; // Check to see if user has clicked this button if (this.IsMouseOver()) { mSprite.Play("On"); if (mouse.LeftButton == ButtonState.Pressed) { mLeftClickedThisCycle = true; OnLeftClick(); } if (mouse.RightButton == ButtonState.Pressed) { mRightClickedThisCycle = true; OnRightClick(); } } else { mSprite.Play("Default"); } } /// /// Specify code to be executed when a mouse click is detected /// public virtual void OnLeftClick() { } /// /// Specify code to be executed when a mouse click is detected /// public virtual void OnRightClick() { } } }