UIPlayback.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. public class UIPlayback : MonoBehaviour
  6. {
  7. // Inspector variables
  8. public string logFileName = "";
  9. // Get from LoggingManager
  10. private int ParticipantID;
  11. private string logPathFolder = "";
  12. // Changes on click
  13. private int current = 0; // -1 == Rewind; 0 == Pause; 1 == Play
  14. private int countClicksRewind = 0;
  15. private int countClicksPause = 0;
  16. private int countClicksPlay = 0;
  17. private float timerRewind = 0f;
  18. private float timerPause = 0f;
  19. private float timerPlay = 0f;
  20. private string logPath;
  21. private float time = 0f;
  22. private void Start()
  23. {
  24. ParticipantID = gameObject.GetComponentInParent<LoggingManager>().ParticipantID;
  25. logPathFolder = gameObject.GetComponentInParent<LoggingManager>().LogPathFolder;
  26. logFileName = "log_" + ParticipantID + "_" + logFileName + ".csv";
  27. logPath = Path.Combine(logPathFolder, logFileName);
  28. using (FileStream stream = File.Open(logPath, FileMode.Create))
  29. {
  30. using (StreamWriter writer = new StreamWriter(stream))
  31. {
  32. writer.WriteLine(GetLogFileHeader());
  33. writer.Flush();
  34. }
  35. }
  36. Debug.Log("Created new Logfile " + logFileName);
  37. }
  38. private string GetLogFileHeader()
  39. {
  40. StringBuilder header = new StringBuilder("ParticipantID");
  41. header.Append(";Time");
  42. header.Append(";Acc Clicks Rewind");
  43. header.Append(";Acc Clicks Pause");
  44. header.Append(";Acc Clicks Play");
  45. header.Append(";Acc Time Rewind");
  46. header.Append(";Acc Time Pause");
  47. header.Append(";Acc Time Play");
  48. header.Append(";Current");
  49. return header.ToString();
  50. }
  51. public void registerOnRewindClicked()
  52. {
  53. countClicksRewind++;
  54. current = -1;
  55. }
  56. public void registerOnPauseClicked()
  57. {
  58. countClicksPause++;
  59. current = 0;
  60. }
  61. public void registerOnPlayClicked()
  62. {
  63. countClicksPlay++;
  64. current = 1;
  65. }
  66. private void FixedUpdate()
  67. {
  68. if (File.Exists(logPath))
  69. {
  70. try
  71. {
  72. using (StreamWriter writer = new StreamWriter(logPath, true))
  73. {
  74. StringBuilder line = new StringBuilder();
  75. line.Append(ParticipantID);
  76. line.Append(";" + time);
  77. line.Append(";" + countClicksRewind);
  78. line.Append(";" + countClicksPause);
  79. line.Append(";" + countClicksPlay);
  80. line.Append(";" + timerRewind);
  81. line.Append(";" + timerPause);
  82. line.Append(";" + timerPlay);
  83. line.Append(";" + current);
  84. writer.WriteLine(line);
  85. writer.Flush();
  86. }
  87. }
  88. catch (Exception e)
  89. {
  90. throw new ApplicationException("Something went wrong by writing into a csv file: ", e);
  91. }
  92. }
  93. time += Time.deltaTime;
  94. if (current == -1)
  95. timerRewind += Time.deltaTime;
  96. else if(current == 0)
  97. timerPause += Time.deltaTime;
  98. else if (current == 1)
  99. timerPlay += Time.deltaTime;
  100. }
  101. }