Skip to content

Commit

Permalink
Fix crash when trying to add a playback button to the space sceen
Browse files Browse the repository at this point in the history
  • Loading branch information
Osmodium committed Jan 18, 2024
1 parent dfe13b1 commit b399d9b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
By [Osmodium](https://github.com/Osmodium)

## This mod is made for Warhammer 40K: Rogue Trader and introduces TTS (TextToSpeech) in most places.
Version: 0.9.2
Version: 0.9.3

**Disclaimer: UNDER DEVELOPMENT**

Expand Down
2 changes: 1 addition & 1 deletion SpeechMod/Info.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"AssemblyName": "W40KRTSpeechMod.dll",
"EntryMethod": "SpeechMod.Main.Load",
"HomePage": "https://www.nexusmods.com/warhammer40kroguetrader/mods/75",
"Version": "0.9.2"
"Version": "0.9.3"
}
2 changes: 1 addition & 1 deletion SpeechMod/Patches/DialogAnswerBaseView_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void TryAddDialogButton(TextMeshProUGUI textMeshPro, DialogAnswer
return;

// 3. We want the button but it doesn't exist.
playButtonGameObject = ButtonFactory.CreatePlayButton(transform, () =>
playButtonGameObject = ButtonFactory.TryCreatePlayButton(transform, () =>
{
if (textMeshPro == null)
return;
Expand Down
8 changes: 4 additions & 4 deletions SpeechMod/Patches/Encyclopedia_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static void AddTitleHook(EncyclopediaPageBaseView __instance)
#endif

__instance.m_Title.HookupTextToSpeech();
__instance.m_PageAdditionText.TryAddButton(PAGE_VIEW_ADDITION_BUTTON_NAME, new Vector2(24f, -4f), new Vector3(0.8f, 0.8f, 1f));
__instance.m_GlossaryEntryBlockPrefab?.m_Description.TryAddButton(PAGE_VIEW_GLOSSARY_BUTTON_NAME, new Vector2(0f, -4f), new Vector3(0.8f, 0.8f, 1f));
__instance.m_PageAdditionText.TryAddButtonToTextMeshPro(PAGE_VIEW_ADDITION_BUTTON_NAME, new Vector2(24f, -4f), new Vector3(0.8f, 0.8f, 1f));
__instance.m_GlossaryEntryBlockPrefab?.m_Description.TryAddButtonToTextMeshPro(PAGE_VIEW_GLOSSARY_BUTTON_NAME, new Vector2(0f, -4f), new Vector3(0.8f, 0.8f, 1f));
}

[HarmonyPatch(typeof(EncyclopediaPageBlockTextPCView), "BindViewImplementation")]
Expand All @@ -40,7 +40,7 @@ public static void AddPageBlockTextHook(EncyclopediaPageBlockTextPCView __instan
#if DEBUG
Debug.Log($"{nameof(EncyclopediaPageBlockTextPCView)}_BindViewImplementation_Postfix");
#endif
__instance.m_Text.TryAddButton(TEXT_VIEW_GLOSSARY_BUTTON_NAME, new Vector2(-1f, -4f), new Vector3(0.8f, 0.8f, 1f));
__instance.m_Text.TryAddButtonToTextMeshPro(TEXT_VIEW_GLOSSARY_BUTTON_NAME, new Vector2(-1f, -4f), new Vector3(0.8f, 0.8f, 1f));
}

[HarmonyPatch(typeof(EncyclopediaPageBlockGlossaryEntryPCView), "BindViewImplementation")]
Expand All @@ -54,6 +54,6 @@ public static void AddPageBlockGlossaryHooks(EncyclopediaPageBlockGlossaryEntryP
#endif

__instance.m_Title.HookupTextToSpeech();
__instance.m_Description.TryAddButton(BLOCK_VIEW_GLOSSARY_BUTTON_NAME, new Vector2(-2f, -4f), new Vector3(0.8f, 0.8f, 1f));
__instance.m_Description.TryAddButtonToTextMeshPro(BLOCK_VIEW_GLOSSARY_BUTTON_NAME, new Vector2(-2f, -4f), new Vector3(0.8f, 0.8f, 1f));
}
}
2 changes: 1 addition & 1 deletion SpeechMod/Patches/JournalQuest_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void AddButtonsToParts(JournalQuestObjectiveBaseView __instance)
Debug.Log($"{nameof(JournalQuestObjectiveBaseView)}_{nameof(JournalQuestObjectiveBaseView.BindViewImplementation)}_Postfix");
#endif

__instance.m_Title.TryAddButton(BUTTON_NAME, new Vector2(18f, -11f), new Vector3(0.8f, 0.8f, 1f));
__instance.m_Title.TryAddButtonToTextMeshPro(BUTTON_NAME, new Vector2(18f, -11f), new Vector3(0.8f, 0.8f, 1f));
__instance.m_Description.HookupTextToSpeech();
__instance.m_Destination.HookupTextToSpeech();
__instance.m_EtudeCounter.HookupTextToSpeech();
Expand Down
2 changes: 1 addition & 1 deletion SpeechMod/Patches/ShipPCView_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void AddHoks(ShipPCView __instance)
__instance.m_ShipName.HookupTextToSpeech();

var textMeshProUguis = new[] { __instance.m_ExperienceText, __instance.m_Experience, __instance.m_LevelText, __instance.m_Level };
__instance.m_ExperienceText.TryAddButton("SpeechMod_ExpButton", new Vector2(0f, 0f), new Vector3(0.8f, 0.8f, 1f), textMeshProUguis);
__instance.m_ExperienceText.TryAddButtonToTextMeshPro("SpeechMod_ExpButton", new Vector2(0f, 0f), new Vector3(0.8f, 0.8f, 1f), textMeshProUguis);
}

[HarmonyPatch(typeof(ShipComponentSlotBaseView<ItemSlotBaseView>), nameof(ShipComponentSlotBaseView<ItemSlotBaseView>.BindViewImplementation))]
Expand Down
7 changes: 6 additions & 1 deletion SpeechMod/Patches/SurfaceStaticPartPCView_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,16 @@ private static void AddDialogSpeechButton(string path)
return;
}

var buttonGameObject = ButtonFactory.CreatePlayButton(parent, () =>
var buttonGameObject = ButtonFactory.TryCreatePlayButton(parent, () =>
{
Main.Speech.SpeakDialog(Game.Instance?.DialogController?.CurrentCue?.DisplayText);
});

if (buttonGameObject == null)
{
return;
}

buttonGameObject.name = "SpeechMod_DialogButton";
buttonGameObject.RectAlignTopLeft(new Vector2(40, 10));
buttonGameObject.transform.localRotation = Quaternion.Euler(0, 0, 270);
Expand Down
34 changes: 20 additions & 14 deletions SpeechMod/Unity/ButtonFactory.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using JetBrains.Annotations;
using Kingmaker.Code.UI.MVVM.VM.Tooltip.Templates;
using Kingmaker.Code.UI.MVVM.VM.Tooltip.Templates;
using Kingmaker.Code.UI.MVVM.VM.Tooltip.Utils;
using Owlcat.Runtime.UI.Controls.Button;
using SpeechMod.Unity.Extensions;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using Object = UnityEngine.Object;

namespace SpeechMod.Unity;

Expand All @@ -30,16 +30,20 @@ private static void TryTakeArrowBackup()
return;

m_ArrowButtonPrefab = Object.Instantiate(ArrowButton);
if (m_ArrowButtonPrefab == null)
return;
Object.DontDestroyOnLoad(m_ArrowButtonPrefab);
}

public static GameObject CreatePlayButton(Transform parent, UnityAction action)
public static GameObject TryCreatePlayButton(Transform parent, UnityAction action)
{
return CreatePlayButton(parent, action, null, null);
}

private static GameObject CreatePlayButton(Transform parent, UnityAction action, string text, string toolTip)
{
GameObject buttonGameObject = null;

if (ArrowButton == null)
{
#if DEBUG
Expand All @@ -53,28 +57,30 @@ private static GameObject CreatePlayButton(Transform parent, UnityAction action,
return null;
}

var prefabButtonGameObject = Object.Instantiate(m_ArrowButtonPrefab, parent);
SetupButton(prefabButtonGameObject, action, text, toolTip);
return prefabButtonGameObject;
buttonGameObject = Object.Instantiate(m_ArrowButtonPrefab, parent);
}
else
{
TryTakeArrowBackup();
buttonGameObject = Object.Instantiate(ArrowButton, parent);
}

TryTakeArrowBackup();

var buttonGameObject = Object.Instantiate(ArrowButton, parent);
SetupButton(buttonGameObject, action, text, toolTip);

return buttonGameObject;
}

private static void SetupButton(GameObject buttonGameObject, UnityAction action, string text, string toolTip)
{
var button = buttonGameObject!.GetComponent<OwlcatMultiButton>();
if (buttonGameObject == null)
return;

var button = buttonGameObject.GetComponent<OwlcatMultiButton>();
if (button == null)
{
button = buttonGameObject.AddComponent<OwlcatMultiButton>();
}

button.OnLeftClick.RemoveAllListeners();
button!.OnLeftClick!.RemoveAllListeners();
button.OnLeftClick.AddListener(action);

if (!string.IsNullOrWhiteSpace(text))
Expand All @@ -83,7 +89,7 @@ private static void SetupButton(GameObject buttonGameObject, UnityAction action,
button.SetInteractable(true);
}

public static GameObject TryAddButton(this TextMeshProUGUI textMeshPro, string buttonName, Vector2? anchoredPosition = null, Vector3? scale = null, [CanBeNull] TextMeshProUGUI[] textMeshProUguis = null)
public static GameObject TryAddButtonToTextMeshPro(this TextMeshProUGUI textMeshPro, string buttonName, Vector2? anchoredPosition = null, Vector3? scale = null, TextMeshProUGUI[] textMeshProUguis = null)
{
var transform = textMeshPro?.transform;
var tmpButton = transform.TryFind(buttonName)?.gameObject;
Expand All @@ -94,7 +100,7 @@ public static GameObject TryAddButton(this TextMeshProUGUI textMeshPro, string b
Debug.Log($"Adding playbutton to {textMeshPro?.name}...");
#endif

var button = CreatePlayButton(transform, () =>
var button = TryCreatePlayButton(transform, () =>
{
var text = textMeshPro?.text;
if (textMeshProUguis != null)
Expand Down

0 comments on commit b399d9b

Please sign in to comment.