-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThinksyQuestionDisplay.cs
More file actions
168 lines (144 loc) · 5.31 KB
/
ThinksyQuestionDisplay.cs
File metadata and controls
168 lines (144 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ThinksyQuestionDisplay : MonoBehaviour
{
public GameObject questionTextPrefab;
public GameObject questionImagePrefab;
public Transform questionParent;
[Range(0f, 0.5f)]
public float indentMultipleChoices = 0.15f;
[Range(0f, 0.03f)]
public float spaceImageAreas = 0.01f;
public int imagesPerRow = 10;
private GameObject[] richTextAreas = new GameObject[0];
private static uint displayedCategoryNumber;
void Awake()
{
if (imagesPerRow <= 0)
Debug.LogWarning ("imagesPerRow should not be zero or negative");
}
void Start()
{
ThinksyQuestionDisplay.DisplayCurrentQuestion ();
}
/// <summary>
/// Displays ThinksyPlugin.GetMostRecentProblem() on ALL Question displays.
/// </summary>
public static void DisplayCurrentQuestion()
{
ThinksyQuestionDisplay[] instances = FindObjectsOfType<ThinksyQuestionDisplay> ();
foreach (ThinksyQuestionDisplay questionDisplay in instances)
{
questionDisplay.InstanceDisplayCurrentQuestion ();
}
}
private void InstanceDisplayCurrentQuestion()
{
DisplayProblem (ThinksyPlugin.GetMostRecentProblem());
}
/// <summary>
/// Displays the problem on this question display only.
/// </summary>
/// <param name="problemToDisplay">Problem to display.</param>
public void DisplayProblem(Problem problemToDisplay)
{
ClearRichTextAreas ();
PopulateRichTextAreas (problemToDisplay);
InvokeCategoryAdvancementIfNeeded (problemToDisplay);
}
private void InvokeCategoryAdvancementIfNeeded(Problem problemToDisplay)
{
//Debug.Log ("new: " + problemToDisplay.GetCategoryNumber () + " old: " + displayedCategoryNumber);
if (problemToDisplay.GetCategoryNumber() > displayedCategoryNumber)
{
ThinksyEvents.InvokeCategoryAdvancement();
}
displayedCategoryNumber = problemToDisplay.GetCategoryNumber ();
}
private void ClearRichTextAreas()
{
foreach(GameObject textArea in richTextAreas)
{
Destroy(textArea);
}
richTextAreas = new GameObject[0];
}
private void PopulateRichTextAreas(Problem problemToDisplay)
{
Question question = problemToDisplay.GetQuestion ();
int textAreaCount = question.GetQuestionPartCount ();
float ySpacePerArea = 1f / (float)(textAreaCount - question.GetMultipleChoiceLetterCount());
float xSpacePerImage = (float)1/Mathf.Clamp((float)question.GetMaximumQuestionPartRepeated(), 1, imagesPerRow);
float ySpacePerImage = (float)1 / Mathf.Ceil((float)question.GetMaximumQuestionPartRepeated()
/ (float)imagesPerRow);
int row = 0;
bool previousAreaWasMultipleChoice = false;
if (textAreaCount == 0)
{
Senseix.Logger.BasicLog("I got a problem with no question atoms.");
return;//throw new UnityException ("I got a problem with no question atoms.");
}
richTextAreas = new GameObject[textAreaCount];
for (int i = 0; i < textAreaCount; i++)
{
GameObject newArea = Instantiate(questionTextPrefab) as GameObject;
ProblemPart problemPart = problemToDisplay.GetQuestion().GetQuestionPart(i);
if (newArea.GetComponent<RectTransform>() == null ||
newArea.GetComponent<Text>() == null )
throw new UnityException("richTextArea must have a rect transform and Text");
if (problemPart.HasString())
{
newArea.GetComponent<Text>().text += problemPart.GetString();
}
float indentedX = 0f;
if (previousAreaWasMultipleChoice)
indentedX = indentMultipleChoices;
newArea.GetComponent<RectTransform>().SetParent(questionParent);
PositionRectTransform(newArea.GetComponent<RectTransform>(),
indentedX,
1 - (row + 1) * ySpacePerArea,
1,
1 - row * ySpacePerArea);
if (problemPart.HasSprite())
{
//squish this a little bit to provide space between image groups
newArea.GetComponent<RectTransform>().anchorMin += new Vector2(0f, spaceImageAreas);
newArea.GetComponent<RectTransform>().anchorMax -= new Vector2(0f, spaceImageAreas);
for (int j = 0; j < problemPart.TimesRepeated(); j++)
{
GameObject newImage = Instantiate(questionImagePrefab) as GameObject;
newImage.GetComponent<RectTransform>().SetParent(newArea.transform);
int imageColumn = j % imagesPerRow;
int imageRow = Mathf.FloorToInt((float)j / (float)imagesPerRow);
PositionRectTransform(newImage.GetComponent<RectTransform>(),
(imageColumn * xSpacePerImage),
1 - (imageRow + 1) * ySpacePerImage,
((imageColumn + 1) * xSpacePerImage),
1 - imageRow * ySpacePerImage);
newImage.GetComponent<Image>().sprite = problemPart.GetSprite();
}
}
richTextAreas[i] = newArea;
if (!problemPart.IsMultipleChoiceLetter())
{
row++;
previousAreaWasMultipleChoice = false;
}
else
{
previousAreaWasMultipleChoice = true;
}
}
}
private void PositionRectTransform(RectTransform positionMe, float minX, float minY, float maxX, float maxY)
{
positionMe.anchorMin = new Vector2(minX,
minY);
positionMe.anchorMax = new Vector2(maxX,
maxY);
positionMe.offsetMin = new Vector2(0f, 0f);
positionMe.offsetMax = new Vector2(0f, 0f);
positionMe.localScale = new Vector3(1f, 1f, 1f);
}
}