//--------------------------------------------------------------------------------------------------------------------------------------------------- // // Copyright (C)2007 DarkWynter Studios. All rights reserved. // //--------------------------------------------------------------------------------------------------------------------------------------------------- // {License Information: Creative Commons} //--------------------------------------------------------------------------------------------------------------------------------------------------- namespace Presentation { #region Using Statements using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using RobLoach; #endregion // Generic Menu Enumeration public enum MenuElementType { BUTTON, OPTION, VALUE, LABEL, NONE }; // Wrapper class for Game Menu Elements public class GameMenuElement { protected string title = "Untitled"; protected MenuElementType type; protected bool visible = true; public GameMenuElement() { type = MenuElementType.NONE; } public MenuElementType GetElementType() { return type; } public void SetTitle(string title) { this.title = title; } public string GetTitle() { return title; } public void Hide() { visible = false; } public void Show() { visible = true; } public bool isVisible() { return visible; } } // 2D Label public class GameMenuLabel : GameMenuElement { public GameMenuLabel() { type = MenuElementType.LABEL; } public GameMenuLabel(string title) { type = MenuElementType.LABEL; this.title = title; } } // 2D Button public class GameMenuButton : GameMenuElement { public GameMenuButton() { type = MenuElementType.BUTTON; } public GameMenuButton(string title) { this.title = title; type = MenuElementType.BUTTON; } } // 2D Renderable public class GameMenuValueInput : GameMenuElement { private int value = 0; private int maxValue = 0; private int minValue = 0; private int valueIncrement = 1; public GameMenuValueInput() { type = MenuElementType.VALUE; } public GameMenuValueInput(string title, int minValue, int maxValue, int defaultValue, int increment) { this.title = title; this.minValue = minValue; this.maxValue = maxValue; this.value = defaultValue; this.valueIncrement = increment; type = MenuElementType.VALUE; } public int GetValue() { return value; } public void Increment() { value += valueIncrement; if (value > maxValue) { value = maxValue; } } public void Decrement() { value -= valueIncrement; if (value < minValue) { value = minValue; } } } // 2D Value Scroller public class GameMenuOption : GameMenuElement { private List options = new List(); private int activeOptionIndex = 0; public GameMenuOption() { type = MenuElementType.OPTION; } public GameMenuOption(string title) { this.title = title; type = MenuElementType.OPTION; } public void AddOption(string option) { options.Add(option); } public void PreviousOption() { activeOptionIndex--; if (activeOptionIndex < 0) { activeOptionIndex = 0; } } public void NextOption() { activeOptionIndex++; if (activeOptionIndex >= options.Count) { activeOptionIndex = options.Count - 1; } } public string GetActiveOption() { return options[activeOptionIndex]; } public int GetActiveIndex() { return activeOptionIndex; } } // Constructors, Gets, and Sets public class GameMenu { private string title = "Untitled Menu"; private List menuElements = new List(); private int activeElementIndex = 0; private Vector2 position = new Vector2(); public GameMenu(float x, float y) { position.X = x; position.Y = y; } public GameMenu(string title, float x, float y) { this.title = title; position.X = x; position.Y = y; } public void SetTitle(string title) { this.title = title; } public void SetPosition(float x, float y) { position.X = x; position.Y = y; } public void AddMenuButton(string title) { menuElements.Add(new GameMenuButton(title)); } public void AddMenuOption(string title, params string[] options) { GameMenuOption element = new GameMenuOption(title); foreach (string s in options) { element.AddOption(s); } menuElements.Add(element); } public void AddValueInput(string title, int minValue, int maxValue, int defaultValue, int increment) { GameMenuValueInput valueInput = new GameMenuValueInput(title, minValue, maxValue, defaultValue, increment); menuElements.Add(valueInput); } public void AddLabel(string title) { GameMenuLabel label = new GameMenuLabel(title); menuElements.Add(label); } public void PreviousMenuElement() { activeElementIndex--; if (activeElementIndex < 0) { activeElementIndex = 0; } if (GetActiveMenuElement().isVisible() == false) { PreviousMenuElement(); } if (GetActiveMenuElement().GetElementType() == MenuElementType.LABEL) { if (activeElementIndex == 0) { NextMenuElement(); } else { PreviousMenuElement(); } } } public void NextMenuElement() { activeElementIndex++; if (activeElementIndex >= menuElements.Count) { activeElementIndex = menuElements.Count - 1; } if (GetActiveMenuElement().isVisible() == false) { NextMenuElement(); } if (GetActiveMenuElement().GetElementType() == MenuElementType.LABEL) { if (activeElementIndex == menuElements.Count - 1) { PreviousMenuElement(); } else { NextMenuElement(); } } } public string GetTitle() { return title; } public GameMenuElement GetActiveMenuElement() { return menuElements[activeElementIndex]; } public int GetActiveIndex() { return activeElementIndex; } public GameMenuElement GetMenuElement(int index) { return menuElements[index]; } public GameMenuElement GetMenuElement(string title) { foreach (GameMenuElement element in menuElements) { if (element.GetTitle() == title) { return element; } } return new GameMenuElement(); } public GameMenuElement GetMenuElement(string title, int fromIndex) { for (int i = fromIndex; i < menuElements.Count; i++) { if (menuElements[i].GetTitle() == title) { return menuElements[i]; } } return new GameMenuElement(); } public void Draw(SpriteBatch spriteBatch) { float yPosition = position.Y; int titleLength = 0; TextWriter.Ogilvie24.Draw(position.X, position.Y, title, Alignment.Left, Color.White); //Extra padding below the big font yPosition += 10; for (int i = 0; i < menuElements.Count; i++) { if (menuElements[i].isVisible() == true) { yPosition += 20; if (i == activeElementIndex) { TextWriter.MeadBold16.Draw(position.X, yPosition, menuElements[i].GetTitle(), Alignment.Left, Color.Red); } else { TextWriter.MeadBold16.Draw(position.X, yPosition, menuElements[i].GetTitle(), Alignment.Left, Color.White); } titleLength = TextWriter.MeadBold16.TextWidth(menuElements[i].GetTitle()) + 10; if (menuElements[i].GetElementType() == MenuElementType.OPTION) { GameMenuOption option = (GameMenuOption)menuElements[i]; TextWriter.MeadBold16.Draw(position.X + titleLength, yPosition, option.GetActiveOption(), Alignment.Left, Color.White); } else if (menuElements[i].GetElementType() == MenuElementType.VALUE) { GameMenuValueInput valueInput = (GameMenuValueInput)menuElements[i]; TextWriter.MeadBold16.Draw(position.X + titleLength, yPosition, "" + valueInput.GetValue(), Alignment.Left, Color.White); } } } } } }