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);
            radioListQuestion     = question as RadioListQuestion;
            instructionsText.text = question.instructions;
            idText.text           = question.id;


            toggles = new List <Toggle>();
            //VariableGridLayoutGroup gridLayout;
            //LayoutGroup layoutGroup;

            //if(radioListQuestion.horizontal) {
            //	layoutGroup = itemsUI.gameObject.AddComponent<HorizontalLayoutGroup>();
            //	layoutGroup.childAlignment = TextAnchor.MiddleCenter;
            //	layoutGroup.padding.left = 1;
            //	layoutGroup.padding.right = 1;
            //	layoutGroup.padding.top = 1;
            //	layoutGroup.padding.bottom = 1;
            //} else {
            //	layoutGroup = itemsUI.gameObject.AddComponent<VerticalLayoutGroup>();
            //	layoutGroup.childAlignment = TextAnchor.MiddleCenter;
            //	layoutGroup.padding.left = 1;
            //	layoutGroup.padding.right = 1;
            //	layoutGroup.padding.top = 1;
            //	layoutGroup.padding.bottom = 1;
            //}
            VariableGridLayoutGroup gridLayoutGroup = itemsUI.gameObject.AddComponent <VariableGridLayoutGroup>();

            gridLayoutGroup.childAlignment  = TextAnchor.MiddleCenter;
            gridLayoutGroup.padding.left    = 1;
            gridLayoutGroup.padding.right   = 1;
            gridLayoutGroup.padding.top     = 1;
            gridLayoutGroup.padding.bottom  = 1;
            gridLayoutGroup.spacing         = new Vector2(0, skinData.fontSizeBody);
            gridLayoutGroup.constraint      = VariableGridLayoutGroup.Constraint.FixedColumnCount;
            gridLayoutGroup.constraintCount = 2;


            for (int i = 0; i < radioListQuestion.labels.Length; i++)
            {
                //GameObject container = new GameObject("container",typeof(RectTransform));
                //container.AddComponent<HorizontalLayoutGroup>();
                GameObject label = Instantiate(labelPrefab);

                LayoutElement labelLayout = label.GetComponent <LayoutElement>();
                labelLayout.flexibleWidth  = skinData.canvasSize.x * 0.6f;                // 10;
                labelLayout.preferredWidth = skinData.canvasSize.x * 0.75f;
                labelLayout.minWidth       = -1;
                labelLayout.minHeight      = -1;


                //labelLayout.flexibleWidth = 10;
                //labelLayout.preferredWidth = 12;
                //labelLayout.minWidth = -1;
                //labelLayout.minHeight = -1;

                TMP_Text text = label.GetComponent <TMP_Text>();
                text.text = radioListQuestion.labels[i];
                //text.autoSizeTextContainer = true;
                //text.enableAutoSizing = true;
                text.ForceMeshUpdate(true);

                text.GetComponent <SkinText>().textFormat = TextFormat.Body;

                text.margin = new Vector4(text.fontSize, 0, 0, 0);

                GameObject    radioItem    = Instantiate(radioItemPrefab);
                Toggle        toggle       = radioItem.GetComponent <Toggle>();
                LayoutElement toggleLayout = toggle.GetComponent <LayoutElement>();
                toggleLayout.flexibleWidth = 1;

                toggle.SetIsOnWithoutNotify(false);
                toggles.Add(toggle);
                toggle.onValueChanged.AddListener((val) => {
                    OnItemSelected(toggle, radioListQuestion.id, val);
                });
                //container.transform.parent = itemsUI;
                radioItem.transform.parent = itemsUI;
                label.transform.parent     = itemsUI;


                //container.transform.localPosition = Vector3.zero;
                //container.transform.localRotation = Quaternion.identity;
                //container.transform.localScale = Vector3.one;

                label.transform.localPosition = Vector3.zero;
                label.transform.localRotation = Quaternion.identity;
                label.transform.localScale    = Vector3.one;              // label.transform.parent.localScale;

                radioItem.transform.localPosition = Vector3.zero;
                radioItem.transform.localRotation = Quaternion.identity;
                radioItem.transform.localScale    = Vector3.one;             // radioItem.transform.parent.localScale;

                if (label.GetComponent <Button>())
                {
                    Button btn = label.GetComponent <Button>();
                    btn.onClick.AddListener(() => {
                        toggle.isOn = !toggle.isOn;
                    });
                }
            }
            LayoutRebuilder.ForceRebuildLayoutImmediate(itemsUI);
            //Canvas.ForceUpdateCanvases();
        }