123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- using UnityEngine;
- using UnityEngine.UI;
- using QFramework;
- using System.Collections.Generic;
- using ChivaXR;
- using System.Collections;
- namespace QFramework
- {
- public class QuestionSetPanelData : UIPanelData
- {
- }
- public partial class QuestionSetPanel : UIPanel
- {
- public string[] capital = new string[] { "A", "B", "C", "D", "E", "F", "G" };
- public List<OptionSetItem> optionSetItems = new List<OptionSetItem>();
- protected override void OnInit(IUIData uiData = null)
- {
- mData = uiData as QuestionSetPanelData ?? new QuestionSetPanelData();
- InitQuesiongPanelData();
- AddOptionBtn.onClick.AddListener(() =>
- {
- OptionSetItem tmpOptionSetItem = AddOptionSetItem();
- if (OptionTypeDropdown.captionText.text != "多选")
- {
- tmpOptionSetItem.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
- }
- });
- OptionTypeDropdown.onValueChanged.AddListener(OnOptionTypeDropdownClick);
- NextBtn.onClick.AddListener(OnNextBtnClick);
- PreviousBtn.onClick.AddListener(OnPreviousBtnClick);
- ExitBtn.onClick.AddListener(OnExitBtnClick);
- }
- protected override void OnOpen(IUIData uiData = null)
- {
- }
- protected override void OnShow()
- {
- }
- protected override void OnHide()
- {
- }
- protected override void OnClose()
- {
-
- }
- public void InitQuesiongPanelData()
- {
- DAL.Instance.Get<QuestionProxy>().ReadStepMsgInfoFromTable(OperateSetting.Instance.m_CourseName + "_题库");
- QuestionInfo tmpQuestionInfo = DAL.Instance.Get<QuestionProxy>().GetQuestionInfoByStepId(ProcessManagement.Instance.currentStepID);
- if (tmpQuestionInfo != null)
- {
- //设置题目类型
- OptionTypeDropdown.value = OptionTypeDropdown.options.IndexOf(OptionTypeDropdown.options.Find(t => t.text == tmpQuestionInfo.QuestionType));
- OptionTypeDropdown.captionText.text = tmpQuestionInfo.QuestionType;
- //设置题目
- TopicInputField.text = tmpQuestionInfo.Topic;
- //设置选项
- var optionArr = tmpQuestionInfo.Options.Split(';');
- //题型判断
- if (tmpQuestionInfo.QuestionType == "单选" || tmpQuestionInfo.QuestionType == "多选")
- {
- //单选和多选
- for (int i = 0; i < optionArr.Length; i++)
- {
- OptionSetItem optionSetItem = AddOptionSetItem(-1, optionArr[i]);
- if (tmpQuestionInfo.QuestionType == "单选")
- {
- optionSetItem.GetComponent<Toggle>().group = OptionConent.GetComponent<ToggleGroup>();
- }
- if (tmpQuestionInfo.Answer.Contains(optionArr[i].Split('、')[0]))
- {
- optionSetItem.GetComponent<Toggle>().isOn = true;
- }
- }
- }else
- {
- if (tmpQuestionInfo.Answer == "对")
- {
- optionSetItems[0].GetComponent<Toggle>().isOn = true;
- }
- else
- {
- optionSetItems[1].GetComponent<Toggle>().isOn = true;
- }
- }
- }
- }
- /// <summary>
- /// 添加选项
- /// </summary>
- /// <param name="option"></param>
- /// <returns></returns>
- private OptionSetItem AddOptionSetItem(int index = -1, string option = "")
- {
- var tempOptionSetItem = Instantiate(OptionSetItem.gameObject, OptionConent.transform);
- tempOptionSetItem.SetActive(true);
- OptionSetItem optionSetItem = tempOptionSetItem.GetComponent<OptionSetItem>();
- optionSetItem.OptionInputField.text = option;
- optionSetItems.Add(optionSetItem);
- LayoutRebuilder.ForceRebuildLayoutImmediate(OptionConent.GetComponent<RectTransform>());
- return optionSetItem;
- }
- /// <summary>
- /// 题型选项
- /// </summary>
- /// <param name="option"></param>
- private void OnOptionTypeDropdownClick(int option)
- {
- ClearAllData();
- if (OptionTypeDropdown.captionText.text == "多选")
- {
- OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = true;
- foreach (var item in optionSetItems) item.m_Toggle.group = null;
- AddOptionBtn.gameObject.SetActive(true);
- }
- else if (OptionTypeDropdown.captionText.text == "判断")
- {
- for (int i = 0; i < 2; i++)
- {
- if (i == 0) AddOptionSetItem(-1, "对");
- else AddOptionSetItem(-1, "错");
- }
- foreach (var item in optionSetItems) item.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
- OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = false;
- AddOptionBtn.gameObject.SetActive(false);
- }
- else
- {
- foreach (var item in optionSetItems) item.m_Toggle.group = OptionConent.GetComponent<ToggleGroup>();
- OptionConent.GetComponent<ToggleGroup>().allowSwitchOff = false;
- AddOptionBtn.gameObject.SetActive(true);
- }
- LayoutRebuilder.ForceRebuildLayoutImmediate(OptionConent.GetComponent<RectTransform>());
- }
- /// <summary>
- /// 下一步
- /// </summary>
- private void OnNextBtnClick()
- {
- if (OnSaveData())
- {
- ClearAllData();
- ProcessManagement.Instance.JumpProcessState(ProcessManagement.Instance.currentStepID + 1);
- ProcessManagement.Instance.ActiveCurrentProcess();
- InitQuesiongPanelData();
- }
- }
- /// <summary>
- /// 下一步
- /// </summary>
- private void OnPreviousBtnClick()
- {
- if (OnSaveData())
- {
- ClearAllData();
- ProcessManagement.Instance.JumpProcessState(ProcessManagement.Instance.currentStepID - 1);
- ProcessManagement.Instance.ActiveCurrentProcess();
- InitQuesiongPanelData();
- }
- }
- /// <summary>
- /// 退出
- /// </summary>
- private void OnExitBtnClick()
- {
- CloseSelf();
- }
- private void ClearAllData()
- {
- for (int i = 0; i < optionSetItems.Count; i++)
- {
- Destroy(optionSetItems[i].gameObject);
- }
- optionSetItems.Clear();
- }
- /// <summary>
- /// 保存
- /// </summary>
- private bool OnSaveData()
- {
- QuestionInfo questionInfo = GetPanelQuestionSetInfo();
- if (string.IsNullOrEmpty(questionInfo.Answer))
- {
- StartCoroutine(ShowWrongMsg("选项不能为空"));
- return false;
- }
- DAL.Instance.Get<QuestionProxy>().SetQuestionInfo(questionInfo);
- return true;
- }
- /// <summary>
- /// 获取面板上配置的试题信息
- /// </summary>
- /// <returns></returns>
- private QuestionInfo GetPanelQuestionSetInfo()
- {
- QuestionInfo questionInfo = new QuestionInfo();
- questionInfo.StepId = ProcessManagement.Instance.currentStepID.ToString();
- questionInfo.QuestionType = OptionTypeDropdown.captionText.text;
- questionInfo.Topic = TopicInputField.text;
- if (questionInfo.QuestionType != "判断")
- {
- string optionStr = string.Empty;
- for (int i = 0; i < optionSetItems.Count; i++)
- {
- string[] tmpOptions = optionSetItems[i].OptionInputField.text.Split('、');
- if (tmpOptions.Length > 1)
- {
- optionStr += capital[i] + "、" + tmpOptions[1];
- }
- else
- {
- optionStr += capital[i] + "、" + tmpOptions[0];
- }
- if (i != optionSetItems.Count - 1) optionStr += ";";
- }
- questionInfo.Options = optionStr;
- }
- if (questionInfo.QuestionType == "判断")
- {
- if (optionSetItems.Count != 0)
- {
- if (optionSetItems[0].m_Toggle.isOn) questionInfo.Answer = "对";
- if (optionSetItems[1].m_Toggle.isOn) questionInfo.Answer = "错";
- }
- }
- else
- {
- string answerStr = string.Empty;
- for (int i = 0; i < optionSetItems.Count; i++)
- {
- if (optionSetItems[i].m_Toggle.isOn)
- {
- answerStr += capital[i];
- }
- }
- questionInfo.Answer = answerStr;
- }
- return questionInfo;
- }
- private IEnumerator ShowWrongMsg(string wrongMsg)
- {
- WrongText.text = wrongMsg;
- yield return new WaitForSeconds(2f);
- WrongText.text = "";
- }
- }
- }
|