using UnityEngine; using System.Collections; using System; public class ConfirmScreen : GameScreen { private Rect _rectBackground, _rectSelector, _rectTitle; private Rect[] _rectsOptions; private string _title; private string[] _menuOptions; private int _currentSelection = 0; private int _fontSize; private Action _confirmationFunction; void Start () { // Initialization Type = ScreenType.Confirm; _fontSize = GUIState.GuiSkin.label.fontSize; // Menu options _menuOptions = new string[] { "Yes", "No" }; // Display rects float width = GUIState.Width / 2; float height = _fontSize * (_menuOptions.Length + 2) * 1.5f; float xPos = GUIState.ViewPort.x + (GUIState.ViewPort.width / 2) - (width / 2); float yPos = GUIState.ViewPort.y + (GUIState.ViewPort.height / 2) - (height / 2); _rectBackground = new Rect(xPos, yPos, width, height); _rectTitle = new Rect(_rectBackground.x + _fontSize, _rectBackground.y + (_fontSize), _rectBackground.width - _fontSize, _fontSize * 1.5f); _rectsOptions = new Rect[_menuOptions.Length]; float optionIndent = _fontSize * 3f; for (int i = 0; i < _rectsOptions.Length; i++) { _rectsOptions[i] = new Rect(_rectBackground.x + optionIndent, _rectBackground.y + _fontSize + (_fontSize * 1.5f * (i + 1)), _rectBackground.width - optionIndent, _fontSize * 1.5f); } // Selector UpdateSelectorPosition(); gameObject.SetActive(false); } void OnGUI() { GUI.skin = GUIState.GuiSkin; GUI.Box(_rectBackground, ""); for(int i = 0; i < _menuOptions.Length; i++) { GUI.Label(_rectsOptions[i], _menuOptions[i]); } GUI.Label(_rectTitle, _title); GUI.DrawTexture(_rectSelector, GUIState.Selector); } private void UpdateSelectorPosition() { Rect selected = _rectsOptions[_currentSelection]; _rectSelector = new Rect(selected.x - _fontSize, selected.y + (selected.height / 3) - (_fontSize / 4), _fontSize / 2, _fontSize / 2); } public override void HandleDirection(Direction in_direction) { switch (in_direction) { case Direction.North: if (_currentSelection > 0) { _currentSelection--; } break; case Direction.South: if (_currentSelection < _menuOptions.Length - 1) { _currentSelection++; } break; } UpdateSelectorPosition(); } public override void HandleAButton() { switch (_menuOptions[_currentSelection].ToLower()) { case "yes": _confirmationFunction(); break; case "no": GUIState.PreviousScreen(); break; } } public override void HandleBButton() { GUIState.PreviousScreen(); } public override void HandleStartButton() { // Do nothing } public void Setup(string in_title, Action in_confirmationFunction) { _title = in_title; _confirmationFunction = in_confirmationFunction; } }