From b22af63b975eff558603572149bf1b5e0945c0d7 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Tue, 7 Mar 2023 11:29:23 +0530 Subject: [PATCH] Improved and organised code in SocialPagePatch.cs --- .../GameMenuPatches/OptionsPagePatch.cs | 68 ++--- .../GameMenuPatches/SocialPagePatch.cs | 265 +++++++++--------- 2 files changed, 168 insertions(+), 165 deletions(-) diff --git a/stardew-access/Patches/GameMenuPatches/OptionsPagePatch.cs b/stardew-access/Patches/GameMenuPatches/OptionsPagePatch.cs index a05d478..f4d9879 100644 --- a/stardew-access/Patches/GameMenuPatches/OptionsPagePatch.cs +++ b/stardew-access/Patches/GameMenuPatches/OptionsPagePatch.cs @@ -15,42 +15,42 @@ namespace stardew_access.Patches int x = Game1.getMouseX(true), y = Game1.getMouseY(true); for (int i = 0; i < __instance.optionSlots.Count; i++) { - if (__instance.optionSlots[i].bounds.Contains(x, y) && currentItemIndex + i < __instance.options.Count && __instance.options[currentItemIndex + i].bounds.Contains(x - __instance.optionSlots[i].bounds.X, y - __instance.optionSlots[i].bounds.Y)) + if (!__instance.optionSlots[i].bounds.Contains(x, y) || currentItemIndex + i >= __instance.options.Count || !__instance.options[currentItemIndex + i].bounds.Contains(x - __instance.optionSlots[i].bounds.X, y - __instance.optionSlots[i].bounds.Y)) + continue; + + OptionsElement optionsElement = __instance.options[currentItemIndex + i]; + string toSpeak = optionsElement.label; + + if (optionsElement is OptionsButton) + toSpeak = $" {toSpeak} Button"; + else if (optionsElement is OptionsCheckbox) + toSpeak = (((OptionsCheckbox)optionsElement).isChecked ? "Enabled" : "Disabled") + $" {toSpeak} Checkbox"; + else if (optionsElement is OptionsDropDown) + toSpeak = $"{toSpeak} Dropdown, option {((OptionsDropDown)optionsElement).dropDownDisplayOptions[((OptionsDropDown)optionsElement).selectedOption]} selected"; + else if (optionsElement is OptionsSlider) + toSpeak = $"{((OptionsSlider)optionsElement).value}% {toSpeak} Slider"; + else if (optionsElement is OptionsPlusMinus) + toSpeak = $"{((OptionsPlusMinus)optionsElement).displayOptions[((OptionsPlusMinus)optionsElement).selected]} selected of {toSpeak}"; + else if (optionsElement is OptionsInputListener) { - OptionsElement optionsElement = __instance.options[currentItemIndex + i]; - string toSpeak = optionsElement.label; - - if (optionsElement is OptionsButton) - toSpeak = $" {toSpeak} Button"; - else if (optionsElement is OptionsCheckbox) - toSpeak = (((OptionsCheckbox)optionsElement).isChecked ? "Enabled" : "Disabled") + $" {toSpeak} Checkbox"; - else if (optionsElement is OptionsDropDown) - toSpeak = $"{toSpeak} Dropdown, option {((OptionsDropDown)optionsElement).dropDownDisplayOptions[((OptionsDropDown)optionsElement).selectedOption]} selected"; - else if (optionsElement is OptionsSlider) - toSpeak = $"{((OptionsSlider)optionsElement).value}% {toSpeak} Slider"; - else if (optionsElement is OptionsPlusMinus) - toSpeak = $"{((OptionsPlusMinus)optionsElement).displayOptions[((OptionsPlusMinus)optionsElement).selected]} selected of {toSpeak}"; - else if (optionsElement is OptionsInputListener) - { - string buttons = ""; - ((OptionsInputListener)optionsElement).buttonNames.ForEach(name => { buttons += $", {name}"; }); - toSpeak = $"{toSpeak} is bound to {buttons}. Left click to change."; - } - else - { - if (toSpeak.Contains(":")) - toSpeak = toSpeak.Replace(":", ""); - - toSpeak = $"{toSpeak} Options:"; - } - - if (optionsPageQueryKey != toSpeak) - { - optionsPageQueryKey = toSpeak; - MainClass.ScreenReader.Say(toSpeak, true); - } - return; + string buttons = ""; + ((OptionsInputListener)optionsElement).buttonNames.ForEach(name => { buttons += $", {name}"; }); + toSpeak = $"{toSpeak} is bound to {buttons}. Left click to change."; } + else + { + if (toSpeak.Contains(":")) + toSpeak = toSpeak.Replace(":", ""); + + toSpeak = $"{toSpeak} Options:"; + } + + if (optionsPageQueryKey != toSpeak) + { + optionsPageQueryKey = toSpeak; + MainClass.ScreenReader.Say(toSpeak, true); + } + return; } } catch (Exception e) diff --git a/stardew-access/Patches/GameMenuPatches/SocialPagePatch.cs b/stardew-access/Patches/GameMenuPatches/SocialPagePatch.cs index 3c03245..81b2ad0 100644 --- a/stardew-access/Patches/GameMenuPatches/SocialPagePatch.cs +++ b/stardew-access/Patches/GameMenuPatches/SocialPagePatch.cs @@ -14,138 +14,16 @@ namespace stardew_access.Patches int x = Game1.getMouseX(true), y = Game1.getMouseY(true); // Mouse x and y position for (int i = ___slotPosition; i < ___slotPosition + 5; i++) { - if (i < ___sprites.Count) + if (i >= ___sprites.Count) + continue; + + if (__instance.names[i] is string && narrateNPCDetails(__instance, i, ___kidsNames, x, y)) { - if (__instance.names[i] is string) - { - #region For NPCs - if (__instance.characterSlots[i].bounds.Contains(Game1.getMouseX(true), Game1.getMouseY(true))) - { - string name = $"{__instance.names[i] as string}"; - int heartLevel = Game1.player.getFriendshipHeartLevelForNPC(name); - bool datable = SocialPage.isDatable(name); - Friendship friendship = __instance.getFriendship(name); - int giftsThisWeek = friendship.GiftsThisWeek; - bool hasTalked = Game1.player.hasPlayerTalkedToNPC(name); - bool spouse = friendship.IsMarried(); - bool housemate = spouse && SocialPage.isRoommateOfAnyone(name); - ___kidsNames.Add("Robin"); - ___kidsNames.Add("Pierre"); - ___kidsNames.Add("Caroline"); - ___kidsNames.Add("Jodi"); - ___kidsNames.Add("Kent"); - ___kidsNames.Add("George"); - ___kidsNames.Add("Evelyn"); - ___kidsNames.Add("Demetrius"); - - - - string toSpeak = $"{name}"; - - if (!hasTalked) - { - toSpeak = $"{toSpeak}, not talked yet"; - } - - - if (datable | housemate) - { - string text2 = (LocalizedContentManager.CurrentLanguageCode != LocalizedContentManager.LanguageCode.pt) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635") : ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').First() : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').Last()); - if (housemate) - { - text2 = Game1.content.LoadString("Strings\\StringsFromCSFiles:Housemate"); - } - else if (spouse) - { - text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11636") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11637")); - } - else if (__instance.isMarriedToAnyone(name)) - { - text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_MaleNPC") : Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_FemaleNPC")); - } - else if (!Game1.player.isMarried() && friendship.IsDating()) - { - text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11639") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11640")); - } - else if (__instance.getFriendship(name).IsDivorced()) - { - text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11642") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11643")); - } - - toSpeak = $"{toSpeak}, {text2}"; - } - if (!__instance.getFriendship(name).IsMarried() && ___kidsNames.Contains(name)) - { - toSpeak = $"{toSpeak}, married"; - } - if (spouse) - { - toSpeak = $"{toSpeak}, spouse"; - } - else if (friendship.IsDating()) - { - toSpeak = $"{toSpeak}, dating"; - } - - toSpeak = $"{toSpeak}, {heartLevel} hearts, {giftsThisWeek} gifts given this week."; - - - if (socialPageQuery != toSpeak) - { - socialPageQuery = toSpeak; - MainClass.ScreenReader.Say(toSpeak, true); - } - return; - } - #endregion - } - else if (__instance.names[i] is long) - { - #region For Farmers - - long farmerID = (long)__instance.names[i]; - Farmer farmer = Game1.getFarmerMaybeOffline(farmerID); - if (farmer != null) - { - int gender = (!farmer.IsMale) ? 1 : 0; - ClickableTextureComponent clickableTextureComponent = ___sprites[i]; - if (clickableTextureComponent.containsPoint(x, y)) - { - Friendship friendship = Game1.player.team.GetFriendship(Game1.player.UniqueMultiplayerID, farmerID); - bool spouse = friendship.IsMarried(); - string toSpeak = ""; - - string text2 = (LocalizedContentManager.CurrentLanguageCode != LocalizedContentManager.LanguageCode.pt) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635") : ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').First() : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').Last()); - if (spouse) - { - text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11636") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11637")); - } - else if (farmer.isMarried() && !farmer.hasRoommate()) - { - text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_MaleNPC") : Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_FemaleNPC")); - } - else if (!Game1.player.isMarried() && friendship.IsDating()) - { - text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11639") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11640")); - } - else if (friendship.IsDivorced()) - { - text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11642") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11643")); - } - - toSpeak = $"{farmer.displayName}, {text2}"; - - if (socialPageQuery != toSpeak) - { - socialPageQuery = toSpeak; - MainClass.ScreenReader.Say(toSpeak, true); - } - return; - } - } - - #endregion - } + return; + } + else if (__instance.names[i] is long && narrateFarmerDetails(__instance, i, ___sprites, x, y)) + { + return; } } } @@ -155,6 +33,131 @@ namespace stardew_access.Patches } } + private static bool narrateNPCDetails(SocialPage __instance, int i, List ___kidsNames, int x, int y) + { + if (!__instance.characterSlots[i].bounds.Contains(x, y)) + return false; + + string name = $"{__instance.names[i] as string}"; + int heartLevel = Game1.player.getFriendshipHeartLevelForNPC(name); + bool datable = SocialPage.isDatable(name); + Friendship friendship = __instance.getFriendship(name); + int giftsThisWeek = friendship.GiftsThisWeek; + bool hasTalked = Game1.player.hasPlayerTalkedToNPC(name); + bool spouse = friendship.IsMarried(); + bool housemate = spouse && SocialPage.isRoommateOfAnyone(name); + ___kidsNames.Add("Robin"); + ___kidsNames.Add("Pierre"); + ___kidsNames.Add("Caroline"); + ___kidsNames.Add("Jodi"); + ___kidsNames.Add("Kent"); + ___kidsNames.Add("George"); + ___kidsNames.Add("Evelyn"); + ___kidsNames.Add("Demetrius"); + + string toSpeak = $"{name}"; + + if (!hasTalked) + { + toSpeak = $"{toSpeak}, not talked yet"; + } + + + if (datable | housemate) + { + string text2 = (LocalizedContentManager.CurrentLanguageCode != LocalizedContentManager.LanguageCode.pt) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635") : ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').First() : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').Last()); + if (housemate) + { + text2 = Game1.content.LoadString("Strings\\StringsFromCSFiles:Housemate"); + } + else if (spouse) + { + text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11636") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11637")); + } + else if (__instance.isMarriedToAnyone(name)) + { + text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_MaleNPC") : Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_FemaleNPC")); + } + else if (!Game1.player.isMarried() && friendship.IsDating()) + { + text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11639") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11640")); + } + else if (__instance.getFriendship(name).IsDivorced()) + { + text2 = ((__instance.getGender(name) == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11642") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11643")); + } + + toSpeak = $"{toSpeak}, {text2}"; + } + + if (!__instance.getFriendship(name).IsMarried() && ___kidsNames.Contains(name)) + { + toSpeak = $"{toSpeak}, married"; + } + + if (spouse) + { + toSpeak = $"{toSpeak}, spouse"; + } + else if (friendship.IsDating()) + { + toSpeak = $"{toSpeak}, dating"; + } + + toSpeak = $"{toSpeak}, {heartLevel} hearts, {giftsThisWeek} gifts given this week."; + + if (socialPageQuery != toSpeak) + { + socialPageQuery = toSpeak; + MainClass.ScreenReader.Say(toSpeak, true); + } + return true; + } + + private static bool narrateFarmerDetails(SocialPage __instance, int i, List ___sprites, int x, int y) + { + long farmerID = (long)__instance.names[i]; + Farmer farmer = Game1.getFarmerMaybeOffline(farmerID); + if (farmer == null) + return false; + + int gender = (!farmer.IsMale) ? 1 : 0; + ClickableTextureComponent clickableTextureComponent = ___sprites[i]; + if (!clickableTextureComponent.containsPoint(x, y)) + return false; + + Friendship friendship = Game1.player.team.GetFriendship(Game1.player.UniqueMultiplayerID, farmerID); + bool spouse = friendship.IsMarried(); + string toSpeak = ""; + + string text2 = (LocalizedContentManager.CurrentLanguageCode != LocalizedContentManager.LanguageCode.pt) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635") : ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').First() : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11635").Split('/').Last()); + if (spouse) + { + text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11636") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11637")); + } + else if (farmer.isMarried() && !farmer.hasRoommate()) + { + text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_MaleNPC") : Game1.content.LoadString("Strings\\UI:SocialPage_MarriedToOtherPlayer_FemaleNPC")); + } + else if (!Game1.player.isMarried() && friendship.IsDating()) + { + text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11639") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11640")); + } + else if (friendship.IsDivorced()) + { + text2 = ((gender == 0) ? Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11642") : Game1.content.LoadString("Strings\\StringsFromCSFiles:SocialPage.cs.11643")); + } + + toSpeak = $"{farmer.displayName}, {text2}"; + + if (socialPageQuery != toSpeak) + { + socialPageQuery = toSpeak; + MainClass.ScreenReader.Say(toSpeak, true); + } + return true; + } + internal static void Cleanup() { socialPageQuery = "";