Skip to content

Commit 3872e72

Browse files
authored
Merge pull request #142 from ahmed-shariff/feat/block-events
Adding OnBlockBegin and OnBlockEnd UnityEvents
2 parents 06b9d26 + b8414c0 commit 3872e72

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

Assets/UXF/Scripts/Etc/Editor/SessionEditor.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,23 @@ protected override void DrawInspector()
108108

109109
EditorGUILayout.Space();
110110
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onSessionBegin")).tooltip, MessageType.Info);
111-
DrawPropertiesFromUpTo("onSessionBegin", "onTrialBegin");
111+
DrawPropertiesFromUpTo("onSessionBegin", "onBlockBegin");
112+
113+
EditorGUILayout.Space();
114+
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onBlockBegin")).tooltip, MessageType.Info);
115+
DrawPropertiesFromUpTo("onBlockBegin", "onTrialBegin");
112116

113117
EditorGUILayout.Space();
114118
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onTrialBegin")).tooltip, MessageType.Info);
115119
DrawPropertiesFromUpTo("onTrialBegin", "onTrialEnd");
116120

117121
EditorGUILayout.Space();
118122
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onTrialEnd")).tooltip, MessageType.Info);
119-
DrawPropertiesFromUpTo("onTrialEnd", "preSessionEnd");
123+
DrawPropertiesFromUpTo("onTrialEnd", "onBlockEnd");
124+
125+
EditorGUILayout.Space();
126+
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("onBlockEnd")).tooltip, MessageType.Info);
127+
DrawPropertiesFromUpTo("onBlockEnd", "preSessionEnd");
120128

121129
EditorGUILayout.Space();
122130
EditorGUILayout.HelpBox(GetTooltip(session.GetType().GetField("preSessionEnd")).tooltip, MessageType.Info);

Assets/UXF/Scripts/Etc/Events.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ public class SessionEvent : UnityEvent<Session>
1515

1616
}
1717

18+
/// <summary>
19+
/// Event containing a Block as a parameter
20+
/// </summary>
21+
[Serializable]
22+
public class BlockEvent : UnityEvent<Block>
23+
{
24+
25+
}
26+
1827
/// <summary>
1928
/// Event containing a Trial as a parameter
2029
/// </summary>

Assets/UXF/Scripts/Etc/Trial.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ public void Begin()
119119
Utilities.UXFDebugLogWarning("An item in the Tracked Objects field of the UXF session if empty (null)!");
120120
}
121121
}
122+
123+
if (this == block.firstTrial)
124+
{
125+
session.onBlockBegin.Invoke(block);
126+
}
127+
122128
session.onTrialBegin.Invoke(this);
123129
}
124130

@@ -137,6 +143,11 @@ public void End()
137143
}
138144

139145
session.onTrialEnd.Invoke(this);
146+
147+
if (this == block.lastTrial)
148+
{
149+
session.onBlockEnd.Invoke(block);
150+
}
140151
}
141152

142153
public bool CheckDataTypeIsValid(string dataName, UXFDataType dataType)

Assets/UXF/Scripts/Session.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ public class Session : MonoBehaviour, IExperimentUnit, IDataAssociatable
9393
[Tooltip("Items in this event will be triggered when the session begins. Useful generating your trials & blocks, setting up the scene, and triggering the first trial.")]
9494
public SessionEvent onSessionBegin = new SessionEvent();
9595

96+
/// <summary>
97+
/// Event(s) to trigger when a block begins. Can pass the instance of the Block as a dynamic argument
98+
/// </summary>
99+
/// <returns></returns>
100+
[Tooltip("Items in this event will be triggered each time a block begins.")]
101+
public BlockEvent onBlockBegin = new BlockEvent();
102+
96103
/// <summary>
97104
/// Event(s) to trigger when a trial begins. Can pass the instance of the Trial as a dynamic argument
98105
/// </summary>
@@ -107,6 +114,13 @@ public class Session : MonoBehaviour, IExperimentUnit, IDataAssociatable
107114
[Tooltip("Items in this event will be triggered each time a trial ends. Useful for collecting results from the trial as well as showing feedback.")]
108115
public TrialEvent onTrialEnd = new TrialEvent();
109116

117+
/// <summary>
118+
/// Event(s) to trigger when a block ends. Can pass the instance of the Block as a dynamic argument
119+
/// </summary>
120+
/// <returns></returns>
121+
[Tooltip("Items in this event will be triggered each time a block ends.")]
122+
public BlockEvent onBlockEnd = new BlockEvent();
123+
110124
/// <summary>
111125
/// Event(s) to trigger just before the session has ended. If you wish to perform any summary statistics or write any final session data this is the time to do it. Do not use this event to quit the application.
112126
/// </summary>
@@ -486,15 +500,17 @@ Trial GetFirstTrial()
486500
Trial GetLastTrial()
487501
{
488502
if (blocks.Count == 0) throw new NoSuchTrialException("There is no last trial because no blocks have been created!");
489-
503+
490504
Block lastValidBlock;
505+
Trial lastTrial;
491506
int i = blocks.Count - 1;
492507
while (i >= 0)
493508
{
494509
lastValidBlock = blocks[i];
495-
if (lastValidBlock.trials.Count > 0)
510+
lastTrial = lastValidBlock.lastTrial;
511+
if (lastTrial != null)
496512
{
497-
return lastValidBlock.trials[lastValidBlock.trials.Count - 1];
513+
return lastTrial;
498514
}
499515
i--;
500516
}

0 commit comments

Comments
 (0)