ToolDisplayForm.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using QFramework;
  4. using System.Collections.Generic;
  5. namespace QFramework
  6. {
  7. public class ToolDisplayFormData : UIPanelData
  8. {
  9. public List<ToolConfig> m_ToolConfigs = new List<ToolConfig>();
  10. }
  11. public partial class ToolDisplayForm : UIPanel
  12. {
  13. /// <summary>
  14. /// 包含所有生成的工具元素
  15. /// </summary>
  16. List<ToolItemElement> m_ToolItemElements = new List<ToolItemElement>();
  17. protected override void ProcessMsg(int eventId, QMsg msg)
  18. {
  19. throw new System.NotImplementedException();
  20. }
  21. protected override void OnInit(IUIData uiData = null)
  22. {
  23. mData = uiData as ToolDisplayFormData ?? new ToolDisplayFormData();
  24. }
  25. protected override void OnOpen(IUIData uiData = null)
  26. {
  27. mData = uiData as ToolDisplayFormData ?? new ToolDisplayFormData();
  28. HideAllToolItemElements();
  29. if (mData.m_ToolConfigs == null || mData.m_ToolConfigs.Count == 0)
  30. {
  31. ToolDisplayList.gameObject.SetActive(false);
  32. Bottom.gameObject.SetActive(false);
  33. return;
  34. }else
  35. {
  36. ToolDisplayList.gameObject.SetActive(true);
  37. Bottom.gameObject.SetActive(true);
  38. }
  39. GenerateToolDisplay(mData.m_ToolConfigs);
  40. }
  41. protected override void OnShow()
  42. {
  43. }
  44. protected override void OnHide()
  45. {
  46. }
  47. protected override void OnClose()
  48. {
  49. }
  50. private void GenerateToolDisplay(List<ToolConfig> toolConfigs)
  51. {
  52. HideAllToolItemElements();
  53. foreach (ToolConfig toolConfig in toolConfigs)
  54. {
  55. ToolItemElement toolItemElement = GetToolItemElement();
  56. toolItemElement.InitData(toolConfig);
  57. toolItemElement.gameObject.SetActive(true);
  58. }
  59. ToolConent.gameObject.SetActive(GetToolItemElementActiveCount() > 0);
  60. Mask.gameObject.SetActive(GetToolItemElementActiveCount() >= 7);
  61. }
  62. /// <summary>
  63. /// 获取一个可用的ToolItemElement
  64. /// </summary>
  65. /// <returns></returns>
  66. private ToolItemElement GetToolItemElement()
  67. {
  68. ToolItemElement toolItemElement = null;
  69. toolItemElement = m_ToolItemElements.Find(t => t.gameObject.activeSelf == false);
  70. if (toolItemElement == null)
  71. {
  72. GameObject toolElement = Instantiate<GameObject>(ToolItemElement.gameObject, Conent.transform);
  73. toolItemElement = toolElement.GetComponent<ToolItemElement>();
  74. m_ToolItemElements.Add(toolItemElement);
  75. }
  76. return toolItemElement;
  77. }
  78. private void HideAllToolItemElements()
  79. {
  80. foreach (var item in m_ToolItemElements)
  81. {
  82. item.gameObject.SetActive(false);
  83. }
  84. ToolConent.gameObject.SetActive(false);
  85. }
  86. public int GetToolItemElementActiveCount()
  87. {
  88. return m_ToolItemElements.FindAll(t => t.gameObject.activeSelf).Count;
  89. }
  90. }
  91. }