Skip to content

Commit

Permalink
[ChatBox] Fixed ChatBoxes getting stuck and Change ChatBoxes to use t…
Browse files Browse the repository at this point in the history
…he same shader as nameplates (they will rotate the same way now)
  • Loading branch information
kafeijao committed Apr 17, 2024
1 parent acbda8d commit ce2f7e0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
63 changes: 57 additions & 6 deletions ChatBox/ChatBoxBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ public class ChatBoxBehavior : MonoBehaviour {
private const float NameplateOffsetYTyping = -65f;
private const float NameplateOffsetYTypingMultiplier = 25f;

private static Coroutine _resetTextAfterDelayCoroutine;
private static Coroutine _resetTypingAfterDelayCoroutine;
private Coroutine _resetTextAfterDelayCoroutine;
private Coroutine _resetTypingAfterDelayCoroutine;

private static readonly int FadeStartDistance = Shader.PropertyToID("_FadeStartDistance");
private static readonly int FadeEndDistance = Shader.PropertyToID("_FadeEndDistance");
private static readonly int PerspectiveFilter = Shader.PropertyToID("_PerspectiveFilter");

private readonly List<Material> _materialsToCleanup = [];

// Config updates
private static float _volume;
Expand Down Expand Up @@ -188,21 +194,51 @@ void SetCustomRolloff(bool isGlobal) {
});
}

private void ApplyNameplateMaterial(Image img, Material sourceMaterial) {
var newMat = new Material(sourceMaterial);
// MelonLogger.Msg($"src: {sourceMaterial.name}, new: {newMat.name}");
_materialsToCleanup.Add(newMat);
newMat.SetFloat(FadeStartDistance, 0f);
newMat.SetFloat(FadeEndDistance, 0f);
newMat.SetFloat(PerspectiveFilter, 0f);
img.material = newMat;
}

private void ApplyNameplateMaterial(TextMeshProUGUI tmpTrg, TMP_Text tmpSrc) {

var newMat = new Material(tmpTrg.m_sharedMaterial);
_materialsToCleanup.Add(newMat);
newMat.shader = tmpSrc.m_sharedMaterial.shader;

newMat.SetFloat(FadeStartDistance, 0f);
newMat.SetFloat(FadeEndDistance, 0f);
newMat.SetFloat(PerspectiveFilter, 0f);

tmpTrg.m_sharedMaterial = newMat;
tmpTrg.fontSharedMaterial = newMat;
}

private void Start() {

_nameplate = transform.GetComponent<PlayerNameplate>();
_playerGuid = _nameplate.player.ownerId;

// Setup the game object
_root = Instantiate(ModConfig.ChatBoxPrefab, transform);
// prefab.layer = LayerMask.NameToLayer("UI Internal");

_root.name = $"[{nameof(ChatBox)} Mod]";
_root.transform.rotation = _nameplate.transform.rotation;

// Copy layer from the nameplate canvas
_root.layer = _nameplate._canvasGroup.gameObject.layer;

var nameplateBackgroundMaterial = _nameplate.nameplateBackground.material;

_root.transform.localPosition = Vector3.zero;
_root.transform.rotation = _nameplate.transform.rotation;
_root.transform.localScale = ChatBoxDefaultLocalScale;

// Handle the chat box position and scale
_root.AddComponent<CameraFacingObject>();
// _root.AddComponent<CameraFacingObject>();

// Add Canvas Group
_canvasGroup = _root.GetComponent<CanvasGroup>();
Expand All @@ -216,17 +252,26 @@ private void Start() {
_typingGo = _typingTransform.gameObject;
_typingBackground = _typingGo.transform.GetChild(0).GetComponent<Image>();
_typingBackground.color = Green;
ApplyNameplateMaterial(_typingBackground, nameplateBackgroundMaterial);
for (var i = 0; i < _typingBackground.transform.childCount; i++) {
_typingGoChildren.Add(_typingBackground.transform.GetChild(i).gameObject);
var typingGoChild = _typingBackground.transform.GetChild(i).gameObject;
_typingGoChildren.Add(typingGoChild);
ApplyNameplateMaterial(typingGoChild.GetComponent<Image>(), nameplateBackgroundMaterial);
}

// Text Bubble
_textBubbleGo = _root.transform.Find(ChildTextBubbleName).gameObject;
_textBubbleTransform = _textBubbleGo.transform;
var tmpGo = _textBubbleTransform.Find(ChildTextBubbleOutputName);

_textBubbleHexagonImg = _textBubbleTransform.Find(ChildTextBubbleHexagonName).GetComponent<Image>();
ApplyNameplateMaterial(_textBubbleHexagonImg, nameplateBackgroundMaterial);

_textBubbleRoundImg = _textBubbleTransform.Find(ChildTextBubbleRoundName).GetComponent<Image>();
ApplyNameplateMaterial(_textBubbleRoundImg, nameplateBackgroundMaterial);

_textBubbleOutputTMP = tmpGo.GetComponent<TextMeshProUGUI>();
ApplyNameplateMaterial(_textBubbleOutputTMP, _nameplate.usrNameText);

// Needed to prevent funny crashes :c
_textBubbleOutputTMP.richText = false;
Expand Down Expand Up @@ -266,6 +311,12 @@ private void Start() {

private void OnDestroy() {
if (ChatBoxes.ContainsKey(_playerGuid)) ChatBoxes.Remove(_playerGuid);

// Clean the materials we instantiated
foreach (var material in _materialsToCleanup.Where(material => material != null)) {
Destroy(material);
}
_materialsToCleanup.Clear();
}

private void StopTyping() {
Expand Down
1 change: 1 addition & 0 deletions ChatBox/HistoryBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Globalization;
using System.Text.RegularExpressions;
using ABI_RC.Core;
using ABI_RC.Core.AudioEffects;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Networking;
Expand Down
2 changes: 1 addition & 1 deletion ChatBox/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace Kafe.ChatBox.Properties;
internal static class AssemblyInfoParams {
public const string Version = "1.0.16";
public const string Version = "1.0.17";
public const string Author = "kafeijao";
public const string AstroDoge = "AstroDoge";
public const string BTKUILibName = "BTKUILib";
Expand Down
4 changes: 2 additions & 2 deletions ChatBox/Properties/CVRMG.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_id": 168,
"name": "ChatBox",
"modversion": "1.0.16",
"modversion": "1.0.17",
"gameversion": "2024r175",
"loaderversion": "v0.6.1",
"modtype": "Mod",
Expand All @@ -17,6 +17,6 @@
"requirements": ["[BTKUILib](https://api.cvrmg.com/v1/mods/download/113)"],
"downloadlink": "https://github.com/kafeijao/Kafe_CVR_Mods/releases/download/r67/ChatBox.dll",
"sourcelink": "https://github.com/kafeijao/Kafe_CVR_Mods/tree/master/ChatBox",
"changelog": "- Fixed for 2024r175",
"changelog": "- Fixed ChatBoxes getting stuck\n- Change ChatBoxes to use the same shader as nameplates (they will rotate the same way now).",
"embedcolor": "16C60C"
}

0 comments on commit ce2f7e0

Please sign in to comment.