QuestionSetPanel.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using QFramework;
  4. using System.Collections.Generic;
  5. using ChivaXR;
  6. using System.Collections;
  7. namespace QFramework
  8. {
  9. public class QuestionSetPanelData : UIPanelData
  10. {
  11. }
  12. public partial class QuestionSetPanel : UIPanel
  13. {
  14. public string[] capital = new string[] { "A", "B", "C", "D", "E", "F", "G" };
  15. public List<OptionSetItem> optionSetItems = new List<OptionSetItem>();
  16. protected override void OnInit(IUIData uiData = null)
  17. {
  18. mData = uiData as QuestionSetPanelData ?? new QuestionSetPanelData();
  19. InitQuesiongPanelData();
  20. AddOptionBtn.onClick.AddListener(() =>
  21. {
  22. OptionSetItem tmpOptionSetItem = AddOptionSetItem();
  23. if (OptionTypeDropdown.captionText.text != "多选")
  24. {
  25. tmpOptionSetItem.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
  26. }
  27. });
  28. OptionTypeDropdown.onValueChanged.AddListener(OnOptionTypeDropdownClick);
  29. NextBtn.onClick.AddListener(OnNextBtnClick);
  30. PreviousBtn.onClick.AddListener(OnPreviousBtnClick);
  31. ExitBtn.onClick.AddListener(OnExitBtnClick);
  32. }
  33. protected override void OnOpen(IUIData uiData = null)
  34. {
  35. }
  36. protected override void OnShow()
  37. {
  38. }
  39. protected override void OnHide()
  40. {
  41. }
  42. protected override void OnClose()
  43. {
  44. }
  45. public void InitQuesiongPanelData()
  46. {
  47. DAL.Instance.Get<QuestionProxy>().ReadStepMsgInfoFromTable(OperateSetting.Instance.m_CourseName + "_题库");
  48. QuestionInfo tmpQuestionInfo = DAL.Instance.Get<QuestionProxy>().GetQuestionInfoByStepId(ProcessManagement.Instance.currentStepID);
  49. if (tmpQuestionInfo != null)
  50. {
  51. //设置题目类型
  52. OptionTypeDropdown.value = OptionTypeDropdown.options.IndexOf(OptionTypeDropdown.options.Find(t => t.text == tmpQuestionInfo.QuestionType));
  53. OptionTypeDropdown.captionText.text = tmpQuestionInfo.QuestionType;
  54. //设置题目
  55. TopicInputField.text = tmpQuestionInfo.Topic;
  56. //设置选项
  57. var optionArr = tmpQuestionInfo.Options.Split(';');
  58. //题型判断
  59. if (tmpQuestionInfo.QuestionType == "单选" || tmpQuestionInfo.QuestionType == "多选")
  60. {
  61. //单选和多选
  62. for (int i = 0; i < optionArr.Length; i++)
  63. {
  64. OptionSetItem optionSetItem = AddOptionSetItem(-1, optionArr[i]);
  65. if (tmpQuestionInfo.QuestionType == "单选")
  66. {
  67. optionSetItem.GetComponent<Toggle>().group = OptionConent.GetComponent<ToggleGroup>();
  68. }
  69. if (tmpQuestionInfo.Answer.Contains(optionArr[i].Split('、')[0]))
  70. {
  71. optionSetItem.GetComponent<Toggle>().isOn = true;
  72. }
  73. }
  74. }else
  75. {
  76. if (tmpQuestionInfo.Answer == "对")
  77. {
  78. optionSetItems[0].GetComponent<Toggle>().isOn = true;
  79. }
  80. else
  81. {
  82. optionSetItems[1].GetComponent<Toggle>().isOn = true;
  83. }
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// 添加选项
  89. /// </summary>
  90. /// <param name="option"></param>
  91. /// <returns></returns>
  92. private OptionSetItem AddOptionSetItem(int index = -1, string option = "")
  93. {
  94. var tempOptionSetItem = Instantiate(OptionSetItem.gameObject, OptionConent.transform);
  95. tempOptionSetItem.SetActive(true);
  96. OptionSetItem optionSetItem = tempOptionSetItem.GetComponent<OptionSetItem>();
  97. optionSetItem.OptionInputField.text = option;
  98. optionSetItems.Add(optionSetItem);
  99. LayoutRebuilder.ForceRebuildLayoutImmediate(OptionConent.GetComponent<RectTransform>());
  100. return optionSetItem;
  101. }
  102. /// <summary>
  103. /// 题型选项
  104. /// </summary>
  105. /// <param name="option"></param>
  106. private void OnOptionTypeDropdownClick(int option)
  107. {
  108. ClearAllData();
  109. if (OptionTypeDropdown.captionText.text == "多选")
  110. {
  111. OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = true;
  112. foreach (var item in optionSetItems) item.m_Toggle.group = null;
  113. AddOptionBtn.gameObject.SetActive(true);
  114. }
  115. else if (OptionTypeDropdown.captionText.text == "判断")
  116. {
  117. for (int i = 0; i < 2; i++)
  118. {
  119. if (i == 0) AddOptionSetItem(-1, "对");
  120. else AddOptionSetItem(-1, "错");
  121. }
  122. foreach (var item in optionSetItems) item.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
  123. OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = false;
  124. AddOptionBtn.gameObject.SetActive(false);
  125. }
  126. else
  127. {
  128. foreach (var item in optionSetItems) item.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
  129. OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = false;
  130. AddOptionBtn.gameObject.SetActive(true);
  131. }
  132. LayoutRebuilder.ForceRebuildLayoutImmediate(OptionConent.GetComponent<RectTransform>());
  133. }
  134. /// <summary>
  135. /// 下一步
  136. /// </summary>
  137. private void OnNextBtnClick()
  138. {
  139. if (OnSaveData())
  140. {
  141. ClearAllData();
  142. ProcessManagement.Instance.JumpProcessState(ProcessManagement.Instance.currentStepID + 1);
  143. ProcessManagement.Instance.ActiveCurrentProcess();
  144. InitQuesiongPanelData();
  145. }
  146. }
  147. /// <summary>
  148. /// 下一步
  149. /// </summary>
  150. private void OnPreviousBtnClick()
  151. {
  152. if (OnSaveData())
  153. {
  154. ClearAllData();
  155. ProcessManagement.Instance.JumpProcessState(ProcessManagement.Instance.currentStepID - 1);
  156. ProcessManagement.Instance.ActiveCurrentProcess();
  157. InitQuesiongPanelData();
  158. }
  159. }
  160. /// <summary>
  161. /// 退出
  162. /// </summary>
  163. private void OnExitBtnClick()
  164. {
  165. CloseSelf();
  166. }
  167. private void ClearAllData()
  168. {
  169. for (int i = 0; i < optionSetItems.Count; i++)
  170. {
  171. Destroy(optionSetItems[i].gameObject);
  172. }
  173. optionSetItems.Clear();
  174. }
  175. /// <summary>
  176. /// 保存
  177. /// </summary>
  178. private bool OnSaveData()
  179. {
  180. QuestionInfo questionInfo = GetPanelQuestionSetInfo();
  181. if (string.IsNullOrEmpty(questionInfo.Answer))
  182. {
  183. StartCoroutine(ShowWrongMsg("选项不能为空"));
  184. return false;
  185. }
  186. DAL.Instance.Get<QuestionProxy>().SetQuestionInfo(questionInfo);
  187. return true;
  188. }
  189. /// <summary>
  190. /// 获取面板上配置的试题信息
  191. /// </summary>
  192. /// <returns></returns>
  193. private QuestionInfo GetPanelQuestionSetInfo()
  194. {
  195. QuestionInfo questionInfo = new QuestionInfo();
  196. questionInfo.StepId = ProcessManagement.Instance.currentStepID.ToString();
  197. questionInfo.QuestionType = OptionTypeDropdown.captionText.text;
  198. questionInfo.Topic = TopicInputField.text;
  199. if (questionInfo.QuestionType != "判断")
  200. {
  201. string optionStr = string.Empty;
  202. for (int i = 0; i < optionSetItems.Count; i++)
  203. {
  204. string[] tmpOptions = optionSetItems[i].OptionInputField.text.Split('、');
  205. if (tmpOptions.Length > 1)
  206. {
  207. optionStr += capital[i] + "、" + tmpOptions[1];
  208. }
  209. else
  210. {
  211. optionStr += capital[i] + "、" + tmpOptions[0];
  212. }
  213. if (i != optionSetItems.Count - 1) optionStr += ";";
  214. }
  215. questionInfo.Options = optionStr;
  216. }
  217. if (questionInfo.QuestionType == "判断")
  218. {
  219. if (optionSetItems.Count != 0)
  220. {
  221. if (optionSetItems[0].m_Toggle.isOn) questionInfo.Answer = "对";
  222. if (optionSetItems[1].m_Toggle.isOn) questionInfo.Answer = "错";
  223. }
  224. }
  225. else
  226. {
  227. string answerStr = string.Empty;
  228. for (int i = 0; i < optionSetItems.Count; i++)
  229. {
  230. if (optionSetItems[i].m_Toggle.isOn)
  231. {
  232. answerStr += capital[i];
  233. }
  234. }
  235. questionInfo.Answer = answerStr;
  236. }
  237. return questionInfo;
  238. }
  239. private IEnumerator ShowWrongMsg(string wrongMsg)
  240. {
  241. WrongText.text = wrongMsg;
  242. yield return new WaitForSeconds(2f);
  243. WrongText.text = "";
  244. }
  245. }
  246. }