PlayLog.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. Debug.Log("INIT LOG");
  88. OnAddLog += AddLogString;
  89. OnLinkPressed += ShowCard;
  90. }
  91. public void AddLogString(string logText)
  92. {
  93. AddLogStringCoroutine(DataBase.ReplaceToASCII(logText));
  94. }
  95. void AddLogStringCoroutine(string log)
  96. {
  97. Debug.Log("ADD LOG STRING");
  98. _logList.Add(AddLink(log));
  99. while (GetLogString().Length >= _maxLogCharacterLength)
  100. {
  101. if (_logList.Count >= 1)
  102. {
  103. _logList.RemoveAt(0);
  104. }
  105. }
  106. _logText.text = GetLogString();
  107. }
  108. string AddLink(string log)
  109. {
  110. List<int> startIndex = AllIndexesOf(log, "(");
  111. List<int> endIndex = AllIndexesOf(log, ")");
  112. List<string> subStrings = new List<string>();
  113. for(int i = 0; i < startIndex.Count; i++)
  114. {
  115. if (startIndex[i] < 0 && endIndex[i] < 0)
  116. continue;
  117. startIndex[i] += 1;
  118. string str = log.Substring(startIndex[i], endIndex[i] - startIndex[i]);
  119. if(!subStrings.Contains(str))
  120. subStrings.Add(str);
  121. }
  122. foreach(string str in subStrings)
  123. log = log.Replace(str, $"<link={str}><color=#92F6FF><u>{str}</u></color></link>");
  124. return log;
  125. }
  126. void ShowCard(string cardID)
  127. {
  128. CardSource founcdCardSource = GManager.instance.turnStateMachine.gameContext.ActiveCardList
  129. .Find(cardSource1 => cardSource1.CardID == cardID);
  130. if (founcdCardSource != null)
  131. {
  132. GManager.instance.cardDetail.OpenCardDetail(founcdCardSource, true);
  133. }
  134. }
  135. //Might need to move this more relavent
  136. List<int> AllIndexesOf(string str, string value)
  137. {
  138. if (String.IsNullOrEmpty(value))
  139. throw new ArgumentException("the string to find may not be empty", "value");
  140. List<int> indexes = new List<int>();
  141. for (int index = 0; ; index += value.Length)
  142. {
  143. index = str.IndexOf(value, index);
  144. if (index == -1)
  145. return indexes;
  146. indexes.Add(index);
  147. }
  148. }
  149. }