#region File Information //----------------------------------------------------------------------------- // Button.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Input.Touch; #endregion namespace DynamicMenu.Controls { /// /// A control which reads touch input within its bounds and fires a tapped event when touched /// public class Button : TextControl { #region Fields //Keep a button pressed for this long in seconds before executing the event private const double PRESS_TIME_SECONDS = .2; private double pressStartTime = 0; #endregion #region Events /// /// Indicates that the button was tapped /// public event EventHandler Tapped; #endregion #region Properties /// /// The name of the texture that is shown when this button is pressed /// [ContentSerializer(Optional = true)] public string PressedTextureName { get; set; } /// /// The texture that is shown when this button is pressed /// [ContentSerializerIgnore] public Texture2D PressedTexture { get; set; } /// /// Whether the button is pressed /// [ContentSerializerIgnore] public bool Pressed { get; set; } #endregion #region Initialization /// /// Loads this control's content /// public override void LoadContent(GraphicsDevice _graphics, ContentManager _content) { base.LoadContent(_graphics, _content); if (!string.IsNullOrEmpty(PressedTextureName)) { PressedTexture = _content.Load(PressedTextureName); } } #endregion #region Update /// /// Updates this control /// public override void Update(GameTime gameTime, List gestures) { base.Update(gameTime, gestures); foreach (GestureSample sample in gestures) { if (sample.GestureType != GestureType.Tap) continue; if (ContainsPos(sample.Position)) { Pressed = true; pressStartTime = gameTime.TotalGameTime.TotalSeconds; HandlePressed(); break; } } if (Pressed && (gameTime.TotalGameTime.TotalSeconds > pressStartTime + PRESS_TIME_SECONDS)) { Pressed = false; if (Tapped != null) { Tapped(this, new EventArgs()); } } } #endregion #region Draw /// /// Draws this control /// public override void Draw(GameTime gameTime, SpriteBatch spriteBatch) { base.Draw(gameTime, spriteBatch); DrawCenteredText(spriteBatch, Font, GetAbsoluteRect(), Text, TextColor); } #endregion #region Methods /// /// Gets the current texture based on whether the button is pressed /// public override Texture2D GetCurrTexture() { if (Pressed && PressedTexture != null) { return PressedTexture; } return base.GetCurrTexture(); } protected virtual void HandlePressed() { // Can be overriden to do special processing when this is clicked } #endregion } }