TuioTime.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * the time since session start in seconds
  33. */
  34. private long seconds = 0;
  35. /**
  36. * time fraction in microseconds
  37. */
  38. private long micro_seconds = 0;
  39. /**
  40. * the session start time in seconds
  41. */
  42. private static long start_seconds = 0;
  43. /**
  44. * start time fraction in microseconds
  45. */
  46. private static long start_micro_seconds = 0;
  47. /**
  48. * The default constructor takes no arguments and sets
  49. * the Seconds and Microseconds attributes of the newly created TuioTime both to zero.
  50. */
  51. public TuioTime () {
  52. this.seconds = 0;
  53. this.micro_seconds = 0;
  54. }
  55. /**
  56. * This constructor takes the provided time represented in total Milliseconds
  57. * and assigs this value to the newly created TuioTime.
  58. *
  59. * @param msec the total time in Millseconds
  60. */
  61. public TuioTime (long msec) {
  62. this.seconds = msec/1000;
  63. this.micro_seconds = 1000*(msec%1000);
  64. }
  65. /**
  66. * This constructor takes the provided time represented in Seconds and Microseconds
  67. * and assigs these value to the newly created TuioTime.
  68. *
  69. * @param sec the total time in seconds
  70. * @param usec the microseconds time component
  71. */
  72. public TuioTime (long sec, long usec) {
  73. this.seconds = sec;
  74. this.micro_seconds = usec;
  75. }
  76. /**
  77. * This constructor takes the provided TuioTime
  78. * and assigs its Seconds and Microseconds values to the newly created TuioTime.
  79. *
  80. * @param ttime the TuioTime used to copy
  81. */
  82. public TuioTime (TuioTime ttime) {
  83. this.seconds = ttime.getSeconds();
  84. this.micro_seconds = ttime.getMicroseconds();
  85. }
  86. /**
  87. * Sums the provided time value represented in total Microseconds to the base TuioTime.
  88. *
  89. * @param btime the base TuioTime
  90. * @param us the total time to add in Microseconds
  91. * @return the sum of this TuioTime with the provided argument in microseconds
  92. */
  93. public static TuioTime operator + (TuioTime atime, long us) {
  94. long sec = atime.getSeconds() + us/1000000;
  95. long usec = atime.getMicroseconds() + us%1000000;
  96. return new TuioTime(sec,usec);
  97. }
  98. /**
  99. * Sums the provided TuioTime to the base TuioTime.
  100. *
  101. * @param btime the base TuioTime
  102. * @param ttime the TuioTime to add
  103. * @return the sum of this TuioTime with the provided TuioTime argument
  104. */
  105. public static TuioTime operator + (TuioTime btime, TuioTime ttime) {
  106. long sec = btime.getSeconds() + ttime.getSeconds();
  107. long usec = btime.getMicroseconds() + ttime.getMicroseconds();
  108. sec += usec/1000000;
  109. usec = usec%1000000;
  110. return new TuioTime(sec,usec);
  111. }
  112. /**
  113. * Subtracts the provided time represented in Microseconds from the base TuioTime.
  114. *
  115. * @param btime the base TuioTime
  116. * @param us the total time to subtract in Microseconds
  117. * @return the subtraction result of this TuioTime minus the provided time in Microseconds
  118. */
  119. public static TuioTime operator - (TuioTime btime, long us) {
  120. long sec = btime.getSeconds() - us/1000000;
  121. long usec = btime.getMicroseconds() - us%1000000;
  122. if (usec<0) {
  123. usec += 1000000;
  124. sec--;
  125. }
  126. return new TuioTime(sec,usec);
  127. }
  128. /**
  129. * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes.
  130. *
  131. * @param btime the base TuioTime
  132. * @param ttime the TuioTime to subtract
  133. * @return the subtraction result of this TuioTime minus the provided TuioTime
  134. */
  135. public static TuioTime operator - (TuioTime btime, TuioTime ttime) {
  136. long sec = btime.getSeconds() - ttime.getSeconds();
  137. long usec = btime.getMicroseconds() - ttime.getMicroseconds();
  138. if (usec<0) {
  139. usec += 1000000;
  140. sec--;
  141. }
  142. return new TuioTime(sec,usec);
  143. }
  144. /**
  145. * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes.
  146. *
  147. * @param ttime the TuioTime to compare
  148. * @return true if the two TuioTime have equal Seconds and Microseconds attributes
  149. */
  150. public bool Equals(TuioTime ttime) {
  151. if ((seconds==ttime.getSeconds()) && (micro_seconds==ttime.getMicroseconds())) return true;
  152. else return false;
  153. }
  154. /**
  155. * Resets the seconds and micro_seconds attributes to zero.
  156. */
  157. public void reset() {
  158. seconds = 0;
  159. micro_seconds = 0;
  160. }
  161. /**
  162. * Returns the TuioTime Seconds component.
  163. * @return the TuioTime Seconds component
  164. */
  165. public long getSeconds() {
  166. return seconds;
  167. }
  168. /**
  169. * Returns the TuioTime Microseconds component.
  170. * @return the TuioTime Microseconds component
  171. */
  172. public long getMicroseconds() {
  173. return micro_seconds;
  174. }
  175. /**
  176. * Returns the total TuioTime in Milliseconds.
  177. * @return the total TuioTime in Milliseconds
  178. */
  179. public long getTotalMilliseconds() {
  180. return seconds*1000+micro_seconds/1000;
  181. }
  182. /**
  183. * This static method globally resets the TUIO session time.
  184. */
  185. public static void initSession() {
  186. TuioTime startTime = getSystemTime();
  187. start_seconds = startTime.getSeconds();
  188. start_micro_seconds = startTime.getMicroseconds();
  189. }
  190. /**
  191. * Returns the present TuioTime representing the time since session start.
  192. * @return the present TuioTime representing the time since session start
  193. */
  194. public static TuioTime getSessionTime() {
  195. return getSystemTime()-getStartTime();
  196. }
  197. /**
  198. * Returns the absolut TuioTime representing the session start.
  199. * @return the absolut TuioTime representing the session start
  200. */
  201. public static TuioTime getStartTime() {
  202. return new TuioTime(start_seconds,start_micro_seconds);
  203. }
  204. /**
  205. * Returns the absolut TuioTime representing the current system time.
  206. * @return the absolut TuioTime representing the current system time
  207. */
  208. public static TuioTime getSystemTime() {
  209. long usec = DateTime.Now.Ticks/10;
  210. return new TuioTime(usec/1000000,usec%1000000);
  211. }
  212. public String ToString()
  213. {
  214. int mins = (int)((seconds % 3600) / 60);
  215. int secs = (int)(seconds % 60);
  216. int msecs = (int)(micro_seconds % 1000);
  217. return mins + ":" + secs + ":" + msecs;
  218. }
  219. }
  220. }