private void contextMenu_ItemClicked(MyGuiControlContextMenu sender, MyGuiControlContextMenu.EventArgs args)
        {
            int idx = args.ItemIndex;
            var toolbar = MyToolbarComponent.CurrentToolbar;
            Debug.Assert(toolbar != null);
            if (toolbar == null) return;

            int slot = toolbar.IndexToSlot(m_contextMenuItemIndex);

            if (toolbar.IsValidSlot(slot))
            {
                var item = toolbar.GetSlotItem(slot);
                Debug.Assert(item is MyToolbarItemActions);
                var actionsItem = item as MyToolbarItemActions;
                if (item != null)
                {
                    //"Remove from toolbar" index (usually the last one)
                    if (idx < 0 || idx >= actionsItem.PossibleActions(ShownToolbar.ToolbarType).Count)
                        RemoveToolbarItem(slot);
                    else
                    {
                        //First updates the action
                        actionsItem.ActionId = (string)args.UserData;

                        //Removes all items equal to the one you're adding
                        for (int i = 0; i < MyToolbarComponent.CurrentToolbar.SlotCount; ++i)
                        {
                            var cur = toolbar.GetSlotItem(i);
                            if (cur != null && cur.Equals(actionsItem))
                                MyToolbarComponent.CurrentToolbar.SetItemAtSlot(i, null);
                        }

                        //And then put it to the slot
                        MyToolbarComponent.CurrentToolbar.SetItemAtSlot(slot, actionsItem);
                    }
                }
            }

            m_contextMenuItemIndex = -1;
        }
        private void RecreateControls(bool contructor)
        {
            m_toolbarItemsGrid = new MyGuiControlGrid()
            {
                OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM,
                VisualStyle = MyGuiControlGridStyleEnum.Toolbar,
                ColumnsCount = MyToolbarComponent.CurrentToolbar.SlotCount + 1,
                RowsCount = 1
            };
            m_toolbarItemsGrid.ItemDoubleClicked += grid_ItemDoubleClicked;
            m_toolbarItemsGrid.ItemClickedWithoutDoubleClick += grid_ItemClicked;

            m_selectedItemLabel = new MyGuiControlLabel();
            m_colorVariantPanel = new MyGuiControlPanel(size: new Vector2(0.1f, 0.025f));
            m_colorVariantPanel.BackgroundTexture = MyGuiConstants.TEXTURE_GUI_BLANK;

            m_contextMenu = new MyGuiControlContextMenu();
            m_contextMenu.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM;
            m_contextMenu.Deactivate();
            m_contextMenu.ItemClicked += contextMenu_ItemClicked;

            Elements.Add(m_colorVariantPanel);
            Elements.Add(m_selectedItemLabel);
            Elements.Add(m_toolbarItemsGrid);
            Elements.Add(m_contextMenu);

            m_colorVariantPanel.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_BOTTOM;
            m_selectedItemLabel.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_BOTTOM;
            m_toolbarItemsGrid.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_BOTTOM;
            m_contextMenu.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM;

            RefreshInternals();
        }
        void OnContextMenu_ItemClicked(MyGuiControlContextMenu sender, MyGuiControlContextMenu.EventArgs eventArgs)
        {
            var actionItem = (ContextMenuFavoriteActionItem)eventArgs.UserData;
            var server = actionItem.Server;
            if (server != null)
            {
                switch (actionItem.Action)
                {
                    case ContextMenuFavoriteAction.Add:
                        UInt32 unixTimestamp = (UInt32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                        SteamAPI.Instance.AddFavoriteGame(server.AppID, System.Net.IPAddressExtensions.ToIPv4NetworkOrder(server.NetAdr.Address), (UInt16)server.NetAdr.Port, (UInt16)server.NetAdr.Port, FavoriteEnum.Favorite, unixTimestamp);
                        break;
                    case ContextMenuFavoriteAction.Remove:
                        SteamAPI.Instance.RemoveFavoriteGame(server.AppID, System.Net.IPAddressExtensions.ToIPv4NetworkOrder(server.NetAdr.Address), (UInt16)server.NetAdr.Port, (UInt16)server.NetAdr.Port, FavoriteEnum.Favorite);

                        m_gamesTable.RemoveSelectedRow();
                        m_favoritesPage.Text = new StringBuilder().Append(MyTexts.Get(MySpaceTexts.JoinGame_TabTitle_Favorites).ToString()).Append(" (").Append(m_gamesTable.RowsCount).Append(")");
                        break;
                    default:
                        throw new InvalidBranchException();
                        break;
                }
            }
        }
        void joinGameTabs_OnPageChanged()
        {
            var serversPage = (MyGuiControlTabPage)m_joinGameTabs.Controls.GetControlByName("PageServersPanel");
            var lobbiesPage = (MyGuiControlTabPage)m_joinGameTabs.Controls.GetControlByName("PageLobbiesPanel");
            var favoritesPage = (MyGuiControlTabPage)m_joinGameTabs.Controls.GetControlByName("PageFavoritesPanel");
            var historyPage = (MyGuiControlTabPage)m_joinGameTabs.Controls.GetControlByName("PageHistoryPanel");
            var LANPage = (MyGuiControlTabPage)m_joinGameTabs.Controls.GetControlByName("PageLANPanel");
            var friendsPage = (MyGuiControlTabPage)m_joinGameTabs.Controls.GetControlByName("PageFriendsPanel");

            if (m_selectedPage == serversPage)
            {
                CloseServersPage();
            }
            else if (m_selectedPage == lobbiesPage)
            {
                CloseLobbyPage();
            }
            else if (m_selectedPage == favoritesPage)
            {
                CloseFavoritesPage();
            }
            else if (m_selectedPage == LANPage)
            {
                CloseLANPage();
            }
            else if (m_selectedPage == historyPage)
            {
                CloseHistoryPage();
            }
            else if (m_selectedPage == friendsPage)
            {
                CloseFriendsPage();
            }

            m_selectedPage = m_joinGameTabs.GetTabSubControl(m_joinGameTabs.SelectedPage);

            InitPageControls(m_selectedPage);
            if (m_selectedPage == serversPage)
            {
                InitServersPage();
                m_showOnlyFriends.Enabled = false;
                m_showOnlyFriendsText.Enabled = false;
            }
            else if (m_selectedPage == lobbiesPage)
            {
                InitLobbyPage();
                m_showOnlyFriends.Enabled = true;
                m_showOnlyFriendsText.Enabled = true;
            }
            else if (m_selectedPage == favoritesPage)
            {
                InitFavoritesPage();
                m_showOnlyFriends.Enabled = false;
                m_showOnlyFriendsText.Enabled = false;
            }
            else if (m_selectedPage == historyPage)
            {
                InitHistoryPage();
                m_showOnlyFriends.Enabled = false;
                m_showOnlyFriendsText.Enabled = false;
            }
            else if (m_selectedPage == LANPage)
            {
                InitLANPage();
                m_showOnlyFriends.Enabled = false;
                m_showOnlyFriendsText.Enabled = false;
            }
            else if (m_selectedPage == friendsPage)
            {
                InitFriendsPage();
                m_showOnlyFriends.Enabled = false;
                m_showOnlyFriendsText.Enabled = false;
            }

            if(m_contextMenu != null)
            {
                m_contextMenu.Deactivate();
                m_contextMenu = null;
            }

            m_contextMenu = new MyGuiControlContextMenu();
            m_contextMenu.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_BOTTOM;
            m_contextMenu.Deactivate();
            m_contextMenu.ItemClicked += OnContextMenu_ItemClicked;
            Controls.Add(m_contextMenu);
        }
        public override void RecreateControls(bool constructor)
        {
            //This is probably very wrong!
            m_screenScale = (MyGuiManager.GetHudSize().X / MyGuiManager.GetHudSize().Y) / MyGuiConstants.SAFE_ASPECT_RATIO;

            m_size = new Vector2(m_screenScale, 0.5f);

            base.RecreateControls(constructor);
            Vector4 consoleTextColor = new Vector4(1, 1, 0, 1);
            float consoleTextScale = 1f;
            
            m_commandLine = new MyGuiControlTextbox
            (
                position: new Vector2(0, 0.25f),
                textColor: consoleTextColor
            );

            m_commandLine.Position -= new Vector2(0, m_commandLine.Size.Y + m_margin.Y/2);
            m_commandLine.Size = new Vector2(m_screenScale, m_commandLine.Size.Y) - 2 * m_margin;
            m_commandLine.ColorMask = new Vector4(0, 0, 0, 0.5f);
            m_commandLine.VisualStyle = MyGuiControlTextboxStyleEnum.Debug;
            m_commandLine.TextChanged += commandLine_TextChanged;
            m_commandLine.Name = "CommandLine";


            m_autoComplete = new MyGuiControlContextMenu();
            m_autoComplete.ItemClicked += autoComplete_ItemClicked;
            m_autoComplete.Deactivate();
            m_autoComplete.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP;
            m_autoComplete.ColorMask = new Vector4(0, 0, 0, .5f);
            m_autoComplete.AllowKeyboardNavigation = true;
            m_autoComplete.Name = "AutoComplete";

            m_displayScreen = new MyGuiControlMultilineText
            (
                position: new Vector2(-0.5f * m_screenScale, -0.25f) + m_margin,
                size: new Vector2(m_screenScale, 0.5f - m_commandLine.Size.Y) - 2 * m_margin,
                font: MyFontEnum.Debug,
                textAlign: MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP,
                textBoxAlign: MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP,
                selectable: true
            );

            m_displayScreen.TextColor = Color.Yellow;
            m_displayScreen.TextScale = consoleTextScale;
            m_displayScreen.OriginAlign = MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_TOP;
            m_displayScreen.Text = MyConsole.DisplayScreen;
            m_displayScreen.ColorMask = new Vector4(0, 0, 0, .5f);
            m_displayScreen.Name = "DisplayScreen";

            Controls.Add(m_displayScreen);
            Controls.Add(m_commandLine);
            Controls.Add(m_autoComplete);
        }
 public void autoComplete_ItemClicked(MyGuiControlContextMenu sender, MyGuiControlContextMenu.EventArgs args)
 {
     m_commandLine.Text += (string) m_autoComplete.Items[args.ItemIndex].UserData;
     m_commandLine.MoveCarriageToEnd();
     FocusedControl = m_commandLine;
 }