TuioTime.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 static long start_micro_seconds = 0;
  36. private static long start_seconds = 0;
  37. private long micro_seconds = 0;
  38. private long seconds = 0;
  39. /**
  40. * time fraction in microseconds
  41. */
  42. /**
  43. * the session start time in seconds
  44. */
  45. /**
  46. * start time fraction in microseconds
  47. */
  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 getSessionTime()
  99. {
  100. return getSystemTime() - getStartTime();
  101. }
  102. public static TuioTime getStartTime()
  103. {
  104. return new TuioTime(start_seconds, start_micro_seconds);
  105. }
  106. public static TuioTime getSystemTime()
  107. {
  108. long usec = DateTime.Now.Ticks / 10;
  109. return new TuioTime(usec / 1000000, usec % 1000000);
  110. }
  111. public static void initSession()
  112. {
  113. TuioTime startTime = getSystemTime();
  114. start_seconds = startTime.getSeconds();
  115. start_micro_seconds = startTime.getMicroseconds();
  116. }
  117. public static TuioTime operator -(TuioTime btime, long us)
  118. {
  119. long sec = btime.getSeconds() - us / 1000000;
  120. long usec = btime.getMicroseconds() - us % 1000000;
  121. if (usec < 0)
  122. {
  123. usec += 1000000;
  124. sec--;
  125. }
  126. return new TuioTime(sec, usec);
  127. }
  128. public static TuioTime operator -(TuioTime btime, TuioTime ttime)
  129. {
  130. long sec = btime.getSeconds() - ttime.getSeconds();
  131. long usec = btime.getMicroseconds() - ttime.getMicroseconds();
  132. if (usec < 0)
  133. {
  134. usec += 1000000;
  135. sec--;
  136. }
  137. return new TuioTime(sec, usec);
  138. }
  139. public static TuioTime operator +(TuioTime atime, long us)
  140. {
  141. long sec = atime.getSeconds() + us / 1000000;
  142. long usec = atime.getMicroseconds() + us % 1000000;
  143. return new TuioTime(sec, usec);
  144. }
  145. /**
  146. * Sums the provided TuioTime to the base TuioTime.
  147. *
  148. * @param btime the base TuioTime
  149. * @param ttime the TuioTime to add
  150. * @return the sum of this TuioTime with the provided TuioTime argument
  151. */
  152. public static TuioTime operator +(TuioTime btime, TuioTime ttime)
  153. {
  154. long sec = btime.getSeconds() + ttime.getSeconds();
  155. long usec = btime.getMicroseconds() + ttime.getMicroseconds();
  156. sec += usec / 1000000;
  157. usec = usec % 1000000;
  158. return new TuioTime(sec, usec);
  159. }
  160. /**
  161. * Subtracts the provided time represented in Microseconds from the base TuioTime.
  162. *
  163. * @param btime the base TuioTime
  164. * @param us the total time to subtract in Microseconds
  165. * @return the subtraction result of this TuioTime minus the provided time in Microseconds
  166. */
  167. /**
  168. * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes.
  169. *
  170. * @param btime the base TuioTime
  171. * @param ttime the TuioTime to subtract
  172. * @return the subtraction result of this TuioTime minus the provided TuioTime
  173. */
  174. /**
  175. * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes.
  176. *
  177. * @param ttime the TuioTime to compare
  178. * @return true if the two TuioTime have equal Seconds and Microseconds attributes
  179. */
  180. public bool Equals(TuioTime ttime)
  181. {
  182. if ((seconds == ttime.getSeconds()) && (micro_seconds == ttime.getMicroseconds())) return true;
  183. else return false;
  184. }
  185. /**
  186. * Resets the seconds and micro_seconds attributes to zero.
  187. */
  188. public long getMicroseconds()
  189. {
  190. return micro_seconds;
  191. }
  192. public long getSeconds()
  193. {
  194. return seconds;
  195. }
  196. public long getTotalMilliseconds()
  197. {
  198. return seconds * 1000 + micro_seconds / 1000;
  199. }
  200. public void reset()
  201. {
  202. seconds = 0;
  203. micro_seconds = 0;
  204. }
  205. /**
  206. * Returns the TuioTime Seconds component.
  207. * @return the TuioTime Seconds component
  208. */
  209. /**
  210. * Returns the TuioTime Microseconds component.
  211. * @return the TuioTime Microseconds component
  212. */
  213. /**
  214. * Returns the total TuioTime in Milliseconds.
  215. * @return the total TuioTime in Milliseconds
  216. */
  217. /**
  218. * This static method globally resets the TUIO session time.
  219. */
  220. /**
  221. * Returns the present TuioTime representing the time since session start.
  222. * @return the present TuioTime representing the time since session start
  223. */
  224. /**
  225. * Returns the absolut TuioTime representing the session start.
  226. * @return the absolut TuioTime representing the session start
  227. */
  228. /**
  229. * Returns the absolut TuioTime representing the current system time.
  230. * @return the absolut TuioTime representing the current system time
  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. }