Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="title">Title of the menu</param>
        /// <param name="menuItemList">List of Menu Items</param>
        /// <param name="x">X Coordinate of the Menu</param>
        /// <param name="y">Y Coordinate of the Menu</param>
        /// <param name="rowMax">Maximum amount of Menu Items Displayed</param>
        /// <param name="colorConfig"></param>
        /// <returns>Name of the chosen Menu Item</returns>
        public static string Show(string title, string[] menuItemList, int x, int y, int rowMax, ColorConfiguration colorConfig)
        {
            List <Error> errorList = ValidateParameters(title, menuItemList, x, y, rowMax, colorConfig);

            int menuMaxLength = menuItemList.Aggregate((max, cur) => max.Length > cur.Length ? max : cur).Length;

            if (rowMax > menuItemList.Length)
            {
                rowMax = menuItemList.Length - 1;
            }
            else
            {
                rowMax -= 1;
            }

            int currentSelection = 0;

            ConsoleKey key;

            Console.CursorVisible = false;
            Console.Clear();

            DisplayTitle(x, y, title, colorConfig.title_fgColor, colorConfig.title_bgColor);

            int menuY = y + 2;
            int menuX = x;

            for (int i = 0; i <= rowMax; i++)
            {
                DisplayMenuItem(menuX, menuY + i, menuItemList[i], colorConfig.menu_fgColor, colorConfig.menu_bgColor, menuMaxLength);
            }
            DisplayMenuItem(menuX, menuY, menuItemList[0], colorConfig.selected_fgColor, colorConfig.selected_bgColor, menuMaxLength);

            do
            {
                key = Console.ReadKey(true).Key;

                switch (key)
                {
                case ConsoleKey.UpArrow:
                    if (currentSelection != 0)
                    {
                        currentSelection--;
                    }

                    break;

                case ConsoleKey.DownArrow:
                    if (currentSelection < menuItemList.Length - 1)
                    {
                        currentSelection += 1;
                    }
                    break;
                }

                if (currentSelection < rowMax)
                {
                    for (int i = 0; i <= rowMax; i++)
                    {
                        if (i == currentSelection)
                        {
                            DisplayMenuItem(menuX, menuY + i, menuItemList[i], colorConfig.selected_fgColor, colorConfig.selected_bgColor, menuMaxLength);
                        }
                        else
                        {
                            DisplayMenuItem(menuX, menuY + i, menuItemList[i], colorConfig.menu_fgColor, colorConfig.menu_bgColor, menuMaxLength);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < rowMax; i++)
                    {
                        DisplayMenuItem(menuX, menuY + i, menuItemList[currentSelection - rowMax + i], colorConfig.menu_fgColor, colorConfig.menu_bgColor, menuMaxLength);
                    }
                    DisplayMenuItem(menuX, menuY + rowMax, menuItemList[currentSelection], colorConfig.selected_fgColor, colorConfig.selected_bgColor, menuMaxLength);
                }
            } while (key != ConsoleKey.Enter);

            Console.CursorVisible = true;

            return(menuItemList[currentSelection]);
        }
Пример #2
0
        private static List <Error> ValidateParameters(string title, string[] menuItemList, int x, int y, int rowMax, ColorConfiguration colorConfig)
        {
            List <Error> errorList = new List <Error>();

            if (x >= Console.WindowWidth || x < 0)
            {
                errorList.Add(new Error()
                {
                    Code = 1, Title = "Out of Range Error", Description = "Parameter x is out of visible range!"
                });
            }
            if (y >= Console.WindowHeight || y < 0)
            {
                errorList.Add(new Error()
                {
                    Code = 1, Title = "Out of Range Error", Description = "Parameter y is out of visible range!"
                });
            }
            if (menuItemList.Length <= 1)
            {
                errorList.Add(new Error()
                {
                    Code = 2, Title = "Not enough Menu Items", Description = "You need to provide more than 1 Menu Item!"
                });
            }

            if (rowMax < 2 || rowMax >= Console.WindowHeight)
            {
                errorList.Add(new Error()
                {
                    Code = 1, Title = "Out of Range Error", Description = "Parameter rowMax is out of range!"
                });
            }


            return(errorList);
        }