PlayLog.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. using System;
  7. public class PlayLog : MonoBehaviour
  8. {
  9. [SerializeField]
  10. TMP_Text _logText;
  11. [SerializeField]
  12. ScrollRect _scroll;
  13. #region Events
  14. public static Action<string> OnAddLog;
  15. public static Action<string> OnLinkPressed;
  16. #endregion
  17. private void OnDestroy()
  18. {
  19. OnAddLog -= AddLogString;
  20. OnLinkPressed -= ShowCard;
  21. }
  22. string GetLogString()
  23. {
  24. string logString = "";
  25. foreach (string log in _logList)
  26. {
  27. logString += log;
  28. }
  29. return logString;
  30. }
  31. List<string> _logList = new List<string>();
  32. //16250, 13000
  33. int _maxLogCharacterLength = 11000;
  34. public void OnClickLiogButton()
  35. {
  36. if (gameObject.activeSelf)
  37. {
  38. OffPlayLog();
  39. }
  40. else
  41. {
  42. SetUpPlayLog();
  43. }
  44. }
  45. public void SetUpPlayLog()
  46. {
  47. ContinuousController.instance.StartCoroutine(SetUpPlayLogCoroutine());
  48. }
  49. IEnumerator SetUpPlayLogCoroutine()
  50. {
  51. this.gameObject.SetActive(true);
  52. if (Opening.instance != null)
  53. {
  54. Opening.instance.PlayDecisionSE();
  55. }
  56. else if (GManager.instance != null)
  57. {
  58. GManager.instance.PlayDecisionSE();
  59. }
  60. _logText.text = GetLogString();
  61. _scroll.content.GetComponent<ContentSizeFitter>().SetLayoutVertical();
  62. yield return new WaitForSeconds(Time.deltaTime);
  63. _scroll.verticalNormalizedPosition = 0;
  64. }
  65. bool _first = false;
  66. public void OffPlayLog()
  67. {
  68. if (_first)
  69. {
  70. if (Opening.instance != null)
  71. {
  72. Opening.instance.PlayCancelSE();
  73. }
  74. else if (GManager.instance != null)
  75. {
  76. GManager.instance.PlayCancelSE();
  77. }
  78. }
  79. _first = true;
  80. gameObject.SetActive(false);
  81. }
  82. public void Init()
  83. {
  84. OffPlayLog();
  85. _logText.text = "";
  86. _logList = new List<string>();
  87. OnAddLog += AddLogString;
  88. OnLinkPressed += ShowCard;
  89. }
  90. public void AddLogString(string logText)
  91. {
  92. ContinuousController.instance.StartCoroutine(AddLogStringCoroutine(DataBase.ReplaceToASCII(logText)));
  93. }
  94. IEnumerator AddLogStringCoroutine(string log)
  95. {
  96. _logList.Add(AddLink(log));
  97. while (GetLogString().Length >= _maxLogCharacterLength)
  98. {
  99. if (_logList.Count >= 1)
  100. {
  101. _logList.RemoveAt(0);
  102. }
  103. }
  104. if (gameObject.activeSelf)
  105. {
  106. _logText.text = GetLogString();
  107. yield break;
  108. }
  109. }
  110. string AddLink(string log)
  111. {
  112. List<int> startIndex = AllIndexesOf(log, "(");
  113. List<int> endIndex = AllIndexesOf(log, ")");
  114. List<string> subStrings = new List<string>();
  115. for(int i = 0; i < startIndex.Count; i++)
  116. {
  117. if (startIndex[i] < 0 && endIndex[i] < 0)
  118. continue;
  119. startIndex[i] += 1;
  120. string str = log.Substring(startIndex[i], endIndex[i] - startIndex[i]);
  121. if(!subStrings.Contains(str))
  122. subStrings.Add(str);
  123. }
  124. foreach(string str in subStrings)
  125. log = log.Replace(str, $"<link={str}><color=#92F6FF><u>{str}</u></color></link>");
  126. return log;
  127. }
  128. void ShowCard(string cardID)
  129. {
  130. CardSource founcdCardSource = GManager.instance.turnStateMachine.gameContext.ActiveCardList
  131. .Find(cardSource1 => cardSource1.CardID == cardID);
  132. if (founcdCardSource != null)
  133. {
  134. GManager.instance.cardDetail.OpenCardDetail(founcdCardSource, true);
  135. }
  136. }
  137. //Might need to move this more relavent
  138. List<int> AllIndexesOf(string str, string value)
  139. {
  140. if (String.IsNullOrEmpty(value))
  141. throw new ArgumentException("the string to find may not be empty", "value");
  142. List<int> indexes = new List<int>();
  143. for (int index = 0; ; index += value.Length)
  144. {
  145. index = str.IndexOf(value, index);
  146. if (index == -1)
  147. return indexes;
  148. indexes.Add(index);
  149. }
  150. }
  151. }