public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Get the properties
        SerializedProperty questionsToPick = property.FindPropertyRelative(nameof(questionsToPick));
        SerializedProperty questionPool    = property.FindPropertyRelative(nameof(questionPool));

        // Layout the foldout
        property.isExpanded = EditorGUIAuto.Foldout(ref position, property.isExpanded, label);

        if (property.isExpanded)
        {
            EditorGUI.indentLevel++;

            // Display the slider only if there are questions to pick
            if (questionPool.arraySize >= 1)
            {
                questionsToPick.intValue = Mathf.Clamp(questionsToPick.intValue, 1, questionPool.arraySize);
                EditorGUIAuto.IntSlider(ref position, questionsToPick, 1, questionPool.arraySize);
            }
            // If there are no questions to pick then do not display an int slider
            else
            {
                questionsToPick.intValue = 0;
                EditorGUIAuto.PrefixedLabelField(ref position, new GUIContent(questionsToPick.displayName), "(No questions to pick)");
            }
            // Layout the question pool
            EditorGUIAuto.PropertyField(ref position, questionPool, true);

            EditorGUI.indentLevel--;
        }
    }
Beispiel #2
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Get the sub-properties
        SerializedProperty template = property.FindPropertyRelative(nameof(template));
        SerializedProperty answers  = property.FindPropertyRelative(nameof(answers));

        // Set out the foldout
        property.isExpanded = EditorGUIAuto.Foldout(ref position, property.isExpanded, label);

        if (property.isExpanded)
        {
            // Increase indent
            EditorGUI.indentLevel++;

            // Layout the template
            EditorGUIAuto.PropertyField(ref position, template);

            // If an object reference exists then layout the answers
            if (template.objectReferenceValue)
            {
                // Get the quiz template
                QuizTemplate quizTemplate = template.objectReferenceValue as QuizTemplate;
                CheckRuntimeTemplate(quizTemplate);

                // Use a button to regenerate quiz questions from the template
                GUI.enabled = quizTemplate.Dynamic;
                if (GUIAuto.Button(ref position, "Generate new questions"))
                {
                    runtimeTemplate = new QuizRuntimeTemplate(quizTemplate);
                }
                GUI.enabled = true;

                quizAnswersEditor.OnGUI(position, answers, runtimeTemplate.Questions);
                position.y += quizAnswersEditor.GetPropertyHeight(answers, runtimeTemplate.Questions);

                // Get a list of all the answers as integers
                int[] answersArray = Answers(property);
                resultsFoldout = EditorGUIAuto.Foldout(ref position, resultsFoldout, "Quiz Results");

                if (resultsFoldout)
                {
                    // Indent the next part and disable it
                    EditorGUI.indentLevel++;
                    GUI.enabled = false;

                    // Compute if the important questions passed, what the score is, and what the max score was
                    bool   passed      = QuizInstance.PassedImportantQuestions(runtimeTemplate, answersArray);
                    int    score       = QuizInstance.ComputeScoreInImportantCategories(runtimeTemplate, answersArray);
                    int    maxScore    = runtimeTemplate.GetMaximumPossibleScoreInImportantCategories();
                    string scoreString = $"{score} / {maxScore} - {(passed ? "Pass" : "Fail")}";

                    // Show the score on important questions
                    EditorGUIAuto.PrefixedLabelField(ref position, new GUIContent("Score on Important Questions"), scoreString);

                    // Compute if the unimportant questions passed, what the score is, and what the max score was
                    passed      = QuizInstance.PassedUnimportantQuestions(runtimeTemplate, answersArray);
                    score       = QuizInstance.ComputeScoreInUnimportantCategories(runtimeTemplate, answersArray);
                    maxScore    = runtimeTemplate.GetMaximumPossibleScoreInUnimportantCategories();
                    scoreString = $"{score} / {maxScore} - {(passed ? "Pass" : "Fail")}";

                    // Show the score on unimportant questions
                    EditorGUIAuto.PrefixedLabelField(ref position, new GUIContent("Score on Unimportant Questions"), scoreString);

                    // Show the final grade
                    EditorGUIAuto.PrefixedLabelField(ref position, new GUIContent("Final Grade"),
                                                     QuizInstance.ComputeGrade(runtimeTemplate, answersArray).ToString());

                    // Change the values back to before
                    EditorGUI.indentLevel--;
                    GUI.enabled = true;
                }
            }

            // Restore indent
            EditorGUI.indentLevel--;
        }
    }