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)
        {
            base.SetQuestion(q, answeredEvent, skinData);

            instructionsText.text = question.instructions;
            idText.text           = question.id;

            toggles              = new List <Toggle>();
            checkQuestion        = (question as CheckListQuestion);
            gridLayout.startAxis = VariableGridLayoutGroup.Axis.Horizontal;


            int factor = checkQuestion.questions.Length / maxQuestionsVertical;

            factor = (factor == 0 ? 1 : factor);

            //if(checkQuestion.horizontal) {
            //	gridLayout.constraint = VariableGridLayoutGroup.Constraint.FixedRowCount;
            //	gridLayout.constraintCount = 1;
            //	gridLayout.childAlignment = TextAnchor.MiddleCenter;
            //}else{
            gridLayout.constraint = VariableGridLayoutGroup.Constraint.FixedColumnCount;

            gridLayout.constraintCount = 2 * factor;
            gridLayout.childAlignment  = TextAnchor.MiddleCenter;
            gridLayout.spacing         = spacing;

            maxWidth  = maxTextWidth / factor;
            maxHeight = maxTextHeight / factor;
            //}

            for (int i = 0; i < checkQuestion.questions.Length; i++)
            {
                //GameObject labeledItem = Instantiate(checkItemLabeledPrefab);
                //GameObject checkItem = labeledItem.transform.Find("Checkbox").gameObject;

                GameObject checkItem = Instantiate(checkItemPrefab);
                Toggle     toggle    = checkItem.GetComponent <Toggle>();
                toggle.isOn = false;
                toggle.onValueChanged.AddListener(HandleToggleValueChanged);

                LayoutElement toggleLayout = toggle.GetComponent <LayoutElement>();
                toggleLayout.enabled   = true;
                toggleLayout.minWidth  = skinData.toggleSize.x;
                toggleLayout.minHeight = skinData.toggleSize.y;

                toggles.Add(toggle);


                //GameObject label = labeledItem.transform.Find("QuestionLabelInteractive").gameObject;
                GameObject label = Instantiate(labelPrefab);
                TMP_Text   text  = label.GetComponent <TMP_Text>();
                text.text   = checkQuestion.questions[i].text;
                text.margin = new Vector4(-spacing.x / 4f, 0, 0, 0);

                text.autoSizeTextContainer = true;
                //text.enableAutoSizing = true;
                text.ForceMeshUpdate(true);


                LayoutElement labelLayout = label.GetComponent <LayoutElement>();
                //float w = text.preferredWidth * preferredWidthScaler;
                //float h = text.preferredHeight * preferredHeightScaler;
                ////labelLayout.preferredWidth = - 1;
                ////labelLayout.preferredHeight = - 1;
                //labelLayout.preferredWidth = -1; //Mathf.Clamp(w,0,maxWidth);
                //labelLayout.preferredHeight = -1; // Mathf.Clamp(h,0,maxHeight);
                if (factor == 1)
                {
                    labelLayout.flexibleWidth  = skinData.canvasSize.x * 0.6f;                    // 10;
                    labelLayout.preferredWidth = skinData.canvasSize.x * 0.75f;
                    labelLayout.minWidth       = -1;
                    labelLayout.minHeight      = -1;
                }

                if (label.GetComponent <Button>())
                {
                    Button btn = label.GetComponent <Button>();
                    btn.onClick.AddListener(() => {
                        toggle.isOn = !toggle.isOn;
                    });
                }

                //labeledItem.transform.parent = itemsUI;
                checkItem.transform.parent = itemsUI;
                label.transform.parent     = itemsUI;

                label.transform.localPosition = new Vector3(0, 0, skinData.radioZOffset);
                label.transform.localRotation = Quaternion.identity;
                label.transform.localScale    = label.transform.parent.localScale;

                checkItem.transform.localPosition = new Vector3(0, 0, skinData.radioZOffset);
                //checkItem.transform.localPosition = Vector3.zero;
                checkItem.transform.localRotation = Quaternion.identity;
                checkItem.transform.localScale    = label.transform.parent.localScale;
            }
            LayoutRebuilder.ForceRebuildLayoutImmediate(itemsUI);
        }