TuioTime.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. TUIO C# Library - part of the reacTIVision project
  3. http://reactivision.sourceforge.net/
  4. Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. using System;
  18. namespace TUIO
  19. {
  20. /**
  21. * The TuioTime class is a simple structure that is used to reprent the time that has elapsed since the session start.
  22. * The time is internally represented as seconds and fractions of microseconds which should be more than sufficient for gesture related timing requirements.
  23. * Therefore at the beginning of a typical TUIO session the static method initSession() will set the reference time for the session.
  24. * Another important static method getSessionTime will return a TuioTime object representing the time elapsed since the session start.
  25. * The class also provides various addtional convience method, which allow some simple time arithmetics.
  26. *
  27. * @author Martin Kaltenbrunner
  28. * @version 1.4
  29. */
  30. public class TuioTime
  31. {
  32. /**
  33. * the time since session start in seconds
  34. */
  35. private long seconds = 0;
  36. /**
  37. * time fraction in microseconds
  38. */
  39. private long micro_seconds = 0;
  40. /**
  41. * the session start time in seconds
  42. */
  43. private static long start_seconds = 0;
  44. /**
  45. * start time fraction in microseconds
  46. */
  47. private static long start_micro_seconds = 0;
  48. /**
  49. * The default constructor takes no arguments and sets
  50. * the Seconds and Microseconds attributes of the newly created TuioTime both to zero.
  51. */
  52. public TuioTime()
  53. {
  54. this.seconds = 0;
  55. this.micro_seconds = 0;
  56. }
  57. /**
  58. * This constructor takes the provided time represented in total Milliseconds
  59. * and assigs this value to the newly created TuioTime.
  60. *
  61. * @param msec the total time in Millseconds
  62. */
  63. public TuioTime(long msec)
  64. {
  65. this.seconds = msec / 1000;
  66. this.micro_seconds = 1000 * (msec % 1000);
  67. }
  68. /**
  69. * This constructor takes the provided time represented in Seconds and Microseconds
  70. * and assigs these value to the newly created TuioTime.
  71. *
  72. * @param sec the total time in seconds
  73. * @param usec the microseconds time component
  74. */
  75. public TuioTime(long sec, long usec)
  76. {
  77. this.seconds = sec;
  78. this.micro_seconds = usec;
  79. }
  80. /**
  81. * This constructor takes the provided TuioTime
  82. * and assigs its Seconds and Microseconds values to the newly created TuioTime.
  83. *
  84. * @param ttime the TuioTime used to copy
  85. */
  86. public TuioTime(TuioTime ttime)
  87. {
  88. this.seconds = ttime.getSeconds();
  89. this.micro_seconds = ttime.getMicroseconds();
  90. }
  91. /**
  92. * Sums the provided time value represented in total Microseconds to the base TuioTime.
  93. *
  94. * @param btime the base TuioTime
  95. * @param us the total time to add in Microseconds
  96. * @return the sum of this TuioTime with the provided argument in microseconds
  97. */
  98. public static TuioTime operator +(TuioTime atime, long us)
  99. {
  100. long sec = atime.getSeconds() + us / 1000000;
  101. long usec = atime.getMicroseconds() + us % 1000000;
  102. return new TuioTime(sec, usec);
  103. }
  104. /**
  105. * Sums the provided TuioTime to the base TuioTime.
  106. *
  107. * @param btime the base TuioTime
  108. * @param ttime the TuioTime to add
  109. * @return the sum of this TuioTime with the provided TuioTime argument
  110. */
  111. public static TuioTime operator +(TuioTime btime, TuioTime ttime)
  112. {
  113. long sec = btime.getSeconds() + ttime.getSeconds();
  114. long usec = btime.getMicroseconds() + ttime.getMicroseconds();
  115. sec += usec / 1000000;
  116. usec = usec % 1000000;
  117. return new TuioTime(sec, usec);
  118. }
  119. /**
  120. * Subtracts the provided time represented in Microseconds from the base TuioTime.
  121. *
  122. * @param btime the base TuioTime
  123. * @param us the total time to subtract in Microseconds
  124. * @return the subtraction result of this TuioTime minus the provided time in Microseconds
  125. */
  126. public static TuioTime operator -(TuioTime btime, long us)
  127. {
  128. long sec = btime.getSeconds() - us / 1000000;
  129. long usec = btime.getMicroseconds() - us % 1000000;
  130. if (usec < 0)
  131. {
  132. usec += 1000000;
  133. sec--;
  134. }
  135. return new TuioTime(sec, usec);
  136. }
  137. /**
  138. * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes.
  139. *
  140. * @param btime the base TuioTime
  141. * @param ttime the TuioTime to subtract
  142. * @return the subtraction result of this TuioTime minus the provided TuioTime
  143. */
  144. public static TuioTime operator -(TuioTime btime, TuioTime ttime)
  145. {
  146. long sec = btime.getSeconds() - ttime.getSeconds();
  147. long usec = btime.getMicroseconds() - ttime.getMicroseconds();
  148. if (usec < 0)
  149. {
  150. usec += 1000000;
  151. sec--;
  152. }
  153. return new TuioTime(sec, usec);
  154. }
  155. /**
  156. * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes.
  157. *
  158. * @param ttime the TuioTime to compare
  159. * @return true if the two TuioTime have equal Seconds and Microseconds attributes
  160. */
  161. public bool Equals(TuioTime ttime)
  162. {
  163. if ((seconds == ttime.getSeconds()) && (micro_seconds == ttime.getMicroseconds())) return true;
  164. else return false;
  165. }
  166. /**
  167. * Resets the seconds and micro_seconds attributes to zero.
  168. */
  169. public void reset()
  170. {
  171. seconds = 0;
  172. micro_seconds = 0;
  173. }
  174. /**
  175. * Returns the TuioTime Seconds component.
  176. * @return the TuioTime Seconds component
  177. */
  178. public long getSeconds()
  179. {
  180. return seconds;
  181. }
  182. /**
  183. * Returns the TuioTime Microseconds component.
  184. * @return the TuioTime Microseconds component
  185. */
  186. public long getMicroseconds()
  187. {
  188. return micro_seconds;
  189. }
  190. /**
  191. * Returns the total TuioTime in Milliseconds.
  192. * @return the total TuioTime in Milliseconds
  193. */
  194. public long getTotalMilliseconds()
  195. {
  196. return seconds * 1000 + micro_seconds / 1000;
  197. }
  198. /**
  199. * This static method globally resets the TUIO session time.
  200. */
  201. public static void initSession()
  202. {
  203. TuioTime startTime = getSystemTime();
  204. start_seconds = startTime.getSeconds();
  205. start_micro_seconds = startTime.getMicroseconds();
  206. }
  207. /**
  208. * Returns the present TuioTime representing the time since session start.
  209. * @return the present TuioTime representing the time since session start
  210. */
  211. public static TuioTime getSessionTime()
  212. {
  213. return getSystemTime() - getStartTime();
  214. }
  215. /**
  216. * Returns the absolut TuioTime representing the session start.
  217. * @return the absolut TuioTime representing the session start
  218. */
  219. public static TuioTime getStartTime()
  220. {
  221. return new TuioTime(start_seconds, start_micro_seconds);
  222. }
  223. /**
  224. * Returns the absolut TuioTime representing the current system time.
  225. * @return the absolut TuioTime representing the current system time
  226. */
  227. public static TuioTime getSystemTime()
  228. {
  229. long usec = DateTime.Now.Ticks / 10;
  230. return new TuioTime(usec / 1000000, usec % 1000000);
  231. }
  232. public override String ToString()
  233. {
  234. int mins = (int)((seconds % 3600) / 60);
  235. int secs = (int)(seconds % 60);
  236. int msecs = (int)(micro_seconds % 1000);
  237. return mins + ":" + secs + ":" + msecs;
  238. }
  239. }
  240. }