AGENT.Contrib.UI.Menu.Render C# (CSharp) Method

Render() public method

public Render ( ) : void
return void
        public void Render()
        {
            _screen.Clear();

            //border
            _screen.DrawRectangle(Color.White, 1, 0, 0, AgentSize, AgentSize, 0, 0, Color.Black, 0, 0, Color.Black, 0, 0,
                                  255);

            int first = 0;
            int last = Items.Count-1;
            if ((last - first) > _maxVisible)
            {
                first = SelectedIndex - (_maxVisible/2);
                if (first < 0)
                {
                    first = 0;
                }

                last = first +  _maxVisible - 1;
                if (last > Items.Count - 1) last = Items.Count - 1;

            }

            int top = 0;
            int height = MenuFont.Height;

            for (int index = first; index <= last; index++)
            {
                MenuItem item = (Items[index] as MenuItem);
                item.Selected = false;
                if (index == SelectedIndex) item.Selected = true;

                Color backColor = Color.White;
                Color textColor = Color.Black;
                if (item.Selected)
                {
                    backColor = Color.Black;
                    textColor = Color.White;
                }
                _screen.DrawRectangle(backColor, 1, 1, top, Menu.AgentSize - 2, height, 0, 0, backColor, 0, 0, backColor,
                                      0,
                                      0, 255);

                _screen.DrawText(item.Title, MenuFont, textColor, 16, top + 1);

                if (item.Image != null) _screen.DrawImage(1, top, item.Image, 0,0,item.Image.Width, item.Image.Height);
                top += height + 1;
            }
            _screen.Flush();
        }
        

Usage Example

Example #1
0
        public static void Main()
        {
            //make sure we are using all of the right buttons
            ButtonHelper.ButtonSetup = new Buttons[]{ Buttons.TopRight, Buttons.MiddleRight, Buttons.BottomRight };

            //get our menu font
            var font = Resources.GetFont(Resources.FontResources.NinaB);
            //get a sample image for the menu items
            var arrow = new Bitmap(Resources.GetBytes(Resources.BinaryResources.right_arrow), Bitmap.BitmapImageType.Gif);
            //new up our menu
            var menu = new AGENT.Contrib.UI.Menu(font);

            //setup our menu items
            menu.Items.Add(new MenuItem() { Title = "Hello", CommandName = "Hello", CommandArg = "World", Image = arrow });
            menu.Items.Add(new MenuItem() { Title = "World", CommandName = "World", CommandArg = "World" });
            menu.Items.Add(new MenuItem() { Title = "This is not so long", CommandName = "NotLong", CommandArg = "World", Image = arrow });
            menu.Items.Add(new MenuItem() { Title = "This text is very very long", CommandName = "Long", CommandArg = "World" });
            menu.Items.Add(new MenuItem() { Title = "Nice and short", CommandName = "Short", CommandArg = "World", Image = arrow });
            menu.Items.Add(new MenuItem() { Title = "A", CommandName = "A", CommandArg = "A" });
            menu.Items.Add(new MenuItem() { Title = "B", CommandName = "A", CommandArg = "A", Image = arrow });
            menu.Items.Add(new MenuItem() { Title = "C", CommandName = "A", CommandArg = "A" });
            menu.Items.Add(new MenuItem() { Title = "D", CommandName = "A", CommandArg = "A", Image = arrow });
            menu.Items.Add(new MenuItem() { Title = "E", CommandName = "A", CommandArg = "A" });
            menu.Items.Add(new MenuItem() { Title = "F", CommandName = "A", CommandArg = "A", Image = arrow });
            menu.Items.Add(new MenuItem() { Title = "G", CommandName = "A", CommandArg = "A" });
            menu.Items.Add(new MenuItem() { Title = "H", CommandName = "A", CommandArg = "A", Image = arrow });
            menu.Items.Add(new MenuItem() { Title = "I", CommandName = "A", CommandArg = "A" });

            //responid to item clicks
            menu.OnMenuItemClicked += menu_OnMenuItemClicked;

            //render our menu
            menu.Render();

            //done
            System.Threading.Thread.Sleep(Timeout.Infinite);
        }