QuestionSetPanel.cs 10 KB

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