FileItem.cs 5.6 KB

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