QuestionSetPanel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. if (tmpQuestionInfo.QuestionType == "图文") return;
  52. //设置题目类型
  53. OptionTypeDropdown.value = OptionTypeDropdown.options.IndexOf(OptionTypeDropdown.options.Find(t => t.text == tmpQuestionInfo.QuestionType));
  54. OptionTypeDropdown.captionText.text = tmpQuestionInfo.QuestionType;
  55. //设置题目
  56. TopicInputField.text = tmpQuestionInfo.Topic;
  57. //设置选项
  58. var optionArr = tmpQuestionInfo.Options.Split(';');
  59. //题型判断
  60. if (tmpQuestionInfo.QuestionType == "单选" || tmpQuestionInfo.QuestionType == "多选")
  61. {
  62. //单选和多选
  63. for (int i = 0; i < optionArr.Length; i++)
  64. {
  65. OptionSetItem optionSetItem = AddOptionSetItem(-1, optionArr[i]);
  66. //根据单选或者多选设置是否允许多选或单选
  67. if (tmpQuestionInfo.QuestionType == "单选") optionSetItem.GetComponent<Toggle>().group = OptionConent.GetComponent<ToggleGroup>();
  68. //设置正确选项
  69. if (tmpQuestionInfo.Answer.Contains(optionArr[i].Split('、')[0])) optionSetItem.GetComponent<Toggle>().isOn = true;
  70. }
  71. }
  72. else if (tmpQuestionInfo.QuestionType == "判断")
  73. {
  74. AddOptionSetItem(-1,"对");
  75. AddOptionSetItem(-1, "错");
  76. if (tmpQuestionInfo.Answer == "对") optionSetItems[0].GetComponent<Toggle>().isOn = true;
  77. else optionSetItems[1].GetComponent<Toggle>().isOn = true;
  78. }
  79. else
  80. {
  81. }
  82. }
  83. }
  84. /// <summary>
  85. /// 添加选项
  86. /// </summary>
  87. /// <param name="option"></param>
  88. /// <returns></returns>
  89. private OptionSetItem AddOptionSetItem(int index = -1, string option = "")
  90. {
  91. var tempOptionSetItem = Instantiate(OptionSetItem.gameObject, OptionConent.transform);
  92. tempOptionSetItem.SetActive(true);
  93. OptionSetItem optionSetItem = tempOptionSetItem.GetComponent<OptionSetItem>();
  94. optionSetItem.OptionInputField.text = option;
  95. optionSetItems.Add(optionSetItem);
  96. LayoutRebuilder.ForceRebuildLayoutImmediate(OptionConent.GetComponent<RectTransform>());
  97. return optionSetItem;
  98. }
  99. /// <summary>
  100. /// 题型选项
  101. /// </summary>
  102. /// <param name="option"></param>
  103. private void OnOptionTypeDropdownClick(int option)
  104. {
  105. //ClearAllData();
  106. if (OptionTypeDropdown.captionText.text == "多选")
  107. {
  108. OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = true;
  109. foreach (var item in optionSetItems) item.m_Toggle.group = null;
  110. AddOptionBtn.gameObject.SetActive(true);
  111. }
  112. else if (OptionTypeDropdown.captionText.text == "判断")
  113. {
  114. for (int i = 0; i < 2; i++)
  115. {
  116. if (i == 0) AddOptionSetItem(-1, "对");
  117. else AddOptionSetItem(-1, "错");
  118. }
  119. foreach (var item in optionSetItems) item.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
  120. OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = false;
  121. AddOptionBtn.gameObject.SetActive(false);
  122. }
  123. else if (OptionTypeDropdown.captionText.text == "单选")
  124. {
  125. foreach (var item in optionSetItems) item.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
  126. OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = false;
  127. AddOptionBtn.gameObject.SetActive(true);
  128. }
  129. else
  130. {
  131. Debug.LogError("选择的为None");
  132. }
  133. LayoutRebuilder.ForceRebuildLayoutImmediate(OptionConent.GetComponent<RectTransform>());
  134. }
  135. /// <summary>
  136. /// 下一步
  137. /// </summary>
  138. private void OnNextBtnClick()
  139. {
  140. if (OnSaveData())
  141. {
  142. OptionTypeDropdown.value = 0;
  143. ClearAllData();
  144. ProcessManagement.Instance.JumpProcessState(ProcessManagement.Instance.currentStepID + 1);
  145. ProcessManagement.Instance.ActiveCurrentProcess();
  146. InitQuesiongPanelData();
  147. }
  148. }
  149. /// <summary>
  150. /// 下一步
  151. /// </summary>
  152. private void OnPreviousBtnClick()
  153. {
  154. if (OnSaveData())
  155. {
  156. OptionTypeDropdown.value = 0;
  157. ClearAllData();
  158. ProcessManagement.Instance.JumpProcessState(ProcessManagement.Instance.currentStepID - 1);
  159. ProcessManagement.Instance.ActiveCurrentProcess();
  160. InitQuesiongPanelData();
  161. }
  162. }
  163. /// <summary>
  164. /// 退出
  165. /// </summary>
  166. private void OnExitBtnClick()
  167. {
  168. CloseSelf();
  169. }
  170. private void ClearAllData()
  171. {
  172. TopicInputField.text = string.Empty;
  173. for (int i = 0; i < optionSetItems.Count; i++) Destroy(optionSetItems[i].gameObject);
  174. optionSetItems.Clear();
  175. }
  176. /// <summary>
  177. /// 保存
  178. /// </summary>
  179. private bool OnSaveData()
  180. {
  181. if (OptionTypeDropdown.value == 0)
  182. {
  183. return true;
  184. }
  185. else
  186. {
  187. QuestionInfo questionInfo = GetPanelQuestionSetInfo();
  188. if (string.IsNullOrEmpty(questionInfo.Answer))
  189. {
  190. StartCoroutine(ShowWrongMsg("选项不能为空"));
  191. return false;
  192. }
  193. DAL.Instance.Get<QuestionProxy>().SetQuestionInfo(questionInfo);
  194. return true;
  195. }
  196. }
  197. /// <summary>
  198. /// 获取面板上配置的试题信息
  199. /// </summary>
  200. /// <returns></returns>
  201. private QuestionInfo GetPanelQuestionSetInfo()
  202. {
  203. QuestionInfo questionInfo = new QuestionInfo();
  204. questionInfo.StepId = ProcessManagement.Instance.currentStepID.ToString();
  205. questionInfo.QuestionType = OptionTypeDropdown.captionText.text;
  206. questionInfo.Topic = TopicInputField.text;
  207. if (questionInfo.QuestionType != "判断")
  208. {
  209. string optionStr = string.Empty;
  210. for (int i = 0; i < optionSetItems.Count; i++)
  211. {
  212. string[] tmpOptions = optionSetItems[i].OptionInputField.text.Split('、');
  213. if (tmpOptions.Length > 1)
  214. {
  215. optionStr += capital[i] + "、" + tmpOptions[1];
  216. }
  217. else
  218. {
  219. optionStr += capital[i] + "、" + tmpOptions[0];
  220. }
  221. if (i != optionSetItems.Count - 1) optionStr += ";";
  222. }
  223. questionInfo.Options = optionStr;
  224. }
  225. if (questionInfo.QuestionType == "判断")
  226. {
  227. if (optionSetItems.Count != 0)
  228. {
  229. if (optionSetItems[0].m_Toggle.isOn) questionInfo.Answer = "对";
  230. if (optionSetItems[1].m_Toggle.isOn) questionInfo.Answer = "错";
  231. }
  232. }
  233. else
  234. {
  235. string answerStr = string.Empty;
  236. for (int i = 0; i < optionSetItems.Count; i++)
  237. {
  238. if (optionSetItems[i].m_Toggle.isOn)
  239. {
  240. answerStr += capital[i];
  241. }
  242. }
  243. questionInfo.Answer = answerStr;
  244. }
  245. return questionInfo;
  246. }
  247. private IEnumerator ShowWrongMsg(string wrongMsg)
  248. {
  249. WrongText.text = wrongMsg;
  250. yield return new WaitForSeconds(2f);
  251. WrongText.text = "";
  252. }
  253. }
  254. }