예제 #1
0
        private static int getMenuResult(int x, int y, MenuItem menuItem, bool isNewMenu)
        {
            #region Prepare Variables

            menuItem._validate();
            var menuItems		= menuItem.ChildMenuItems;
            var options			= menuItem.Options;
            var menuItemsText	= menuItems.Select(a => a.Caption).ToList();
            var maxItemLength	= menuItemsText.Max(a => a.Length);

            var useArrows		= options.Mode != MenuMode.ShortkeysOnly;
            var useShortkeys	= options.Mode != MenuMode.ArrowsOnly;
            var colorScheme		= options.ColorScheme;

            #endregion

            #region Draw menu if necessary

            if (isNewMenu)
            {
                if (currentMenuPosition == 0)
                {
                    currentMenuPosition = -1;
                    for (var i = 0; i < menuItems.Count; i++)
                    {
                        if (menuItems[i].IsActive)
                        {
                            currentMenuPosition = i;
                            break;
                        }
                    }
                }

                if (currentMenuPosition == -1)
                    return -1;

                drawMenu(x, y, menuItem);
            }

            var linesPerItem = options.ItemSpacing+1;
            var xPos = x + 1 + options.FrameSpacingHorizontal + (options.Mode != MenuMode.ArrowsOnly ? 4 : 0);
            #endregion

            #region	Get action result

            while (true)
            {
                #region Highlight current choice if needed

                var yPos = y + 1 + options.FrameSpacingVertical + currentMenuPosition*linesPerItem;
                if (useArrows)
                {
                    var currentMenuItemText = options.UseSelectedCaps ? menuItemsText[currentMenuPosition].ToUpper() : menuItemsText[currentMenuPosition];
                    ZOutput.Print(
                        xPos, yPos,
                        currentMenuItemText.PadRight(maxItemLength, ' '),
                        options.UseSelectedForeColor ? colorScheme.SelectedForeColor :  colorScheme.TextForeColor,
                        options.UseSelectedBackColor ? colorScheme.SelectedBackColor :  colorScheme.TextBackColor);
                }

                #endregion

                #region Escape key and any additional hotkeys

                var key = Console.ReadKey(true).Key;
                if (key == ConsoleKey.Escape  ||  key == ConsoleKey.Backspace)
                    return -1;

                if (hotKeyValues != null  &&  hotKeyValues.ContainsKey(key))
                {
                    hotKeyValues[key]();
                }

                #endregion

                #region Short Keys logic

                if (useShortkeys  &&  key >= ConsoleKey.D1  &&  key <= (ConsoleKey)(menuItems.Count+48))
                {
                    var val = (int)key - 49;
                    if (menuItems[val].IsActive)
                        return val;
                }

                #endregion

                #region Arrow Control logic

                if (useArrows)
                {
                    var oldPosition = currentMenuPosition;

                    switch (key)
                    {
                        case ConsoleKey.DownArrow:
                            do
                            {
                                currentMenuPosition = (currentMenuPosition < menuItems.Count-1) ? currentMenuPosition + 1 : 0;
                            } while (!menuItems[currentMenuPosition].IsActive);
                            break;

                        case ConsoleKey.UpArrow:
                            do
                            {
                                currentMenuPosition = (currentMenuPosition > 0) ? currentMenuPosition - 1 : menuItems.Count-1;
                            } while (!menuItems[currentMenuPosition].IsActive);
                            break;

                        case ConsoleKey.RightArrow:
                            if (menuItems[currentMenuPosition].HasChilds)
                                return currentMenuPosition;
                            break;

                        case ConsoleKey.LeftArrow:
                            if (menuItem.Parent != null)
                                return -1;
                            break;

                        case ConsoleKey.Enter:
                        case ConsoleKey.Spacebar:
                            return currentMenuPosition;
                    }

                    if (oldPosition != currentMenuPosition)
                    {
                        ZOutput.Print(xPos, yPos, menuItemsText[oldPosition].PadRight(maxItemLength, ' '), colorScheme.TextForeColor, colorScheme.TextBackColor);
                    }
                }

                #endregion
            }

            #endregion
        }