/* * Util.BindingsScreen.cs * Authors: Adam Nabinger * 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. */ // TODO: Util.Mouse Bindings, Key Released Bindings, Cleanup using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; namespace Util { /// /// A Test of a screen to change key binding, partially functional. /// public sealed class BindingsScreen : GameScreen { #region Fields // The input handler to modify private readonly InputHandler OtherInputHandler; // The event code type to bind with private readonly Type EventCodeType; // List of menu items in the screen private readonly List m_MenuItems = new List(); // The index of the currently selected menu item private int m_ItemSelected; // The sprite batch to render textures with private SpriteBatch m_SpriteBatch; // The sprite font to render text with private SpriteFont m_SpriteFont; // A blank texture private Texture2D blankTexture; // The dictionary mappings for all events to keys private Dictionary Mapping = new Dictionary(); // The current input handler of the screen private InputHandler inputHandler; // The event code value associated with the new saved key private int valfor; // Whether or not the binding screen is looking for a new key private bool gettingNewKey; #endregion #region Creation /// /// Create a new Binding edit screen, with the bindings from the given input handler, and the names from the given type. /// /// The input handler to bind with /// The type of event code to bind with public BindingsScreen(InputHandler inputBindings, Type eventCodeType) { OtherInputHandler = inputBindings; EventCodeType = eventCodeType; } #endregion #region Initialization /// /// Initialize our own input, and load bindings from the given inputhandler. /// public override void Initialize() { Content.RootDirectory = mGame.Content.RootDirectory; inputHandler = GetInputHandler(); inputHandler.Initialize(); inputHandler.BindButton(Keys.Down, Trigger.Activated, 12001); inputHandler.BindButton(Keys.Up, Trigger.Activated, 12002); inputHandler.BindButton(Keys.Enter, Trigger.Activated, 12003); inputHandler.BindButton(Keys.Escape, Trigger.Activated, 12004); Mapping = OtherInputHandler.GetBindings(); foreach (int i in Mapping.Keys) { m_MenuItems.Add(Enum.ToObject(EventCodeType, i).ToString()); } m_MenuItems.Add("Save"); m_MenuItems.Add("Cancel"); base.Initialize(); } #endregion #region Management /// /// Loads in all necessary information for all content dependent objects /// protected override void LoadContent() { m_SpriteBatch = new SpriteBatch(GraphicsDevice); m_SpriteFont = Content.Load(@"Fonts\menufont"); blankTexture = Content.Load("blank"); base.LoadContent(); } /// /// Disposes of any unmanaged objects /// /// Whether or not this is currently being disposed protected override void Dispose(bool disposing) { if (disposing) { m_SpriteBatch.Dispose(); } base.Dispose(disposing); } #endregion #region Update /// /// Updates the binding screen /// /// Time that has passed in game public override void Update(GameTime gameTime) { base.Update(gameTime); while (inputHandler.HasEvent()) { KeyInputEvent e = inputHandler.GetNextEvent() as KeyInputEvent; if (e == null) { continue; } if (gettingNewKey) { if (e.Trigger == Trigger.Activated && !Mapping.ContainsValue(e.Key)) { Mapping[valfor] = e.Key; gettingNewKey = false; } } else { switch (e.EventCode) { case 12001: if (++m_ItemSelected >= m_MenuItems.Count) { m_ItemSelected = 0; } break; case 12002: if (--m_ItemSelected < 0) { m_ItemSelected += m_MenuItems.Count; } break; case 12003: if (m_ItemSelected < Mapping.Count) { inputHandler.ReportAllEvents = true; valfor = (int)Enum.Parse(EventCodeType, m_MenuItems[m_ItemSelected]); gettingNewKey = true; } else switch (m_MenuItems[m_ItemSelected]) { case "Save": foreach (int i in Mapping.Keys) { OtherInputHandler.BindButton(Mapping[i], Trigger.Activated, i); } Done(); break; case "Cancel": Done(); break; } break; case 12004: Done(); break; } } } } #endregion #region Render /// /// Renders the binding screen /// public override void Draw(GameTime gameTime) { base.Draw(gameTime); Vector2 location = new Vector2(60, 0); m_SpriteBatch.Begin(); m_SpriteBatch.Draw(blankTexture, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Color(0, 0, 0, 200)); m_SpriteBatch.DrawString(m_SpriteFont, "Keyboard Remapping Screen (Alpha)", location, Color.Green); for (int i = 0; i < m_MenuItems.Count; i++) { location.Y += 30; if (i == m_ItemSelected) { m_SpriteBatch.DrawString(m_SpriteFont, m_MenuItems[i], location, Color.Yellow); if (Enum.IsDefined(EventCodeType, m_MenuItems[i])) { m_SpriteBatch.DrawString(m_SpriteFont, Mapping[(int)Enum.Parse(EventCodeType, m_MenuItems[i])].ToString(), location + new Vector2(400, 0), Color.Yellow); } } else { m_SpriteBatch.DrawString(m_SpriteFont, m_MenuItems[i], location, Color.White); if (Enum.IsDefined(EventCodeType, m_MenuItems[i])) { m_SpriteBatch.DrawString(m_SpriteFont, Mapping[(int)Enum.Parse(EventCodeType, m_MenuItems[i])].ToString(), location + new Vector2(400, 0), Color.White); } } } if (gettingNewKey) { m_SpriteBatch.Draw(blankTexture, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), new Color(0, 0, 0, 200)); m_SpriteBatch.DrawString(m_SpriteFont, "Press a new key", new Vector2(GraphicsDevice.Viewport.Width * 0.4f, GraphicsDevice.Viewport.Height * 0.4f), Color.White); } m_SpriteBatch.End(); } #endregion } }