MainMenuController.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using SimpleJSON;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using WS;
  9. [ObjectSystem]
  10. public class MainMenuControllerUpdateSystem : UpdateSystem<MainMenuController>
  11. {
  12. public override void Update(MainMenuController self)
  13. {
  14. self.Update();
  15. }
  16. }
  17. /// <summary> 课程选项数据 </summary>
  18. class CourseItemDate
  19. {
  20. /// <summary> 课程索引 </summary>
  21. public int courseIndex;
  22. /// <summary> 课程名称 </summary>
  23. public string name;
  24. /// <summary> 课程类型 </summary>
  25. public CoursType coursType;
  26. }
  27. public class MainMenuController : Controller<MainMenuView, MainMenuModel>
  28. {
  29. List<CoursType> courses = new List<CoursType>
  30. {
  31. CoursType.中药检索,
  32. CoursType.中药学古文献,
  33. CoursType.药物采集,
  34. CoursType.药物炮制,
  35. CoursType.药物化学,
  36. CoursType.药物药理,
  37. CoursType.中药药剂,
  38. CoursType.中药资源与鉴定,
  39. CoursType.中药质量控制与分析,
  40. CoursType.药事管理法规,
  41. CoursType.中医药方剂
  42. };
  43. /// <summary> 当前页的课程类型列表 </summary>
  44. CoursType[] currentPageCourse = new CoursType[6];
  45. protected override void BindingViewModel()
  46. {
  47. _Model.currentPageIndex.Value = 1;
  48. refreshCourseList();
  49. ToggleGroupAddListener();
  50. _Model.Bind(_View.nextPageBtn,NextBtnClick);
  51. _Model.Bind(_View.lastPageBtn, LastBtnClick);
  52. }
  53. public override void OpenView()
  54. {
  55. base.OpenView();
  56. }
  57. public void Update()
  58. {
  59. if (_Model.currentPageIndex.Value == Math.Ceiling((double)courses.Count / 6))
  60. {
  61. _View.nextPageBtn.interactable = false;
  62. }
  63. else
  64. {
  65. _View.nextPageBtn.interactable = true;
  66. }
  67. if (_Model.currentPageIndex.Value == 1)
  68. {
  69. _View.lastPageBtn.interactable = false;
  70. }
  71. else
  72. {
  73. _View.lastPageBtn.interactable = true;
  74. }
  75. }
  76. void NextBtnClick()
  77. {
  78. _Model.currentPageIndex.Value++;
  79. refreshCourseList() ;
  80. }
  81. void LastBtnClick()
  82. {
  83. _Model.currentPageIndex.Value--;
  84. refreshCourseList() ;
  85. }
  86. /// <summary> 刷新页面数据 </summary>
  87. void refreshCourseList()
  88. {
  89. ReferenceCollector[] rcArray = _View.group.GetComponentsInChildren<ReferenceCollector>();
  90. for (int i = 0; i < rcArray.Length; i++)
  91. {
  92. int courseIndex = (_Model.currentPageIndex.Value - 1)*6+i;
  93. if (courseIndex>=courses.Count)
  94. {
  95. rcArray[i].GetComponent<CanvasGroup>().alpha = 0;
  96. }
  97. else
  98. {
  99. rcArray[i].GetComponent<CanvasGroup>().alpha=1;
  100. rcArray[i].GetComponent<TextMeshProUGUI>("Index").text = courseIndex.ToString();
  101. rcArray[i].GetComponent<TextMeshProUGUI>("CourseName").text = courses[courseIndex].ToString();
  102. currentPageCourse[i] = (CoursType)courseIndex;
  103. }
  104. }
  105. }
  106. void ToggleGroupAddListener()
  107. {
  108. Toggle[] toggles = _View.group.GetComponentsInChildren<Toggle>();
  109. for (int i = 0; i < toggles.Length; i++)
  110. {
  111. var index = i;
  112. var rc = toggles[index].GetComponent<ReferenceCollector>();
  113. rc.GetComponent<Button>("EnterBtn").onClick.AddListener(OnEnterBtnClick);
  114. toggles[index].onValueChanged.AddListener((isOn) =>
  115. {
  116. if (isOn)
  117. {
  118. _Model.onSelectCourse.Value = currentPageCourse[index];
  119. //Debug.Log(_Model.onSelectCourse.Value);
  120. }
  121. Color color;
  122. ColorUtility.TryParseHtmlString("#FFDC63",out color);
  123. rc.GetComponent<TextMeshProUGUI>("Index").color=isOn?color:Color.black;
  124. rc.GetComponent<TextMeshProUGUI>("CourseName").color = isOn ? color : Color.black;
  125. rc.Get<GameObject>("EnterBtn").SetActive(isOn);
  126. toggles[index].GetComponent<Canvas>().sortingOrder = isOn?1001:1000;
  127. });
  128. }
  129. toggles[0].isOn = true;
  130. }
  131. void OnEnterBtnClick()
  132. {
  133. Debug.Log(_Model.onSelectCourse.Value);
  134. if (_Model.onSelectCourse.Value==CoursType.中药检索)
  135. {
  136. SearchViewController searchView = FacadeComponent.Instance.CreateController<SearchViewController>();
  137. searchView.OpenView();
  138. }
  139. else
  140. {
  141. SearchTypeEntryController controller = FacadeComponent.Instance.CreateController<SearchTypeEntryController>();
  142. controller.OpenView();
  143. }
  144. CloseView();
  145. }
  146. }
  147. public class MainMenuView : View
  148. {
  149. public Transform group;
  150. public Button nextPageBtn;
  151. public Button lastPageBtn;
  152. List<string> courses = new List<string>
  153. {
  154. "中药检索","中药学古文献","药物化学","药物化学","药物化学"
  155. };
  156. public override void LoadInit()
  157. {
  158. var rc = UIGameObject.GetComponent<ReferenceCollector>();
  159. group = rc.GetComponent<Transform>("CourseList");
  160. nextPageBtn = rc.GetComponent<Button>("NextPageBtn");
  161. lastPageBtn = rc.GetComponent<Button>("LastPageBtn");
  162. }
  163. public void InitCourseList()
  164. {
  165. //var choiceItemPre = Resources.Load<GameObject>("UI/Common/Course");
  166. //for (int i = 0; i < courses.Count; i++)
  167. //{
  168. // var choiceItem = GameObject.Instantiate(choiceItemPre, group);
  169. // var rc = choiceItem.GetComponent<ReferenceCollector>();
  170. // TextMeshProUGUI courseName = rc.GetComponent<TextMeshProUGUI>("CourseName_Normal");
  171. // courseName.text = courses[i];
  172. // Toggle toggle = choiceItem.GetComponent<Toggle>();
  173. // toggle.group = group.GetComponent<ToggleGroup>();
  174. //}
  175. }
  176. }
  177. public class MainMenuModel : Model
  178. {
  179. /// <summary>
  180. /// 当前页索引
  181. /// </summary>
  182. public BindableProperty<int> currentPageIndex;
  183. /// <summary>
  184. /// 当前选中课程
  185. /// </summary>
  186. public BindableProperty<CoursType> onSelectCourse;
  187. public override void InitProperty()
  188. {
  189. currentPageIndex = CreateProperty<int>();
  190. onSelectCourse = CreateProperty<CoursType>();
  191. }
  192. }