ChatGui.OnGUI C# (CSharp) Method

OnGUI() public method

public OnGUI ( ) : void
return void
    public void OnGUI()
    {
        if (!this.IsVisible)
        {
            return;
        }

        GUI.skin.label.wordWrap = true;
        //GUI.skin.button.richText = true;      // this allows toolbar buttons to have bold/colored text. nice to indicate new msgs
        //GUILayout.Button("<b>lala</b>");      // as richText, html tags could be in text


        if (Event.current.type == EventType.KeyDown && (Event.current.keyCode == KeyCode.KeypadEnter || Event.current.keyCode == KeyCode.Return))
        {
            if ("ChatInput".Equals(GUI.GetNameOfFocusedControl()))
            {
                // focus on input -> submit it
                GuiSendsMsg();
                return; // showing the now modified list would result in an error. to avoid this, we just skip this single frame
            }
            else
            {
                // assign focus to input
                GUI.FocusControl("ChatInput");
            }
        }

        GUI.SetNextControlName("");
        GUILayout.BeginArea(this.GuiRect);

        GUILayout.FlexibleSpace();

        if (this.chatClient == null || this.chatClient.State != ChatState.ConnectedToFrontEnd)
        {
            GUILayout.Label("Connecting...");
        }
        else
        {
            List<string> channels = new List<string>(this.chatClient.PublicChannels.Keys);    // this could be cached
            int countOfPublicChannels = channels.Count;
            channels.AddRange(this.chatClient.PrivateChannels.Keys);

            if (channels.Count > 0)
            {
                int previouslySelectedChannelIndex = this.selectedChannelIndex;
                int channelIndex = channels.IndexOf(this.selectedChannelName);
                this.selectedChannelIndex = (channelIndex >= 0) ? channelIndex : 0;
                
                this.selectedChannelIndex = GUILayout.Toolbar(this.selectedChannelIndex, channels.ToArray(), GUILayout.ExpandWidth(false));
                this.scrollPos = GUILayout.BeginScrollView(this.scrollPos);

                this.doingPrivateChat = (this.selectedChannelIndex >= countOfPublicChannels);
                this.selectedChannelName = channels[this.selectedChannelIndex];

                if (this.selectedChannelIndex != previouslySelectedChannelIndex)
                {
                    // changed channel -> scroll down, if private: pre-fill "to" field with target user's name
                    this.scrollPos.y = float.MaxValue;
                    if (this.doingPrivateChat)
                    {
                        string[] pieces = this.selectedChannelName.Split(new char[] {':'}, 3);
                        this.userIdInput = pieces[1];
                    }
                }

                GUILayout.Label(ChatGui.WelcomeText);

                if (this.chatClient.TryGetChannel(selectedChannelName, this.doingPrivateChat, out this.selectedChannel))
                {
                    for (int i = 0; i < this.selectedChannel.Messages.Count; i++)
                    {
						string sender = this.selectedChannel.Senders[i];
						string message = this.selectedChannel.Messages[i].ToString();

						if (message.StartsWith("/"))
						{
							GUILayout.Label(message.Remove(0, 1));
						}
						else
						{
							GUILayout.Label(string.Format("{0}: {1}", sender,  message));
						}
                    }
                }

                GUILayout.EndScrollView();
            }
        }


        GUILayout.BeginHorizontal();
        if (doingPrivateChat)
        {
            GUILayout.Label("to:", GUILayout.ExpandWidth(false));
            GUI.SetNextControlName("WhisperTo");
            this.userIdInput = GUILayout.TextField(this.userIdInput, GUILayout.MinWidth(100), GUILayout.ExpandWidth(false));
            string focussed = GUI.GetNameOfFocusedControl();
            if (focussed.Equals("WhisperTo"))
            {
                if (this.userIdInput.Equals("username"))
                {
                    this.userIdInput = "";
                }
            }
            else if (string.IsNullOrEmpty(this.userIdInput))
            {
                this.userIdInput = "username";
            }

        }
        GUI.SetNextControlName("ChatInput");
        inputLine = GUILayout.TextField(inputLine);
        if (GUILayout.Button("Send", GUILayout.ExpandWidth(false)))
        {
            GuiSendsMsg();
        }
        GUILayout.EndHorizontal();
        GUILayout.EndArea();
    }