| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #region File Description
- //-----------------------------------------------------------------------------
- // Button.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- using System;
- using GameStateManagement;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace GameStateManagementSample
- {
- /// <summary>
- /// A special button that handles toggling between "On" and "Off"
- /// </summary>
- class BooleanButton : Button
- {
- private string option;
- private bool value;
- /// <summary>
- /// Creates a new BooleanButton.
- /// </summary>
- /// <param name="option">The string text to display for the option.</param>
- /// <param name="value">The initial value of the button.</param>
- public BooleanButton(string option, bool value)
- : base(option)
- {
- this.option = option;
- this.value = value;
- GenerateText();
- }
- protected override void OnTapped()
- {
- // When tapped we need to toggle the value and regenerate the text
- value = !value;
- GenerateText();
- base.OnTapped();
- }
- /// <summary>
- /// Helper that generates the actual Text value the base class uses for drawing.
- /// </summary>
- private void GenerateText()
- {
- Text = string.Format("{0}: {1}", option, value ? "On" : "Off");
- }
- }
- /// <summary>
- /// Represents a touchable button.
- /// </summary>
- class Button
- {
- /// <summary>
- /// The text displayed in the button.
- /// </summary>
- public string Text = "Button";
- /// <summary>
- /// The position of the top-left corner of the button.
- /// </summary>
- public Vector2 Position = Vector2.Zero;
- /// <summary>
- /// The size of the button.
- /// </summary>
- public Vector2 Size = new Vector2(250, 75);
- /// <summary>
- /// The thickness of the border drawn for the button.
- /// </summary>
- public int BorderThickness = 4;
- /// <summary>
- /// The color of the button border.
- /// </summary>
- public Color BorderColor = new Color(200, 200, 200);
- /// <summary>
- /// The color of the button background.
- /// </summary>
- public Color FillColor = new Color(100, 100, 100) * .75f;
- /// <summary>
- /// The color of the text.
- /// </summary>
- public Color TextColor = Color.White;
- /// <summary>
- /// The opacity of the button.
- /// </summary>
- public float Alpha = 0f;
- /// <summary>
- /// Invoked when the button is tapped.
- /// </summary>
- public event EventHandler<EventArgs> Tapped;
-
- /// <summary>
- /// Creates a new Button.
- /// </summary>
- /// <param name="text">The text to display in the button.</param>
- public Button(string text)
- {
- Text = text;
- }
- /// <summary>
- /// Invokes the Tapped event and allows subclasses to perform actions when tapped.
- /// </summary>
- protected virtual void OnTapped()
- {
- if (Tapped != null)
- Tapped(this, EventArgs.Empty);
- }
- /// <summary>
- /// Passes a tap location to the button for handling.
- /// </summary>
- /// <param name="tap">The location of the tap.</param>
- /// <returns>True if the button was tapped, false otherwise.</returns>
- public bool HandleTap(Vector2 tap)
- {
- if (tap.X >= Position.X &&
- tap.Y >= Position.Y &&
- tap.X <= Position.X + Size.X &&
- tap.Y <= Position.Y + Size.Y)
- {
- OnTapped();
- return true;
- }
- return false;
- }
- /// <summary>
- /// Draws the button
- /// </summary>
- /// <param name="screen">The screen drawing the button</param>
- public void Draw(GameScreen screen)
- {
- // Grab some common items from the ScreenManager
- SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;
- SpriteFont font = screen.ScreenManager.Font;
- Texture2D blank = screen.ScreenManager.BlankTexture;
- // Compute the button's rectangle
- Rectangle r = new Rectangle(
- (int)Position.X,
- (int)Position.Y,
- (int)Size.X,
- (int)Size.Y);
- // Fill the button
- spriteBatch.Draw(blank, r, FillColor * Alpha);
- // Draw the border
- spriteBatch.Draw(
- blank,
- new Rectangle(r.Left, r.Top, r.Width, BorderThickness),
- BorderColor * Alpha);
- spriteBatch.Draw(
- blank,
- new Rectangle(r.Left, r.Top, BorderThickness, r.Height),
- BorderColor * Alpha);
- spriteBatch.Draw(
- blank,
- new Rectangle(r.Right - BorderThickness, r.Top, BorderThickness, r.Height),
- BorderColor * Alpha);
- spriteBatch.Draw(
- blank,
- new Rectangle(r.Left, r.Bottom - BorderThickness, r.Width, BorderThickness),
- BorderColor * Alpha);
- // Draw the text centered in the button
- Vector2 textSize = font.MeasureString(Text);
- Vector2 textPosition = new Vector2(r.Center.X, r.Center.Y) - textSize / 2f;
- textPosition.X = (int)textPosition.X;
- textPosition.Y = (int)textPosition.Y;
- spriteBatch.DrawString(font, Text, textPosition, TextColor * Alpha);
- }
- }
- }
|