示例#1
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);
        }
示例#2
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);
            }
        }