using System; using System.Collections; using System.Collections.Generic; using LitJson; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class FileItem : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler { public Toggle mainToggle; public Text nameText; public Button deleteBtn; public Image iconImage; private FileConfig fileConfig; public Sprite picIcon; public Sprite mp4Icon; public Sprite pdfIcon; public Sprite zipIcon; public Sprite unkownIcon; void Start() { mainToggle.onValueChanged.AddListener(ClickOnMainToggle); deleteBtn.onClick.AddListener(ClickOnDeleteBtn); //RoomFile.OnFileListAction += DeletFile; RoomFile.ClickOnPrefabAction += ClickOnPrefab; RoomFile.HidefileChooseAction += HideFileChoose; WSHandler.Rtc.OnDeleteFileSucess += DeleteFileSucess; WSHandler.Rtc.OnDeleteFile += NotifyDeleteFile; RoomFile.SetIsOnFalse += SetIsOn; } private void SetIsOn() { if (mainToggle.isOn) { mainToggle.isOn = false; } } private void HideFileChoose(FileConfig fileConfig) { if (this.fileConfig != null) { if (this.fileConfig == fileConfig) { mainToggle.isOn = false; } } } private void ClickOnPrefab(FileConfig fileConfig) { if (this.fileConfig != null) { if (this.fileConfig == fileConfig) { mainToggle.isOn = true; } else { mainToggle.isOn = false; } } } private void OnDestroy() { //RoomFile.OnFileListAction -= DeletFile; RoomFile.ClickOnPrefabAction -= ClickOnPrefab; RoomFile.HidefileChooseAction -= HideFileChoose; WSHandler.Rtc.OnDeleteFileSucess -= DeleteFileSucess; WSHandler.Rtc.OnDeleteFile -= NotifyDeleteFile; RoomFile.SetIsOnFalse -= SetIsOn; } private void DeleteFileSucess(JsonData data) { if (data["data"]["code"].ToString() == "200") { string path = data["data"]["data"]["path"].ToString(); if (!string.IsNullOrEmpty(path)) { if (this.fileConfig != null && this.fileConfig.Path == path) { //RoomFile.OnFilePrefabListAction?.Invoke(this.fileConfig); Destroy(this.gameObject); if (FileList.Instance) { FileList.Instance.RemoveItem(this.fileConfig); } } } } } private void NotifyDeleteFile(JsonData data) { if (this.fileConfig != null) { if (data["data"]["path"].ToString() == this.fileConfig.Path) { //RoomFile.OnFilePrefabListAction?.Invoke(this.fileConfig); Destroy(this.gameObject); if (FileList.Instance) { FileList.Instance.RemoveItem(this.fileConfig); } } } } private void ClickOnMainToggle(bool isOn) { if (isOn) { if (fileConfig != null) { RoomFile.ClickFileItemAction?.Invoke(fileConfig); } } else { if (fileConfig != null) { RoomFile.HideChooseKuangAction?.Invoke(fileConfig); } } } public void Init(FileConfig fileConfig) { mainToggle.group = transform.parent.GetComponent(); if (fileConfig != null) { this.fileConfig = fileConfig; if (fileConfig.FileName.Length <= 6) { nameText.text = fileConfig.FileName; } else { string str1 = fileConfig.FileName.Substring(0, 6); nameText.text = str1+"..."; } switch (fileConfig.FileType) { case RoomFileType.png: iconImage.sprite = picIcon; break; case RoomFileType.jpg: iconImage.sprite = picIcon; break; case RoomFileType.jpeg: iconImage.sprite = picIcon; break; case RoomFileType.mp4: iconImage.sprite = mp4Icon; break; case RoomFileType.zip: iconImage.sprite = zipIcon; break; case RoomFileType.pdf: iconImage.sprite = pdfIcon; break; case RoomFileType.unknown: iconImage.sprite = unkownIcon; break; } } } //private void DeletFile(FileConfig fileConfig) //{ // if (this.fileConfig != null && this.fileConfig == fileConfig) // { // Destroy(this.gameObject); // if (FileList.Instance) // { // FileList.Instance.RemoveItem(this.fileConfig); // } // } //} private void ClickOnDeleteBtn() { if (this.fileConfig != null) { deleteBtn.enabled = false; WSHandler.Rtc.deleteFile(this.fileConfig.Path); } } public void OnPointerEnter(PointerEventData eventData) { if (RoomMainInfo.isCreator == "0") { deleteBtn.gameObject.SetActive(true); } } public void OnPointerExit(PointerEventData eventData) { if (RoomMainInfo.isCreator == "0") { deleteBtn.gameObject.SetActive(false); } } }