예제 #1
0
        public IngameChatLogic(Widget widget, OrderManager orderManager, World world, ModData modData)
        {
            this.orderManager = orderManager;
            this.modRules = modData.DefaultRules;

            chatTraits = world.WorldActor.TraitsImplementing<INotifyChat>().ToArray();

            var players = world.Players.Where(p => p != world.LocalPlayer && !p.NonCombatant && !p.IsBot);
            disableTeamChat = world.IsReplay || world.LobbyInfo.IsSinglePlayer || (world.LocalPlayer != null && !players.Any(p => p.IsAlliedWith(world.LocalPlayer)));
            teamChat = !disableTeamChat;

            tabCompletion.Commands = chatTraits.OfType<ChatCommands>().SelectMany(x => x.Commands.Keys).ToList();
            tabCompletion.Names = orderManager.LobbyInfo.Clients.Select(c => c.Name).Distinct().ToList();

            var chatPanel = (ContainerWidget)widget;
            chatOverlay = chatPanel.Get<ContainerWidget>("CHAT_OVERLAY");
            chatOverlayDisplay = chatOverlay.Get<ChatDisplayWidget>("CHAT_DISPLAY");
            chatOverlay.Visible = false;

            chatChrome = chatPanel.Get<ContainerWidget>("CHAT_CHROME");
            chatChrome.Visible = true;

            var chatMode = chatChrome.Get<ButtonWidget>("CHAT_MODE");
            chatMode.GetText = () => teamChat ? "Team" : "All";
            chatMode.OnClick = () => teamChat ^= true;
            chatMode.IsDisabled = () => disableTeamChat;

            chatText = chatChrome.Get<TextFieldWidget>("CHAT_TEXTFIELD");
            chatText.OnEnterKey = () =>
            {
                var team = teamChat && !disableTeamChat;
                if (chatText.Text != "")
                {
                    if (!chatText.Text.StartsWith("/"))
                        orderManager.IssueOrder(Order.Chat(team, chatText.Text.Trim()));
                    else if (chatTraits != null)
                    {
                        var text = chatText.Text.Trim();
                        foreach (var trait in chatTraits)
                            trait.OnChat(orderManager.LocalClient.Name, text);
                    }
                }

                chatText.Text = "";
                CloseChat();
                return true;
            };

            chatText.OnTabKey = () =>
            {
                var previousText = chatText.Text;
                chatText.Text = tabCompletion.Complete(chatText.Text);
                chatText.CursorPosition = chatText.Text.Length;

                if (chatText.Text == previousText)
                    return SwitchTeamChat();
                else
                    return true;
            };

            chatText.OnEscKey = () => { CloseChat(); return true; };

            var chatClose = chatChrome.Get<ButtonWidget>("CHAT_CLOSE");
            chatClose.OnClick += CloseChat;

            chatPanel.OnKeyPress = e =>
            {
                if (e.Event == KeyInputEvent.Up)
                    return false;

                if (!chatChrome.IsVisible() && (e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER))
                {
                    OpenChat();
                    return true;
                }

                return false;
            };

            chatScrollPanel = chatChrome.Get<ScrollPanelWidget>("CHAT_SCROLLPANEL");
            chatTemplate = chatScrollPanel.Get<ContainerWidget>("CHAT_TEMPLATE");
            chatScrollPanel.RemoveChildren();
            chatScrollPanel.ScrollToBottom();

            foreach (var chatLine in orderManager.ChatCache)
                AddChatLine(chatLine.Color, chatLine.Name, chatLine.Text, true);

            orderManager.AddChatLine += AddChatLineWrapper;
            Game.BeforeGameStart += UnregisterEvents;

            CloseChat();
            chatText.IsDisabled = () => world.IsReplay;

            var keyListener = chatChrome.Get<LogicKeyListenerWidget>("KEY_LISTENER");
            keyListener.OnKeyPress = e =>
            {
                if (e.Event == KeyInputEvent.Up || !chatText.IsDisabled())
                    return false;

                if ((e.Key == Keycode.RETURN || e.Key == Keycode.KP_ENTER || e.Key == Keycode.ESCAPE) && e.Modifiers == Modifiers.None)
                {
                    CloseChat();
                    return true;
                }

                return false;
            };
        }