PlayLog.cs 4.2 KB

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