/* TUIO C# Library - part of the reacTIVision project Copyright (c) 2005-2014 Martin Kaltenbrunner This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. */ using System; namespace TUIO { /** * * The TuioTime class is a simple structure that is used to reprent the time that has elapsed since the session start. * The time is internally represented as seconds and fractions of microseconds which should be more than sufficient for gesture related timing requirements. * Therefore at the beginning of a typical TUIO session the static method initSession() will set the reference time for the session. * Another important static method getSessionTime will return a TuioTime object representing the time elapsed since the session start. * The class also provides various addtional convience method, which allow some simple time arithmetics. * * * @author Martin Kaltenbrunner * @version 1.1.5 */ public class TuioTime { /** * * the time since session start in seconds */ private long seconds = 0; /** * * time fraction in microseconds */ private long micro_seconds = 0; /** * * the session start time in seconds */ private static long start_seconds = 0; /** * * start time fraction in microseconds */ private static long start_micro_seconds = 0; #region Constructors /** * * The default constructor takes no arguments and sets * the Seconds and Microseconds attributes of the newly created TuioTime both to zero. */ public TuioTime() { this.seconds = 0; this.micro_seconds = 0; } /** * * This constructor takes the provided time represented in total Milliseconds * and assigs this value to the newly created TuioTime. * * the total time in Millseconds */ public TuioTime(long msec) { this.seconds = msec / 1000; this.micro_seconds = 1000 * (msec % 1000); } /** * * This constructor takes the provided time represented in Seconds and Microseconds * and assigs these value to the newly created TuioTime. * * the total time in seconds * the microseconds time component */ public TuioTime(long sec, long usec) { this.seconds = sec; this.micro_seconds = usec; } /** * * This constructor takes the provided TuioTime * and assigs its Seconds and Microseconds values to the newly created TuioTime. * * the TuioTime used to copy */ public TuioTime(TuioTime ttime) { this.seconds = ttime.Seconds; this.micro_seconds = ttime.Microseconds; } #endregion #region Operator Overloads /** * * Sums the provided time value represented in total Microseconds to the base TuioTime. * * the base TuioTime * the total time to add in Microseconds * the sum of this TuioTime with the provided argument in microseconds */ public static TuioTime operator +(TuioTime atime, long us) { long sec = atime.Seconds + us / 1000000; long usec = atime.Microseconds + us % 1000000; return new TuioTime(sec, usec); } /** * * Sums the provided TuioTime to the base TuioTime. * * the base TuioTime * the TuioTime to add * the sum of this TuioTime with the provided TuioTime argument */ public static TuioTime operator +(TuioTime btime, TuioTime ttime) { long sec = btime.Seconds + ttime.Seconds; long usec = btime.Microseconds + ttime.Microseconds; sec += usec / 1000000; usec = usec % 1000000; return new TuioTime(sec, usec); } /** * * Subtracts the provided time represented in Microseconds from the base TuioTime. * * the base TuioTime * the total time to subtract in Microseconds * the subtraction result of this TuioTime minus the provided time in Microseconds */ public static TuioTime operator -(TuioTime btime, long us) { long sec = btime.Seconds - us / 1000000; long usec = btime.Microseconds - us % 1000000; if (usec < 0) { usec += 1000000; sec--; } return new TuioTime(sec, usec); } /** * * Subtracts the provided TuioTime from the private Seconds and Microseconds attributes. * * the base TuioTime * the TuioTime to subtract * the subtraction result of this TuioTime minus the provided TuioTime */ public static TuioTime operator -(TuioTime btime, TuioTime ttime) { long sec = btime.Seconds - ttime.Seconds; long usec = btime.Microseconds - ttime.Microseconds; if (usec < 0) { usec += 1000000; sec--; } return new TuioTime(sec, usec); } /** * * Takes a TuioTime argument and compares the provided TuioTime to the private Seconds and Microseconds attributes. * * the TuioTime to compare * true if the two TuioTime have equal Seconds and Microseconds attributes */ public bool Equals(TuioTime ttime) { if ((seconds == ttime.Seconds) && (micro_seconds == ttime.Microseconds)) return true; else return false; } #endregion /** * * Resets the seconds and micro_seconds attributes to zero. */ public void reset() { seconds = 0; micro_seconds = 0; } /** * * Returns the TuioTime Seconds component. * the TuioTime Seconds component */ public long Seconds { get { return seconds; } } /** * * Returns the TuioTime Microseconds component. * the TuioTime Microseconds component */ public long Microseconds { get { return micro_seconds; } } /** * * Returns the total TuioTime in Milliseconds. * the total TuioTime in Milliseconds */ public long TotalMilliseconds { get { return seconds * 1000 + micro_seconds / 1000; } } /** * * This static method globally resets the TUIO session time. */ public static void initSession() { TuioTime startTime = SystemTime; start_seconds = startTime.Seconds; start_micro_seconds = startTime.Microseconds; } /** * * Returns the present TuioTime representing the time since session start. * the present TuioTime representing the time since session start */ public static TuioTime SessionTime { get { return SystemTime - StartTime; } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property SessionTime instead is recommended.")] public static TuioTime getSessionTime() { return SessionTime; } /** * * Returns the absolut TuioTime representing the session start. * the absolut TuioTime representing the session start */ public static TuioTime StartTime { get { return new TuioTime(start_seconds, start_micro_seconds); } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property StartTime instead is recommended.")] public static TuioTime getStartTime() { return StartTime; } /** * * Returns the absolut TuioTime representing the current system time. * the absolut TuioTime representing the current system time */ public static TuioTime SystemTime { get { long usec = DateTime.Now.Ticks / 10; return new TuioTime(usec / 1000000, usec % 1000000); } } [Obsolete("This method is provided only for compatability with legacy code. Use of the property SystemTime instead is recommended.")] public static TuioTime getSystemTime() { return SystemTime; } } }