/* * Button.cs * Authors: * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace StarJack { class Button:Entity { protected bool mMouseOver; public bool MouseOver { get { return mMouseOver; } } protected TextureInfo mMouseOverTexture; public TextureInfo MouseOverTexture { get { return mMouseOverTexture; } set { mMouseOverTexture = value; } } protected bool mActive; public bool Active { get { return mActive; } set { mActive = value;} } protected TextureInfo mActiveTexture; public TextureInfo ActiveTexture { get { return mActiveTexture; } set { mActiveTexture = value; } } protected TextureInfo mInactiveTexture; public TextureInfo InactiveTexture { get { return mInactiveTexture; } set { mInactiveTexture = value; } } public override float Depth { get { return base.Depth; } set { base.Depth = value; if (mIcons != null) { foreach (Entity e in mIcons.Keys) { e.Depth = Depth - 0.001f; } } } } public enum Orientation{ Left, Top, Center, Right, Bottom, Above, Below, ToTheRight, ToTheLeft, TopLeft, TopRight, BottomRight, BottomLeft } protected Dictionary mIcons; /// /// This will add an icon to the button /// /// /// /// /// /// public Entity AddIcon(string name, string group, string textureName,Orientation or, float space) { Entity mIcon = new Entity(name); mIcon.Depth = Depth + 0.01f; mIcon.Group = group; mIcon.TextureName = textureName; mIcon.Height = Height; //* space); mIcon.Width = Width;// * space); SetOrientation(this,mIcon, or); mIcons.Add(mIcon, or); return mIcon; } public TextEntity AddText(string text, string fontName,Color textColor, Color textShadow, Orientation or, float space) { TextEntity mText = new TextEntity(text); mText.Depth = Depth + 0.02f; mText.FontName = fontName; mText.Color = textColor; mText.ShadowColor = textShadow; mText.Height = (int)(Height * space); mText.Width = (int)(Width * space); SetOrientation(this, mText, or); mIcons.Add(mText, or); return mText; } public Button(String name) : base(name) { Depth = Room.Depth.UIButton; InactiveTexture = new TextureInfo(string.Empty); ActiveTexture = new TextureInfo(string.Empty); MouseOverTexture = new TextureInfo(string.Empty); mIcons = new Dictionary(); Active = true; ActiveTexture.Name = "Active"; ActiveTexture.TextureGroup = "Button"; InactiveTexture.Name = "Inactive"; InactiveTexture.TextureGroup = "Button"; MouseOverTexture.Name = "MouseOver"; MouseOverTexture.TextureGroup = "Button"; } public virtual void Initialize() { } /// /// Oreients the icon in relation to the button /// /// /// public static void SetOrientation(Entity orienter, Entity orientee, Orientation oritent) { switch (oritent) { case Orientation.Above: orientee.X = (orienter.Width - orientee.Width) / 2 + orienter.X; orientee.Y = orienter.Y - orientee.Height; break; case Orientation.Below: orientee.X = (orienter.Width - orientee.Width) / 2 + orienter.X; orientee.Y = orienter.Y + orienter.Height; break; case Orientation.Bottom: orientee.X = (orienter.Width - orientee.Width) / 2 + orienter.X; orientee.Y = orienter.Y + orienter.Height - orientee.Height; break; case Orientation.BottomLeft: orientee.X = orienter.X; orientee.Y = orienter.Y - orientee.Height; break; case Orientation.BottomRight: orientee.X = orienter.X + orienter.Width - orientee.Width; orientee.Y = orienter.Y + orienter.Height - orientee.Height; break; case Orientation.Center: orientee.X = (orienter.Width - orientee.Width) / 2 + orienter.X; orientee.Y = (orienter.Height - orientee.Height) / 2 + orienter.Y; break; case Orientation.Left: orientee.X = orienter.X; orientee.Y = (orienter.Height - orientee.Height) / 2 + orienter.Y; break; case Orientation.Right: orientee.X = orienter.X + orienter.Width - orientee.Width; orientee.Y = (orienter.Height - orientee.Height) / 2 + orienter.Y; break; case Orientation.Top: orientee.X = (orienter.Width - orientee.Width) / 2 + orienter.X; orientee.Y = orienter.Y; break; case Orientation.TopLeft: orientee.X = orienter.X; orientee.Y = orienter.Y; break; case Orientation.TopRight: orientee.X = orienter.X + orienter.Width - orientee.Width; orientee.Y = orienter.Y; break; case Orientation.ToTheLeft: orientee.X = orienter.X - orientee.Width; orientee.Y = (orienter.Height - orientee.Height) / 2 + orienter.Y; break; case Orientation.ToTheRight: orientee.X = orienter.X + orienter.Width; orientee.Y = (orienter.Height - orientee.Height) / 2 + orienter.Y; break; } } /// /// This will check If the mouse was over the button /// /// public bool CheckMouse(Point p) { if (p.X >= X && p.Y >= Y && p.X <= (X + Width) && p.Y <= (Y + Height)) { return true; } return false; } public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content) { mMouseOverTexture.LoadContent(content); mActiveTexture.LoadContent(content); mInactiveTexture.LoadContent(content); foreach (Entity e in mIcons.Keys) { e.LoadContent(content); } base.LoadContent(content); } public virtual void Update(GameTime time, Point mouse, bool clicked) { bool isMouseOver = CheckMouse(mouse); if (isMouseOver != mMouseOver) { mMouseOver = isMouseOver; } if (Active) { if (MouseOver) { base.Texture = MouseOverTexture; if (mMouseOver && clicked) { DoAction(); } } else { Texture = ActiveTexture; } } else { Texture = InactiveTexture; } base.Update(time); } public virtual void DoAction() { Console.WriteLine("Action"); } public override void Draw(SpriteBatch pSpritebatch) { base.Draw(pSpritebatch); if (mIcons.Count >0 ) { foreach (Entity e in mIcons.Keys) { e.Draw(pSpritebatch); } } } } }