Ejemplo n.º 1
0
 public virtual void DrawChatSnippet(SpriteBatch spriteBatch, ChatSnippet snippet, ref float offset, bool drawShadow = true)
 {
     if (snippet.emojiIndex != -1)
     {
         spriteBatch.Draw(
             ChatBox.emojiTexture,
             new Vector2(offset, DrawOrigin.Y),
             new Rectangle?(new Rectangle(
                                snippet.emojiIndex * 9 % ChatBox.emojiTexture.Width,
                                snippet.emojiIndex * 9 / ChatBox.emojiTexture.Width * 9,
                                9,
                                9)),
             Color.White,
             0f,
             Vector2.Zero,
             4f,
             SpriteEffects.None,
             0.99f);
     }
     else if (snippet.message != null)
     {
         spriteBatch.DrawString(
             ChatBox.messageFont(LocalizedContentManager.CurrentLanguageCode),
             snippet.message,
             new Vector2(offset, DrawOrigin.Y),
             ChatMessage.getColorFromName(Game1.player.defaultChatColor),
             0f, Vector2.Zero,
             1f,
             SpriteEffects.None,
             0.99f);
     }
     offset += snippet.myLength;
 }
        /// <summary>Add text to the text box.</summary>
        public override void RecieveTextInput(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            if (this.finalText.Count == 0)
            {
                this.finalText.Add(new ChatSnippet("", LocalizedContentManager.CurrentLanguageCode));
            }

            if ((double)this.currentWidth +
                ChatBox.messageFont(LocalizedContentManager.CurrentLanguageCode).MeasureString(text).X >=
                this.Width - 16)
            {
                return;
            }

            if (this.finalText[this.currentSnippetIndex].message == null)
            {
                //The current snippet is an emoji
                if (this.currentInsertPosition == 0) //Create a new text snippet before this one.
                {
                    this.finalText.Insert(this.currentSnippetIndex,
                                          new ChatSnippet(text, LocalizedContentManager.CurrentLanguageCode));
                }
                else //Create a new text snippet after this one.
                {
                    this.finalText.Insert(this.currentSnippetIndex + 1,
                                          new ChatSnippet(text, LocalizedContentManager.CurrentLanguageCode));
                    this.currentSnippetIndex++;
                }

                this.currentInsertPosition = this.GetLastIndexOfCurrentSnippet();
            }
            else
            {
                //The current snippet is a text snippet, add text to it.
                ChatSnippet currSnippet = this.finalText[this.currentSnippetIndex];
                currSnippet.message = currSnippet.message.Substring(0, this.currentInsertPosition) + text +
                                      currSnippet.message.Substring(this.currentInsertPosition);
                this.currentInsertPosition += text.Length;

                ReMeasureSnippetLength(currSnippet);
            }

            if (!this.CheckForWhisper())
            {
                this.CheckForWhisperReply();
            }

            this.updateWidth();
        }
 /// <summary>Re measures the length of the given snippet.</summary>
 /// <param name="snippet"></param>
 private static void ReMeasureSnippetLength(ChatSnippet snippet)
 {
     if (snippet.message == null)
     {
         snippet.myLength = 40;
     }
     else
     {
         snippet.myLength = ChatBox.messageFont(LocalizedContentManager.CurrentLanguageCode)
                            .MeasureString(snippet.message).X;
     }
 }
        /// <summary>Add an emoji to the typed text.</summary>
        public void ReceiveEmoji(int emoji)
        {
            if (this.currentWidth + 40.0 > this.Width - 16)
            {
                return;
            }
            if (this.currentInsertPosition == 0)
            {
                //Inserting at the start of a text or emoji snippet - add before the current snippet.
                this.finalText.Insert(this.currentSnippetIndex, new ChatSnippet(emoji));
                if (this.finalText.Count != 1)
                {
                    this.currentSnippetIndex++;
                }
                else
                {
                    this.currentInsertPosition = this.GetLastIndexOfCurrentSnippet();
                }
            }
            else if (this.currentInsertPosition == this.GetLastIndexOfCurrentSnippet())
            {
                //Inserting at the end of a text or emoji snippet message - add after current snippet.
                ChatSnippet emojiSnippet = new ChatSnippet(emoji);
                this.finalText.Insert(this.currentSnippetIndex + 1, emojiSnippet);
                this.currentSnippetIndex++;
                this.currentInsertPosition = GetLastIndexOfMessage(emojiSnippet);
            }
            else
            {
                //Inserting at the middle of a text message - split the message in two.
                ChatSnippet orig = this.finalText[this.currentSnippetIndex];

                string first  = orig.message.Substring(0, this.currentInsertPosition);
                string second = orig.message.Substring(this.currentInsertPosition);

                this.finalText.RemoveAt(this.currentSnippetIndex);
                this.finalText.Insert(this.currentSnippetIndex,
                                      new ChatSnippet(second, LocalizedContentManager.CurrentLanguageCode));
                this.finalText.Insert(this.currentSnippetIndex, new ChatSnippet(emoji));
                this.finalText.Insert(this.currentSnippetIndex,
                                      new ChatSnippet(first, LocalizedContentManager.CurrentLanguageCode));
                this.currentSnippetIndex  += 2;
                this.currentInsertPosition = 0;
            }

            this.updateWidth();
        }
Ejemplo n.º 5
0
        public static bool receiveEmoji(ChatTextBox __instance, int emoji)
        {
            if (__instance.currentWidth + 40f > 830)
            {
                return(false);
            }
            int         index       = 0;
            ChatSnippet chatSnippet = new ChatSnippet(emoji);

            for (int i = 0; i < __instance.finalText.Count; i++)
            {
                ChatSnippet item = __instance.finalText[i];
                index += item.emojiIndex != -1 ? 1 : item.message.Length;
                if (index == ModEntry.textbox_h.ACP_Start)//[text message/emoji][caret]
                {
                    __instance.finalText.Insert(i + 1, chatSnippet);
                    goto FinalEmoji;
                }
                else if (index > ModEntry.textbox_h.ACP_Start)//[text  [caret]   message]
                {
                    var sep_str1 = new ChatSnippet(item.message.Substring(0, ModEntry.textbox_h.ACP_Start - (index - item.message.Length)), LocalizedContentManager.CurrentLanguageCode);
                    var sep_str2 = new ChatSnippet(item.message.Substring(ModEntry.textbox_h.ACP_Start - (index - item.message.Length)), LocalizedContentManager.CurrentLanguageCode);
                    __instance.finalText[i] = sep_str1;
                    __instance.finalText.Insert(i + 1, chatSnippet);
                    __instance.finalText.Insert(i + 2, sep_str2);
                    goto FinalEmoji;
                }
            }
            __instance.finalText.Add(chatSnippet);
FinalEmoji:
            __instance.updateWidth();
            ModEntry.textbox_h.ACP_Start++;
            ModEntry.textbox_h.ACP_End++;
            ModEntry.tsf.onTextChange();
            return(false);
        }
Ejemplo n.º 6
0
        public new void receiveEmoji(int emoji)
        {
            ReplaceSelection("");
            if (currentWidth + 40f > Width - 66)
            {
                return;
            }
            int         index       = 0;
            ChatSnippet chatSnippet = new ChatSnippet(emoji);

            for (int i = 0; i < finalText.Count; i++)
            {
                ChatSnippet item = finalText[i];
                index += item.emojiIndex != -1 ? 1 : item.message.Length;
                if (index == acp.Start)//[text message/emoji][caret]
                {
                    finalText.Insert(i + 1, chatSnippet);
                    goto FinalEmoji;
                }
                else if (index > acp.Start)//[text  [caret]   message]
                {
                    var sep_str1 = new ChatSnippet(item.message.Substring(0, acp.Start - (index - item.message.Length)), LocalizedContentManager.CurrentLanguageCode);
                    var sep_str2 = new ChatSnippet(item.message.Substring(acp.Start - (index - item.message.Length)), LocalizedContentManager.CurrentLanguageCode);
                    finalText[i] = sep_str1;
                    finalText.Insert(i + 1, chatSnippet);
                    finalText.Insert(i + 2, sep_str2);
                    goto FinalEmoji;
                }
            }
            finalText.Add(chatSnippet);
FinalEmoji:
            updateWidth();
            acp.Start++;
            acp.End++;
            return;
        }
Ejemplo n.º 7
0
 internal static ChatSnippet CopyChatSnippet(ChatSnippet snippet)
 {
     return(snippet.message == null
         ? new ChatSnippet(snippet.emojiIndex)
         : new ChatSnippet(snippet.message, LocalizedContentManager.CurrentLanguageCode));
 }
 /// <summary>Gets the last insertion index of the given snippet.</summary>
 private static int GetLastIndexOfMessage(ChatSnippet snippet)
 {
     return(snippet.message?.Length ?? 1);
 }
        /// <summary>Handle the backspace key being pressed.</summary>
        public void Backspace()
        {
            if (this.finalText.Any())
            {
                if (this.currentInsertPosition == 0)
                {
                    //The current snippet is the first one.
                    if (this.currentSnippetIndex == 0)
                    {
                        return;
                    }

                    if (this.finalText[this.currentSnippetIndex].message == null)
                    {
                        //The current snippet is an emoji,
                        //so the before this one is either text or an emoji
                        ChatSnippet lastSnippet = this.finalText[this.currentSnippetIndex - 1];

                        if (lastSnippet.message == null)
                        {
                            //The previous snippet is an emoji.
                            //But the current snippet is also an emoji, so no merging - just delete it.
                            this.finalText.RemoveAt(this.currentSnippetIndex - 1);
                            this.currentSnippetIndex--;
                        }
                        else
                        {
                            //The previous snippet is text, delete a character from the end of it,
                            //and remove it if necessary.
                            lastSnippet.message = lastSnippet.message.Substring(0, lastSnippet.message.Length - 1);
                            if (lastSnippet.message.Length == 0)
                            {
                                this.finalText.Remove(lastSnippet);
                                this.currentSnippetIndex--;
                            }
                            else
                            {
                                ReMeasureSnippetLength(lastSnippet);
                            }
                        }
                    }
                    else
                    {
                        //The current snippet is a text snippet.

                        //There must be an emoji before this snippet, remove it.
                        this.finalText.RemoveAt(this.currentSnippetIndex - 1);
                        this.currentSnippetIndex--;

                        //This snippet now needs to merge with the previous snippet,
                        //which may not exist (no merging), or may be an emoji (no merging).

                        //No previous snippet.
                        if (this.currentSnippetIndex == 0)
                        {
                            return;
                        }
                        //Previous snippet is an emoji.
                        if (this.finalText[this.currentSnippetIndex - 1].message == null)
                        {
                            return;
                        }

                        //Merge the current snippet with the previous one.
                        ChatSnippet last = this.finalText[this.currentSnippetIndex - 1];
                        this.currentInsertPosition = last.message.Length;
                        last.message += this.finalText[this.currentSnippetIndex].message;

                        ReMeasureSnippetLength(last);

                        this.finalText.RemoveAt(this.currentSnippetIndex);
                        this.currentSnippetIndex--;
                    }
                }
                else if (this.finalText[this.currentSnippetIndex].message == null)
                {
                    //The current snippet is an emoji, and the insert position has to be one,
                    //so we delete the emoji.
                    this.finalText.RemoveAt(this.currentSnippetIndex);
                    if (this.currentSnippetIndex != 0)
                    {
                        //The removed emoji was not the first snippet.
                        this.currentSnippetIndex--;
                        this.currentInsertPosition = this.GetLastIndexOfCurrentSnippet();

                        //If this emoji seperated two text snippets, they need to be
                        //merged.
                        if (this.currentSnippetIndex != this.finalText.Count - 1 &&
                            this.finalText[this.currentSnippetIndex + 1].message != null)
                        {
                            //Merge the next and current text snippets.
                            ChatSnippet next = this.finalText[this.currentSnippetIndex + 1];
                            ChatSnippet curr = this.finalText[this.currentSnippetIndex];
                            this.currentInsertPosition = curr.message.Length;
                            curr.message += next.message;

                            ReMeasureSnippetLength(curr);
                            this.finalText.Remove(next);
                        }
                    }
                    else
                    {
                        this.currentInsertPosition = 0;
                    }
                }
                else
                {
                    //The current snippet is a text snippet, and the current insert position
                    //is not at the start of the it, so a character is removed from it.
                    ChatSnippet currSnippet = this.finalText[this.currentSnippetIndex];
                    currSnippet.message = currSnippet.message.Remove(this.currentInsertPosition - 1, 1);

                    if (currSnippet.message.Length == 0)
                    {
                        //If the entire snippet is now empty, remove it.
                        this.finalText.Remove(currSnippet);
                        if (this.currentSnippetIndex != 0)
                        {
                            this.currentSnippetIndex--;
                            this.currentInsertPosition = this.GetLastIndexOfCurrentSnippet();
                        }
                        else
                        {
                            this.currentInsertPosition = 0;
                        }
                    }
                    else
                    {
                        ReMeasureSnippetLength(currSnippet);
                        this.currentInsertPosition--;
                    }
                }
            }

            this.updateWidth();
        }
Ejemplo n.º 10
0
        protected virtual void DrawByAcp(SpriteBatch spriteBatch, Acp acp, ref float offset, Color color, bool drawShadow = true)
        {
            var start = Math.Min(acp.Start, acp.End);
            var end   = Math.Max(acp.Start, acp.End);
            int index = 0;

            if (end == 0 || start == GetTextLength())
            {
                return;
            }
            bool foundstart = start == 0;

            foreach (ChatSnippet item in finalText)
            {
                var len = item.emojiIndex != -1 ? 1 : item.message.Length;
                if ((!foundstart && index + len > start) || (foundstart && index + len >= end))
                {
                    if (!foundstart)
                    {
                        if (item.emojiIndex != -1)
                        {
                            index++;
                            DrawChatSnippet(spriteBatch, item, ref offset, drawShadow);
                            if (index == end)
                            {
                                goto Finish;
                            }
                        }
                        else
                        {
                            var sub_len = Math.Min(start - index, len);
                            if (index + len >= end)
                            {
                                ChatSnippet sep_text = new ChatSnippet(item.message.Substring(sub_len, end - start), LocalizedContentManager.CurrentLanguageCode);
                                DrawChatSnippet(spriteBatch, sep_text, ref offset, drawShadow);
                                goto Finish;
                            }
                            else
                            {
                                ChatSnippet sep_text = new ChatSnippet(item.message.Substring(sub_len), LocalizedContentManager.CurrentLanguageCode);
                                DrawChatSnippet(spriteBatch, sep_text, ref offset, drawShadow);
                                index += len;
                            }
                        }
                        foundstart = true;
                        continue;
                    }
                    else
                    {
                        if (item.emojiIndex != -1)
                        {
                            DrawChatSnippet(spriteBatch, item, ref offset, drawShadow);
                        }
                        else
                        {
                            var         sub_len  = end - index;
                            ChatSnippet sep_text = new ChatSnippet(item.message.Substring(0, sub_len), LocalizedContentManager.CurrentLanguageCode);
                            DrawChatSnippet(spriteBatch, sep_text, ref offset, drawShadow);
                        }
                        goto Finish;
                    }
                }
                index += len;
                if (foundstart)
                {
                    DrawChatSnippet(spriteBatch, item, ref offset, drawShadow);
                }
            }
Finish:
            return;
        }
Ejemplo n.º 11
0
        public void ReplaceSelection(string _text)
        {
            if (acp.Start != acp.End)//delete selection
            {
                if (acp.End < acp.Start)
                {
                    var temp = acp.Start;
                    acp.Start = acp.End;
                    acp.End   = temp;
                }
                int _index = 0;
                for (int i = 0; i < finalText.Count && acp.End - acp.Start > 0; i++)//delete text/emoji before end reach start
                {
                    ChatSnippet item = finalText[i];
                    _index += item.emojiIndex != -1 ? 1 : item.message.Length;
                    if (_index > acp.Start)
                    {
                        if (item.emojiIndex != -1)
                        {
                            finalText.RemoveAt(i);
                            i--;
                            acp.End--;
                            _index--;
                            if (i >= 0 && finalText.Count > i + 1 && finalText[i].emojiIndex == -1 && finalText[i + 1].emojiIndex == -1)
                            {
                                //both text,merge it
                                _index -= finalText[i].message.Length;
                                finalText[i].message  += finalText[i + 1].message;
                                finalText[i].myLength += finalText[i + 1].myLength;
                                finalText.RemoveAt(i + 1);
                                //re-handle this snippet
                                i--;
                            }
                        }
                        else
                        {
                            //acp selection may cross snippet, dont out of range
                            var start = acp.Start - (_index - item.message.Length);
                            int len   = Math.Min(acp.End - acp.Start, item.message.Length - start);
                            item.message = item.message.Remove(start, len);
                            acp.End     -= len;
                            _index      -= len;
                            if (item.message.Length == 0)//empty, remove it
                            {
                                finalText.RemoveAt(i);
                                i--;
                            }
                            else
                            {
                                item.myLength = Font.MeasureString(item.message).X;
                            }
                        }
                    }
                }
                updateWidth();
            }
            int         index       = 0;
            ChatSnippet chatSnippet = new ChatSnippet(_text, LocalizedContentManager.CurrentLanguageCode);

            if (chatSnippet.myLength == 0)
            {
                return;
            }
            for (int i = 0; i < finalText.Count; i++)
            {
                if (chatSnippet.myLength + currentWidth >= Width - 66)
                {
                    acp.End = acp.Start;
                    return;
                }
                ChatSnippet item = finalText[i];
                index += item.emojiIndex != -1 ? 1 : item.message.Length;
                if (index >= acp.Start && item.emojiIndex == -1)//[text  [caret > ]   message][ = caret (index)]
                {
                    item.message   = item.message.Insert(acp.Start - (index - item.message.Length), chatSnippet.message);
                    item.myLength += chatSnippet.myLength;
                    goto Final;
                }
                else if (index > acp.Start)//[nothing/emoji][caret here][emoji(now index is here, larger than caret pos)]
                {
                    finalText.Insert(i, chatSnippet);
                    goto Final;
                }
            }
            finalText.Add(chatSnippet);
Final:
            acp.Start = acp.End = acp.Start + chatSnippet.message.Length;
            updateWidth();
            //IME input dont play sound, english input sound is handled at IKeyboadSubscriber
            //Game1.playSound("cowboy_monsterhit");//TSF may replace some word, which will make the sound strange
        }
Ejemplo n.º 12
0
        public Acp GetAcpByRange(RECT rect)
        {
            Acp   result = new Acp();
            float width  = DrawOrigin.X;

            //emoji Menu button 61------>
            if (rect.left <= X + Width - 61 && rect.top <= Y + Height && rect.right >= X && rect.bottom >= Y)//check if overlap textbox
            {
                if (rect.right <= width)
                {
                    result.Start = result.End = 0;
                }
                else if (rect.left >= currentWidth + width)
                {
                    result.Start = result.End = GetTextLength();
                }
                else
                {
                    bool found_start = false;
                    for (int j = 0; j < finalText.Count; j++)
                    {
                        ChatSnippet item = finalText[j];
                        if ((!found_start && width + item.myLength > rect.left) || (found_start && width + item.myLength > rect.right))
                        {
                            if (item.emojiIndex != -1)
                            {
                                width += item.myLength;
                                if (!found_start)
                                {
                                    //divide char from middle, if selection is on the left part, we dont sel this word
                                    result.Start += (width - item.myLength / 2) <= rect.left ? 1 : 0;
                                    result.End    = result.Start;
                                    result.End   += (((width - item.myLength / 2) < rect.right) && ((width - item.myLength / 2) > rect.left)) ? 1 : 0;
                                    found_start   = true;
                                    if (width >= rect.right)
                                    {
                                        return(result);
                                    }
                                    continue;
                                }
                                else
                                {
                                    //divide char from middle, if selection is on the left part, we dont sel this word
                                    result.End += (width - item.myLength / 2) < rect.right ? 1 : 0;
                                    return(result);
                                }
                            }
                            else
                            {
                                foreach (char ch in item.message)
                                {
                                    var char_x = Font.MeasureString(ch.ToString()).X;
                                    width += char_x;
                                    if (!found_start && width > rect.left)
                                    {
                                        //divide char from middle, if selection is on the left part, we dont sel this word
                                        result.Start += (width - char_x / 2) <= rect.left ? 1 : 0;
                                        result.End    = result.Start;
                                        found_start   = true;
                                        result.End   += (((width - char_x / 2) < rect.right) && ((width - char_x / 2) > rect.left)) ? 1 : 0;
                                        if (width >= rect.right)
                                        {
                                            return(result);
                                        }
                                        continue;
                                    }
                                    else if (found_start && width > rect.right)
                                    {
                                        //divide char from middle, if selection is on the left part, we dont sel this word
                                        result.End += (width - char_x / 2) < rect.right ? 1 : 0;
                                        return(result);
                                    }
                                    if (found_start)
                                    {
                                        result.End++;
                                    }
                                    else
                                    {
                                        result.Start++;
                                    }
                                }
                            }
                            continue;
                        }
                        width += item.myLength;
                        if (found_start)
                        {
                            result.End += item.emojiIndex != -1 ? 1 : item.message.Length;
                        }
                        else
                        {
                            result.Start += item.emojiIndex != -1 ? 1 : item.message.Length;
                        }
                    }
                }
            }
            else
            {
                result.Start = result.End = -1;
            }
            return(result);
        }
Ejemplo n.º 13
0
        public static bool Draw(TextBox __instance, SpriteBatch spriteBatch, Texture2D ____textBoxTexture, bool drawShadow = true)
        {
            if (!__instance.Selected || __instance != ModEntry.textbox_h.current)
            {
                return(true);
            }
            try
            {
                bool caretVisible = DateTime.UtcNow.Millisecond % 1000 >= 500;
                //draw background
                if (____textBoxTexture != null)
                {
                    spriteBatch.Draw(____textBoxTexture, new Rectangle(__instance.X, __instance.Y, 16, __instance.Height), new Rectangle?(new Rectangle(0, 0, 16, __instance.Height)), Color.White);
                    spriteBatch.Draw(____textBoxTexture, new Rectangle(__instance.X + 16, __instance.Y, __instance.Width - 32, __instance.Height), new Rectangle?(new Rectangle(16, 0, 4, __instance.Height)), Color.White);
                    spriteBatch.Draw(____textBoxTexture, new Rectangle(__instance.X + __instance.Width - 16, __instance.Y, 16, __instance.Height), new Rectangle?(new Rectangle(____textBoxTexture.Bounds.Width - 16, 0, 16, __instance.Height)), Color.White);
                }
                else
                {
                    Game1.drawDialogueBox(__instance.X - 32, __instance.Y - 112 + 10, __instance.Width + 80, __instance.Height, false, true, null, false, true, -1, -1, -1);
                }
                //draw text
                if (__instance is ChatTextBox)
                {
                    ChatTextBox chat = __instance as ChatTextBox;

                    float xPositionSoFar = 12f;

                    int  index       = 0;
                    bool caretDrawed = false;
                    for (int i = 0; i < chat.finalText.Count; i++)
                    {
                        ChatSnippet item = chat.finalText[i];

                        index += item.emojiIndex != -1 ? 1 : item.message.Length;

                        if (index == ModEntry.textbox_h.ACP_Start && !caretDrawed)
                        {
                            if (item.emojiIndex != -1)
                            {
                                spriteBatch.Draw(ChatBox.emojiTexture,
                                                 new Vector2(__instance.X + xPositionSoFar, __instance.Y + 12),
                                                 new Rectangle?(new Rectangle(
                                                                    item.emojiIndex * 9 % ChatBox.emojiTexture.Width,
                                                                    item.emojiIndex * 9 / ChatBox.emojiTexture.Width * 9,
                                                                    9,
                                                                    9)),
                                                 Color.White,
                                                 0f,
                                                 Vector2.Zero,
                                                 4f,
                                                 SpriteEffects.None,
                                                 0.99f);
                                xPositionSoFar += item.myLength;
                            }
                            if (item.message != null)
                            {
                                spriteBatch.DrawString(ChatBox.messageFont(LocalizedContentManager.CurrentLanguageCode),
                                                       item.message,
                                                       new Vector2(__instance.X + xPositionSoFar, __instance.Y + 12),
                                                       ChatMessage.getColorFromName(Game1.player.defaultChatColor),
                                                       0f, Vector2.Zero,
                                                       1f,
                                                       SpriteEffects.None,
                                                       0.99f);
                                xPositionSoFar += item.myLength;
                            }
                            if (caretVisible)
                            {
                                spriteBatch.Draw(Game1.staminaRect, new Rectangle((int)(xPositionSoFar), __instance.Y + 12, 4, 32), __instance.TextColor);
                            }
                            xPositionSoFar += 4;
                            caretDrawed     = true;
                            continue;
                        }
                        else if (index > ModEntry.textbox_h.ACP_Start && !caretDrawed)//[text  [caret]  message]
                        {
                            if (item.message != null)
                            {
                                //seperate str
                                var sep_str1 = new ChatSnippet(item.message.Substring(0, ModEntry.textbox_h.ACP_Start - (index - item.message.Length)), LocalizedContentManager.CurrentLanguageCode);
                                var sep_str2 = new ChatSnippet(item.message.Substring(ModEntry.textbox_h.ACP_Start - (index - item.message.Length)), LocalizedContentManager.CurrentLanguageCode);
                                if (sep_str1.message != null)
                                {
                                    spriteBatch.DrawString(ChatBox.messageFont(LocalizedContentManager.CurrentLanguageCode),
                                                           sep_str1.message,
                                                           new Vector2(__instance.X + xPositionSoFar, __instance.Y + 12),
                                                           ChatMessage.getColorFromName(Game1.player.defaultChatColor),
                                                           0f, Vector2.Zero,
                                                           1f,
                                                           SpriteEffects.None,
                                                           0.99f);
                                }
                                xPositionSoFar += sep_str1.myLength;

                                if (caretVisible)
                                {
                                    spriteBatch.Draw(Game1.staminaRect, new Rectangle((int)(xPositionSoFar), __instance.Y + 12, 4, 32), __instance.TextColor);
                                }
                                xPositionSoFar += 4;

                                if (sep_str2.message != null)
                                {
                                    spriteBatch.DrawString(ChatBox.messageFont(LocalizedContentManager.CurrentLanguageCode),
                                                           sep_str2.message,
                                                           new Vector2(__instance.X + xPositionSoFar, __instance.Y + 12),
                                                           ChatMessage.getColorFromName(Game1.player.defaultChatColor),
                                                           0f, Vector2.Zero,
                                                           1f,
                                                           SpriteEffects.None,
                                                           0.99f);
                                }
                                xPositionSoFar += sep_str2.myLength;
                            }
                            else
                            {
                                if (caretVisible)
                                {
                                    spriteBatch.Draw(Game1.staminaRect, new Rectangle((int)(xPositionSoFar), __instance.Y + 12, 4, 32), __instance.TextColor);
                                }
                                xPositionSoFar += 4;
                                if (item.emojiIndex != -1)
                                {
                                    spriteBatch.Draw(ChatBox.emojiTexture,
                                                     new Vector2(__instance.X + xPositionSoFar, __instance.Y + 12),
                                                     new Rectangle?(new Rectangle(
                                                                        item.emojiIndex * 9 % ChatBox.emojiTexture.Width,
                                                                        item.emojiIndex * 9 / ChatBox.emojiTexture.Width * 9,
                                                                        9,
                                                                        9)),
                                                     Color.White,
                                                     0f,
                                                     Vector2.Zero,
                                                     4f,
                                                     SpriteEffects.None,
                                                     0.99f);
                                    xPositionSoFar += item.myLength;
                                }
                            }
                            caretDrawed = true;
                            continue;
                        }

                        if (item.emojiIndex != -1)
                        {
                            spriteBatch.Draw(ChatBox.emojiTexture,
                                             new Vector2(__instance.X + xPositionSoFar, __instance.Y + 12),
                                             new Rectangle?(new Rectangle(
                                                                item.emojiIndex * 9 % ChatBox.emojiTexture.Width,
                                                                item.emojiIndex * 9 / ChatBox.emojiTexture.Width * 9,
                                                                9,
                                                                9)),
                                             Color.White,
                                             0f,
                                             Vector2.Zero,
                                             4f,
                                             SpriteEffects.None,
                                             0.99f);
                            xPositionSoFar += item.myLength;
                        }
                        if (item.message != null)
                        {
                            spriteBatch.DrawString(ChatBox.messageFont(LocalizedContentManager.CurrentLanguageCode),
                                                   item.message,
                                                   new Vector2(__instance.X + xPositionSoFar, __instance.Y + 12),
                                                   ChatMessage.getColorFromName(Game1.player.defaultChatColor),
                                                   0f, Vector2.Zero,
                                                   1f,
                                                   SpriteEffects.None,
                                                   0.99f);
                            xPositionSoFar += item.myLength;
                        }
                    }
                    if (!caretDrawed && caretVisible)
                    {
                        spriteBatch.Draw(Game1.staminaRect, new Rectangle((int)(xPositionSoFar), __instance.Y + 12, 4, 32), __instance.TextColor);
                    }
                }
                else
                {
                    string toDraw = __instance.PasswordBox ? new string('*', __instance.Text.Length) : __instance.Text;

                    int offset = __instance.X + 16;

                    var sep_str1 = toDraw.Substring(0, ModEntry.textbox_h.ACP_Start);
                    var sep_str2 = toDraw.Substring(ModEntry.textbox_h.ACP_Start);
                    var sep1_len = __instance.Font.MeasureString(sep_str1).X;

                    if (caretVisible)
                    {
                        //caret width = 4
                        spriteBatch.Draw(Game1.staminaRect, new Rectangle(offset + (int)sep1_len, __instance.Y + 8, 4, 32), __instance.TextColor);
                    }
                    if (drawShadow)
                    {
                        Utility.drawTextWithShadow(spriteBatch, sep_str1, __instance.Font, new Vector2(offset, __instance.Y + ((____textBoxTexture != null) ? 12 : 8)), __instance.TextColor, 1f, -1f, -1, -1, 1f, 3);
                        Utility.drawTextWithShadow(spriteBatch, sep_str2, __instance.Font, new Vector2(offset + sep1_len + 4, __instance.Y + ((____textBoxTexture != null) ? 12 : 8)), __instance.TextColor, 1f, -1f, -1, -1, 1f, 3);
                    }
                    else
                    {
                        spriteBatch.DrawString(__instance.Font, sep_str1, new Vector2(offset, __instance.Y + ((____textBoxTexture != null) ? 12 : 8)), __instance.TextColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.99f);
                        spriteBatch.DrawString(__instance.Font, sep_str2, new Vector2(offset + sep1_len + 4, __instance.Y + ((____textBoxTexture != null) ? 12 : 8)), __instance.TextColor, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0.99f);
                    }
                }
            }
            catch (Exception e)
            {
                ModEntry.monitor.Log("Failed when drawing TextBox", LogLevel.Error);
                ModEntry.monitor.Log("Message:" + e.Message + " Source:" + e.Source, LogLevel.Error);
                ModEntry.monitor.Log(e.StackTrace, LogLevel.Error);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 14
0
        public static void ReplaceSel(string replace)
        {
            if (ModEntry.textbox_h.current != null)
            {
                ModEntry.monitor.Log("ACP_Start:" + ModEntry.textbox_h.ACP_Start + "ACP_End:" + ModEntry.textbox_h.ACP_End, StardewModdingAPI.LogLevel.Trace);
                if (ModEntry.textbox_h.ACP_End < ModEntry.textbox_h.ACP_Start)
                {
                    var temp_acp = ModEntry.textbox_h.ACP_Start;
                    ModEntry.textbox_h.ACP_Start = ModEntry.textbox_h.ACP_End;
                    ModEntry.textbox_h.ACP_End   = temp_acp;
                    try
                    {
                        if (ModEntry.textbox_h.current is ChatTextBox)
                        {
                            ChatTextBox chat  = ModEntry.textbox_h.current as ChatTextBox;
                            int         index = 0;
                            for (int i = 0; i < chat.finalText.Count && ModEntry.textbox_h.ACP_End - ModEntry.textbox_h.ACP_Start > 0; i++)
                            {
                                ChatSnippet item = chat.finalText[i];
                                index += item.emojiIndex != -1 ? 1 : item.message.Length;
                                if (index >= ModEntry.textbox_h.ACP_End)
                                {
                                    if (item.emojiIndex != -1)
                                    {
                                        chat.finalText.RemoveAt(i);
                                        i--;
                                        ModEntry.textbox_h.ACP_End--;
                                        index--;
                                        if (i >= 0 && chat.finalText.Count > i + 1 && chat.finalText[i].emojiIndex == -1 && chat.finalText[i + 1].emojiIndex == -1)
                                        {
                                            //both text,merge it
                                            chat.finalText[i].message  += chat.finalText[i + 1].message;
                                            chat.finalText[i].myLength += chat.finalText[i + 1].myLength;
                                            chat.finalText.RemoveAt(i + 1);
                                        }
                                    }
                                    else
                                    {
                                        //acp selection may cross snippet, dont out of range
                                        var start = ModEntry.textbox_h.ACP_Start - (index - item.message.Length);
                                        int len   = Math.Min(ModEntry.textbox_h.ACP_End - ModEntry.textbox_h.ACP_Start, item.message.Length - start);
                                        item.message = item.message.Remove(start, len);
                                        ModEntry.textbox_h.ACP_End -= len;
                                        index -= len;
                                        if (item.message.Length == 0)//empty, remove it
                                        {
                                            chat.finalText.RemoveAt(i);
                                            i--;
                                        }
                                        else
                                        {
                                            item.myLength = ModEntry.textbox_h.font.MeasureString(item.message).X;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            ModEntry.textbox_h.current.Text = ModEntry.textbox_h.current.Text.Remove(ModEntry.textbox_h.ACP_Start,
                                                                                                     ModEntry.textbox_h.ACP_End - ModEntry.textbox_h.ACP_Start);

                            ModEntry.textbox_h.ACP_End = ModEntry.textbox_h.ACP_Start;
                        }
                        ModEntry.monitor.Log("After Remove ACP_Start:" + ModEntry.textbox_h.ACP_Start + "ACP_End:" + ModEntry.textbox_h.ACP_End, StardewModdingAPI.LogLevel.Trace);
                    }
                    catch (Exception)
                    {
                        ModEntry.textbox_h.resetAcp();
                        ModEntry.monitor.Log("Reset acp", StardewModdingAPI.LogLevel.Error);
                    }
                }
                if (ModEntry.textbox_h.current is ChatTextBox)
                {
                    ChatTextBox chat = ModEntry.textbox_h.current as ChatTextBox;
                    chat.updateWidth();
                    int         index       = 0;
                    ChatSnippet chatSnippet = new ChatSnippet(replace, LocalizedContentManager.CurrentLanguageCode);
                    if (chatSnippet.myLength + chat.currentWidth >= 830)
                    {
                        ModEntry.textbox_h.ACP_End = ModEntry.textbox_h.ACP_Start;
                        ModEntry.monitor.Log("Full ACP_Start:" + ModEntry.textbox_h.ACP_Start + "ACP_End:" + ModEntry.textbox_h.ACP_End, StardewModdingAPI.LogLevel.Trace);
                        return;
                    }
                    for (int i = 0; i < chat.finalText.Count; i++)
                    {
                        ChatSnippet item = chat.finalText[i];
                        index += item.emojiIndex != -1 ? 1 : item.message.Length;
                        if (index >= ModEntry.textbox_h.ACP_Start && item.emojiIndex == -1)//[text  [caret > ]   message][ = caret (index)]
                        {
                            item.message   = item.message.Insert(ModEntry.textbox_h.ACP_Start - (index - item.message.Length), chatSnippet.message);
                            item.myLength += chatSnippet.myLength;
                            goto Final;
                        }
                        else if (index > ModEntry.textbox_h.ACP_Start)//[nothing/emoji][caret here][emoji(now index is here, larger than caret pos)]
                        {
                            chat.finalText.Insert(i, chatSnippet);
                            goto Final;
                        }
                    }
                    chat.finalText.Add(chatSnippet);
Final:
                    ModEntry.textbox_h.ACP_End = ModEntry.textbox_h.ACP_Start + chatSnippet.message.Length;
                    chat.updateWidth();
                }
                else
                {
                    var temp = ModEntry.textbox_h.current.Text.Length;
                    ModEntry.textbox_h.current.Text = ModEntry.textbox_h.current.Text.Insert(ModEntry.textbox_h.ACP_Start, replace);
                    ModEntry.textbox_h.ACP_End      = ModEntry.textbox_h.ACP_Start + ModEntry.textbox_h.current.Text.Length - temp;
                }
                ModEntry.monitor.Log("After Set ACP_Start:" + ModEntry.textbox_h.ACP_Start + "ACP_End:" + ModEntry.textbox_h.ACP_End, StardewModdingAPI.LogLevel.Trace);
            }
        }