private void RefreshBroadcastChatHistory()
        {
            m_chatHistory.Clear();
            var history = MyChatSystem.GetChatHistory(MySession.Static.LocalPlayerId);

            var chat = history.GlobalChatHistory.Chat;

            foreach (var text in chat)
            {
                var identity = MySession.Static.Players.TryGetIdentity(text.IdentityId);

                if (identity == null)
                {
                    continue;
                }
                bool isPlayer = identity.IdentityId == MySession.Static.LocalPlayerId;

                m_chatHistory.AppendText(identity.DisplayName, isPlayer ? MyFontEnum.DarkBlue : MyFontEnum.Blue, m_chatHistory.TextScale, Vector4.One);

                m_chatHistory.AppendText(": ", isPlayer ? MyFontEnum.DarkBlue : MyFontEnum.Blue, m_chatHistory.TextScale, Vector4.One);
                m_chatHistory.AppendText(text.Text, MyFontEnum.White, m_chatHistory.TextScale, Vector4.One);
                m_chatHistory.AppendLine();
            }

            m_factionList.SelectedItems.Clear();
            m_chatHistory.ScrollbarOffset = 1.0f;
        }
        private void RefreshPlayerChatHistory(MyIdentity playerIdentity)
        {
            m_chatHistory.Clear();
            var history = MyChatSystem.GetChatHistory(MySession.Static.LocalPlayerId);

            var playerId = playerIdentity.IdentityId;
            MyPlayerChatHistory playerChat;

            if (history.PlayerChatHistory.TryGetValue(playerId, out playerChat))
            {
                var chat = playerChat.Chat;
                foreach (var text in chat)
                {
                    var identity = MySession.Static.Players.TryGetIdentity(text.IdentityId);

                    if (identity == null)
                    {
                        continue;
                    }
                    bool isPlayer = identity.IdentityId == MySession.Static.LocalPlayerId;

                    m_chatHistory.AppendText(identity.DisplayName, isPlayer ? MyFontEnum.DarkBlue : MyFontEnum.Blue, m_chatHistory.TextScale, Vector4.One);
                    if (!text.Sent)
                    {
                        m_tempStringBuilder.Clear();
                        m_tempStringBuilder.Append(" (");
                        m_tempStringBuilder.Append(MyTexts.GetString(MySpaceTexts.TerminalTab_Chat_Pending));
                        m_tempStringBuilder.Append(")");
                        m_chatHistory.AppendText(m_tempStringBuilder, MyFontEnum.Red, m_chatHistory.TextScale, Vector4.One);
                    }
                    m_chatHistory.AppendText(": ", isPlayer ? MyFontEnum.DarkBlue : MyFontEnum.Blue, m_chatHistory.TextScale, Vector4.One);
                    m_chatHistory.AppendText(text.Text, MyFontEnum.White, m_chatHistory.TextScale, Vector4.One);
                    m_chatHistory.AppendLine();
                }
            }

            m_factionList.SelectedItems.Clear();
            m_chatHistory.ScrollbarOffset = 1.0f;
        }
        private void SendMessage()
        {
            //Cannot send any message if local character is missing
            if (MySession.Static.LocalCharacter == null)
            {
                return;
            }

            m_chatboxText.Clear();
            m_chatbox.GetText(m_chatboxText);

            MyDebug.AssertDebug(m_chatboxText.Length > 0, "Length of chat text should be positive");
            MyDebug.AssertDebug(m_chatboxText.Length <= MyChatConstants.MAX_CHAT_STRING_LENGTH, "Length of chat text should not exceed maximum allowed");

            var history = MyChatSystem.GetChatHistory(MySession.Static.LocalPlayerId);

            if (m_playerList.SelectedItems.Count > 0)
            {
                var selectedItem = m_playerList.SelectedItems[0];

                if (selectedItem == m_globalItem)
                {
                    //messages entered in the global chat history should be treated as normal ingame chat
                    if (MyMultiplayer.Static != null)
                    {
                        MyMultiplayer.Static.SendChatMessage(m_chatboxText.ToString());
                    }
                    else
                    {
                        MyHud.Chat.ShowMessage(MySession.Static.LocalHumanPlayer == null ? "Player" : MySession.Static.LocalHumanPlayer.DisplayName, m_chatboxText.ToString());
                    }

                    //add the message to history
                    //MySession.Static.GlobalChatHistory.GlobalChatHistory.Chat.Enqueue(new MyGlobalChatItem
                    //{
                    //    IdentityId = MySession.Static.LocalPlayerId,
                    //    Text = m_chatboxText.ToString()
                    //});

                    RefreshGlobalChatHistory();
                }
                else if (selectedItem == m_broadcastItem)
                {
                    MySession.Static.LocalCharacter.SendNewGlobalMessage(MySession.Static.LocalHumanPlayer.Id, m_chatboxText.ToString());
                }
                else
                {
                    var playerIdentity = (MyIdentity)selectedItem.UserData;

                    MySession.Static.ChatHistory[MySession.Static.LocalPlayerId].AddPlayerChatItem(new MyPlayerChatItem(m_chatboxText.ToString(), MySession.Static.LocalPlayerId, MySession.Static.ElapsedGameTime, false), playerIdentity.IdentityId);
                    RefreshPlayerChatHistory(playerIdentity);
                }
            }
            else if (m_factionList.SelectedItems.Count > 0)
            {
                var toSendTo      = new Dictionary <long, bool>();
                var selectedItem  = m_factionList.SelectedItems[0];
                var targetFaction = (MyFaction)selectedItem.UserData;

                foreach (var member in targetFaction.Members)
                {
                    toSendTo.Add(member.Value.PlayerId, false);
                }

                if (!targetFaction.IsMember(MySession.Static.LocalPlayerId))
                {
                    var localFaction = MySession.Static.Factions.TryGetPlayerFaction(MySession.Static.LocalPlayerId);
                    if (localFaction != null)
                    {
                        foreach (var member in localFaction.Members)
                        {
                            toSendTo.Add(member.Value.PlayerId, false);
                        }
                    }
                }

                var factionChatItem = new MyFactionChatItem(m_chatboxText.ToString(), MySession.Static.LocalPlayerId, MySession.Static.ElapsedGameTime, toSendTo);

                //This has to exist!
                var currentFaction = MySession.Static.Factions.TryGetPlayerFaction(MySession.Static.LocalPlayerId);

                MySession.Static.LocalCharacter.SendNewFactionMessage(targetFaction.FactionId, currentFaction.FactionId, factionChatItem);

                RefreshFactionChatHistory(targetFaction);
            }

            m_chatbox.SetText(m_emptyText);
        }