//    //find indent and vertical spacing values
    //    while (transform.childCount < 2) //wait until a text object has been created
    //    {
    //        yield return null;
    //    }

    //    yield return null;
    //    TextMessageController bubble = transform.GetChild(1).GetComponent<TextMessageController>();
    //    vertSpace = bubble.vertSpace;
    //    indent = bubble.edgeSpace;

    //    yield return null;
    //}

    public void SetTextBubbleAppearance(GameObject bubble, TextMessageController bubbleController)
    {
        bubble.transform.localScale = new Vector3(1f, 1f, 1f);
        bubbleController.ResizeBubble();

        if (vertSpace == 0)
        {
            vertSpace = bubbleController.vertSpace;
        }

        if (indent == 0)
        {
            indent = bubbleController.edgeSpace;
        }
    }
    IEnumerator ShowText()
    {
        bool wasOpened = false;

        while (true)
        {
            //wait for permission from TextAppController to show conversation
            //bool textWasNull = false;
            string thisMessage;
            bool   isPlayerTalking;
            bool   hasNextMessage = true;
            firstTextDisplayed = false;

            while (!TextMsgObjManager.TryGetCurrentMsg(CurrentSpeaker, out thisMessage) || (AppCallbackModule.Instance != null && !TextAppController.Instance.IsShowBeforeTransition) || needDecisionMaking)
            {
                yield return(null);
                //textWasNull = true;
            }
            //TextMsgObj text = currText;

            //if (textWasNull)
            //    text = GetText(firstText);

            //process changes that affect player profile here

            //display messages
            if (typingTimeNPC == 0)
            {
                typingTimeNPC = GetTypingSpeed(CurrentSpeaker);
            }
            //for (int i = currentIndex; i < text.message.Length; i++)
            while (hasNextMessage)
            {
                if (bubble != null)
                {
                    prevBubble = bubble;
                }

                if (!TextMsgObjManager.TryGetIsPlayerSpeaking(CurrentSpeaker, out isPlayerTalking))
                {
                    yield break;
                }

                if (thisMessage.Length > 0)
                {
                    //create text bubble object
                    if (!isPlayerTalking)
                    {
                        bubble = Instantiate(receivedText);
                        bubble.transform.SetParent(this.transform);
                        bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, nextTextLoc);
                        //bubble.transform.localPosition = new Vector2(textVisualizer.screenWidth, -textVisualizer.screenHeight / 2f);
                    }
                    else
                    {
                        bubble = Instantiate(sentText);
                        bubble.transform.SetParent(this.transform);
                        bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, nextTextLoc);

                        //bubble.transform.localPosition = new Vector2(-textVisualizer.screenWidth, -textVisualizer.screenHeight / 2f);
                    }
                    textVisualizer.AddBubble(bubble);
                    bubble.transform.GetChild(bubble.transform.childCount - 1).GetComponent <Text>().text = thisMessage;
                    yield return(null);

                    TextMessageController bubbleController = bubble.GetComponent <TextMessageController>();
                    textVisualizer.SetTextBubbleAppearance(bubble, bubbleController);

                    //if (transform.childCount < 3) //no texts on screen yet other than the one we just created

                    //move bubble to the start position
                    if (isPlayerTalking)
                    {
                        bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, nextTextLoc) - new Vector2(-textVisualizer.indent, textVisualizer.indent);

                        //bubble.transform.localPosition = new Vector2(textVisualizer.screenWidth / 2f, nextTextLoc)
                        //    - new Vector2(-textVisualizer.indent, textVisualizer.indent);
                    }
                    else
                    {
                        bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, nextTextLoc) - new Vector2(textVisualizer.indent, textVisualizer.indent);

                        //bubble.transform.localPosition = new Vector2(-textVisualizer.screenWidth / 2f, nextTextLoc)
                        //    - new Vector2(textVisualizer.indent, textVisualizer.indent);
                    }

                    //make text invisible
                    Color bubbleColor = bubble.transform.GetChild(0).GetComponent <Image>().color;
                    Color textColor   = bubble.transform.GetChild(bubble.transform.childCount - 1).GetComponent <Text>().color;
                    SetBubbleOpacity(bubble, bubbleColor, textColor, 0);

                    //move bubble to correct location and state
                    Vector2 startLoc = bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition;
                    endLoc = new Vector2(0f, nextTextLoc);
                    //if (isPlayerTalking)
                    //    endLoc = new Vector3(textVisualizer.screenWidth / 2f, nextTextLoc);
                    //else
                    //    endLoc = new Vector3(-textVisualizer.screenWidth / 2f, nextTextLoc);

                    textVisualizer.AdjustScrolling(endLoc, typingBubble.sizeDelta.y);

                    //first text automatically on screen, others must appear over time
                    if (firstTextDisplayed || wasOpened)
                    {
                        float startTime;

                        //typing bubble appears if player didn't just make a choice
                        if (!justMadeChoice)
                        {
                            //typing bubble
                            typingBubble.anchoredPosition = endLoc;
                            Image typingBubbleImg = typingBubble.GetComponent <Image>();
                            typingBubbleImg.color = new Color(1, 1, 1, 0);

                            //flip depending on speaker
                            if (isPlayerTalking)
                            {
                                typingBubble.transform.localScale = new Vector3(-1f, 1f, 1f);
                            }
                            else
                            {
                                typingBubble.transform.localScale = new Vector3(1f, 1f, 1f);
                            }

                            //figure out if new typing bubble is visible to the player and go there if not
                            //bool zoomToBubble = !NewTextVisible(endLoc.y);
                            //float endScrollBubble = -endLoc.y + typingBubble.sizeDelta.y + textVisualizer.vertSpace +
                            //    textVisualizer.panel.sizeDelta.y - (textVisualizer.screenHeight / 2f);
                            //float startScrollBubble = textVisualizer.rt.localPosition.y;

                            //make typing bubble appear on screen
                            startTime = Time.time;
                            float endTime = startTime + typingBubbleAppearTime;
                            while (Time.time < endTime)
                            {
                                float prog = (Time.time - startTime) / (endTime - startTime);
                                if (isPlayerTalking)
                                {
                                    typingBubble.anchoredPosition = new Vector2(endLoc.x - textVisualizer.indent * prog, endLoc.y);
                                }
                                else
                                {
                                    typingBubble.anchoredPosition = new Vector2(endLoc.x + textVisualizer.indent * prog, endLoc.y);
                                }
                                typingBubbleImg.color = new Color(1, 1, 1, prog);

                                //if (zoomToBubble)
                                //{
                                //    float y = prog * endScrollBubble + (1 - prog) * startScrollBubble;
                                //    textVisualizer.rt.localPosition = new Vector3(textVisualizer.rt.localPosition.x, y, 0f);
                                //}

                                yield return(null);
                            }
                            if (isPlayerTalking)
                            {
                                typingBubble.anchoredPosition = new Vector2(endLoc.x - textVisualizer.indent, endLoc.y);
                            }
                            else
                            {
                                typingBubble.anchoredPosition = new Vector2(endLoc.x + textVisualizer.indent, endLoc.y);
                            }
                            typingBubbleImg.color = new Color(1, 1, 1, 1);

                            //if (zoomToBubble)
                            //{
                            //    textVisualizer.rt.localPosition = new Vector3(textVisualizer.rt.localPosition.x, endScrollBubble, 0f);
                            //}

                            //typing bubble stays on screen
                            if (thisMessage.Length > 0)
                            {
                                if (isPlayerTalking)
                                {
                                    yield return(new WaitForSeconds(typingTimePlayer * thisMessage.Length));
                                }
                                else
                                {
                                    yield return(new WaitForSeconds(typingTimeNPC * thisMessage.Length));
                                }
                            }
                            else
                            {
                                yield return(new WaitForSeconds(bubbleDisappearTime));
                                //textVisualizer.maxScroll -= typingBubble.sizeDelta.y;
                            }

                            //make typing bubble leave screen
                            startTime = Time.time;
                            endTime   = startTime + typingBubbleAppearTime;
                            while (Time.time < endTime)
                            {
                                float prog = (Time.time - startTime) / (endTime - startTime);
                                if (isPlayerTalking)
                                {
                                    typingBubble.anchoredPosition = new Vector2(endLoc.x - textVisualizer.indent * (1 - prog), endLoc.y);
                                }
                                else
                                {
                                    typingBubble.anchoredPosition = new Vector2(endLoc.x + textVisualizer.indent * (1 - prog), endLoc.y);
                                }
                                typingBubbleImg.color = new Color(1, 1, 1, 1 - prog);
                                yield return(null);
                            }
                            typingBubble.anchoredPosition = endLoc;
                            typingBubbleImg.color         = new Color(1, 1, 1, 0);
                        }

                        //figure out if new text is visible to the player and go there if not
                        //bool zoomToText = !NewTextVisible(endLoc.y);
                        //float endScroll = -endLoc.y + bubbleController.bubbleHeight + textVisualizer.vertSpace +
                        //    textVisualizer.panel.sizeDelta.y - (textVisualizer.screenHeight / 2f);
                        //float startScroll = textVisualizer.rt.localPosition.y;

                        //advance how far the player can scroll
                        textVisualizer.AdjustScrolling(endLoc, bubbleController.bubbleHeight);

                        //bring on the text
                        if (isPlayerTalking)
                        {
                            AudioManager.Instance.Play(AudioEnum.Text_Sent);
                        }
                        else
                        {
                            AudioManager.Instance.Play(AudioEnum.Text_Received);
                        }
                        startTime = Time.time;
                        float appearTime = (textVisualizer.indent * Mathf.Sqrt(2f)) / moveSpeed; //time taken to appear

                        while (Time.time < startTime + appearTime)
                        {
                            float   prog = (Time.time - startTime) / appearTime;
                            Vector2 pos  = (1 - prog) * startLoc + prog * endLoc;
                            bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = pos;
                            SetBubbleOpacity(bubble, bubbleColor, textColor, prog);

                            //if (zoomToText)
                            //{
                            //    float y = prog * endScroll + (1 - prog) * startScroll;
                            //    textVisualizer.rt.localPosition = new Vector3(textVisualizer.rt.localPosition.x, y, 0f);
                            //}

                            yield return(null);
                        }

                        //if (zoomToText)
                        //textVisualizer.rt.localPosition = new Vector3(textVisualizer.rt.localPosition.x, endScroll, 0f);
                    }

                    firstTextDisplayed = true;

                    //make sure bubble ends in the right place and state
                    bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = endLoc;
                    SetBubbleOpacity(bubble, bubbleColor, textColor, 1);
                    bubbleController.placed = true;


                    bubble = null;
                    //currentIndex++;

                    //prepare next text

                    nextTextLoc -= (bubbleController.bubbleHeight + textVisualizer.vertSpace);
                }
                if (hasNextMessage = TextMsgObjManager.TryGetNextMsg(CurrentSpeaker, out thisMessage))
                {
                    if (NotificationController.Instance && !isPlayerTalking && thisMessage != "")
                    {
                        MessageFrom_0_NotificationData data = (MessageFrom_0_NotificationData)NotificationController.Instance.CreateDataInstance(Utility.App.Text, "MessageFrom");
                        data.FromWho = CurrentSpeaker;
                        data.Message = thisMessage;
                        NotificationController.Instance.PushNotification(data, CurrentSpeaker);
                    }
                    yield return(new WaitForSeconds(bubbleDisappearTime));
                }
                else
                {
                    string message;
                    TextMsgObjManager.TryGetLastMessage(CurrentSpeaker, out message);
                    if (NotificationController.Instance && !isPlayerTalking && message != "")
                    {
                        MessageFrom_0_NotificationData data = (MessageFrom_0_NotificationData)NotificationController.Instance.CreateDataInstance(Utility.App.Text, "MessageFrom");
                        data.FromWho = CurrentSpeaker;
                        data.Message = message;
                        NotificationController.Instance.PushNotification(data, CurrentSpeaker);
                    }
                }
            }
            wasOpened = true;
            AdvanceTexts();
            yield return(null);
        }
    }
 public void ReceiveTextMessage(MessageToUser message) => TextMessageController?.GetTextMessageFromUser(message.Username, message.Text);
Exemple #4
0
        private async Task SendMessage()
        {
            await Task.Run(() => TextMessageController?.SendTextMessage());

            messageBox.Clear();
        }
    //ignore this function. it's just for testing purposes
    //void TestMessageLoading()
    //{
    //    Dictionary<string, List<TextDataManager.Conversation>> dataHashMap = new Dictionary<string, List<TextDataManager.Conversation>>();

    //    List<TextDataManager.Conversation> conversation = new List<TextDataManager.Conversation>();

    //    TextDataManager.Conversation message1 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.NPC, "Oh my god did you hear about Brittney??? ", 0);
    //    TextDataManager.Conversation message2 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.Player, "", 1);
    //    TextDataManager.Conversation message3 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.Player, "OMG yes!", 2);
    //    TextDataManager.Conversation message4 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.Player, "Sooo gross lmfao", 3);
    //    TextDataManager.Conversation message5 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.NPC, "It makes me wonder if man has a higher purpose in this grand universe. In the eyes of whatever god may live, are we not just ants on an infinite plane? Are we not replaceable and miniscule?", 4);
    //    TextDataManager.Conversation message6 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.Player, "wtf?", 5);
    //    TextDataManager.Conversation message7 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.NPC, "Philosophers have pondered it for millenia, but what grand importance makes them powerful enough to decide the ways of all of creation? What gives them the right to such arrogance? We are but atoms, Erika. To assume otherwise is a lunatic's dream.", 6);
    //    TextDataManager.Conversation message8 = new TextDataManager.Conversation(TextDataManager.Conversation.Sender.Player, "You're like so lucky you're pretty or I wouldn't be friends with you.", 7);

    //    conversation.Add(message1);
    //    conversation.Add(message2);
    //    conversation.Add(message3);
    //    conversation.Add(message4);
    //    conversation.Add(message5);
    //    conversation.Add(message6);
    //    conversation.Add(message7);
    //    conversation.Add(message8);

    //    dataHashMap["existentialism"] = conversation;

    //    RecallConversation(dataHashMap, "existentialism");
    //}

    public void UpdateHistoryMessagesView(string sender)
    {
        //create bubbles with history
        int messageLength = TextAppController.Instance.TextDataManager.GetMessageCount(sender);

        TextDataManager.Conversation conversation;
        GameObject bubble = null;

        for (int i = 0; i < messageLength; i++)
        {
            conversation = TextAppController.Instance.TextDataManager.GetMessage(sender, i);
            switch (conversation.speaker)
            {
            case TextDataManager.Conversation.Sender.Player:
                bubble = Instantiate(textProgression.sentText);
                AddBubble(bubble);
                break;

            case TextDataManager.Conversation.Sender.NPC:
                bubble = Instantiate(textProgression.receivedText);
                AddBubble(bubble);
                break;
            }
            if (bubble == null)
            {
                continue;
            }
            bubble.transform.SetParent(transform);
            bubble.transform.GetChild(bubble.transform.childCount - 1).GetComponent <Text>().text = conversation.message;
            TextMessageController bubbleController = bubble.GetComponent <TextMessageController>();
            SetTextBubbleAppearance(bubble, bubbleController);

            if (transform.childCount < 3) //no texts created except the one that was just created
            {
                Debug.Log("--------------------------------------------------------------");
                Debug.Log(textProgression.nextTextLoc);
                //textProgression.nextTextLoc -= (vertSpace + header.sizeDelta.y);
                Debug.Log(textProgression.nextTextLoc);
                Debug.Log("~");
                Debug.Log(vertSpace);
                Debug.Log(header.sizeDelta.y);
                Debug.Log("--------------------------------------------------------------");
            }

            //move the bubble to the correct location
            bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, textProgression.nextTextLoc);

            //switch (conversation.speaker)
            //{
            //    case TextDataManager.Conversation.Sender.Player:
            //        bubble.GetComponent<TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, textProgression.nextTextLoc);
            //        //bubble.transform.localPosition = (new Vector2(screenWidth / 2f, textProgression.nextTextLoc));
            //        break;
            //    case TextDataManager.Conversation.Sender.NPC:
            //        bubble.GetComponent<TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, textProgression.nextTextLoc);
            //        //bubble.transform.localPosition = (new Vector2(-screenWidth / 2f, textProgression.nextTextLoc));
            //        break;
            //}
            textProgression.endLoc = bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition;

            textProgression.nextTextLoc -= (bubbleController.bubbleHeight + vertSpace);
        }

        //create bubble with TxtMsgObj
        string[] oldMessages = null;
        if (TextAppController.Instance.TextMsgObjManager.TryGetOldMessages(sender, out oldMessages))
        {
            for (int i = 0; i < oldMessages.Length; i++)
            {
                bool isPlayerSpeaking;
                if (!TextAppController.Instance.TextMsgObjManager.TryGetIsPlayerSpeaking(sender, out isPlayerSpeaking))
                {
                    continue;
                }
                if (isPlayerSpeaking)
                {
                    bubble = Instantiate(textProgression.sentText);
                    AddBubble(bubble);
                }
                else
                {
                    bubble = Instantiate(textProgression.receivedText);
                    AddBubble(bubble);
                }
                //switch (conversation.speaker)
                //{
                //    case TextDataManager.Conversation.Sender.Player:
                //        bubble = Instantiate(textProgression.sentText);
                //        AddBubble(bubble);
                //        break;
                //    case TextDataManager.Conversation.Sender.NPC:
                //        bubble = Instantiate(textProgression.receivedText);
                //        AddBubble(bubble);
                //        break;
                //}
                if (bubble == null)
                {
                    continue;
                }

                bubble.transform.SetParent(transform);
                bubble.transform.GetChild(bubble.transform.childCount - 1).GetComponent <Text>().text = oldMessages[i];
                TextMessageController bubbleController = bubble.GetComponent <TextMessageController>();
                SetTextBubbleAppearance(bubble, bubbleController);

                if (transform.childCount < 3) //no texts created except the one that was just created
                {
                    Debug.Log("--------------------------------------------------------------");
                    Debug.Log(textProgression.nextTextLoc);
                    //textProgression.nextTextLoc -= (vertSpace + header.sizeDelta.y);
                    Debug.Log(textProgression.nextTextLoc);
                    Debug.Log("~");
                    Debug.Log(vertSpace);
                    Debug.Log(header.sizeDelta.y);
                    Debug.Log("--------------------------------------------------------------");
                }

                //move the bubble to the correct location
                bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition = new Vector2(0f, textProgression.nextTextLoc);

                //if (isPlayerSpeaking)
                //{
                //    bubble.transform.localPosition = (new Vector2(screenWidth / 2f, textProgression.nextTextLoc));
                //}
                //else
                //{
                //    bubble.transform.localPosition = (new Vector2(-screenWidth / 2f, textProgression.nextTextLoc));
                //}
                //switch (conversation.speaker)
                //{
                //    case TextDataManager.Conversation.Sender.Player:
                //        bubble.transform.localPosition = (new Vector2(screenWidth / 2f, textProgression.nextTextLoc));
                //        break;
                //    case TextDataManager.Conversation.Sender.NPC:
                //        bubble.transform.localPosition = (new Vector2(-screenWidth / 2f, textProgression.nextTextLoc));
                //        break;
                //}
                textProgression.endLoc       = bubble.GetComponent <TextMessageController>().RectTransform.anchoredPosition;
                textProgression.nextTextLoc -= (bubbleController.bubbleHeight + vertSpace);
            }
        }
        if (transform.GetChild(transform.childCount - 1).GetComponent <TextMessageController>())
        {
            AdjustScrolling(textProgression.endLoc, transform.GetChild(transform.childCount - 1).GetComponent <TextMessageController>().bubbleHeight, false);
        }
    }