public void DoTab0Contents(Rect inRect)
        {
            var iconWidth      = 25f;
            var iconWidthSpase = 30f;

            //StatusTemp = Text.CalcSize("VersionLabel").y.ToString();
            //Widgets.Label(new Rect(inRect.width - 100f, inRect.height - 30f, inRect.width, inRect.height), StatusTemp + " " + lbCannalsLastSelectedIndex.ToString());

            /// -----------------------------------------------------------------------------------------
            /// Список каналов
            ///
            if (SessionClientController.Data.Chats != null)
            {
                if (SessionClientController.Data.ChatNotReadPost > 0)
                {
                    SessionClientController.Data.ChatNotReadPost = 0;
                }

                //Loger.Log("Client " + SessionClientController.Data.Chats.Count);
                if (lbCannalsHeight == 0)
                {
                    var textHeight = new DialogControlBase().TextHeight;
                    lbCannalsHeight = (float)Math.Round((decimal)(inRect.height / 2f / textHeight)) * textHeight;
                }
                Widgets.Label(new Rect(inRect.x, inRect.y + iconWidthSpase + lbCannalsHeight, 100f, 22f), "OCity_Dialog_Players".Translate());

                if (lbCannals == null)
                {
                    //первый запуск
                    lbCannals      = new ListBox <string>();
                    lbCannals.Area = new Rect(inRect.x
                                              , inRect.y + iconWidthSpase
                                              , 100f
                                              , lbCannalsHeight);
                    lbCannals.OnClick      += (index, text) => DataLastChatsTime = DateTime.MinValue; /*StatusTemp = text;*/
                    lbCannals.SelectedIndex = 0;
                }

                if (lbPlayers == null)
                {
                    //первый запуск
                    lbPlayers = new ListBox <ListBoxPlayerItem>();

                    /*
                     * lbPlayers.Area = new Rect(inRect.x
                     *  , inRect.y + iconWidthSpase + lbCannalsHeight + 22f
                     *  , 100f
                     *  , inRect.height - (iconWidthSpase + lbCannalsHeight + 22f));
                     */
                    lbPlayers.OnClick += (index, item) =>
                    {
                        //убираем выделение
                        lbPlayers.SelectedIndex = -1;
                        //вызываем контекстное меню
                        PlayerItemMenu(item);
                    };

                    lbPlayers.Tooltip = (item) => item.Tooltip;
                }
                if (DoTab0LastHeight != inRect.height)
                {
                    DoTab0LastHeight = inRect.height;
                    lbPlayers.Area   = new Rect(inRect.x
                                                , inRect.y + iconWidthSpase + lbCannalsHeight + 22f
                                                , 100f
                                                , inRect.height - (iconWidthSpase + lbCannalsHeight + 22f));
                }

                if (DataLastChatsTime != SessionClientController.Data.ChatsTime ||
                    DataLastChatsTimeUpdateTime < DateTime.UtcNow.AddSeconds(-5))
                {
                    DataLastChatsTimeUpdateTime = DateTime.UtcNow;
                    //пишем в лог
                    var updateLogHash = SessionClientController.Data.Chats.Count * 1000000
                                        + SessionClientController.Data.Chats.Sum(c => c.Posts.Count);
                    if (updateLogHash != UpdateLogHash)
                    {
                        UpdateLogHash = updateLogHash;
                        Loger.Log("Client UpdateChats count="
                                  + SessionClientController.Data.Chats.Count.ToString()
                                  + " posts=" + (updateLogHash % 1000000).ToString()
                                  + " - " + SessionClientController.Data.ChatsTime.ToString(Loger.Culture));
                    }
                    //устанавливаем данные
                    lbCannals.DataSource = SessionClientController.Data.Chats
                                           //.OrderBy(c => (c.OwnerMaker ? "2" : "1") + c.Name) нелья просто отсортировать, т.к. потом находим по индексу
                                           .Select(c => c.Name)
                                           .ToList();
                    if (lbCannalsGoToChat != null)
                    {
                        var lbCannalsGoToChatIndex = lbCannals.DataSource.IndexOf(lbCannalsGoToChat);
                        if (lbCannalsGoToChatIndex >= 0)
                        {
                            lbCannals.SelectedIndex = lbCannalsGoToChatIndex;
                            lbCannalsGoToChat       = null;
                        }
                    }

                    //Заполняем список игроков по группами {
                    lbPlayers.DataSource = new List <ListBoxPlayerItem>();
                    var allreadyLogin = new List <string>();
                    Func <string, string, ListBoxPlayerItem> addPl = (login, text) =>
                    {
                        allreadyLogin.Add(login);
                        var n = new ListBoxPlayerItem()
                        {
                            Login   = login,
                            Text    = text,
                            Tooltip = login
                        };
                        lbPlayers.DataSource.Add(n);
                        return(n);
                    };
                    Action <string> addTit = (text) =>
                    {
                        if (lbPlayers.DataSource.Count > 0)
                        {
                            addPl(null, " ").GroupTitle = true;
                        }
                        addPl(null, " <i>– " + text + " –</i> ").GroupTitle = true;
                    };
                    Func <string, bool> isOnline = (login) => login == SessionClientController.My.Login ||
                                                   SessionClientController.Data.Players[login].Online;
                    Func <bool, string, string> frameOnline = (online, txt) =>
                                                              online
                        ? "<b>" + txt + "</b>"
                        : "<color=#888888ff>" + txt + "</color>";

                    if (lbCannals.SelectedIndex > 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                    {
                        var selectCannal = SessionClientController.Data.Chats[lbCannals.SelectedIndex];

                        // в чате создатель
                        addTit("в чате".NeedTranslate());
                        var n = addPl(selectCannal.OwnerLogin
                                      , frameOnline(isOnline(selectCannal.OwnerLogin), "★ " + selectCannal.OwnerLogin));
                        n.Tooltip += "OCity_Dialog_ChennelOwn".Translate();
                        n.InChat   = true;

                        // в чате
                        var offlinePartyLogin = new List <string>();
                        for (int i = 0; i < selectCannal.PartyLogin.Count; i++)
                        {
                            var lo = selectCannal.PartyLogin[i];
                            if (lo != "system" && lo != selectCannal.OwnerLogin)
                            {
                                if (isOnline(lo))
                                {
                                    n          = addPl(lo, frameOnline(true, lo));
                                    n.Tooltip += "OCity_Dialog_ChennelUser".Translate();
                                    n.InChat   = true;
                                }
                                else
                                {
                                    offlinePartyLogin.Add(lo);
                                }
                            }
                        }

                        // в чате оффлайн
                        //addTit("оффлайн".NeedTranslate());
                        for (int i = 0; i < offlinePartyLogin.Count; i++)
                        {
                            var lo = offlinePartyLogin[i];
                            n          = addPl(lo, frameOnline(false, lo));
                            n.Tooltip += "OCity_Dialog_ChennelUser".Translate();
                            n.InChat   = true;
                        }
                    }

                    var other = SessionClientController.Data.Chats[0].PartyLogin
                                .Where(p => p != "" && p != "system" && !allreadyLogin.Any(al => al == p))
                                .ToList();
                    if (other.Count > 0)
                    {
                        // игроки
                        addTit("игроки".NeedTranslate());
                        var offlinePartyLogin = new List <string>();
                        for (int i = 0; i < other.Count; i++)
                        {
                            var lo = other[i];
                            if (isOnline(lo))
                            {
                                var n = addPl(lo, frameOnline(true, lo));
                                //n.Tooltip += "OCity_Dialog_ChennelUser".Translate();
                            }
                            else
                            {
                                offlinePartyLogin.Add(lo);
                            }
                        }

                        // игроки оффлайн
                        //addTit("оффлайн".NeedTranslate());
                        for (int i = 0; i < offlinePartyLogin.Count; i++)
                        {
                            var lo = offlinePartyLogin[i];
                            var n  = addPl(lo, frameOnline(false, lo));
                            //n.Tooltip += "OCity_Dialog_ChennelUser".Translate();
                        }
                    }
                    DataLastChatsTime          = SessionClientController.Data.ChatsTime;
                    lbCannalsLastSelectedIndex = -1; //сброс для обновления содержимого окна
                }
                // }

                lbCannals.Drow();
                lbPlayers.Drow();

                var iconRect = new Rect(inRect.x, inRect.y, iconWidth, iconWidth);
                TooltipHandler.TipRegion(iconRect, "OCity_Dialog_ChennelCreate".Translate());
                if (Widgets.ButtonImage(iconRect, IconAddTex))
                {
                    CannalAdd();
                }

                if (lbCannals.SelectedIndex > 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                {
                    //Если что-то выделено, и это не общий чат (строка 0)
                    iconRect.x += iconWidthSpase;
                    TooltipHandler.TipRegion(iconRect, "OCity_Dialog_ChennelClose".Translate());
                    if (Widgets.ButtonImage(iconRect, IconDelTex))
                    {
                        CannalDelete();
                    }
                }

                if (lbCannals.SelectedIndex >= 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                {
                    iconRect.x += iconWidthSpase;
                    TooltipHandler.TipRegion(iconRect, "OCity_Dialog_OthersFunctions".Translate());
                    if (Widgets.ButtonImage(iconRect, IconSubMenuTex))
                    {
                        CannalsMenuShow();
                    }
                }

                /// -----------------------------------------------------------------------------------------
                /// Чат
                ///
                if (lbCannalsLastSelectedIndex != lbCannals.SelectedIndex)
                {
                    lbCannalsLastSelectedIndex = lbCannals.SelectedIndex;
                    if (lbCannals.SelectedIndex >= 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                    {
                        var selectCannal     = SessionClientController.Data.Chats[lbCannals.SelectedIndex];
                        var chatLastPostTime = selectCannal.Posts.Max(p => p.Time);
                        if (ChatLastPostTime != chatLastPostTime)
                        {
                            ChatLastPostTime = chatLastPostTime;
                            Func <ChatPost, string> getPost = (cp) => "[" + cp.Time.ToGoodUtcString("dd HH:mm ") + cp.OwnerLogin + "]: " + cp.Message;

                            var totalLength = 0;
                            ChatBox.Text = selectCannal.Posts
                                           .Reverse <ChatPost>()
                                           .Where(i => (totalLength += i.Message.Length) < 5000)
                                           .Aggregate("", (r, i) => getPost(i) + (r == "" ? "" : Environment.NewLine + r));
                            ChatScrollToDown = true;
                        }
                    }
                    else
                    {
                        ChatBox.Text = "";
                    }
                }

                if (lbCannals.SelectedIndex >= 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                {
                    var selectCannal  = SessionClientController.Data.Chats[lbCannals.SelectedIndex];
                    var chatAreaOuter = new Rect(inRect.x + 110f, inRect.y, inRect.width - 110f, inRect.height - 30f);
                    ChatBox.Drow(chatAreaOuter, ChatScrollToDown);
                    ChatScrollToDown = false;

                    var rrect = new Rect(inRect.x + inRect.width - 25f, inRect.y + inRect.height - 25f, 25f, 25f);
                    Text.Font   = GameFont.Medium;
                    Text.Anchor = TextAnchor.MiddleCenter;
                    Widgets.Label(rrect, "▶");
                    Text.Font   = GameFont.Small;
                    Text.Anchor = TextAnchor.MiddleLeft;
                    bool rrcklick = Widgets.ButtonInvisible(rrect);

                    if (ChatInputText != "")
                    {
                        if (Mouse.IsOver(rrect))
                        {
                            Widgets.DrawHighlight(rrect);
                        }

                        var ev = Event.current;
                        if (ev.isKey && ev.type == EventType.keyDown && ev.keyCode == KeyCode.Return ||
                            rrcklick)
                        {
                            SoundDefOf.RadioButtonClicked.PlayOneShotOnCamera();
                            SessionClientController.Command((connect) =>
                            {
                                connect.PostingChat(selectCannal.Id, ChatInputText);
                            });
                            ChatInputText = "";
                        }
                    }

                    GUI.SetNextControlName("StartTextField");
                    ChatInputText = GUI.TextField(new Rect(inRect.x + 110f, inRect.y + inRect.height - 25f, inRect.width - 110f - 30f, 25f)
                                                  , ChatInputText, 10000);

                    if (NeedFockus)
                    {
                        NeedFockus = false;
                        GUI.FocusControl("StartTextField");
                    }
                }
            }
        }
Beispiel #2
0
        public void DoTab0Contents(Rect inRect)
        {
            var iconWidth      = 25f;
            var iconWidthSpase = 30f;

            //StatusTemp = Text.CalcSize("VersionLabel").y.ToString();
            //Widgets.Label(new Rect(inRect.width - 100f, inRect.height - 30f, inRect.width, inRect.height), StatusTemp + " " + lbCannalsLastSelectedIndex.ToString());

            /// -----------------------------------------------------------------------------------------
            /// Список каналов
            ///
            if (SessionClientController.Data.Chats != null)
            {
                //Loger.Log("Client " + SessionClientController.Data.Chats.Count);
                if (lbCannalsHeight == 0)
                {
                    var textHeight = new DialogControlBase().TextHeight;
                    lbCannalsHeight = (float)Math.Round((decimal)(inRect.height / 2f / textHeight)) * textHeight;
                }
                Widgets.Label(new Rect(inRect.x, inRect.y + iconWidthSpase + lbCannalsHeight, 100f, 22f), "OCity_Dialog_Players".Translate());

                if (lbCannals == null)
                {
                    //первый запуск
                    lbCannals      = new ListBox <string>();
                    lbCannals.Area = new Rect(inRect.x
                                              , inRect.y + iconWidthSpase
                                              , 100f
                                              , lbCannalsHeight);
                    //lbCannals.OnClick += (index, text) => StatusTemp = text;
                    lbCannals.SelectedIndex = 0;
                }

                if (lbPlayers == null)
                {
                    //первый запуск
                    lbPlayers      = new ListBox <ListBoxPlayerItem>();
                    lbPlayers.Area = new Rect(inRect.x
                                              , inRect.y + iconWidthSpase + lbCannalsHeight + 22f
                                              , 100f
                                              , inRect.height - (iconWidthSpase + lbCannalsHeight + 22f));
                    lbPlayers.OnClick += (index, item) =>
                    {
                        //убираем выделение
                        lbPlayers.SelectedIndex = -1;
                        //вызываем контекстное меню
                        PlayerItemMenu(item);
                    };

                    lbPlayers.Tooltip = (item) => item.Tooltip;
                }
                if (DataLastChatsTime != SessionClientController.Data.ChatsTime)
                {
                    //пишем в лог
                    var updateLogHash = SessionClientController.Data.Chats.Count * 1000000
                                        + SessionClientController.Data.Chats.Sum(c => c.Posts.Count);
                    if (updateLogHash != UpdateLogHash)
                    {
                        UpdateLogHash = updateLogHash;
                        Loger.Log("Client UpdateChats count="
                                  + SessionClientController.Data.Chats.Count.ToString()
                                  + " posts=" + (updateLogHash % 1000000).ToString()
                                  + " - " + SessionClientController.Data.ChatsTime.ToString(Loger.Culture));
                    }
                    //устанавливаем данные
                    lbCannals.DataSource = SessionClientController.Data.Chats
                                           //.OrderBy(c => (c.OwnerMaker ? "2" : "1") + c.Name) нелья просто отсортировать, т.к. потом находим по индексу
                                           .Select(c => c.Name)
                                           .ToList();
                    if (lbCannalsGoToChat != null)
                    {
                        var lbCannalsGoToChatIndex = lbCannals.DataSource.IndexOf(lbCannalsGoToChat);
                        if (lbCannalsGoToChatIndex >= 0)
                        {
                            lbCannals.SelectedIndex = lbCannalsGoToChatIndex;
                            lbCannalsGoToChat       = null;
                        }
                    }

                    var listPlayersInChat = new List <string>();
                    if (lbCannals.SelectedIndex >= 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                    {
                        var selectCannal = SessionClientController.Data.Chats[lbCannals.SelectedIndex];
                        listPlayersInChat    = selectCannal.PartyLogin;
                        lbPlayers.DataSource = listPlayersInChat
                                               .Where(p => p != "system")
                                               .OrderBy(p => (selectCannal.OwnerLogin == p ? "1" : "2") + p)
                                               .Select(p => new ListBoxPlayerItem()
                        {
                            Login   = p,
                            Text    = (selectCannal.OwnerLogin == p ? "<b>★ " + p + "</b>" : "<b>" + p + "</b>"),
                            Tooltip = p
                                      + (selectCannal.OwnerLogin == p ? "OCity_Dialog_ChennelOwn".Translate() : "OCity_Dialog_ChennelUser".Translate()),
                            InChat = true
                        })
                                               .ToList();
                    }
                    else
                    {
                        lbPlayers.DataSource = new List <ListBoxPlayerItem>();
                    }
                    var mainCannal = SessionClientController.Data.Chats[0];
                    lbPlayers.DataSource.AddRange(mainCannal.PartyLogin
                                                  .Where(p => !listPlayersInChat.Any(sp => sp == p))
                                                  .OrderBy(p => p)
                                                  .Select(p => new ListBoxPlayerItem()
                    {
                        Login   = p,
                        Text    = "<color=#888888ff>" + p + "</color>",
                        Tooltip = p,
                        InChat  = false
                    })
                                                  );

                    DataLastChatsTime          = SessionClientController.Data.ChatsTime;
                    lbCannalsLastSelectedIndex = -1; //сброс для обновления содержимого окна
                }
                lbCannals.Drow();
                lbPlayers.Drow();

                var iconRect = new Rect(inRect.x, inRect.y, iconWidth, iconWidth);
                TooltipHandler.TipRegion(iconRect, "OCity_Dialog_ChennelCreate".Translate());
                if (Widgets.ButtonImage(iconRect, IconAddTex))
                {
                    CannalAdd();
                }

                if (lbCannals.SelectedIndex > 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                {
                    //Если что-то выделено, и это не общий чат (строка 0)
                    iconRect.x += iconWidthSpase;
                    TooltipHandler.TipRegion(iconRect, "OCity_Dialog_ChennelClose".Translate());
                    if (Widgets.ButtonImage(iconRect, IconDelTex))
                    {
                        CannalDelete();
                    }
                }

                if (lbCannals.SelectedIndex >= 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                {
                    iconRect.x += iconWidthSpase;
                    TooltipHandler.TipRegion(iconRect, "OCity_Dialog_OthersFunctions".Translate());
                    if (Widgets.ButtonImage(iconRect, IconSubMenuTex))
                    {
                        CannalsMenuShow();
                    }
                }

                /// -----------------------------------------------------------------------------------------
                /// Чат
                ///
                if (lbCannalsLastSelectedIndex != lbCannals.SelectedIndex)
                {
                    lbCannalsLastSelectedIndex = lbCannals.SelectedIndex;
                    if (lbCannals.SelectedIndex >= 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                    {
                        Func <ChatPost, string> getPost = (cp) => "[" + cp.OwnerLogin + "]: " + cp.Message;

                        var selectCannal = SessionClientController.Data.Chats[lbCannals.SelectedIndex];
                        ChatText = selectCannal.Posts
                                   .Aggregate("", (r, i) => (r == "" ? "" : r + Environment.NewLine) + getPost(i));
                    }
                    else
                    {
                        ChatText = "";
                    }
                }

                if (lbCannals.SelectedIndex >= 0 && SessionClientController.Data.Chats.Count > lbCannals.SelectedIndex)
                {
                    var selectCannal = SessionClientController.Data.Chats[lbCannals.SelectedIndex];

                    //var chatTextSize = Text.CalcSize(ChatText);
                    var chatAreaOuter = new Rect(inRect.x + 110f, inRect.y, inRect.width - 110f, inRect.height - 30f);
                    var chatAreaInner = new Rect(0, 0
                                                 , /*inRect.width - (inRect.x + 110f) */ chatAreaOuter.width - ListBox <string> .WidthScrollLine
                                                 , 0 /*chatTextSize.y*/);
                    chatAreaInner.height = Text.CalcHeight(ChatText, chatAreaInner.width);

                    ChatScrollPosition = GUI.BeginScrollView(chatAreaOuter, ChatScrollPosition, chatAreaInner);
                    GUILayout.BeginArea(chatAreaInner);
                    GUILayout.TextField(ChatText, "Label");
                    GUILayout.EndArea();
                    GUI.EndScrollView();

                    ///

                    if (ChatInputText != "")
                    {
                        var ev = Event.current;
                        if (ev.isKey && ev.type == EventType.keyDown && ev.keyCode == KeyCode.Return)
                        {
                            SessionClientController.Command((connect) =>
                            {
                                connect.PostingChat(selectCannal.Id, ChatInputText);
                            });
                            ChatInputText = "";
                        }
                    }

                    GUI.SetNextControlName("StartTextField");
                    ChatInputText = GUI.TextField(new Rect(inRect.x + 110f, inRect.y + inRect.height - 25f, inRect.width - 110f, 25f)
                                                  , ChatInputText, 10000);

                    if (NeedFockus)
                    {
                        NeedFockus = false;
                        GUI.FocusControl("StartTextField");
                    }
                }
            }
        }