FileItem.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using LitJson;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class FileItem : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
  9. {
  10. public Toggle mainToggle;
  11. public Text nameText;
  12. public Button deleteBtn;
  13. public Image iconImage;
  14. private FileConfig fileConfig;
  15. public Sprite picIcon;
  16. public Sprite mp4Icon;
  17. public Sprite pdfIcon;
  18. public Sprite zipIcon;
  19. public Sprite unkownIcon;
  20. void Start()
  21. {
  22. mainToggle.onValueChanged.AddListener(ClickOnMainToggle);
  23. deleteBtn.onClick.AddListener(ClickOnDeleteBtn);
  24. //RoomFile.OnFileListAction += DeletFile;
  25. RoomFile.ClickOnPrefabAction += ClickOnPrefab;
  26. RoomFile.HidefileChooseAction += HideFileChoose;
  27. WSHandler.Rtc.OnDeleteFileSucess += DeleteFileSucess;
  28. WSHandler.Rtc.OnDeleteFile += NotifyDeleteFile;
  29. RoomFile.SetIsOnFalse += SetIsOn;
  30. }
  31. private void SetIsOn()
  32. {
  33. if (mainToggle.isOn)
  34. {
  35. mainToggle.isOn = false;
  36. }
  37. }
  38. private void HideFileChoose(FileConfig fileConfig)
  39. {
  40. if (this.fileConfig != null)
  41. {
  42. if (this.fileConfig == fileConfig)
  43. {
  44. mainToggle.isOn = false;
  45. }
  46. }
  47. }
  48. private void ClickOnPrefab(FileConfig fileConfig)
  49. {
  50. if (this.fileConfig != null)
  51. {
  52. if (this.fileConfig == fileConfig)
  53. {
  54. mainToggle.isOn = true;
  55. }
  56. else
  57. {
  58. mainToggle.isOn = false;
  59. }
  60. }
  61. }
  62. private void OnDestroy()
  63. {
  64. //RoomFile.OnFileListAction -= DeletFile;
  65. RoomFile.ClickOnPrefabAction -= ClickOnPrefab;
  66. RoomFile.HidefileChooseAction -= HideFileChoose;
  67. WSHandler.Rtc.OnDeleteFileSucess -= DeleteFileSucess;
  68. WSHandler.Rtc.OnDeleteFile -= NotifyDeleteFile;
  69. RoomFile.SetIsOnFalse -= SetIsOn;
  70. }
  71. private void DeleteFileSucess(JsonData data)
  72. {
  73. if (data["data"]["code"].ToString() == "200")
  74. {
  75. string path = data["data"]["data"]["path"].ToString();
  76. if (!string.IsNullOrEmpty(path))
  77. {
  78. if (this.fileConfig != null && this.fileConfig.Path == path)
  79. {
  80. //RoomFile.OnFilePrefabListAction?.Invoke(this.fileConfig);
  81. Destroy(this.gameObject);
  82. if (FileList.Instance)
  83. {
  84. FileList.Instance.RemoveItem(this.fileConfig);
  85. }
  86. }
  87. }
  88. }
  89. }
  90. private void NotifyDeleteFile(JsonData data)
  91. {
  92. if (this.fileConfig != null)
  93. {
  94. if (data["data"]["path"].ToString() == this.fileConfig.Path)
  95. {
  96. //RoomFile.OnFilePrefabListAction?.Invoke(this.fileConfig);
  97. Destroy(this.gameObject);
  98. if (FileList.Instance)
  99. {
  100. FileList.Instance.RemoveItem(this.fileConfig);
  101. }
  102. }
  103. }
  104. }
  105. private void ClickOnMainToggle(bool isOn)
  106. {
  107. if (isOn)
  108. {
  109. if (fileConfig != null)
  110. {
  111. RoomFile.ClickFileItemAction?.Invoke(fileConfig);
  112. }
  113. }
  114. else
  115. {
  116. if (fileConfig != null)
  117. {
  118. RoomFile.HideChooseKuangAction?.Invoke(fileConfig);
  119. }
  120. }
  121. }
  122. public void Init(FileConfig fileConfig)
  123. {
  124. mainToggle.group = transform.parent.GetComponent<ToggleGroup>();
  125. if (fileConfig != null)
  126. {
  127. this.fileConfig = fileConfig;
  128. if (fileConfig.FileName.Length <= 6)
  129. {
  130. nameText.text = fileConfig.FileName;
  131. }
  132. else
  133. {
  134. string str1 = fileConfig.FileName.Substring(0, 6);
  135. nameText.text = str1+"...";
  136. }
  137. switch (fileConfig.FileType)
  138. {
  139. case RoomFileType.png:
  140. iconImage.sprite = picIcon;
  141. break;
  142. case RoomFileType.jpg:
  143. iconImage.sprite = picIcon;
  144. break;
  145. case RoomFileType.jpeg:
  146. iconImage.sprite = picIcon;
  147. break;
  148. case RoomFileType.mp4:
  149. iconImage.sprite = mp4Icon;
  150. break;
  151. case RoomFileType.zip:
  152. iconImage.sprite = zipIcon;
  153. break;
  154. case RoomFileType.pdf:
  155. iconImage.sprite = pdfIcon;
  156. break;
  157. case RoomFileType.unknown:
  158. iconImage.sprite = unkownIcon;
  159. break;
  160. }
  161. }
  162. }
  163. //private void DeletFile(FileConfig fileConfig)
  164. //{
  165. // if (this.fileConfig != null && this.fileConfig == fileConfig)
  166. // {
  167. // Destroy(this.gameObject);
  168. // if (FileList.Instance)
  169. // {
  170. // FileList.Instance.RemoveItem(this.fileConfig);
  171. // }
  172. // }
  173. //}
  174. private void ClickOnDeleteBtn()
  175. {
  176. if (this.fileConfig != null)
  177. {
  178. deleteBtn.enabled = false;
  179. WSHandler.Rtc.deleteFile(this.fileConfig.Path);
  180. }
  181. }
  182. public void OnPointerEnter(PointerEventData eventData)
  183. {
  184. if (RoomMainInfo.isCreator == "0")
  185. {
  186. deleteBtn.gameObject.SetActive(true);
  187. }
  188. }
  189. public void OnPointerExit(PointerEventData eventData)
  190. {
  191. if (RoomMainInfo.isCreator == "0")
  192. {
  193. deleteBtn.gameObject.SetActive(false);
  194. }
  195. }
  196. }