示例#1
0
        public void Visit(ChoiceQuestion q)
        {
            var dynamicFieldSize = _dynamicField.GetComponent <RectTransform>().sizeDelta;
            var nRows            = q.NRows;
            var nColumns         = q.NColumns;
            var answernumber     = 0;

            var qmb = _questionPlaceholder.GetComponent <QuestionMenuButtons>();

            var multiColObject = GameObjectUtils.InstatiatePrefab("Prefabs/Menus/Questionnaire/Content/ChoiceQuestionLayout");

            MenuUtils.PlaceElement(multiColObject, _dynamicField);
            var newParent = multiColObject.transform.Find("ResponseRows");

            var isMultiple = q.Choice == Choice.Multiple;

            if (nColumns > 1)
            {
                var cLabels = q.ColumnLabels.Select(l => l.Text).ToList();

                var topRow = GameObjectUtils.InstatiatePrefab("Prefabs/Menus/Questionnaire/Rows/TopRow");

                MenuUtils.PlaceElement(topRow, _questionContent);
                topRow.transform.SetSiblingIndex(1);

                var topLabelSize = MenuUtils.ComputeTopLabelSize(cLabels);
                var oldDelta     = topRow.GetComponent <RectTransform>().sizeDelta;
                topRow.GetComponent <RectTransform>().sizeDelta = new Vector2(oldDelta.x, topLabelSize.y);


                for (var i = 0; i < nColumns; i++)
                {
                    //Set top labels
                    var filenameObj = MenuUtils.AddLabelText("Questionnaire/Rows/Elements/OneLabel", cLabels[i], topRow.transform);
                    filenameObj.GetComponent <RectTransform>().sizeDelta = topLabelSize;
                }

                var widthAllTopLabels = nColumns * topLabelSize.x;
                var sideLabelWidth    = dynamicFieldSize.x - widthAllTopLabels;

                var leftPlaceholder = topRow.transform.Find("Placeholder");
                leftPlaceholder.GetComponent <RectTransform>().sizeDelta = new Vector2(sideLabelWidth, 0);

                for (var i = 0; i < nRows; i++)
                {
                    //Set side labels
                    var oneRow = GameObjectUtils.InstatiatePrefab("Prefabs/Menus/Questionnaire/Rows/OneRow");
                    MenuUtils.PlaceElement(oneRow, newParent);

                    var text        = q.RowLabels != null?q.RowLabels[i].Text:"";
                    var filenameObj = MenuUtils.AddLabelText("Questionnaire/Rows/Elements/OneLabel", text, oneRow.transform);
                    var tex         = filenameObj.GetComponent <Text>();

                    var count       = Regex.Split(tex.text, "\n").Length - 1;
                    var labelLength = MenuUtils.MessagePixelLength(tex.text, tex);
                    var rowHeight   = (int)(Math.Ceiling(labelLength / sideLabelWidth) + count) * 32;
                    filenameObj.GetComponent <RectTransform>().sizeDelta = new Vector2(sideLabelWidth, rowHeight);

                    for (var j = 0; j < nColumns; j++)
                    {
                        var button = MenuUtils.CreateToggleElement("Questionnaire/Rows/Elements/OneToggleButton", oneRow.transform, topLabelSize.x, 100);
                        var btn    = button.GetComponent <Toggle>();
                        if (!isMultiple)
                        {
                            btn.group = oneRow.GetComponent <ToggleGroup>();
                        }


                        var iLocal = answernumber;
                        btn.isOn = _oldAnswers != null && _oldAnswers.ContainsKey(iLocal);
                        btn.onValueChanged.AddListener(isOn =>
                        {
                            qmb.SetAnswerInt(iLocal, isOn ? 1 : 0);
                        });
                        answernumber++;
                    }
                }
            }
            else
            {
                var   rLabels = q.RowLabels.Select(l => l.Text).ToList();
                float length  = MenuUtils.GetMaxTextLength(rLabels);
                for (var i = 0; i < nRows; i++)
                {
                    var hasText = q.RowLabels[i].Answerable.HasValue && q.RowLabels[i].Answerable.Value;

                    //Set side labels
                    GameObject oneRow;
                    if (q.RowLabels[i].Image != null)
                    {
                        oneRow = MenuUtils.AddLabelText("Questionnaire/Rows/ImageChoiceRow", rLabels[i], newParent, "OneLabel", length);
                        oneRow.transform.Find("RawImage").GetComponent <RawImage>().texture = Resources.Load <Sprite>(q.RowLabels[i].Image).texture;
                    }
                    else
                    {
                        var labelType = "Questionnaire/Rows/" + (hasText ? "TextChoiceRow" : "SingleChoiceRow");
                        oneRow = MenuUtils.AddLabelText(labelType, rLabels[i], newParent, "OneLabel", length);
                    }
                    MenuUtils.PlaceElement(oneRow, newParent);

                    var rightPlaceholderWidth = dynamicFieldSize.x - length;
                    var rightPlaceholder      = oneRow.transform.Find("NoTextPlaceholder");
                    rightPlaceholder.GetComponent <RectTransform>().sizeDelta = new Vector2(rightPlaceholderWidth, 0);

                    var toggleObject = oneRow.transform.Find("OneToggleButton").gameObject;
                    toggleObject.GetComponent <RectTransform>().sizeDelta = new Vector2(40, 50);

                    var btn = toggleObject.transform.Find("ToggleButtons").GetComponent <Toggle>();
                    if (!isMultiple)
                    {
                        btn.group = newParent.GetComponent <ToggleGroup>();
                    }



                    var iLocal = answernumber;
                    btn.onValueChanged.AddListener(isOn =>
                    {
                        qmb.SetAnswerInt(iLocal, isOn ? 1 : 0);
                    });
                    btn.isOn = false;

                    if (hasText)
                    {
                        var inpt = oneRow.transform.Find("InputField").GetComponent <InputField>();

                        inpt.onEndEdit.AddListener(answer =>
                        {
                            qmb.SetAnswerString(iLocal, answer);
                        });
                        if (_oldAnswers != null && _oldAnswers.ContainsKey(answernumber))
                        {
                            inpt.text = _oldAnswers[answernumber];
                        }
                        qmb.SetActiveDisableTextField(answernumber, false);
                    }

                    if (_oldAnswers != null && _oldAnswers.ContainsKey(answernumber))
                    {
                        btn.isOn = true;
                        if (hasText)
                        {
                            qmb.SetActiveDisableTextField(answernumber, true);
                            oneRow.transform.Find("InputField").GetComponent <InputField>().text = _oldAnswers[answernumber];
                        }
                    }
                    answernumber++;
                }
            }
        }