Beispiel #1
0
        Question GenerateQuestion(JSONObject json)
        {
            print("question:" + json.ToString());
            string questiontype = json["questiontype"].Str;

            Question question = null;

            switch (questiontype)
            {
            case QuestionTypeNames.radiogrid:
                question = new RadioGridQuestion(json);
                break;

            case QuestionTypeNames.radiolist:
                question = new RadioListQuestion(json);
                break;

            case QuestionTypeNames.checklist:
                question = new CheckListQuestion(json);
                break;

            case QuestionTypeNames.slider:
                question = new SliderQuestion(json);
                break;

            case QuestionTypeNames.field:
                question = new FieldQuestion(json);
                break;

            case QuestionTypeNames.num_field:
                question = new NumFieldQuestion(json);
                break;

            case QuestionTypeNames.multi_field:
                question = new MultiFieldQuestion(json);
                break;

            case QuestionTypeNames.drop_down:
                question = new DropDownQuestion(json);
                break;

            case QuestionTypeNames.textview:
                question = new TextViewQuestion(json);
                break;

            default:
                question = new TextViewQuestion(json);
                break;
            }
            return(question);
        }
Beispiel #2
0
        public override void SetQuestion(Question q, UnityAction <Question> answeredEvent, UISkinData skinData = null)
        {
            base.SetQuestion(q, answeredEvent, skinData);

            radioGridQuestion     = question as RadioGridQuestion;
            instructionsText.text = question.instructions;
            idText.text           = question.id;


            questionItems = new List <List <Toggle> >();
            VerticalLayoutGroup panelLayout = itemsUI.GetComponent <VerticalLayoutGroup>();

            panelLayout.spacing        = 0.0f;
            panelLayout.padding.left   = 0;
            panelLayout.padding.right  = 0;
            panelLayout.padding.top    = 0;
            panelLayout.padding.bottom = 1;
            for (int i = 0; i < radioGridQuestion.q_text.Length; i++)
            {
                GameObject    textObj = Instantiate(labelPrefab);
                LayoutElement layout  = textObj.GetComponent <LayoutElement>();
                layout.enabled         = true;
                layout.minHeight       = 1;
                layout.minWidth        = 1;
                layout.preferredWidth  = 2;
                layout.preferredHeight = 1.5f;


                TMP_Text questionText = textObj.GetComponent <TMP_Text>();

                questionText.text      = radioGridQuestion.q_text[i].text;
                questionText.alignment = TextAlignmentOptions.Bottom;
                Vector2 textSize = questionText.GetPreferredValues(radioGridQuestion.q_text[i].text);
                //questionText.margin = new Vector4(0,0,1,-0.75f);

                textObj.transform.parent        = itemsUI;
                textObj.transform.localPosition = Vector3.zero;
                textObj.transform.localRotation = Quaternion.identity;
                textObj.transform.localScale    = textObj.transform.parent.localScale;
                //RadioGroup radioGroup = new RadioGroup(radioGridQuestion.q_text[i].id);
                List <Toggle> toggles = new List <Toggle>();
                //radioGroup.OnGroupSelected += OnItemSelected;
                GameObject itemsContainer = new GameObject("itemsContainer", typeof(RectTransform));
                itemsContainer.transform.parent        = itemsUI;
                itemsContainer.transform.localPosition = Vector3.zero;
                itemsContainer.transform.localRotation = Quaternion.identity;
                itemsContainer.transform.localScale    = itemsContainer.transform.parent.localScale;
                HorizontalLayoutGroup horizontalLayoutGroup = itemsContainer.AddComponent <HorizontalLayoutGroup>();
                horizontalLayoutGroup.childControlHeight = true;
                horizontalLayoutGroup.childControlWidth  = true;
                //horizontalLayoutGroup.spacing = -2;


                for (int j = 0; j < radioGridQuestion.labels.Length; j++)
                {
                    GameObject          container           = new GameObject("container", typeof(RectTransform));
                    VerticalLayoutGroup verticalLayoutGroup = container.AddComponent <VerticalLayoutGroup>();
                    verticalLayoutGroup.childControlWidth  = true;
                    verticalLayoutGroup.childControlHeight = true;
                    verticalLayoutGroup.spacing            = 0.2f;

                    container.transform.parent        = itemsContainer.transform;
                    container.transform.parent        = container.transform;
                    container.transform.localPosition = Vector3.zero;
                    container.transform.localRotation = Quaternion.identity;
                    container.transform.localScale    = container.transform.parent.localScale;

                    GameObject label     = Instantiate(labelPrefab);
                    TMP_Text   labelText = label.GetComponent <TMP_Text>();
                    labelText.GetComponent <SkinText>().textFormat = TextFormat.Small;
                    labelText.text                = radioGridQuestion.labels[j];
                    labelText.alignment           = TextAlignmentOptions.Center;
                    labelText.fontStyle           = FontStyles.Italic;
                    label.transform.parent        = container.transform;
                    label.transform.localPosition = Vector3.zero;
                    label.transform.localRotation = Quaternion.identity;
                    label.transform.localScale    = Vector3.one;


                    LayoutElement labelLayout = labelText.GetComponent <LayoutElement>();
                    labelLayout.enabled         = true;
                    labelLayout.minWidth        = skinData.toggleSize.x;
                    labelLayout.minHeight       = 0.5f;              // skinData.toggleSize.y;
                    labelLayout.preferredWidth  = skinData.toggleSize.x;
                    labelLayout.preferredHeight = 1.0f;              // skinData.toggleSize.y;


                    GameObject item   = Instantiate(radioItemPrefab);
                    Toggle     toggle = item.GetComponent <Toggle>();
                    //LayoutElement toggleLayout = textObj.GetComponent<LayoutElement>();
                    //toggleLayout.enabled = true;
                    //toggleLayout.minHeight = 1;
                    //toggleLayout.minWidth = 1;
                    //toggleLayout.preferredWidth = 1;
                    //toggleLayout.preferredHeight = 1;
                    //radioGroup.AddToggle(toggle);
                    toggle.SetIsOnWithoutNotify(false);
                    toggle.onValueChanged.AddListener((val) => {
                        OnItemSelected(toggle, radioGridQuestion.id, val);
                    });

                    toggles.Add(toggle);


                    item.transform.parent        = container.transform;
                    item.transform.localPosition = new Vector3(0, 0, skinData.radioZOffset);
                    //item.transform.localPosition = Vector3.zero;
                    item.transform.localRotation = Quaternion.identity;
                    item.transform.localScale    = Vector3.one;
                }
                //radioGroup.Init();
                //questionItems.Add(radioGroup);
                questionItems.Add(toggles);
            }

            //Canvas.ForceUpdateCanvases();
            //LayoutGridElements(); //Invoke("LayoutGridElements",0.16f);
            //Canvas.ForceUpdateCanvases();
        }