From f0c00a8da87bea194024c97278634647efb9ef7a Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Fri, 14 Nov 2014 20:38:44 +1100 Subject: [PATCH 01/40] Starting TimeLibrary --- .../KSPDateTimeUnitTests.csproj | 83 ++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++ KSPDateTimeUnitTests/UnitTest1.cs | 20 ++++ .../SharedStuff/KSPDateTime.cs | 98 +++++++++++++++++++ .../SharedStuff/KSPTimeSpan.cs | 53 ++++++++++ .../TransferWindowPlanner.csproj | 2 + .../TransferWindowPlanner.sln | 10 +- 7 files changed, 300 insertions(+), 2 deletions(-) create mode 100644 KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj create mode 100644 KSPDateTimeUnitTests/Properties/AssemblyInfo.cs create mode 100644 KSPDateTimeUnitTests/UnitTest1.cs create mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTime.cs create mode 100644 TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs diff --git a/KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj b/KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj new file mode 100644 index 0000000..30a7794 --- /dev/null +++ b/KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj @@ -0,0 +1,83 @@ + + + + Debug + AnyCPU + {5EB393E9-C2DA-469B-AAC6-E89032EFB4D4} + Library + Properties + KSPDateTimeUnitTests + KSPDateTimeUnitTests + v4.5 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/KSPDateTimeUnitTests/Properties/AssemblyInfo.cs b/KSPDateTimeUnitTests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..bff27c7 --- /dev/null +++ b/KSPDateTimeUnitTests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("KSPDateTimeUnitTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("KSPDateTimeUnitTests")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("66da5f46-d647-407a-ab1e-e800f24ad604")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs new file mode 100644 index 0000000..dc47b0b --- /dev/null +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -0,0 +1,20 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + + +namespace KSPDateTimeUnitTests +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestDateTimeSpan() + { + Double DateUT = 300; + + + + + } + } +} diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs new file mode 100644 index 0000000..e0beafd --- /dev/null +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace KSPPluginFramework +{ + public struct KSPDateTime + { + //Define the Epoch + static public int EpochDay { get; set; } + static public int EpochYear { get; set; } + + //Define the Calendar + static public int SecondsPerMinute { get; set; } + static public int MinutesPerHour { get; set; } + static public int HoursPerDay { get; set; } + static public int DaysPerYear { get; set; } + + static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } + static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } + static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + + + //Descriptors of DateTime + public int Day { get; set; } + public int Hour { get; set; } + public int Millisecond { get; set; } + public int Minute { get; set; } + public int Second { get; set; } + public int Year { get; set; } + + /// + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// + public Double UT { get; set; } + + + #region Constructors + static public KSPDateTime() + { + EpochYear = 1; + EpochDay = 1; + SecondsPerMinute = 60; + MinutesPerHour = 60; + HoursPerDay = 6; + DaysPerYear = 425; + } + + + public KSPDateTime() + { + + } + public KSPDateTime(int year, int day) + : this() + { + Year = year; Day = day; + } + public KSPDateTime(int year, int day, int hour, int minute, int second) + : this(year, day) + { + Hour = hour; Minute = minute; Second = second; + } + public KSPDateTime(int year, int day, int hour, int minute, int second, int millisecond) + : this(year, day, hour, minute, second) + { + Millisecond = millisecond; + } + + public KSPDateTime(Double ut) + : this() + { + + } + #endregion + + + + + + public KSPDateTime Date { get { return new KSPDateTime(Year,Day); } } + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % SecondsPerDay); } } + + } + + + public struct MyStruct + { + + } + + public class KSPMonth + { + public int Days { get; set; } + public String Name { get; set; } + } +} diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs new file mode 100644 index 0000000..aeb1799 --- /dev/null +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace KSPPluginFramework +{ + public struct KSPTimeSpan + { + + //Descriptors of DateTime + public int Days { get; set; } + public int Hours { get; set; } + public int Milliseconds { get; set; } + public int Minutes { get; set; } + public int Seconds { get; set; } + public int Years { get; set; } + + /// + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// + public Double UT { get; set; } + + #region Constructors + public KSPTimeSpan() + { + + } + public KSPTimeSpan(int hour, int minute, int second) + : this() + { + Hours = hour; Minutes = minute; Seconds = second; + } + public KSPTimeSpan(int year, int day, int hour, int minute, int second) + : this(hour, minute,second) + { + Years = year; Days=day; + } + public KSPTimeSpan(int year, int day, int hour, int minute, int second, int millisecond) + : this(year, day, hour, minute, second) + { + Milliseconds = millisecond; + } + + public KSPTimeSpan(Double ut) + : this() + { + + } + #endregion + + } +} diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 22fce49..35a1305 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -52,6 +52,8 @@ + + diff --git a/TransferWindowPlanner/TransferWindowPlanner.sln b/TransferWindowPlanner/TransferWindowPlanner.sln index b0cf0d0..d8c32a9 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.sln +++ b/TransferWindowPlanner/TransferWindowPlanner.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Express 2013 for Windows Desktop -VisualStudioVersion = 12.0.30110.0 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransferWindowPlanner", "TransferWindowPlanner.csproj", "{942B92F0-C682-424E-814E-C2DDD1E84E2F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KSPDateTimeUnitTests", "..\KSPDateTimeUnitTests\KSPDateTimeUnitTests.csproj", "{5EB393E9-C2DA-469B-AAC6-E89032EFB4D4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {942B92F0-C682-424E-814E-C2DDD1E84E2F}.Debug|Any CPU.Build.0 = Debug|Any CPU {942B92F0-C682-424E-814E-C2DDD1E84E2F}.Release|Any CPU.ActiveCfg = Release|Any CPU {942B92F0-C682-424E-814E-C2DDD1E84E2F}.Release|Any CPU.Build.0 = Release|Any CPU + {5EB393E9-C2DA-469B-AAC6-E89032EFB4D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5EB393E9-C2DA-469B-AAC6-E89032EFB4D4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5EB393E9-C2DA-469B-AAC6-E89032EFB4D4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5EB393E9-C2DA-469B-AAC6-E89032EFB4D4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 94d4cec5a37baeb5478030a933f413802e2cc575 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sat, 15 Nov 2014 21:27:55 +1100 Subject: [PATCH 02/40] Before Extracting Static stuff to Separate Class --- .../KSPDateTimeUnitTests.csproj | 6 + KSPDateTimeUnitTests/UnitTest1.cs | 62 +++++- .../SharedStuff/KSPDateTime.cs | 194 ++++++++++++++++-- .../SharedStuff/KSPDateTimeSpanAbstract.cs | 104 ++++++++++ .../SharedStuff/KSPDateTimeStructure.cs | 34 +++ .../SharedStuff/KSPTimeSpan.cs | 2 +- .../TransferWindowPlanner.csproj | 2 + 7 files changed, 383 insertions(+), 21 deletions(-) create mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs create mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs diff --git a/KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj b/KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj index 30a7794..7000c46 100644 --- a/KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj +++ b/KSPDateTimeUnitTests/KSPDateTimeUnitTests.csproj @@ -53,6 +53,12 @@ + + + {942b92f0-c682-424e-814e-c2ddd1e84e2f} + TransferWindowPlanner + + diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index dc47b0b..dc4fca9 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -1,6 +1,8 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using TransferWindowPlanner; +using KSPPluginFramework; namespace KSPDateTimeUnitTests { @@ -8,13 +10,67 @@ namespace KSPDateTimeUnitTests public class UnitTest1 { [TestMethod] - public void TestDateTimeSpan() + public void TestDateTime() { - Double DateUT = 300; - + Double DateUT = 301.123; + KSPDateTime dt = new KSPDateTime(DateUT); + Assert.AreEqual(5, dt.Minute); + Assert.AreEqual(1, dt.Second); + Assert.AreEqual(123, dt.Millisecond); + Assert.AreEqual(0, dt.Hour); + Assert.AreEqual(1, dt.Day); + Assert.AreEqual(1, dt.Year); + dt.Millisecond = 456; + Assert.AreEqual(5, dt.Minute); + Assert.AreEqual(1, dt.Second); + Assert.AreEqual(456, dt.Millisecond); + + dt.Second = 68; + Assert.AreEqual(6, dt.Minute); + Assert.AreEqual(8, dt.Second); + Assert.AreEqual(456, dt.Millisecond); + + + dt.Year = 2; + Assert.AreEqual(2, dt.Year,"Hello"); + dt.Day = 50; + Assert.AreEqual(50, dt.Day); + + } + + [TestMethod] + public void TestAbstract() + { + + Double DateUT = 301.123; + + KSPDateTime2 dt = new KSPDateTime2(); + dt.UT = DateUT; + Assert.AreEqual(5, dt.Minute); + Assert.AreEqual(1, dt.Second); + Assert.AreEqual(123, dt.Millisecond); + Assert.AreEqual(0, dt.Hour); + Assert.AreEqual(1, dt.Day); + Assert.AreEqual(1, dt.Year); + + dt.Millisecond = 456; + Assert.AreEqual(5, dt.Minute); + Assert.AreEqual(1, dt.Second); + Assert.AreEqual(456, dt.Millisecond); + + dt.Second = 68; + Assert.AreEqual(6, dt.Minute); + Assert.AreEqual(8, dt.Second); + Assert.AreEqual(456, dt.Millisecond); + + + dt.Year = 2; + Assert.AreEqual(2, dt.Year, "Hello"); + dt.Day = 50; + Assert.AreEqual(50, dt.Day); } } } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index e0beafd..bc23d6e 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -5,10 +5,10 @@ namespace KSPPluginFramework { - public struct KSPDateTime + public class KSPDateTime { //Define the Epoch - static public int EpochDay { get; set; } + static public int EpochDay { get { return KSPDateTimeStructure.EpochDay; } set { KSPDateTimeStructure.EpochDay = value; } } static public int EpochYear { get; set; } //Define the Calendar @@ -22,22 +22,43 @@ public struct KSPDateTime static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } - //Descriptors of DateTime - public int Day { get; set; } - public int Hour { get; set; } - public int Millisecond { get; set; } - public int Minute { get; set; } - public int Second { get; set; } - public int Year { get; set; } + + //Descriptors of DateTime - uses UT as the Root value + public int Year { + get { return EpochYear + (Int32)UT / SecondsPerYear; } + set { UT = UT - (Year - EpochYear) * SecondsPerYear + (value - EpochYear) * SecondsPerYear; } + } + public int Day { + get { return EpochDay + (Int32)UT / SecondsPerDay % SecondsPerYear; } + set { UT = UT - (Day-EpochDay) * SecondsPerDay + (value - EpochDay) * SecondsPerDay; } + } + public int Hour { + get { return (Int32)UT / SecondsPerHour % SecondsPerDay; } + set { UT = UT - Hour * SecondsPerHour + value * SecondsPerHour; } + } + public int Minute { + get { return (Int32)UT / SecondsPerMinute % SecondsPerHour; } + set { UT = UT - Minute * SecondsPerMinute + value* SecondsPerMinute; } + } + public int Second { + get { return (Int32)UT % SecondsPerMinute; } + set { UT = UT - Second + value; } + } + public int Millisecond { + get { return (Int32) (Math.Round(UT - Math.Floor(UT), 3) * 1000); } + set { UT = Math.Floor(UT) + ((Double)value / 1000); } + } /// /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 /// - public Double UT { get; set; } + public Double UT { get { return _UT; } set { _UT = value; } } + + private Double _UT; #region Constructors - static public KSPDateTime() + static KSPDateTime() { EpochYear = 1; EpochDay = 1; @@ -50,6 +71,7 @@ static public KSPDateTime() public KSPDateTime() { + _UT = 0; } public KSPDateTime(int year, int day) @@ -71,25 +93,163 @@ public KSPDateTime(int year, int day, int hour, int minute, int second, int mill public KSPDateTime(Double ut) : this() { - + UT = ut; } #endregion + #region Calculated Properties + public KSPDateTime Date { get { return new KSPDateTime(Year, Day); } } + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % SecondsPerDay); } } + public static KSPDateTime Now { + get { return new KSPDateTime(Planetarium.GetUniversalTime()); } + } + public static KSPDateTime Today { + get { return new KSPDateTime(Planetarium.GetUniversalTime()).Date; } + } + #endregion - public KSPDateTime Date { get { return new KSPDateTime(Year,Day); } } - public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % SecondsPerDay); } } - } + #region Instance Methods + #region Mathematic Methods + public KSPDateTime Add(KSPTimeSpan value) + { + return new KSPDateTime(UT + value.UT); + } + public KSPDateTime AddYears(Double value) + { + return new KSPDateTime(UT + value * SecondsPerYear); + } + public KSPDateTime AddDays(Double value) + { + return new KSPDateTime(UT + value * SecondsPerDay); + } + public KSPDateTime AddHours(Double value) + { + return new KSPDateTime(UT + value * SecondsPerHour); + } + public KSPDateTime AddMinutes(Double value) + { + return new KSPDateTime(UT + value * SecondsPerMinute); + } + public KSPDateTime AddSeconds(Double value) + { + return new KSPDateTime(UT + value); + } + public KSPDateTime AddMilliSeconds(Double value) + { + return new KSPDateTime(UT + value / 1000); + } + public KSPDateTime AddUT(Double value) + { + return new KSPDateTime(UT + value); + } + public KSPDateTime Subtract(KSPDateTime value) + { + return new KSPDateTime(UT - value.UT); + } + public KSPTimeSpan Subtract(KSPTimeSpan value) + { + return new KSPTimeSpan(UT - value.UT); + } - public struct MyStruct - { + #endregion + + + #region Comparison Methods + public Int32 CompareTo(KSPDateTime value) { + return KSPDateTime.Compare(this, value); + } + public Int32 CompareTo(System.Object value) { + return this.CompareTo((KSPDateTime)value); + } + public Boolean Equals(KSPDateTime value) { + return KSPDateTime.Equals(this, value); + } + public override bool Equals(System.Object obj) { + return this.Equals((KSPDateTime)obj); + } + #endregion + + public override int GetHashCode() + { + return UT.GetHashCode(); + } + + #endregion + + + #region Static Methods + public static Int32 Compare(KSPDateTime t1, KSPDateTime t2) + { + if (t1.UT < t2.UT) + return -1; + else if (t1.UT > t2.UT) + return 1; + else + return 0; + } + public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) + { + return t1.UT == t2.UT; + } + + + #endregion + + public static KSPTimeSpan operator -(KSPDateTime d1, KSPDateTime d2) { + return new KSPTimeSpan(d1.UT - d2.UT); + } + public static KSPDateTime operator -(KSPDateTime d, KSPTimeSpan t) { + return new KSPDateTime(d.UT - t.UT); + } + public static KSPDateTime operator +(KSPDateTime d, KSPTimeSpan t) { + return new KSPDateTime(d.UT + t.UT); + } + public static Boolean operator !=(KSPDateTime d1, KSPDateTime d2) { + return !(d1 == d2); + } + public static Boolean operator ==(KSPDateTime d1, KSPDateTime d2) { + return d1.UT == d2.UT; + } + + + + public static Boolean operator <=(KSPDateTime d1, KSPDateTime d2) { + return d1.CompareTo(d2)<=0; + } + public static Boolean operator <(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) < 0; + } + public static Boolean operator >=(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) >= 0; + } + public static Boolean operator >(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) > 0; + } + + //DaysInMonth + //Day - is Day Of Month + //DayOfYear + //IsLeapYear + + + + //From + + + //To } + + public class KSPMonth { public int Days { get; set; } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs new file mode 100644 index 0000000..f8bc41f --- /dev/null +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace TransferWindowPlanner +{ + public abstract class KSPDateTimeSpanAbstract + { + //Define the Calendar + static public int SecondsPerMinute { get; set; } + static public int MinutesPerHour { get; set; } + static public int HoursPerDay { get; set; } + static public int DaysPerYear { get; set; } + + static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } + static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } + static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + + static KSPDateTimeSpanAbstract() + { + SecondsPerMinute = 60; + MinutesPerHour = 60; + HoursPerDay = 6; + DaysPerYear = 425; + } + + //Descriptors of DateTime - uses UT as the Root value + public virtual int Year + { + get { return (Int32)UT / SecondsPerYear; } + set { UT = UT - Year * SecondsPerYear + value * SecondsPerYear; } + } + public virtual int Day + { + get { return (Int32)UT / SecondsPerDay % SecondsPerYear; } + set { UT = UT - Day * SecondsPerDay + value * SecondsPerDay; } + } + public int Hour + { + get { return (Int32)UT / SecondsPerHour % SecondsPerDay; } + set { UT = UT - Hour * SecondsPerHour + value * SecondsPerHour; } + } + public int Minute + { + get { return (Int32)UT / SecondsPerMinute % SecondsPerHour; } + set { UT = UT - Minute * SecondsPerMinute + value * SecondsPerMinute; } + } + public int Second + { + get { return (Int32)UT % SecondsPerMinute; } + set { UT = UT - Second + value; } + } + public int Millisecond + { + get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } + set { UT = Math.Floor(UT) + ((Double)value / 1000); } + } + + /// + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// + public Double UT { get { return _UT; } set { _UT = value; } } + + private Double _UT; + } + + + public class KSPDateTime2 : KSPDateTimeSpanAbstract + { + //Define the Epoch + static public int EpochDay { get; set; } + static public int EpochYear { get; set; } + + static KSPDateTime2() + { + EpochYear = 1; + EpochDay = 1; + + } + + public override int Year { + get { + return base.Year + EpochYear; + } + set { + base.Year = value - EpochYear; + } + } + public override int Day { + get { + return base.Day + EpochDay; + } + set { + base.Day = value + EpochDay; + } + } + } + public class KSPTimeSpan2 : KSPDateTimeSpanAbstract + { + + } + +} diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs new file mode 100644 index 0000000..a8e413b --- /dev/null +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace KSPPluginFramework +{ + public static class KSPDateTimeStructure + { + //Define the Epoch + static public int EpochDay { get; set; } + static public int EpochYear { get; set; } + + //Define the Calendar + static public int SecondsPerMinute { get; set; } + static public int MinutesPerHour { get; set; } + static public int HoursPerDay { get; set; } + static public int DaysPerYear { get; set; } + + static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } + static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } + static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + + static KSPDateTimeStructure() + { + EpochYear = 1; + EpochDay = 1; + SecondsPerMinute = 60; + MinutesPerHour = 60; + HoursPerDay = 6; + DaysPerYear = 425; + } + } +} diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs index aeb1799..522ef17 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -5,7 +5,7 @@ namespace KSPPluginFramework { - public struct KSPTimeSpan + public class KSPTimeSpan { //Descriptors of DateTime diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 35a1305..b9d6e23 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -53,6 +53,8 @@ + + From c5334376e6043d314ff4ec3260871885041baff5 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sat, 15 Nov 2014 22:08:43 +1100 Subject: [PATCH 03/40] Base DateTime and Timespan done - UnitTests pass --- KSPDateTimeUnitTests/UnitTest1.cs | 6 + .../SharedStuff/KSPDateTime.cs | 103 ++++------- .../SharedStuff/KSPDateTimeSpanAbstract.cs | 55 +++--- .../SharedStuff/KSPDateTimeStructure.cs | 14 +- .../SharedStuff/KSPTimeSpan.cs | 173 +++++++++++++++++- 5 files changed, 255 insertions(+), 96 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index dc4fca9..5d72dde 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -71,6 +71,12 @@ public void TestAbstract() Assert.AreEqual(2, dt.Year, "Hello"); dt.Day = 50; Assert.AreEqual(50, dt.Day); + + + KSPTimeSpan2 a = new KSPTimeSpan2(); + a.UT = 300; + a.Second = 32; + } } } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index bc23d6e..fab6292 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -7,41 +7,26 @@ namespace KSPPluginFramework { public class KSPDateTime { - //Define the Epoch - static public int EpochDay { get { return KSPDateTimeStructure.EpochDay; } set { KSPDateTimeStructure.EpochDay = value; } } - static public int EpochYear { get; set; } - - //Define the Calendar - static public int SecondsPerMinute { get; set; } - static public int MinutesPerHour { get; set; } - static public int HoursPerDay { get; set; } - static public int DaysPerYear { get; set; } - - static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } - static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } - static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } - - - + //Descriptors of DateTime - uses UT as the Root value - public int Year { - get { return EpochYear + (Int32)UT / SecondsPerYear; } - set { UT = UT - (Year - EpochYear) * SecondsPerYear + (value - EpochYear) * SecondsPerYear; } + public int Year { + get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } } - public int Day { - get { return EpochDay + (Int32)UT / SecondsPerDay % SecondsPerYear; } - set { UT = UT - (Day-EpochDay) * SecondsPerDay + (value - EpochDay) * SecondsPerDay; } + public int Day { + get { return KSPDateTimeStructure.EpochDay + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (Day - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay; } } - public int Hour { - get { return (Int32)UT / SecondsPerHour % SecondsPerDay; } - set { UT = UT - Hour * SecondsPerHour + value * SecondsPerHour; } + public int Hour { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } + set { UT = UT - Hour * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } } public int Minute { - get { return (Int32)UT / SecondsPerMinute % SecondsPerHour; } - set { UT = UT - Minute * SecondsPerMinute + value* SecondsPerMinute; } + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } + set { UT = UT - Minute * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } } - public int Second { - get { return (Int32)UT % SecondsPerMinute; } + public int Second { + get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } set { UT = UT - Second + value; } } public int Millisecond { @@ -58,17 +43,6 @@ public int Millisecond { #region Constructors - static KSPDateTime() - { - EpochYear = 1; - EpochDay = 1; - SecondsPerMinute = 60; - MinutesPerHour = 60; - HoursPerDay = 6; - DaysPerYear = 425; - } - - public KSPDateTime() { _UT = 0; @@ -100,7 +74,7 @@ public KSPDateTime(Double ut) #region Calculated Properties public KSPDateTime Date { get { return new KSPDateTime(Year, Day); } } - public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % SecondsPerDay); } } + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateTimeStructure.SecondsPerDay); } } public static KSPDateTime Now { @@ -120,19 +94,19 @@ public KSPDateTime Add(KSPTimeSpan value) } public KSPDateTime AddYears(Double value) { - return new KSPDateTime(UT + value * SecondsPerYear); + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerYear); } public KSPDateTime AddDays(Double value) { - return new KSPDateTime(UT + value * SecondsPerDay); + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerDay); } public KSPDateTime AddHours(Double value) { - return new KSPDateTime(UT + value * SecondsPerHour); + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerHour); } public KSPDateTime AddMinutes(Double value) { - return new KSPDateTime(UT + value * SecondsPerMinute); + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerMinute); } public KSPDateTime AddSeconds(Double value) { @@ -175,8 +149,7 @@ public override bool Equals(System.Object obj) { #endregion - public override int GetHashCode() - { + public override int GetHashCode() { return UT.GetHashCode(); } @@ -201,26 +174,33 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) #endregion - public static KSPTimeSpan operator -(KSPDateTime d1, KSPDateTime d2) { + #region Operators + public static KSPTimeSpan operator -(KSPDateTime d1, KSPDateTime d2) + { return new KSPTimeSpan(d1.UT - d2.UT); } - public static KSPDateTime operator -(KSPDateTime d, KSPTimeSpan t) { + public static KSPDateTime operator -(KSPDateTime d, KSPTimeSpan t) + { return new KSPDateTime(d.UT - t.UT); } - public static KSPDateTime operator +(KSPDateTime d, KSPTimeSpan t) { + public static KSPDateTime operator +(KSPDateTime d, KSPTimeSpan t) + { return new KSPDateTime(d.UT + t.UT); } - public static Boolean operator !=(KSPDateTime d1, KSPDateTime d2) { + public static Boolean operator !=(KSPDateTime d1, KSPDateTime d2) + { return !(d1 == d2); } - public static Boolean operator ==(KSPDateTime d1, KSPDateTime d2) { + public static Boolean operator ==(KSPDateTime d1, KSPDateTime d2) + { return d1.UT == d2.UT; } - public static Boolean operator <=(KSPDateTime d1, KSPDateTime d2) { - return d1.CompareTo(d2)<=0; + public static Boolean operator <=(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) <= 0; } public static Boolean operator <(KSPDateTime d1, KSPDateTime d2) { @@ -233,7 +213,8 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) public static Boolean operator >(KSPDateTime d1, KSPDateTime d2) { return d1.CompareTo(d2) > 0; - } + } + #endregion //DaysInMonth //Day - is Day Of Month @@ -241,18 +222,10 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) //IsLeapYear - - //From - - - //To + //To String Formats } - public class KSPMonth - { - public int Days { get; set; } - public String Name { get; set; } - } + } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs index f8bc41f..3cb0643 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs @@ -26,24 +26,33 @@ static KSPDateTimeSpanAbstract() } //Descriptors of DateTime - uses UT as the Root value - public virtual int Year + private int _Year; + protected virtual int Year { get { return (Int32)UT / SecondsPerYear; } - set { UT = UT - Year * SecondsPerYear + value * SecondsPerYear; } + set { + UT = UT - _Year * SecondsPerYear + value * SecondsPerYear; + _Year = value; + } } - public virtual int Day + + private int _Day; + protected virtual int Day { - get { return (Int32)UT / SecondsPerDay % SecondsPerYear; } - set { UT = UT - Day * SecondsPerDay + value * SecondsPerDay; } + get { return (Int32)UT / SecondsPerDay % DaysPerYear; } + set { + UT = UT - _Day * SecondsPerDay + value * SecondsPerDay; + _Day = value; + } } public int Hour { - get { return (Int32)UT / SecondsPerHour % SecondsPerDay; } + get { return (Int32)UT / SecondsPerHour % HoursPerDay; } set { UT = UT - Hour * SecondsPerHour + value * SecondsPerHour; } } public int Minute { - get { return (Int32)UT / SecondsPerMinute % SecondsPerHour; } + get { return (Int32)UT / SecondsPerMinute % MinutesPerHour; } set { UT = UT - Minute * SecondsPerMinute + value * SecondsPerMinute; } } public int Second @@ -79,25 +88,29 @@ static KSPDateTime2() } - public override int Year { - get { - return base.Year + EpochYear; - } - set { - base.Year = value - EpochYear; - } + new public int Year { + get { return base.Year + EpochYear; } + set { base.Year = value - EpochYear; } } - public override int Day { - get { - return base.Day + EpochDay; - } - set { - base.Day = value + EpochDay; - } + new public int Day { + get { return base.Day + EpochDay; } + set { base.Day = value - EpochDay; } } + new public int Hour { get { return base.Hour; } set { base.Hour = value; } } + new public int Minute { get { return base.Minute; } set { base.Minute = value; } } + new public int Second { get { return base.Second; } set { base.Second = value; } } + new public int Millisecond { get { return base.Millisecond; } set { base.Millisecond = value; } } + } public class KSPTimeSpan2 : KSPDateTimeSpanAbstract { + public int Years { get { return base.Year; } set { base.Year = value; } } + public int Days { get { return base.Day; } set { base.Day = value; } } + public int Hours { get { return base.Hour; } set { base.Hour = value; } } + public int Minutes { get { return base.Minute; } set { base.Minute = value; } } + public int Seconds { get { return base.Second; } set { base.Second = value; } } + public int Milliseconds { get { return base.Millisecond; } set { base.Millisecond = value; } } + } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs index a8e413b..f95f952 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs @@ -27,8 +27,18 @@ static KSPDateTimeStructure() EpochDay = 1; SecondsPerMinute = 60; MinutesPerHour = 60; - HoursPerDay = 6; - DaysPerYear = 425; + + HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; + DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; } + + + + } + + public class KSPMonth + { + public int Days { get; set; } + public String Name { get; set; } } } diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs index 522ef17..eda50a3 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -7,14 +7,37 @@ namespace KSPPluginFramework { public class KSPTimeSpan { - - //Descriptors of DateTime - public int Days { get; set; } - public int Hours { get; set; } - public int Milliseconds { get; set; } - public int Minutes { get; set; } - public int Seconds { get; set; } - public int Years { get; set; } + //Descriptors of Timespan - uses UT as the Root value + public int Years + { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - Years * KSPDateTimeStructure.SecondsPerYear + value * KSPDateTimeStructure.SecondsPerYear; } + } + public int Days + { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - Days * KSPDateTimeStructure.SecondsPerDay + value * KSPDateTimeStructure.SecondsPerDay; } + } + public int Hours + { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } + set { UT = UT - Hours * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } + } + public int Minutes + { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } + set { UT = UT - Minutes * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } + } + public int Seconds + { + get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } + set { UT = UT - Seconds + value; } + } + public int Milliseconds + { + get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } + set { UT = Math.Floor(UT) + ((Double)value / 1000); } + } /// /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 @@ -49,5 +72,139 @@ public KSPTimeSpan(Double ut) } #endregion + + #region Calculated Properties + public Double TotalMilliseconds { get { return UT * 1000; } } + public Double TotalSeconds { get { return UT; } } + public Double TotalMinutes { get { return UT / KSPDateTimeStructure.SecondsPerMinute; } } + public Double TotalHours { get { return UT / KSPDateTimeStructure.SecondsPerHour; } } + public Double TotalDays { get { return UT / KSPDateTimeStructure.SecondsPerDay; } } + public Double TotalYears { get { return UT / KSPDateTimeStructure.SecondsPerYear; } } + #endregion + + #region Instance Methods + #region Mathematic Methods + public KSPTimeSpan Add(KSPTimeSpan value) { + return new KSPTimeSpan(UT + value.UT); + } + public KSPTimeSpan Duration() { + return new KSPTimeSpan(Math.Abs(UT)); + } + public KSPTimeSpan Negate() { + return new KSPTimeSpan(UT*-1); + } + #endregion + + #region Comparison Methods + public Int32 CompareTo(KSPTimeSpan value) { + return KSPTimeSpan.Compare(this, value); + } + public Int32 CompareTo(System.Object value) { + return this.CompareTo((KSPTimeSpan)value); + } + public Boolean Equals(KSPTimeSpan value) { + return KSPTimeSpan.Equals(this, value); + } + public override bool Equals(System.Object obj) { + return this.Equals((KSPTimeSpan)obj); + } + #endregion + + + public override int GetHashCode() + { + return UT.GetHashCode(); + } + + #endregion + + + #region Static Methods + public static Int32 Compare(KSPTimeSpan t1, KSPTimeSpan t2) + { + if (t1.UT < t2.UT) + return -1; + else if (t1.UT > t2.UT) + return 1; + else + return 0; + } + public static Boolean Equals(KSPTimeSpan t1, KSPTimeSpan t2) + { + return t1.UT == t2.UT; + } + + + public static KSPTimeSpan FromYears(Double value) { + return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerYear); + } + public static KSPTimeSpan FromDays(Double value) { + return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerDay); + } + public static KSPTimeSpan FromHours(Double value) { + return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerHour); + } + public static KSPTimeSpan FromMinutes(Double value) { + return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerMinute); + } + public static KSPTimeSpan FromSeconds(Double value) { + return new KSPTimeSpan(value); + } + public static KSPTimeSpan FromMilliseconds(Double value) { + return new KSPTimeSpan(value / 1000); + } + + #endregion + + #region Operators + public static KSPTimeSpan operator -(KSPTimeSpan t1, KSPTimeSpan t2) + { + return new KSPTimeSpan(t1.UT - t2.UT); + } + public static KSPTimeSpan operator -(KSPTimeSpan t) + { + return new KSPTimeSpan(t.UT).Negate(); + } + public static KSPTimeSpan operator +(KSPTimeSpan t1, KSPTimeSpan t2) + { + return new KSPTimeSpan(t1.UT + t2.UT); + } + public static KSPTimeSpan operator +(KSPTimeSpan t) + { + return new KSPTimeSpan(t.UT); + } + + public static Boolean operator !=(KSPTimeSpan t1, KSPTimeSpan t2) + { + return !(t1 == t2); + } + public static Boolean operator ==(KSPTimeSpan t1, KSPTimeSpan t2) + { + return t1.UT == t2.UT; + } + + + + public static Boolean operator <=(KSPTimeSpan t1, KSPTimeSpan t2) + { + return t1.CompareTo(t2) <= 0; + } + public static Boolean operator <(KSPTimeSpan t1, KSPTimeSpan t2) + { + return t1.CompareTo(t2) < 0; + } + public static Boolean operator >=(KSPTimeSpan t1, KSPTimeSpan t2) + { + return t1.CompareTo(t2) >= 0; + } + public static Boolean operator >(KSPTimeSpan t1, KSPTimeSpan t2) + { + return t1.CompareTo(t2) > 0; + } + #endregion + + + + //To String Formats } } From 4807a59041a027219169922ed5201e3d0a88b28d Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sat, 15 Nov 2014 22:15:36 +1100 Subject: [PATCH 04/40] Updating test playlist --- TransferWindowPlanner/DateAndTimeTests.playlist | 1 + 1 file changed, 1 insertion(+) create mode 100644 TransferWindowPlanner/DateAndTimeTests.playlist diff --git a/TransferWindowPlanner/DateAndTimeTests.playlist b/TransferWindowPlanner/DateAndTimeTests.playlist new file mode 100644 index 0000000..e1d2ba6 --- /dev/null +++ b/TransferWindowPlanner/DateAndTimeTests.playlist @@ -0,0 +1 @@ + \ No newline at end of file From 1cb51aa3c1b9b15ef362ba32ecb9092411548fc4 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sun, 16 Nov 2014 16:19:16 +1100 Subject: [PATCH 05/40] Got basics done with single maths - now to remove sets :) --- .../SharedStuff/KSPDateTime.cs | 77 +++--- .../SharedStuff/KSPDateTime1.cs | 231 ++++++++++++++++++ .../SharedStuff/KSPTimeSpan.cs | 62 ++--- .../TransferWindowPlanner.csproj | 1 + 4 files changed, 314 insertions(+), 57 deletions(-) create mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTime1.cs diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index fab6292..aa3b14f 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -7,42 +7,59 @@ namespace KSPPluginFramework { public class KSPDateTime { - + //Define the Epoch + static public int EpochDay { get; set; } + static public int EpochYear { get; set; } + + //Define the Calendar + static public int SecondsPerMinute { get { return KSPTimeSpan.SecondsPerMinute; } set { KSPTimeSpan.SecondsPerMinute = value; } } + static public int MinutesPerHour { get { return KSPTimeSpan.MinutesPerHour; } set { KSPTimeSpan.MinutesPerHour = value; } } + static public int HoursPerDay { get { return KSPTimeSpan.HoursPerDay; } set { KSPTimeSpan.HoursPerDay = value; } } + static public int DaysPerYear { get; set; } + + static public int SecondsPerHour { get { return KSPTimeSpan.SecondsPerHour; } } + static public int SecondsPerDay { get { return KSPTimeSpan.SecondsPerDay; } } + static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + //Descriptors of DateTime - uses UT as the Root value public int Year { - get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } - } - public int Day { - get { return KSPDateTimeStructure.EpochDay + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (Day - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay; } + get { return EpochYear + (Int32)UT / SecondsPerYear; } + set { UT = UT - (Year - EpochYear) * SecondsPerYear + (value - EpochYear) * SecondsPerYear; } } - public int Hour { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } - set { UT = UT - Hour * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } + public int DayOfYear { + get { return EpochDay + (Int32)UT / SecondsPerDay % SecondsPerYear; } + set { UT = UT - (DayOfYear - EpochDay) * SecondsPerDay + (value - EpochDay) * SecondsPerDay; } } - public int Minute { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } - set { UT = UT - Minute * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } - } - public int Second { - get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } - set { UT = UT - Second + value; } - } - public int Millisecond { - get { return (Int32) (Math.Round(UT - Math.Floor(UT), 3) * 1000); } - set { UT = Math.Floor(UT) + ((Double)value / 1000); } + public int Day + { + } + public int Hour { get { return _TimeSpan.Hours; } set { _TimeSpan.Hours = value; } } + public int Minute { get { return _TimeSpan.Minutes; } set { _TimeSpan.Minutes = value; } } + public int Second { get { return _TimeSpan.Seconds; } set { _TimeSpan.Seconds = value; } } + public int Millisecond { get { return _TimeSpan.Milliseconds; } set { _TimeSpan.Milliseconds = value;} } + /// /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 /// - public Double UT { get { return _UT; } set { _UT = value; } } + public Double UT { + get { return _TimeSpan.UT; } + set { _TimeSpan = new KSPTimeSpan(value); } + } private Double _UT; - + private KSPTimeSpan _TimeSpan; #region Constructors + static KSPDateTime() + { + EpochYear = 1; + EpochDay = 1; + + DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; + } + public KSPDateTime() { _UT = 0; @@ -74,7 +91,7 @@ public KSPDateTime(Double ut) #region Calculated Properties public KSPDateTime Date { get { return new KSPDateTime(Year, Day); } } - public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateTimeStructure.SecondsPerDay); } } + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % SecondsPerDay); } } public static KSPDateTime Now { @@ -92,21 +109,23 @@ public KSPDateTime Add(KSPTimeSpan value) { return new KSPDateTime(UT + value.UT); } - public KSPDateTime AddYears(Double value) + public KSPDateTime AddYears(Int32 value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerYear); + KSPDateTime dteReturn = new KSPDateTime(UT); + dteReturn.Year+=value; + return dteReturn; } public KSPDateTime AddDays(Double value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerDay); + return new KSPDateTime(UT + value * SecondsPerDay); } public KSPDateTime AddHours(Double value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerHour); + return new KSPDateTime(UT + value * SecondsPerHour); } public KSPDateTime AddMinutes(Double value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerMinute); + return new KSPDateTime(UT + value * SecondsPerMinute); } public KSPDateTime AddSeconds(Double value) { diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs new file mode 100644 index 0000000..9b4c630 --- /dev/null +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs @@ -0,0 +1,231 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace KSPPluginFramework +{ + public class KSPDateTime1 + { + + //Descriptors of DateTime - uses UT as the Root value + public int Year { + get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } + } + public int Day { + get { return KSPDateTimeStructure.EpochDay + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (Day - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay; } + } + public int Hour { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } + set { UT = UT - Hour * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } + } + public int Minute { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } + set { UT = UT - Minute * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } + } + public int Second { + get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } + set { UT = UT - Second + value; } + } + public int Millisecond { + get { return (Int32) (Math.Round(UT - Math.Floor(UT), 3) * 1000); } + set { UT = Math.Floor(UT) + ((Double)value / 1000); } + } + + /// + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// + public Double UT { get { return _UT; } set { _UT = value; } } + + private Double _UT; + + + #region Constructors + public KSPDateTime1() + { + _UT = 0; + + } + public KSPDateTime1(int year, int day) + : this() + { + Year = year; Day = day; + } + public KSPDateTime1(int year, int day, int hour, int minute, int second) + : this(year, day) + { + Hour = hour; Minute = minute; Second = second; + } + public KSPDateTime1(int year, int day, int hour, int minute, int second, int millisecond) + : this(year, day, hour, minute, second) + { + Millisecond = millisecond; + } + + public KSPDateTime1(Double ut) + : this() + { + UT = ut; + } + #endregion + + + #region Calculated Properties + public KSPDateTime1 Date { get { return new KSPDateTime1(Year, Day); } } + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateTimeStructure.SecondsPerDay); } } + + + public static KSPDateTime1 Now { + get { return new KSPDateTime1(Planetarium.GetUniversalTime()); } + } + public static KSPDateTime1 Today { + get { return new KSPDateTime1(Planetarium.GetUniversalTime()).Date; } + } + #endregion + + + #region Instance Methods + #region Mathematic Methods + public KSPDateTime1 Add(KSPTimeSpan value) + { + return new KSPDateTime1(UT + value.UT); + } + public KSPDateTime1 AddYears(Double value) + { + return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerYear); + } + public KSPDateTime1 AddDays(Double value) + { + return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerDay); + } + public KSPDateTime1 AddHours(Double value) + { + return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerHour); + } + public KSPDateTime1 AddMinutes(Double value) + { + return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerMinute); + } + public KSPDateTime1 AddSeconds(Double value) + { + return new KSPDateTime1(UT + value); + } + public KSPDateTime1 AddMilliSeconds(Double value) + { + return new KSPDateTime1(UT + value / 1000); + } + public KSPDateTime1 AddUT(Double value) + { + return new KSPDateTime1(UT + value); + } + + public KSPDateTime1 Subtract(KSPDateTime1 value) + { + return new KSPDateTime1(UT - value.UT); + } + public KSPTimeSpan Subtract(KSPTimeSpan value) + { + return new KSPTimeSpan(UT - value.UT); + } + + #endregion + + + #region Comparison Methods + public Int32 CompareTo(KSPDateTime1 value) { + return KSPDateTime1.Compare(this, value); + } + public Int32 CompareTo(System.Object value) { + return this.CompareTo((KSPDateTime1)value); + } + public Boolean Equals(KSPDateTime1 value) { + return KSPDateTime1.Equals(this, value); + } + public override bool Equals(System.Object obj) { + return this.Equals((KSPDateTime1)obj); + } + #endregion + + + public override int GetHashCode() { + return UT.GetHashCode(); + } + + #endregion + + + #region Static Methods + public static Int32 Compare(KSPDateTime1 t1, KSPDateTime1 t2) + { + if (t1.UT < t2.UT) + return -1; + else if (t1.UT > t2.UT) + return 1; + else + return 0; + } + public static Boolean Equals(KSPDateTime1 t1, KSPDateTime1 t2) + { + return t1.UT == t2.UT; + } + + + #endregion + + #region Operators + public static KSPTimeSpan operator -(KSPDateTime1 d1, KSPDateTime1 d2) + { + return new KSPTimeSpan(d1.UT - d2.UT); + } + public static KSPDateTime1 operator -(KSPDateTime1 d, KSPTimeSpan t) + { + return new KSPDateTime1(d.UT - t.UT); + } + public static KSPDateTime1 operator +(KSPDateTime1 d, KSPTimeSpan t) + { + return new KSPDateTime1(d.UT + t.UT); + } + public static Boolean operator !=(KSPDateTime1 d1, KSPDateTime1 d2) + { + return !(d1 == d2); + } + public static Boolean operator ==(KSPDateTime1 d1, KSPDateTime1 d2) + { + return d1.UT == d2.UT; + } + + + + public static Boolean operator <=(KSPDateTime1 d1, KSPDateTime1 d2) + { + return d1.CompareTo(d2) <= 0; + } + public static Boolean operator <(KSPDateTime1 d1, KSPDateTime1 d2) + { + return d1.CompareTo(d2) < 0; + } + public static Boolean operator >=(KSPDateTime1 d1, KSPDateTime1 d2) + { + return d1.CompareTo(d2) >= 0; + } + public static Boolean operator >(KSPDateTime1 d1, KSPDateTime1 d2) + { + return d1.CompareTo(d2) > 0; + } + #endregion + + //DaysInMonth + //Day - is Day Of Month + //DayOfYear + //IsLeapYear + + + //To String Formats + } + + + + +} diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs index eda50a3..e9c9729 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -7,30 +7,33 @@ namespace KSPPluginFramework { public class KSPTimeSpan { + //Define the Calendar + static public int SecondsPerMinute { get; set; } + static public int MinutesPerHour { get; set; } + static public int HoursPerDay { get; set; } + + static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } + static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } + //Descriptors of Timespan - uses UT as the Root value - public int Years - { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - Years * KSPDateTimeStructure.SecondsPerYear + value * KSPDateTimeStructure.SecondsPerYear; } - } public int Days { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - Days * KSPDateTimeStructure.SecondsPerDay + value * KSPDateTimeStructure.SecondsPerDay; } + get { return (Int32)UT / SecondsPerDay; } + set { UT = UT - Days * SecondsPerDay + value * SecondsPerDay; } } public int Hours { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } - set { UT = UT - Hours * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } + get { return (Int32)UT / SecondsPerHour % SecondsPerDay; } + set { UT = UT - Hours * SecondsPerHour + value * SecondsPerHour; } } public int Minutes { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } - set { UT = UT - Minutes * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } + get { return (Int32)UT / SecondsPerMinute % SecondsPerHour; } + set { UT = UT - Minutes * SecondsPerMinute + value * SecondsPerMinute; } } public int Seconds { - get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } + get { return (Int32)UT % SecondsPerMinute; } set { UT = UT - Seconds + value; } } public int Milliseconds @@ -45,22 +48,29 @@ public int Milliseconds public Double UT { get; set; } #region Constructors - public KSPTimeSpan() + static KSPTimeSpan() { + SecondsPerMinute = 60; + MinutesPerHour = 60; + HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; + } + public KSPTimeSpan() + { + UT = 0; } public KSPTimeSpan(int hour, int minute, int second) : this() { Hours = hour; Minutes = minute; Seconds = second; } - public KSPTimeSpan(int year, int day, int hour, int minute, int second) + public KSPTimeSpan(int day, int hour, int minute, int second) : this(hour, minute,second) { - Years = year; Days=day; + Days=day; } - public KSPTimeSpan(int year, int day, int hour, int minute, int second, int millisecond) - : this(year, day, hour, minute, second) + public KSPTimeSpan(int day, int hour, int minute, int second, int millisecond) + : this(day, hour, minute, second) { Milliseconds = millisecond; } @@ -68,7 +78,7 @@ public KSPTimeSpan(int year, int day, int hour, int minute, int second, int mill public KSPTimeSpan(Double ut) : this() { - + UT = ut; } #endregion @@ -76,10 +86,9 @@ public KSPTimeSpan(Double ut) #region Calculated Properties public Double TotalMilliseconds { get { return UT * 1000; } } public Double TotalSeconds { get { return UT; } } - public Double TotalMinutes { get { return UT / KSPDateTimeStructure.SecondsPerMinute; } } - public Double TotalHours { get { return UT / KSPDateTimeStructure.SecondsPerHour; } } - public Double TotalDays { get { return UT / KSPDateTimeStructure.SecondsPerDay; } } - public Double TotalYears { get { return UT / KSPDateTimeStructure.SecondsPerYear; } } + public Double TotalMinutes { get { return UT / SecondsPerMinute; } } + public Double TotalHours { get { return UT / SecondsPerHour; } } + public Double TotalDays { get { return UT / SecondsPerDay; } } #endregion #region Instance Methods @@ -135,17 +144,14 @@ public static Boolean Equals(KSPTimeSpan t1, KSPTimeSpan t2) } - public static KSPTimeSpan FromYears(Double value) { - return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerYear); - } public static KSPTimeSpan FromDays(Double value) { - return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerDay); + return new KSPTimeSpan(value * SecondsPerDay); } public static KSPTimeSpan FromHours(Double value) { - return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerHour); + return new KSPTimeSpan(value * SecondsPerHour); } public static KSPTimeSpan FromMinutes(Double value) { - return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerMinute); + return new KSPTimeSpan(value * SecondsPerMinute); } public static KSPTimeSpan FromSeconds(Double value) { return new KSPTimeSpan(value); diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index b9d6e23..52f343b 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -52,6 +52,7 @@ + From f40b3f99ea15718db0a1c2fba59415546ab8c862 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sun, 16 Nov 2014 23:32:30 +1100 Subject: [PATCH 06/40] WIP-tweaking Details and tests --- KSPDateTimeUnitTests/UnitTest1.cs | 39 ++++++- .../SharedStuff/KSPDateTime.cs | 103 +++++++++++------- .../SharedStuff/KSPDateTime1.cs | 4 +- .../SharedStuff/KSPDateTimeStructure.cs | 100 ++++++++++++++--- .../SharedStuff/KSPTimeSpan.cs | 41 +++---- TransferWindowPlanner/TWPWindowDebug.cs | 52 +++++---- .../TransferWindowPlanner.csproj | 2 +- 7 files changed, 229 insertions(+), 112 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index 5d72dde..dd64e06 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -15,12 +15,13 @@ public void TestDateTime() Double DateUT = 301.123; KSPDateTime dt = new KSPDateTime(DateUT); + //Console.Write(dt.Day); Assert.AreEqual(5, dt.Minute); Assert.AreEqual(1, dt.Second); Assert.AreEqual(123, dt.Millisecond); Assert.AreEqual(0, dt.Hour); - Assert.AreEqual(1, dt.Day); + Assert.AreEqual(1, dt.DayOfYear); Assert.AreEqual(1, dt.Year); dt.Millisecond = 456; @@ -36,11 +37,43 @@ public void TestDateTime() dt.Year = 2; Assert.AreEqual(2, dt.Year,"Hello"); - dt.Day = 50; - Assert.AreEqual(50, dt.Day); + dt.DayOfYear = 50; + Assert.AreEqual(50, dt.DayOfYear); } + [TestMethod] + public void TestEarthDateTime() + { + KSPDateTimeStructure.SetCalendarType(CalendarTypeEnum.Earth); + Double DateUT = 301.123; + KSPDateTime dt = new KSPDateTime(DateUT); + //Console.Write(dt.Day); + + Assert.AreEqual(5, dt.Minute); + Assert.AreEqual(1, dt.Second); + Assert.AreEqual(123, dt.Millisecond); + Assert.AreEqual(0, dt.Hour); + Assert.AreEqual(1, dt.DayOfYear); + Assert.AreEqual(1951, dt.Year); + + dt.Millisecond = 456; + Assert.AreEqual(5, dt.Minute); + Assert.AreEqual(1, dt.Second); + Assert.AreEqual(456, dt.Millisecond); + + dt.Second = 68; + Assert.AreEqual(6, dt.Minute); + Assert.AreEqual(8, dt.Second); + Assert.AreEqual(456, dt.Millisecond); + + + dt.Year = 1969; + Assert.AreEqual(1969, dt.Year, "Hello"); + dt.DayOfYear = 50; + Assert.AreEqual(50, dt.DayOfYear); + + } [TestMethod] public void TestAbstract() { diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index aa3b14f..66d9129 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -7,68 +7,87 @@ namespace KSPPluginFramework { public class KSPDateTime { - //Define the Epoch - static public int EpochDay { get; set; } - static public int EpochYear { get; set; } - - //Define the Calendar - static public int SecondsPerMinute { get { return KSPTimeSpan.SecondsPerMinute; } set { KSPTimeSpan.SecondsPerMinute = value; } } - static public int MinutesPerHour { get { return KSPTimeSpan.MinutesPerHour; } set { KSPTimeSpan.MinutesPerHour = value; } } - static public int HoursPerDay { get { return KSPTimeSpan.HoursPerDay; } set { KSPTimeSpan.HoursPerDay = value; } } - static public int DaysPerYear { get; set; } - - static public int SecondsPerHour { get { return KSPTimeSpan.SecondsPerHour; } } - static public int SecondsPerDay { get { return KSPTimeSpan.SecondsPerDay; } } - static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + private Boolean EarthTime = false; //Descriptors of DateTime - uses UT as the Root value public int Year { - get { return EpochYear + (Int32)UT / SecondsPerYear; } - set { UT = UT - (Year - EpochYear) * SecondsPerYear + (value - EpochYear) * SecondsPerYear; } + get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } } public int DayOfYear { - get { return EpochDay + (Int32)UT / SecondsPerDay % SecondsPerYear; } - set { UT = UT - (DayOfYear - EpochDay) * SecondsPerDay + (value - EpochDay) * SecondsPerDay; } + get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (DayOfYear - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay; } } public int Day { - + get + { + switch (KSPDateTimeStructure.CalendarType) + { + case CalendarTypeEnum.KSPStock: return DayOfYear; + case CalendarTypeEnum.Earth: + break; + case CalendarTypeEnum.Custom: + break; + default: return DayOfYear; + } + return DayOfYear; + } + } + public int Month + { + get + { + switch (KSPDateTimeStructure.CalendarType) + { + case CalendarTypeEnum.KSPStock: return 0; + case CalendarTypeEnum.Earth: + return new DateTime() + break; + case CalendarTypeEnum.Custom: + break; + default: return 0; + } + return 0; + } } - public int Hour { get { return _TimeSpan.Hours; } set { _TimeSpan.Hours = value; } } - public int Minute { get { return _TimeSpan.Minutes; } set { _TimeSpan.Minutes = value; } } + public int Hour { get { return _TimeSpan.Hours; } set { _TimeSpan.Hours = value; } } + public int Minute { get { return _TimeSpan.Minutes; } set { _TimeSpan.Minutes = value; } } public int Second { get { return _TimeSpan.Seconds; } set { _TimeSpan.Seconds = value; } } - public int Millisecond { get { return _TimeSpan.Milliseconds; } set { _TimeSpan.Milliseconds = value;} } + public int Millisecond { get { return _TimeSpan.Milliseconds; } set { _TimeSpan.Milliseconds = value; } } /// /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 /// - public Double UT { - get { return _TimeSpan.UT; } - set { _TimeSpan = new KSPTimeSpan(value); } + public Double UT { + get + { + if (KSPDateTimeStructure.CalendarType== CalendarTypeEnum.Earth) + return _EarthDateTime.Subtract(new DateTime(KSPDateTimeStructure.EpochYear,1,KSPDateTimeStructure.EpochDayOfYear)).TotalSeconds; + else + return _TimeSpan.UT; + } + set { + _TimeSpan = new KSPTimeSpan(value); + if (KSPDateTimeStructure.CalendarType == CalendarTypeEnum.Earth) + _EarthDateTime = new DateTime(KSPDateTimeStructure.EpochYear, 1, KSPDateTimeStructure.EpochDayOfYear).AddSeconds(value); + } } - private Double _UT; private KSPTimeSpan _TimeSpan; + private DateTime _EarthDateTime; + private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateTimeStructure.EpochYear, 1, KSPDateTimeStructure.EpochDayOfYear); } } #region Constructors - static KSPDateTime() - { - EpochYear = 1; - EpochDay = 1; - - DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; - } - public KSPDateTime() { - _UT = 0; - + UT = 0; } - public KSPDateTime(int year, int day) + public KSPDateTime(int year, int dayofyear) : this() { - Year = year; Day = day; + Year = year; DayOfYear = dayofyear; } public KSPDateTime(int year, int day, int hour, int minute, int second) : this(year, day) @@ -90,8 +109,8 @@ public KSPDateTime(Double ut) #region Calculated Properties - public KSPDateTime Date { get { return new KSPDateTime(Year, Day); } } - public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % SecondsPerDay); } } + public KSPDateTime Date { get { return new KSPDateTime(Year, DayOfYear); } } + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateTimeStructure.SecondsPerDay); } } public static KSPDateTime Now { @@ -117,15 +136,15 @@ public KSPDateTime AddYears(Int32 value) } public KSPDateTime AddDays(Double value) { - return new KSPDateTime(UT + value * SecondsPerDay); + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerDay); } public KSPDateTime AddHours(Double value) { - return new KSPDateTime(UT + value * SecondsPerHour); + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerHour); } public KSPDateTime AddMinutes(Double value) { - return new KSPDateTime(UT + value * SecondsPerMinute); + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerMinute); } public KSPDateTime AddSeconds(Double value) { diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs index 9b4c630..344bffb 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs @@ -14,8 +14,8 @@ public int Year { set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } } public int Day { - get { return KSPDateTimeStructure.EpochDay + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (Day - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDay) * KSPDateTimeStructure.SecondsPerDay; } + get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (Day - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay; } } public int Hour { get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs index f95f952..e378b59 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs @@ -8,33 +8,101 @@ namespace KSPPluginFramework public static class KSPDateTimeStructure { //Define the Epoch - static public int EpochDay { get; set; } - static public int EpochYear { get; set; } + static public Int32 EpochDayOfYear { get; private set; } + static public Int32 EpochYear { get; private set; } //Define the Calendar - static public int SecondsPerMinute { get; set; } - static public int MinutesPerHour { get; set; } - static public int HoursPerDay { get; set; } - static public int DaysPerYear { get; set; } + static public Int32 SecondsPerMinute { get; private set; } + static public Int32 MinutesPerHour { get; private set; } + static public Int32 HoursPerDay { get; private set; } + static public Int32 DaysPerYear { get; private set; } - static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } - static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } - static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + static public Int32 SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } + static public Int32 SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } + static public Int32 SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + + static public Int32 CustomEpochDayOfYear { get; set; } + static public Int32 CustomEpochYear { get; set; } + + ////Define the Calendar + static public Int32 CustomSecondsPerMinute { get; set; } + static public Int32 CustomMinutesPerHour { get; set; } + static public Int32 CustomHoursPerDay { get; set; } + static public Int32 CustomDaysPerYear { get {return CustomDaysPerYear;} + set { CustomDaysPerYear=value; + if (CalendarType == CalendarTypeEnum.Custom) + DaysPerYear = value; + } + } + + + static public CalendarTypeEnum CalendarType {get;set;} + + static public void SetCalendarType(CalendarTypeEnum caltype, Int32 EpochYear, Int32 EpochDayOfYear) + { + SetCalendarType(caltype); + CustomEpochYear = EpochYear; + CustomEpochDayOfYear = EpochDayOfYear; + } + static public void SetCalendarType(CalendarTypeEnum caltype) + { + switch (caltype) + { + case CalendarTypeEnum.KSPStock: + EpochYear = 1; + EpochDayOfYear = 1; + SecondsPerMinute = 60; + MinutesPerHour = 60; + + HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; + DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; + break; + case CalendarTypeEnum.Earth: + EpochYear = 1951; + EpochDayOfYear = 1; + SecondsPerMinute = 60; + MinutesPerHour = 60; + HoursPerDay = 24; + DaysPerYear = 365; + break; + case CalendarTypeEnum.Custom: + EpochYear = CustomEpochYear; + EpochDayOfYear = CustomEpochDayOfYear; + SecondsPerMinute = CustomSecondsPerMinute; + MinutesPerHour = CustomMinutesPerHour; + HoursPerDay = CustomHoursPerDay; + DaysPerYear = CustomDaysPerYear; + break; + default: + break; + } + + } + + // } + //} + + static public List Months {get; set;} + static public Int32 MonthCount { get { return Months.Count; } } static KSPDateTimeStructure() { - EpochYear = 1; - EpochDay = 1; - SecondsPerMinute = 60; - MinutesPerHour = 60; - - HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; - DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; + SetCalendarType(CalendarTypeEnum.KSPStock); + + Months = new List(); } } + + + public enum CalendarTypeEnum + { + KSPStock, + Earth, + Custom + } public class KSPMonth { diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs index e9c9729..6a38370 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -7,33 +7,25 @@ namespace KSPPluginFramework { public class KSPTimeSpan { - //Define the Calendar - static public int SecondsPerMinute { get; set; } - static public int MinutesPerHour { get; set; } - static public int HoursPerDay { get; set; } - - static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } - static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } - //Descriptors of Timespan - uses UT as the Root value public int Days { - get { return (Int32)UT / SecondsPerDay; } - set { UT = UT - Days * SecondsPerDay + value * SecondsPerDay; } + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerDay; } + set { UT = UT - Days * KSPDateTimeStructure.SecondsPerDay + value * KSPDateTimeStructure.SecondsPerDay; } } public int Hours { - get { return (Int32)UT / SecondsPerHour % SecondsPerDay; } - set { UT = UT - Hours * SecondsPerHour + value * SecondsPerHour; } + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } + set { UT = UT - Hours * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } } public int Minutes { - get { return (Int32)UT / SecondsPerMinute % SecondsPerHour; } - set { UT = UT - Minutes * SecondsPerMinute + value * SecondsPerMinute; } + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } + set { UT = UT - Minutes * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } } public int Seconds { - get { return (Int32)UT % SecondsPerMinute; } + get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } set { UT = UT - Seconds + value; } } public int Milliseconds @@ -48,13 +40,6 @@ public int Milliseconds public Double UT { get; set; } #region Constructors - static KSPTimeSpan() - { - SecondsPerMinute = 60; - MinutesPerHour = 60; - HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; - } - public KSPTimeSpan() { UT = 0; @@ -86,9 +71,9 @@ public KSPTimeSpan(Double ut) #region Calculated Properties public Double TotalMilliseconds { get { return UT * 1000; } } public Double TotalSeconds { get { return UT; } } - public Double TotalMinutes { get { return UT / SecondsPerMinute; } } - public Double TotalHours { get { return UT / SecondsPerHour; } } - public Double TotalDays { get { return UT / SecondsPerDay; } } + public Double TotalMinutes { get { return UT / KSPDateTimeStructure.SecondsPerMinute; } } + public Double TotalHours { get { return UT / KSPDateTimeStructure.SecondsPerHour; } } + public Double TotalDays { get { return UT / KSPDateTimeStructure.SecondsPerDay; } } #endregion #region Instance Methods @@ -145,13 +130,13 @@ public static Boolean Equals(KSPTimeSpan t1, KSPTimeSpan t2) public static KSPTimeSpan FromDays(Double value) { - return new KSPTimeSpan(value * SecondsPerDay); + return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerDay); } public static KSPTimeSpan FromHours(Double value) { - return new KSPTimeSpan(value * SecondsPerHour); + return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerHour); } public static KSPTimeSpan FromMinutes(Double value) { - return new KSPTimeSpan(value * SecondsPerMinute); + return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerMinute); } public static KSPTimeSpan FromSeconds(Double value) { return new KSPTimeSpan(value); diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index 837dbe5..04ef8f1 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -95,28 +95,40 @@ internal override void DrawWindow(int id) //if (GUILayout.Button("Unity")) SkinsLibrary.SetCurrent("Unity"); //if (GUILayout.Button("UnityWKSPButtons")) SkinsLibrary.SetCurrent("UnityWKSPButtons"); - - if (GUILayout.Button("CreateAlarm")) + if (GUILayout.Button("Make Date")) { - String tmpID = KACWrapper.KAC.CreateAlarm(KACWrapper.KACAPI.AlarmTypeEnum.TransferModelled, - String.Format("{0} -> {1}",mbTWP.windowMain.TransferSelected.Origin.bodyName,mbTWP.windowMain.TransferSelected.Destination.bodyName), - mbTWP.windowMain.TransferSelected.DepartureTime); - - - KACWrapper.KACAPI.KACAlarm alarmNew = KACWrapper.KAC.Alarms.First(a => a.ID == tmpID); - LogFormatted("{0}==11=={1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); - alarmNew.Notes = mbTWP.windowMain.GenerateTransferDetailsText(); - alarmNew.AlarmMargin = settings.KACMargin * 60 * 60; - alarmNew.AlarmAction = settings.KACAlarmAction; - LogFormatted("{0}==22=={1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); - alarmNew.XferOriginBodyName = mbTWP.windowMain.TransferSelected.Origin.bodyName; - LogFormatted("{0}==33=={1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); - alarmNew.XferTargetBodyName = mbTWP.windowMain.TransferSelected.Destination.bodyName; - - LogFormatted("{0}======{1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); - + LogFormatted("a"); + //KSPDateTimeStructure.CalendarType = CalendarTypeEnum.Earth; + KSPDateTime dt = new KSPDateTime(301.123); + + LogFormatted("1:{0}", dt.Minute); + LogFormatted("2:{0}", dt.UT); + LogFormatted("3:{0}", dt.Year); + //LogFormatted("4:{0}", KSPDateTimeStructure.CalendarType); + //LogFormatted("5:{0}", dt.Day); + } - DrawLabel("Windowpadding:{0}", SkinsLibrary.CurrentSkin.window.padding); + //if (GUILayout.Button("CreateAlarm")) + //{ + // String tmpID = KACWrapper.KAC.CreateAlarm(KACWrapper.KACAPI.AlarmTypeEnum.TransferModelled, + // String.Format("{0} -> {1}",mbTWP.windowMain.TransferSelected.Origin.bodyName,mbTWP.windowMain.TransferSelected.Destination.bodyName), + // mbTWP.windowMain.TransferSelected.DepartureTime); + + + // KACWrapper.KACAPI.KACAlarm alarmNew = KACWrapper.KAC.Alarms.First(a => a.ID == tmpID); + // LogFormatted("{0}==11=={1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); + // alarmNew.Notes = mbTWP.windowMain.GenerateTransferDetailsText(); + // alarmNew.AlarmMargin = settings.KACMargin * 60 * 60; + // alarmNew.AlarmAction = settings.KACAlarmAction; + // LogFormatted("{0}==22=={1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); + // alarmNew.XferOriginBodyName = mbTWP.windowMain.TransferSelected.Origin.bodyName; + // LogFormatted("{0}==33=={1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); + // alarmNew.XferTargetBodyName = mbTWP.windowMain.TransferSelected.Destination.bodyName; + + // LogFormatted("{0}======{1}", alarmNew.XferOriginBodyName, alarmNew.XferTargetBodyName); + + //} + //DrawLabel("Windowpadding:{0}", SkinsLibrary.CurrentSkin.window.padding); //DrawLabel("{0}", KACWrapper.KAC.Alarms.Count); //foreach ( KACWrapper.KACAPI.KACAlarm a in KACWrapper.KAC.Alarms) diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 52f343b..784f308 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -127,7 +127,7 @@ rem and copy the new dll copy "$(TargetPath)" "%25DestPath%25" rem and then run the game -"%25Binary%25" +"%25Binary%25" -force-d3d11 goto END From 66dde1f92c18b4f2987d77ad2382fc57eae14fc6 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 17 Nov 2014 22:52:42 +1100 Subject: [PATCH 07/40] WIP - More tweaking --- KSPDateTimeUnitTests/UnitTest1.cs | 42 +-- .../SharedStuff/KSPDateTime.cs | 48 ++-- .../SharedStuff/KSPDateTime2.cs | 268 ++++++++++++++++++ .../SharedStuff/KSPDateTimeSpanAbstract.cs | 6 +- .../SharedStuff/KSPDateTimeStructure.cs | 14 +- .../SharedStuff/KSPTimeSpan.cs | 45 ++- .../SharedStuff/KSPTimeSpan2.cs | 201 +++++++++++++ .../TransferWindowPlanner.csproj | 2 + 8 files changed, 550 insertions(+), 76 deletions(-) create mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTime2.cs create mode 100644 TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index dd64e06..5847c59 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -15,8 +15,7 @@ public void TestDateTime() Double DateUT = 301.123; KSPDateTime dt = new KSPDateTime(DateUT); - //Console.Write(dt.Day); - + Assert.AreEqual(5, dt.Minute); Assert.AreEqual(1, dt.Second); Assert.AreEqual(123, dt.Millisecond); @@ -24,20 +23,16 @@ public void TestDateTime() Assert.AreEqual(1, dt.DayOfYear); Assert.AreEqual(1, dt.Year); - dt.Millisecond = 456; + dt = dt.AddMilliSeconds(456); Assert.AreEqual(5, dt.Minute); Assert.AreEqual(1, dt.Second); - Assert.AreEqual(456, dt.Millisecond); + Assert.AreEqual(579, dt.Millisecond); - dt.Second = 68; + dt = new KSPDateTime(2, 50, 0, 6, 8,456); Assert.AreEqual(6, dt.Minute); Assert.AreEqual(8, dt.Second); Assert.AreEqual(456, dt.Millisecond); - - - dt.Year = 2; Assert.AreEqual(2, dt.Year,"Hello"); - dt.DayOfYear = 50; Assert.AreEqual(50, dt.DayOfYear); } @@ -57,21 +52,26 @@ public void TestEarthDateTime() Assert.AreEqual(1, dt.DayOfYear); Assert.AreEqual(1951, dt.Year); - dt.Millisecond = 456; + dt.AddMilliSeconds(456); Assert.AreEqual(5, dt.Minute); Assert.AreEqual(1, dt.Second); - Assert.AreEqual(456, dt.Millisecond); + Assert.AreEqual(579, dt.Millisecond); - dt.Second = 68; - Assert.AreEqual(6, dt.Minute); - Assert.AreEqual(8, dt.Second); - Assert.AreEqual(456, dt.Millisecond); + //dt.Second = 68; + //Assert.AreEqual(6, dt.Minute); + //Assert.AreEqual(8, dt.Second); + //Assert.AreEqual(456, dt.Millisecond); - dt.Year = 1969; - Assert.AreEqual(1969, dt.Year, "Hello"); - dt.DayOfYear = 50; - Assert.AreEqual(50, dt.DayOfYear); + //dt.Year = 1969; + //Assert.AreEqual(1969, dt.Year, "Hello"); + //dt.DayOfYear = 50; + //Assert.AreEqual(50, dt.DayOfYear); + + //KSPDateTimeStructure.SetCalendarTypeEarth(1951,1,1); + //dt = new KSPDateTime(1951, 50, 10, 20, 30); + //Assert.AreEqual(2, dt.Month); + //Assert.AreEqual(19, dt.Day); } [TestMethod] @@ -80,7 +80,7 @@ public void TestAbstract() Double DateUT = 301.123; - KSPDateTime2 dt = new KSPDateTime2(); + KSPDateTime20 dt = new KSPDateTime20(); dt.UT = DateUT; Assert.AreEqual(5, dt.Minute); Assert.AreEqual(1, dt.Second); @@ -106,7 +106,7 @@ public void TestAbstract() Assert.AreEqual(50, dt.Day); - KSPTimeSpan2 a = new KSPTimeSpan2(); + KSPTimeSpan20 a = new KSPTimeSpan20(); a.UT = 300; a.Second = 32; diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index 66d9129..d8e5d1d 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -12,11 +12,9 @@ public class KSPDateTime //Descriptors of DateTime - uses UT as the Root value public int Year { get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } } public int DayOfYear { - get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (DayOfYear - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay; } + get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.DaysPerYear; } } public int Day { @@ -26,7 +24,7 @@ public int Day { case CalendarTypeEnum.KSPStock: return DayOfYear; case CalendarTypeEnum.Earth: - break; + return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Day; case CalendarTypeEnum.Custom: break; default: return DayOfYear; @@ -42,8 +40,7 @@ public int Month { case CalendarTypeEnum.KSPStock: return 0; case CalendarTypeEnum.Earth: - return new DateTime() - break; + return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Month; case CalendarTypeEnum.Custom: break; default: return 0; @@ -52,10 +49,10 @@ public int Month } } - public int Hour { get { return _TimeSpan.Hours; } set { _TimeSpan.Hours = value; } } - public int Minute { get { return _TimeSpan.Minutes; } set { _TimeSpan.Minutes = value; } } - public int Second { get { return _TimeSpan.Seconds; } set { _TimeSpan.Seconds = value; } } - public int Millisecond { get { return _TimeSpan.Milliseconds; } set { _TimeSpan.Milliseconds = value; } } + public int Hour { get { return _TimeSpanFromEpoch.Hours; } } + public int Minute { get { return _TimeSpanFromEpoch.Minutes; } } + public int Second { get { return _TimeSpanFromEpoch.Seconds; } } + public int Millisecond { get { return _TimeSpanFromEpoch.Milliseconds; } } /// /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 @@ -64,18 +61,18 @@ public Double UT { get { if (KSPDateTimeStructure.CalendarType== CalendarTypeEnum.Earth) - return _EarthDateTime.Subtract(new DateTime(KSPDateTimeStructure.EpochYear,1,KSPDateTimeStructure.EpochDayOfYear)).TotalSeconds; + return _EarthDateTime.Subtract(KSPDateTimeStructure.CustomEpochEarth).TotalSeconds; else - return _TimeSpan.UT; + return _TimeSpanFromEpoch.UT; } set { - _TimeSpan = new KSPTimeSpan(value); - if (KSPDateTimeStructure.CalendarType == CalendarTypeEnum.Earth) - _EarthDateTime = new DateTime(KSPDateTimeStructure.EpochYear, 1, KSPDateTimeStructure.EpochDayOfYear).AddSeconds(value); + _TimeSpanFromEpoch = new KSPTimeSpan(value); + if (KSPDateTimeStructure.CalendarType == CalendarTypeEnum.Earth) + _EarthDateTime = KSPDateTimeStructure.CustomEpochEarth.AddSeconds(value); } } - private KSPTimeSpan _TimeSpan; + private KSPTimeSpan _TimeSpanFromEpoch; private DateTime _EarthDateTime; private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateTimeStructure.EpochYear, 1, KSPDateTimeStructure.EpochDayOfYear); } } @@ -85,23 +82,28 @@ public KSPDateTime() UT = 0; } public KSPDateTime(int year, int dayofyear) - : this() { - Year = year; DayOfYear = dayofyear; + UT = new KSPDateTime(year, dayofyear, 0, 0, 0).UT; } public KSPDateTime(int year, int day, int hour, int minute, int second) - : this(year, day) { - Hour = hour; Minute = minute; Second = second; + + UT = new KSPDateTime(year, day, hour, minute, second, 0).UT; } public KSPDateTime(int year, int day, int hour, int minute, int second, int millisecond) - : this(year, day, hour, minute, second) { - Millisecond = millisecond; + //Test for entering values outside the norm - eg 25 hours, day 600 + + UT = new KSPTimeSpan((year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.DaysPerYear + + (day - KSPDateTimeStructure.EpochDayOfYear), + hour, + minute, + second, + millisecond + ).UT; } public KSPDateTime(Double ut) - : this() { UT = ut; } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs new file mode 100644 index 0000000..5a5be0a --- /dev/null +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs @@ -0,0 +1,268 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace KSPPluginFramework +{ + public class KSPDateTime2 + { + private Boolean EarthTime = false; + + //Descriptors of DateTime - uses UT as the Root value + public int Year { + get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } + } + public int DayOfYear { + get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } + set { UT = UT - (DayOfYear - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay; } + } + public int Day + { + get + { + switch (KSPDateTimeStructure.CalendarType) + { + case CalendarTypeEnum.KSPStock: return DayOfYear; + case CalendarTypeEnum.Earth: + return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Day; + case CalendarTypeEnum.Custom: + break; + default: return DayOfYear; + } + return DayOfYear; + } + } + public int Month + { + get + { + switch (KSPDateTimeStructure.CalendarType) + { + case CalendarTypeEnum.KSPStock: return 0; + case CalendarTypeEnum.Earth: + return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Month; + case CalendarTypeEnum.Custom: + break; + default: return 0; + } + return 0; + } + } + + public int Hour { get { return _TimeSpan.Hours; } set { _TimeSpan.Hours = value; } } + public int Minute { get { return _TimeSpan.Minutes; } set { _TimeSpan.Minutes = value; } } + public int Second { get { return _TimeSpan.Seconds; } set { _TimeSpan.Seconds = value; } } + public int Millisecond { get { return _TimeSpan.Milliseconds; } set { _TimeSpan.Milliseconds = value; } } + + /// + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// + public Double UT { + get + { + if (KSPDateTimeStructure.CalendarType== CalendarTypeEnum.Earth) + return _EarthDateTime.Subtract(KSPDateTimeStructure.CustomEpochEarth).TotalSeconds; + else + return _TimeSpan.UT; + } + set { + _TimeSpan = new KSPTimeSpan2(value); + if (KSPDateTimeStructure.CalendarType == CalendarTypeEnum.Earth) + _EarthDateTime = KSPDateTimeStructure.CustomEpochEarth.AddSeconds(value); + } + } + + private KSPTimeSpan2 _TimeSpan; + private DateTime _EarthDateTime; + private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateTimeStructure.EpochYear, 1, KSPDateTimeStructure.EpochDayOfYear); } } + + #region Constructors + public KSPDateTime2() + { + UT = 0; + } + public KSPDateTime2(int year, int dayofyear) + : this() + { + Year = year; DayOfYear = dayofyear; + } + public KSPDateTime2(int year, int day, int hour, int minute, int second) + : this(year, day) + { + Hour = hour; Minute = minute; Second = second; + } + public KSPDateTime2(int year, int day, int hour, int minute, int second, int millisecond) + : this(year, day, hour, minute, second) + { + Millisecond = millisecond; + } + + public KSPDateTime2(Double ut) + : this() + { + UT = ut; + } + #endregion + + + #region Calculated Properties + public KSPDateTime2 Date { get { return new KSPDateTime2(Year, DayOfYear); } } + public KSPTimeSpan2 TimeOfDay { get { return new KSPTimeSpan2(UT % KSPDateTimeStructure.SecondsPerDay); } } + + + public static KSPDateTime2 Now { + get { return new KSPDateTime2(Planetarium.GetUniversalTime()); } + } + public static KSPDateTime2 Today { + get { return new KSPDateTime2(Planetarium.GetUniversalTime()).Date; } + } + #endregion + + + #region Instance Methods + #region Mathematic Methods + public KSPDateTime2 Add(KSPTimeSpan2 value) + { + return new KSPDateTime2(UT + value.UT); + } + public KSPDateTime2 AddYears(Int32 value) + { + KSPDateTime2 dteReturn = new KSPDateTime2(UT); + dteReturn.Year+=value; + return dteReturn; + } + public KSPDateTime2 AddDays(Double value) + { + return new KSPDateTime2(UT + value * KSPDateTimeStructure.SecondsPerDay); + } + public KSPDateTime2 AddHours(Double value) + { + return new KSPDateTime2(UT + value * KSPDateTimeStructure.SecondsPerHour); + } + public KSPDateTime2 AddMinutes(Double value) + { + return new KSPDateTime2(UT + value * KSPDateTimeStructure.SecondsPerMinute); + } + public KSPDateTime2 AddSeconds(Double value) + { + return new KSPDateTime2(UT + value); + } + public KSPDateTime2 AddMilliSeconds(Double value) + { + return new KSPDateTime2(UT + value / 1000); + } + public KSPDateTime2 AddUT(Double value) + { + return new KSPDateTime2(UT + value); + } + + public KSPDateTime2 Subtract(KSPDateTime2 value) + { + return new KSPDateTime2(UT - value.UT); + } + public KSPTimeSpan2 Subtract(KSPTimeSpan2 value) + { + return new KSPTimeSpan2(UT - value.UT); + } + + #endregion + + + #region Comparison Methods + public Int32 CompareTo(KSPDateTime2 value) { + return KSPDateTime2.Compare(this, value); + } + public Int32 CompareTo(System.Object value) { + return this.CompareTo((KSPDateTime2)value); + } + public Boolean Equals(KSPDateTime2 value) { + return KSPDateTime2.Equals(this, value); + } + public override bool Equals(System.Object obj) { + return this.Equals((KSPDateTime2)obj); + } + #endregion + + + public override int GetHashCode() { + return UT.GetHashCode(); + } + + #endregion + + + #region Static Methods + public static Int32 Compare(KSPDateTime2 t1, KSPDateTime2 t2) + { + if (t1.UT < t2.UT) + return -1; + else if (t1.UT > t2.UT) + return 1; + else + return 0; + } + public static Boolean Equals(KSPDateTime2 t1, KSPDateTime2 t2) + { + return t1.UT == t2.UT; + } + + + #endregion + + #region Operators + public static KSPTimeSpan2 operator -(KSPDateTime2 d1, KSPDateTime2 d2) + { + return new KSPTimeSpan2(d1.UT - d2.UT); + } + public static KSPDateTime2 operator -(KSPDateTime2 d, KSPTimeSpan2 t) + { + return new KSPDateTime2(d.UT - t.UT); + } + public static KSPDateTime2 operator +(KSPDateTime2 d, KSPTimeSpan2 t) + { + return new KSPDateTime2(d.UT + t.UT); + } + public static Boolean operator !=(KSPDateTime2 d1, KSPDateTime2 d2) + { + return !(d1 == d2); + } + public static Boolean operator ==(KSPDateTime2 d1, KSPDateTime2 d2) + { + return d1.UT == d2.UT; + } + + + + public static Boolean operator <=(KSPDateTime2 d1, KSPDateTime2 d2) + { + return d1.CompareTo(d2) <= 0; + } + public static Boolean operator <(KSPDateTime2 d1, KSPDateTime2 d2) + { + return d1.CompareTo(d2) < 0; + } + public static Boolean operator >=(KSPDateTime2 d1, KSPDateTime2 d2) + { + return d1.CompareTo(d2) >= 0; + } + public static Boolean operator >(KSPDateTime2 d1, KSPDateTime2 d2) + { + return d1.CompareTo(d2) > 0; + } + #endregion + + //DaysInMonth + //Day - is Day Of Month + //DayOfYear + //IsLeapYear + + + //To String Formats + } + + + + +} diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs index 3cb0643..f7ebf5a 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs @@ -75,13 +75,13 @@ public int Millisecond } - public class KSPDateTime2 : KSPDateTimeSpanAbstract + public class KSPDateTime20 : KSPDateTimeSpanAbstract { //Define the Epoch static public int EpochDay { get; set; } static public int EpochYear { get; set; } - static KSPDateTime2() + static KSPDateTime20() { EpochYear = 1; EpochDay = 1; @@ -102,7 +102,7 @@ static KSPDateTime2() new public int Millisecond { get { return base.Millisecond; } set { base.Millisecond = value; } } } - public class KSPTimeSpan2 : KSPDateTimeSpanAbstract + public class KSPTimeSpan20 : KSPDateTimeSpanAbstract { public int Years { get { return base.Year; } set { base.Year = value; } } public int Days { get { return base.Day; } set { base.Day = value; } } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs index e378b59..bd7bc1e 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs @@ -21,8 +21,10 @@ public static class KSPDateTimeStructure static public Int32 SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } static public Int32 SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } - static public Int32 CustomEpochDayOfYear { get; set; } - static public Int32 CustomEpochYear { get; set; } + static public Int32 CustomEpochDayOfYear { get; private set; } + static public Int32 CustomEpochYear { get; private set; } + + static public DateTime CustomEpochEarth { get; private set; } ////Define the Calendar static public Int32 CustomSecondsPerMinute { get; set; } @@ -46,6 +48,7 @@ static public void SetCalendarType(CalendarTypeEnum caltype, Int32 EpochYear, In } static public void SetCalendarType(CalendarTypeEnum caltype) { + CalendarType = caltype; switch (caltype) { case CalendarTypeEnum.KSPStock: @@ -79,6 +82,13 @@ static public void SetCalendarType(CalendarTypeEnum caltype) } + static public void SetCalendarTypeEarth(Int32 EpochYear, Int32 EpochMonth, Int32 EpochDay) + { + SetCalendarType( CalendarTypeEnum.Earth); + + CustomEpochEarth = new DateTime(EpochYear, EpochMonth, EpochDay); + } + // } //} diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs index 6a38370..c79e4a0 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -8,34 +8,24 @@ namespace KSPPluginFramework public class KSPTimeSpan { //Descriptors of Timespan - uses UT as the Root value - public int Days - { + public Int32 Days { get { return (Int32)UT / KSPDateTimeStructure.SecondsPerDay; } - set { UT = UT - Days * KSPDateTimeStructure.SecondsPerDay + value * KSPDateTimeStructure.SecondsPerDay; } } - public int Hours - { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } - set { UT = UT - Hours * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } + public int Hours { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.HoursPerDay; } } - public int Minutes - { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } - set { UT = UT - Minutes * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } + public int Minutes { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.MinutesPerHour; } } - public int Seconds - { + public int Seconds { get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } - set { UT = UT - Seconds + value; } } - public int Milliseconds - { + public int Milliseconds { get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } - set { UT = Math.Floor(UT) + ((Double)value / 1000); } } /// - /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// Replaces the normal "Ticks" function. This is Seconds of UT /// public Double UT { get; set; } @@ -44,20 +34,21 @@ public KSPTimeSpan() { UT = 0; } - public KSPTimeSpan(int hour, int minute, int second) - : this() + public KSPTimeSpan(int hours, int minutes, int seconds) { - Hours = hour; Minutes = minute; Seconds = second; + UT = new KSPTimeSpan(0, hours, minutes, seconds, 0).UT; } - public KSPTimeSpan(int day, int hour, int minute, int second) - : this(hour, minute,second) + public KSPTimeSpan(int days, int hours, int minutes, int seconds) { - Days=day; + UT = new KSPTimeSpan(days, hours, minutes, seconds, 0).UT; } - public KSPTimeSpan(int day, int hour, int minute, int second, int millisecond) - : this(day, hour, minute, second) + public KSPTimeSpan(int days, int hours, int minutes, int seconds, int milliseconds) { - Milliseconds = millisecond; + UT = days * KSPDateTimeStructure.SecondsPerDay + + hours * KSPDateTimeStructure.SecondsPerHour + + minutes * KSPDateTimeStructure.SecondsPerMinute + + seconds + + (Double)milliseconds / 1000; } public KSPTimeSpan(Double ut) diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs new file mode 100644 index 0000000..45af5cf --- /dev/null +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs @@ -0,0 +1,201 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace KSPPluginFramework +{ + public class KSPTimeSpan2 + { + //Descriptors of Timespan - uses UT as the Root value + public int Days + { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerDay; } + set { UT = UT - Days * KSPDateTimeStructure.SecondsPerDay + value * KSPDateTimeStructure.SecondsPerDay; } + } + public int Hours + { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } + set { UT = UT - Hours * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } + } + public int Minutes + { + get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } + set { UT = UT - Minutes * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } + } + public int Seconds + { + get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } + set { UT = UT - Seconds + value; } + } + public int Milliseconds + { + get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } + set { UT = Math.Floor(UT) + ((Double)value / 1000); } + } + + /// + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// + public Double UT { get; set; } + + #region Constructors + public KSPTimeSpan2() + { + UT = 0; + } + public KSPTimeSpan2(int hour, int minute, int second) + : this() + { + Hours = hour; Minutes = minute; Seconds = second; + } + public KSPTimeSpan2(int day, int hour, int minute, int second) + : this(hour, minute,second) + { + Days=day; + } + public KSPTimeSpan2(int day, int hour, int minute, int second, int millisecond) + : this(day, hour, minute, second) + { + Milliseconds = millisecond; + } + + public KSPTimeSpan2(Double ut) + : this() + { + UT = ut; + } + #endregion + + + #region Calculated Properties + public Double TotalMilliseconds { get { return UT * 1000; } } + public Double TotalSeconds { get { return UT; } } + public Double TotalMinutes { get { return UT / KSPDateTimeStructure.SecondsPerMinute; } } + public Double TotalHours { get { return UT / KSPDateTimeStructure.SecondsPerHour; } } + public Double TotalDays { get { return UT / KSPDateTimeStructure.SecondsPerDay; } } + #endregion + + #region Instance Methods + #region Mathematic Methods + public KSPTimeSpan2 Add(KSPTimeSpan2 value) { + return new KSPTimeSpan2(UT + value.UT); + } + public KSPTimeSpan2 Duration() { + return new KSPTimeSpan2(Math.Abs(UT)); + } + public KSPTimeSpan2 Negate() { + return new KSPTimeSpan2(UT*-1); + } + #endregion + + #region Comparison Methods + public Int32 CompareTo(KSPTimeSpan2 value) { + return KSPTimeSpan2.Compare(this, value); + } + public Int32 CompareTo(System.Object value) { + return this.CompareTo((KSPTimeSpan2)value); + } + public Boolean Equals(KSPTimeSpan2 value) { + return KSPTimeSpan2.Equals(this, value); + } + public override bool Equals(System.Object obj) { + return this.Equals((KSPTimeSpan2)obj); + } + #endregion + + + public override int GetHashCode() + { + return UT.GetHashCode(); + } + + #endregion + + + #region Static Methods + public static Int32 Compare(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + if (t1.UT < t2.UT) + return -1; + else if (t1.UT > t2.UT) + return 1; + else + return 0; + } + public static Boolean Equals(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return t1.UT == t2.UT; + } + + + public static KSPTimeSpan2 FromDays(Double value) { + return new KSPTimeSpan2(value * KSPDateTimeStructure.SecondsPerDay); + } + public static KSPTimeSpan2 FromHours(Double value) { + return new KSPTimeSpan2(value * KSPDateTimeStructure.SecondsPerHour); + } + public static KSPTimeSpan2 FromMinutes(Double value) { + return new KSPTimeSpan2(value * KSPDateTimeStructure.SecondsPerMinute); + } + public static KSPTimeSpan2 FromSeconds(Double value) { + return new KSPTimeSpan2(value); + } + public static KSPTimeSpan2 FromMilliseconds(Double value) { + return new KSPTimeSpan2(value / 1000); + } + + #endregion + + #region Operators + public static KSPTimeSpan2 operator -(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return new KSPTimeSpan2(t1.UT - t2.UT); + } + public static KSPTimeSpan2 operator -(KSPTimeSpan2 t) + { + return new KSPTimeSpan2(t.UT).Negate(); + } + public static KSPTimeSpan2 operator +(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return new KSPTimeSpan2(t1.UT + t2.UT); + } + public static KSPTimeSpan2 operator +(KSPTimeSpan2 t) + { + return new KSPTimeSpan2(t.UT); + } + + public static Boolean operator !=(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return !(t1 == t2); + } + public static Boolean operator ==(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return t1.UT == t2.UT; + } + + + + public static Boolean operator <=(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return t1.CompareTo(t2) <= 0; + } + public static Boolean operator <(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return t1.CompareTo(t2) < 0; + } + public static Boolean operator >=(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return t1.CompareTo(t2) >= 0; + } + public static Boolean operator >(KSPTimeSpan2 t1, KSPTimeSpan2 t2) + { + return t1.CompareTo(t2) > 0; + } + #endregion + + + + //To String Formats + } +} diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 784f308..64b568c 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -52,10 +52,12 @@ + + From 84a3f26ee5cfb95a9f2a2f2f8d407107348888c5 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Tue, 18 Nov 2014 17:49:35 +1100 Subject: [PATCH 08/40] WIP More tweaks --- KSPDateTimeUnitTests/UnitTest1.cs | 2 +- .../SharedStuff/KSPDateTime.cs | 54 ++++++----- .../SharedStuff/KSPDateTimeStructure.cs | 94 ++++++++----------- 3 files changed, 69 insertions(+), 81 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index 5847c59..88614fa 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -40,7 +40,7 @@ public void TestDateTime() [TestMethod] public void TestEarthDateTime() { - KSPDateTimeStructure.SetCalendarType(CalendarTypeEnum.Earth); + KSPDateTimeStructure.SetEarthCalendar(); Double DateUT = 301.123; KSPDateTime dt = new KSPDateTime(DateUT); //Console.Write(dt.Day); diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index d8e5d1d..215ec7d 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -7,35 +7,43 @@ namespace KSPPluginFramework { public class KSPDateTime { - private Boolean EarthTime = false; + private CalendarTypeEnum CalType { get { return KSPDateTimeStructure.CalendarType; } } + //Descriptors of DateTime - uses UT as the Root value public int Year { - get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } + get { if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Year; + else + return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; + } } public int DayOfYear { - get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.DaysPerYear; } + get { if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.DayOfYear; + else + return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.DaysPerYear; + } } public int Day { - get - { - switch (KSPDateTimeStructure.CalendarType) - { - case CalendarTypeEnum.KSPStock: return DayOfYear; - case CalendarTypeEnum.Earth: - return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Day; - case CalendarTypeEnum.Custom: - break; - default: return DayOfYear; - } - return DayOfYear; + get { if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Day; + else + return DayOfYear; } } public int Month { - get - { + get { if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Month; + else{ + if (KSPDateTimeStructure.MonthCount<1) + return 0; + else + return 1; + } + } switch (KSPDateTimeStructure.CalendarType) { case CalendarTypeEnum.KSPStock: return 0; @@ -49,10 +57,10 @@ public int Month } } - public int Hour { get { return _TimeSpanFromEpoch.Hours; } } - public int Minute { get { return _TimeSpanFromEpoch.Minutes; } } - public int Second { get { return _TimeSpanFromEpoch.Seconds; } } - public int Millisecond { get { return _TimeSpanFromEpoch.Milliseconds; } } + public int Hour { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Hour; else return _TimeSpanFromEpoch.Hours; } } + public int Minute { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Minute; else return _TimeSpanFromEpoch.Minutes; } } + public int Second { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Second; else return _TimeSpanFromEpoch.Seconds; } } + public int Millisecond { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Millisecond; else return _TimeSpanFromEpoch.Milliseconds; } } /// /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 @@ -132,9 +140,7 @@ public KSPDateTime Add(KSPTimeSpan value) } public KSPDateTime AddYears(Int32 value) { - KSPDateTime dteReturn = new KSPDateTime(UT); - dteReturn.Year+=value; - return dteReturn; + return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerYear); } public KSPDateTime AddDays(Double value) { diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs index bd7bc1e..b74ba55 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs @@ -21,83 +21,65 @@ public static class KSPDateTimeStructure static public Int32 SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } static public Int32 SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } - static public Int32 CustomEpochDayOfYear { get; private set; } - static public Int32 CustomEpochYear { get; private set; } - static public DateTime CustomEpochEarth { get; private set; } - ////Define the Calendar - static public Int32 CustomSecondsPerMinute { get; set; } - static public Int32 CustomMinutesPerHour { get; set; } - static public Int32 CustomHoursPerDay { get; set; } - static public Int32 CustomDaysPerYear { get {return CustomDaysPerYear;} - set { CustomDaysPerYear=value; - if (CalendarType == CalendarTypeEnum.Custom) - DaysPerYear = value; - } - } + static public CalendarTypeEnum CalendarType {get; private set;} - static public CalendarTypeEnum CalendarType {get;set;} - static public void SetCalendarType(CalendarTypeEnum caltype, Int32 EpochYear, Int32 EpochDayOfYear) + static public void SetKSPStockCalendar() { - SetCalendarType(caltype); - CustomEpochYear = EpochYear; - CustomEpochDayOfYear = EpochDayOfYear; + CalendarType = CalendarTypeEnum.KSPStock; + + EpochYear = 1; + EpochDayOfYear = 1; + SecondsPerMinute = 60; + MinutesPerHour = 60; + + HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; + DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; } - static public void SetCalendarType(CalendarTypeEnum caltype) + + static public void SetEarthCalendar() { - CalendarType = caltype; - switch (caltype) - { - case CalendarTypeEnum.KSPStock: - EpochYear = 1; - EpochDayOfYear = 1; - SecondsPerMinute = 60; - MinutesPerHour = 60; - - HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; - DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; - break; - case CalendarTypeEnum.Earth: - EpochYear = 1951; - EpochDayOfYear = 1; - SecondsPerMinute = 60; - MinutesPerHour = 60; - HoursPerDay = 24; - DaysPerYear = 365; - break; - case CalendarTypeEnum.Custom: - EpochYear = CustomEpochYear; - EpochDayOfYear = CustomEpochDayOfYear; - SecondsPerMinute = CustomSecondsPerMinute; - MinutesPerHour = CustomMinutesPerHour; - HoursPerDay = CustomHoursPerDay; - DaysPerYear = CustomDaysPerYear; - break; - default: - break; - } + SetEarthCalendar(1951, 1, 1); + } + static public void SetEarthCalendar(Int32 epochyear, Int32 epochmonth, Int32 epochday) + { + CalendarType = CalendarTypeEnum.Earth; + + CustomEpochEarth = new DateTime(epochyear, epochmonth, epochday); + + EpochYear = epochyear; + EpochDayOfYear = CustomEpochEarth.DayOfYear; + SecondsPerMinute = 60; + MinutesPerHour = 60; + + HoursPerDay = 24; + DaysPerYear = 365; } - static public void SetCalendarTypeEarth(Int32 EpochYear, Int32 EpochMonth, Int32 EpochDay) + static public void SetCustomCalendar(Int32 CustomEpochYear, Int32 CustomEpochDayOfYear, Int32 CustomDaysPerYear, Int32 CustomHoursPerDay, Int32 CustomMinutesPerHour, Int32 CustomSecondsPerMinute) { - SetCalendarType( CalendarTypeEnum.Earth); + CalendarType = CalendarTypeEnum.Custom; + + EpochYear = CustomEpochYear; + EpochDayOfYear = CustomEpochDayOfYear; + SecondsPerMinute = CustomSecondsPerMinute; + MinutesPerHour = CustomMinutesPerHour; + HoursPerDay = CustomHoursPerDay; + DaysPerYear = CustomDaysPerYear; - CustomEpochEarth = new DateTime(EpochYear, EpochMonth, EpochDay); } - // } - //} static public List Months {get; set;} static public Int32 MonthCount { get { return Months.Count; } } static KSPDateTimeStructure() { - SetCalendarType(CalendarTypeEnum.KSPStock); + SetKSPStockCalendar(); Months = new List(); } From 64abd7271fc0389946781e2a73884fc153510692 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Tue, 18 Nov 2014 22:50:24 +1100 Subject: [PATCH 09/40] WIPWorked some more - think its close - Unit tests all pass --- KSPDateTimeUnitTests/UnitTest1.cs | 2 +- .../SharedStuff/KSPDateTime.cs | 23 ++++++------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index 88614fa..812afff 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -52,7 +52,7 @@ public void TestEarthDateTime() Assert.AreEqual(1, dt.DayOfYear); Assert.AreEqual(1951, dt.Year); - dt.AddMilliSeconds(456); + dt = dt.AddMilliSeconds(456); Assert.AreEqual(5, dt.Minute); Assert.AreEqual(1, dt.Second); Assert.AreEqual(579, dt.Millisecond); diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index 215ec7d..fa9bff6 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -35,25 +35,16 @@ public int Day } public int Month { - get { if (CalType == CalendarTypeEnum.Earth) - return _EarthDateTime.Month; - else{ - if (KSPDateTimeStructure.MonthCount<1) - return 0; + get { + if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Month; else - return 1; - } - } - switch (KSPDateTimeStructure.CalendarType) { - case CalendarTypeEnum.KSPStock: return 0; - case CalendarTypeEnum.Earth: - return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Month; - case CalendarTypeEnum.Custom: - break; - default: return 0; + if (KSPDateTimeStructure.MonthCount < 1) + return 0; + else + return 1; } - return 0; } } From ca8b6f8a1a2c319c1ebadc90c2f86ba2a8807aef Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Thu, 20 Nov 2014 14:19:53 +1100 Subject: [PATCH 10/40] wip --- TransferWindowPlanner/TransferWindowPlanner.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 64b568c..117251c 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -129,7 +129,7 @@ rem and copy the new dll copy "$(TargetPath)" "%25DestPath%25" rem and then run the game -"%25Binary%25" -force-d3d11 +"%25Binary%25" goto END From 7e287d05ba40b568cdd618ac7eb303c9e27946e8 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Thu, 20 Nov 2014 21:07:19 +1100 Subject: [PATCH 11/40] DateTime Classes should be good - now for using em --- KSPDateTimeUnitTests/UnitTest1.cs | 26 ++++++++++ .../SharedStuff/KSPDateTime.cs | 51 +++++++++++++------ .../SharedStuff/KSPDateTime2.cs | 2 - .../SharedStuff/KSPDateTimeStructure.cs | 29 ++++++++--- 4 files changed, 84 insertions(+), 24 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index 812afff..4407c77 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -74,6 +74,32 @@ public void TestEarthDateTime() //Assert.AreEqual(19, dt.Day); } + + + [TestMethod] + public void TestMonths() + { + KSPDateTimeStructure.SetCustomCalendar(); + + //empty months structure + KSPDateTime dt = new KSPDateTime(1, 100); + Assert.AreEqual(0, dt.Month); + Assert.AreEqual(100, dt.Day); + + //set up some months + KSPDateTimeStructure.Months.Add(new KSPMonth("Billtempber", 200)); + KSPDateTimeStructure.Months.Add(new KSPMonth("Jebuary", 265)); + + Assert.AreEqual(1, dt.Month); + Assert.AreEqual(100, dt.Day); + dt = dt.AddDays(100); + Assert.AreEqual(1, dt.Month); + Assert.AreEqual(200, dt.Day); + dt = dt.AddDays(100); + Assert.AreEqual(2, dt.Month); + dt = dt.AddDays(100); + } + [TestMethod] public void TestAbstract() { diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index fa9bff6..6dcc4a9 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -27,10 +27,12 @@ public int DayOfYear { } public int Day { - get { if (CalType == CalendarTypeEnum.Earth) - return _EarthDateTime.Day; - else - return DayOfYear; + get + { + if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Day; + else + return DayOfMonth; } } public int Month @@ -43,11 +45,39 @@ public int Month if (KSPDateTimeStructure.MonthCount < 1) return 0; else - return 1; + return KSPDateTimeStructure.Months.IndexOf(MonthObj)+1; + } + } + } + + private KSPMonth MonthObj { + get { + if (KSPDateTimeStructure.MonthCount < 1) + return null; + Int32 monthMaxDay=0; + for (int i = 0; i < KSPDateTimeStructure.MonthCount; i++){ + if (DayOfYear <= monthMaxDay + KSPDateTimeStructure.Months[i].Days) + return KSPDateTimeStructure.Months[i]; + } + return KSPDateTimeStructure.Months.Last(); + } + } + private Int32 DayOfMonth { + get { + if (KSPDateTimeStructure.MonthCount < 1) + return DayOfYear; + + Int32 monthMaxDay = 0; + for (int i = 0; i < KSPDateTimeStructure.MonthCount; i++) + { + if (DayOfYear <= monthMaxDay + KSPDateTimeStructure.Months[i].Days) + return DayOfYear - monthMaxDay; } + return DayOfYear; } } + public int Hour { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Hour; else return _TimeSpanFromEpoch.Hours; } } public int Minute { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Minute; else return _TimeSpanFromEpoch.Minutes; } } public int Second { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Second; else return _TimeSpanFromEpoch.Seconds; } } @@ -253,16 +283,5 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) } #endregion - //DaysInMonth - //Day - is Day Of Month - //DayOfYear - //IsLeapYear - - - //To String Formats } - - - - } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs index 5a5be0a..b2fc945 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs @@ -7,8 +7,6 @@ namespace KSPPluginFramework { public class KSPDateTime2 { - private Boolean EarthTime = false; - //Descriptors of DateTime - uses UT as the Root value public int Year { get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs index b74ba55..8f49493 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs @@ -60,6 +60,12 @@ static public void SetEarthCalendar(Int32 epochyear, Int32 epochmonth, Int32 epo } + static public void SetCustomCalendar() + { + SetKSPStockCalendar(); + CalendarType = CalendarTypeEnum.Custom; + } + static public void SetCustomCalendar(Int32 CustomEpochYear, Int32 CustomEpochDayOfYear, Int32 CustomDaysPerYear, Int32 CustomHoursPerDay, Int32 CustomMinutesPerHour, Int32 CustomSecondsPerMinute) { CalendarType = CalendarTypeEnum.Custom; @@ -74,20 +80,20 @@ static public void SetCustomCalendar(Int32 CustomEpochYear, Int32 CustomEpochDay } - static public List Months {get; set;} - static public Int32 MonthCount { get { return Months.Count; } } - static KSPDateTimeStructure() { SetKSPStockCalendar(); Months = new List(); + //LeapDays = new List(); } + static public List Months { get; set; } + static public Int32 MonthCount { get { return Months.Count; } } - + //static public List LeapDays { get; set; } + //static public Int32 LeapDaysCount { get { return LeapDays.Count; } } } - public enum CalendarTypeEnum { @@ -98,7 +104,18 @@ public enum CalendarTypeEnum public class KSPMonth { - public int Days { get; set; } + public KSPMonth(String name, Int32 days) { Name = name; Days = days; } + public String Name { get; set; } + public Int32 Days { get; set; } } + + //public class KSPLeapDay + //{ + // public Int32 Frequency { get; set; } + // public String MonthApplied { get; set; } + // public Int32 DaysToAdd { get; set; } + //} + + } From bf07cd8189c0704dd0c5ebf1a34ba919b067e25e Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Thu, 20 Nov 2014 21:07:33 +1100 Subject: [PATCH 12/40] Clean up --- TransferWindowPlanner/SharedStuff/AppLauncher.cs | 6 +++--- TransferWindowPlanner/StylesAndSkins.cs | 3 ++- TransferWindowPlanner/TWPWindowDebug.cs | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/TransferWindowPlanner/SharedStuff/AppLauncher.cs b/TransferWindowPlanner/SharedStuff/AppLauncher.cs index 482e676..b069691 100644 --- a/TransferWindowPlanner/SharedStuff/AppLauncher.cs +++ b/TransferWindowPlanner/SharedStuff/AppLauncher.cs @@ -93,13 +93,13 @@ void onAppLaunchToggleOff() { } void onAppLaunchHoverOn() { MonoBehaviourExtended.LogFormatted_DebugOnly("HovOn"); - MouseOverAppLauncherBtn = true; + //MouseOverAppLauncherBtn = true; } void onAppLaunchHoverOff() { MonoBehaviourExtended.LogFormatted_DebugOnly("HovOff"); - MouseOverAppLauncherBtn = false; + //MouseOverAppLauncherBtn = false; } - Boolean MouseOverAppLauncherBtn = false; + //Boolean MouseOverAppLauncherBtn = false; } } diff --git a/TransferWindowPlanner/StylesAndSkins.cs b/TransferWindowPlanner/StylesAndSkins.cs index 147e7c5..dc934d1 100644 --- a/TransferWindowPlanner/StylesAndSkins.cs +++ b/TransferWindowPlanner/StylesAndSkins.cs @@ -47,7 +47,8 @@ internal class Styles internal static GUIStyle stylePlotCrossHair; - internal static GUIStyle stylePlotTransferMarker, stylePlotTransferMarkerXAxis, stylePlotTransferMarkerYAxis, stylePlotTransferMarkerDV; + //internal static GUIStyle stylePlotTransferMarker, stylePlotTransferMarkerXAxis, stylePlotTransferMarkerYAxis, + internal static GUIStyle stylePlotTransferMarkerDV; internal static GUIStyle styleSettingsArea; diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index 04ef8f1..f187508 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -66,6 +66,7 @@ namespace TransferWindowPlanner // } //} +#if DEBUG class TWPWindowDebug : MonoBehaviourWindowPlus { internal TransferWindowPlanner mbTWP; @@ -330,4 +331,5 @@ internal override void DrawWindow(int id) } +#endif } From 7c5e3c4aba0a30e8d22b79e452b98c9dd8a4637a Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Thu, 20 Nov 2014 21:09:16 +1100 Subject: [PATCH 13/40] Sorted out excess DateTime stuff - now to get the string formats --- KSPDateTimeUnitTests/UnitTest1.cs | 46 +-- ...teTimeStructure.cs => KSPDateStructure.cs} | 4 +- .../SharedStuff/KSPDateTime.cs | 50 ++-- .../SharedStuff/KSPDateTime1.cs | 231 --------------- .../SharedStuff/KSPDateTime2.cs | 266 ------------------ .../SharedStuff/KSPDateTimeSpanAbstract.cs | 117 -------- .../SharedStuff/KSPTimeSpan.cs | 26 +- .../SharedStuff/KSPTimeSpan2.cs | 201 ------------- .../TransferWindowPlanner.csproj | 6 +- 9 files changed, 46 insertions(+), 901 deletions(-) rename TransferWindowPlanner/SharedStuff/{KSPDateTimeStructure.cs => KSPDateStructure.cs} (97%) delete mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTime1.cs delete mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTime2.cs delete mode 100644 TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs delete mode 100644 TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index 4407c77..456b2e7 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -40,7 +40,7 @@ public void TestDateTime() [TestMethod] public void TestEarthDateTime() { - KSPDateTimeStructure.SetEarthCalendar(); + KSPDateStructure.SetEarthCalendar(); Double DateUT = 301.123; KSPDateTime dt = new KSPDateTime(DateUT); //Console.Write(dt.Day); @@ -79,7 +79,7 @@ public void TestEarthDateTime() [TestMethod] public void TestMonths() { - KSPDateTimeStructure.SetCustomCalendar(); + KSPDateStructure.SetCustomCalendar(); //empty months structure KSPDateTime dt = new KSPDateTime(1, 100); @@ -87,8 +87,8 @@ public void TestMonths() Assert.AreEqual(100, dt.Day); //set up some months - KSPDateTimeStructure.Months.Add(new KSPMonth("Billtempber", 200)); - KSPDateTimeStructure.Months.Add(new KSPMonth("Jebuary", 265)); + KSPDateStructure.Months.Add(new KSPMonth("Billtember", 200)); + KSPDateStructure.Months.Add(new KSPMonth("Jebuary", 265)); Assert.AreEqual(1, dt.Month); Assert.AreEqual(100, dt.Day); @@ -100,42 +100,6 @@ public void TestMonths() dt = dt.AddDays(100); } - [TestMethod] - public void TestAbstract() - { - - Double DateUT = 301.123; - - KSPDateTime20 dt = new KSPDateTime20(); - dt.UT = DateUT; - Assert.AreEqual(5, dt.Minute); - Assert.AreEqual(1, dt.Second); - Assert.AreEqual(123, dt.Millisecond); - Assert.AreEqual(0, dt.Hour); - Assert.AreEqual(1, dt.Day); - Assert.AreEqual(1, dt.Year); - - dt.Millisecond = 456; - Assert.AreEqual(5, dt.Minute); - Assert.AreEqual(1, dt.Second); - Assert.AreEqual(456, dt.Millisecond); - - dt.Second = 68; - Assert.AreEqual(6, dt.Minute); - Assert.AreEqual(8, dt.Second); - Assert.AreEqual(456, dt.Millisecond); - - - dt.Year = 2; - Assert.AreEqual(2, dt.Year, "Hello"); - dt.Day = 50; - Assert.AreEqual(50, dt.Day); - - - KSPTimeSpan20 a = new KSPTimeSpan20(); - a.UT = 300; - a.Second = 32; - - } + } } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs b/TransferWindowPlanner/SharedStuff/KSPDateStructure.cs similarity index 97% rename from TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs rename to TransferWindowPlanner/SharedStuff/KSPDateStructure.cs index 8f49493..5635e55 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeStructure.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateStructure.cs @@ -5,7 +5,7 @@ namespace KSPPluginFramework { - public static class KSPDateTimeStructure + public static class KSPDateStructure { //Define the Epoch static public Int32 EpochDayOfYear { get; private set; } @@ -80,7 +80,7 @@ static public void SetCustomCalendar(Int32 CustomEpochYear, Int32 CustomEpochDay } - static KSPDateTimeStructure() + static KSPDateStructure() { SetKSPStockCalendar(); diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index 6dcc4a9..af72068 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -7,7 +7,7 @@ namespace KSPPluginFramework { public class KSPDateTime { - private CalendarTypeEnum CalType { get { return KSPDateTimeStructure.CalendarType; } } + private CalendarTypeEnum CalType { get { return KSPDateStructure.CalendarType; } } //Descriptors of DateTime - uses UT as the Root value @@ -15,14 +15,14 @@ public int Year { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Year; else - return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; + return KSPDateStructure.EpochYear + (Int32)UT / KSPDateStructure.SecondsPerYear; } } public int DayOfYear { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.DayOfYear; else - return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.DaysPerYear; + return KSPDateStructure.EpochDayOfYear + (Int32)UT / KSPDateStructure.SecondsPerDay % KSPDateStructure.DaysPerYear; } } public int Day @@ -42,35 +42,35 @@ public int Month return _EarthDateTime.Month; else { - if (KSPDateTimeStructure.MonthCount < 1) + if (KSPDateStructure.MonthCount < 1) return 0; else - return KSPDateTimeStructure.Months.IndexOf(MonthObj)+1; + return KSPDateStructure.Months.IndexOf(MonthObj)+1; } } } private KSPMonth MonthObj { get { - if (KSPDateTimeStructure.MonthCount < 1) + if (KSPDateStructure.MonthCount < 1) return null; Int32 monthMaxDay=0; - for (int i = 0; i < KSPDateTimeStructure.MonthCount; i++){ - if (DayOfYear <= monthMaxDay + KSPDateTimeStructure.Months[i].Days) - return KSPDateTimeStructure.Months[i]; + for (int i = 0; i < KSPDateStructure.MonthCount; i++){ + if (DayOfYear <= monthMaxDay + KSPDateStructure.Months[i].Days) + return KSPDateStructure.Months[i]; } - return KSPDateTimeStructure.Months.Last(); + return KSPDateStructure.Months.Last(); } } private Int32 DayOfMonth { get { - if (KSPDateTimeStructure.MonthCount < 1) + if (KSPDateStructure.MonthCount < 1) return DayOfYear; Int32 monthMaxDay = 0; - for (int i = 0; i < KSPDateTimeStructure.MonthCount; i++) + for (int i = 0; i < KSPDateStructure.MonthCount; i++) { - if (DayOfYear <= monthMaxDay + KSPDateTimeStructure.Months[i].Days) + if (DayOfYear <= monthMaxDay + KSPDateStructure.Months[i].Days) return DayOfYear - monthMaxDay; } return DayOfYear; @@ -89,21 +89,21 @@ private Int32 DayOfMonth { public Double UT { get { - if (KSPDateTimeStructure.CalendarType== CalendarTypeEnum.Earth) - return _EarthDateTime.Subtract(KSPDateTimeStructure.CustomEpochEarth).TotalSeconds; + if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) + return _EarthDateTime.Subtract(KSPDateStructure.CustomEpochEarth).TotalSeconds; else return _TimeSpanFromEpoch.UT; } set { _TimeSpanFromEpoch = new KSPTimeSpan(value); - if (KSPDateTimeStructure.CalendarType == CalendarTypeEnum.Earth) - _EarthDateTime = KSPDateTimeStructure.CustomEpochEarth.AddSeconds(value); + if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) + _EarthDateTime = KSPDateStructure.CustomEpochEarth.AddSeconds(value); } } private KSPTimeSpan _TimeSpanFromEpoch; private DateTime _EarthDateTime; - private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateTimeStructure.EpochYear, 1, KSPDateTimeStructure.EpochDayOfYear); } } + private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateStructure.EpochYear, 1, KSPDateStructure.EpochDayOfYear); } } #region Constructors public KSPDateTime() @@ -123,8 +123,8 @@ public KSPDateTime(int year, int day, int hour, int minute, int second, int mill { //Test for entering values outside the norm - eg 25 hours, day 600 - UT = new KSPTimeSpan((year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.DaysPerYear + - (day - KSPDateTimeStructure.EpochDayOfYear), + UT = new KSPTimeSpan((year - KSPDateStructure.EpochYear) * KSPDateStructure.DaysPerYear + + (day - KSPDateStructure.EpochDayOfYear), hour, minute, second, @@ -141,7 +141,7 @@ public KSPDateTime(Double ut) #region Calculated Properties public KSPDateTime Date { get { return new KSPDateTime(Year, DayOfYear); } } - public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateTimeStructure.SecondsPerDay); } } + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateStructure.SecondsPerDay); } } public static KSPDateTime Now { @@ -161,19 +161,19 @@ public KSPDateTime Add(KSPTimeSpan value) } public KSPDateTime AddYears(Int32 value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerYear); + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerYear); } public KSPDateTime AddDays(Double value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerDay); + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerDay); } public KSPDateTime AddHours(Double value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerHour); + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerHour); } public KSPDateTime AddMinutes(Double value) { - return new KSPDateTime(UT + value * KSPDateTimeStructure.SecondsPerMinute); + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerMinute); } public KSPDateTime AddSeconds(Double value) { diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs deleted file mode 100644 index 344bffb..0000000 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime1.cs +++ /dev/null @@ -1,231 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace KSPPluginFramework -{ - public class KSPDateTime1 - { - - //Descriptors of DateTime - uses UT as the Root value - public int Year { - get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } - } - public int Day { - get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (Day - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay; } - } - public int Hour { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } - set { UT = UT - Hour * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } - } - public int Minute { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } - set { UT = UT - Minute * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } - } - public int Second { - get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } - set { UT = UT - Second + value; } - } - public int Millisecond { - get { return (Int32) (Math.Round(UT - Math.Floor(UT), 3) * 1000); } - set { UT = Math.Floor(UT) + ((Double)value / 1000); } - } - - /// - /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 - /// - public Double UT { get { return _UT; } set { _UT = value; } } - - private Double _UT; - - - #region Constructors - public KSPDateTime1() - { - _UT = 0; - - } - public KSPDateTime1(int year, int day) - : this() - { - Year = year; Day = day; - } - public KSPDateTime1(int year, int day, int hour, int minute, int second) - : this(year, day) - { - Hour = hour; Minute = minute; Second = second; - } - public KSPDateTime1(int year, int day, int hour, int minute, int second, int millisecond) - : this(year, day, hour, minute, second) - { - Millisecond = millisecond; - } - - public KSPDateTime1(Double ut) - : this() - { - UT = ut; - } - #endregion - - - #region Calculated Properties - public KSPDateTime1 Date { get { return new KSPDateTime1(Year, Day); } } - public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateTimeStructure.SecondsPerDay); } } - - - public static KSPDateTime1 Now { - get { return new KSPDateTime1(Planetarium.GetUniversalTime()); } - } - public static KSPDateTime1 Today { - get { return new KSPDateTime1(Planetarium.GetUniversalTime()).Date; } - } - #endregion - - - #region Instance Methods - #region Mathematic Methods - public KSPDateTime1 Add(KSPTimeSpan value) - { - return new KSPDateTime1(UT + value.UT); - } - public KSPDateTime1 AddYears(Double value) - { - return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerYear); - } - public KSPDateTime1 AddDays(Double value) - { - return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerDay); - } - public KSPDateTime1 AddHours(Double value) - { - return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerHour); - } - public KSPDateTime1 AddMinutes(Double value) - { - return new KSPDateTime1(UT + value * KSPDateTimeStructure.SecondsPerMinute); - } - public KSPDateTime1 AddSeconds(Double value) - { - return new KSPDateTime1(UT + value); - } - public KSPDateTime1 AddMilliSeconds(Double value) - { - return new KSPDateTime1(UT + value / 1000); - } - public KSPDateTime1 AddUT(Double value) - { - return new KSPDateTime1(UT + value); - } - - public KSPDateTime1 Subtract(KSPDateTime1 value) - { - return new KSPDateTime1(UT - value.UT); - } - public KSPTimeSpan Subtract(KSPTimeSpan value) - { - return new KSPTimeSpan(UT - value.UT); - } - - #endregion - - - #region Comparison Methods - public Int32 CompareTo(KSPDateTime1 value) { - return KSPDateTime1.Compare(this, value); - } - public Int32 CompareTo(System.Object value) { - return this.CompareTo((KSPDateTime1)value); - } - public Boolean Equals(KSPDateTime1 value) { - return KSPDateTime1.Equals(this, value); - } - public override bool Equals(System.Object obj) { - return this.Equals((KSPDateTime1)obj); - } - #endregion - - - public override int GetHashCode() { - return UT.GetHashCode(); - } - - #endregion - - - #region Static Methods - public static Int32 Compare(KSPDateTime1 t1, KSPDateTime1 t2) - { - if (t1.UT < t2.UT) - return -1; - else if (t1.UT > t2.UT) - return 1; - else - return 0; - } - public static Boolean Equals(KSPDateTime1 t1, KSPDateTime1 t2) - { - return t1.UT == t2.UT; - } - - - #endregion - - #region Operators - public static KSPTimeSpan operator -(KSPDateTime1 d1, KSPDateTime1 d2) - { - return new KSPTimeSpan(d1.UT - d2.UT); - } - public static KSPDateTime1 operator -(KSPDateTime1 d, KSPTimeSpan t) - { - return new KSPDateTime1(d.UT - t.UT); - } - public static KSPDateTime1 operator +(KSPDateTime1 d, KSPTimeSpan t) - { - return new KSPDateTime1(d.UT + t.UT); - } - public static Boolean operator !=(KSPDateTime1 d1, KSPDateTime1 d2) - { - return !(d1 == d2); - } - public static Boolean operator ==(KSPDateTime1 d1, KSPDateTime1 d2) - { - return d1.UT == d2.UT; - } - - - - public static Boolean operator <=(KSPDateTime1 d1, KSPDateTime1 d2) - { - return d1.CompareTo(d2) <= 0; - } - public static Boolean operator <(KSPDateTime1 d1, KSPDateTime1 d2) - { - return d1.CompareTo(d2) < 0; - } - public static Boolean operator >=(KSPDateTime1 d1, KSPDateTime1 d2) - { - return d1.CompareTo(d2) >= 0; - } - public static Boolean operator >(KSPDateTime1 d1, KSPDateTime1 d2) - { - return d1.CompareTo(d2) > 0; - } - #endregion - - //DaysInMonth - //Day - is Day Of Month - //DayOfYear - //IsLeapYear - - - //To String Formats - } - - - - -} diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs deleted file mode 100644 index b2fc945..0000000 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime2.cs +++ /dev/null @@ -1,266 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace KSPPluginFramework -{ - public class KSPDateTime2 - { - //Descriptors of DateTime - uses UT as the Root value - public int Year { - get { return KSPDateTimeStructure.EpochYear + (Int32)UT / KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (Year - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear + (value - KSPDateTimeStructure.EpochYear) * KSPDateTimeStructure.SecondsPerYear; } - } - public int DayOfYear { - get { return KSPDateTimeStructure.EpochDayOfYear + (Int32)UT / KSPDateTimeStructure.SecondsPerDay % KSPDateTimeStructure.SecondsPerYear; } - set { UT = UT - (DayOfYear - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay + (value - KSPDateTimeStructure.EpochDayOfYear) * KSPDateTimeStructure.SecondsPerDay; } - } - public int Day - { - get - { - switch (KSPDateTimeStructure.CalendarType) - { - case CalendarTypeEnum.KSPStock: return DayOfYear; - case CalendarTypeEnum.Earth: - return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Day; - case CalendarTypeEnum.Custom: - break; - default: return DayOfYear; - } - return DayOfYear; - } - } - public int Month - { - get - { - switch (KSPDateTimeStructure.CalendarType) - { - case CalendarTypeEnum.KSPStock: return 0; - case CalendarTypeEnum.Earth: - return KSPDateTimeStructure.CustomEpochEarth.AddSeconds(UT).Month; - case CalendarTypeEnum.Custom: - break; - default: return 0; - } - return 0; - } - } - - public int Hour { get { return _TimeSpan.Hours; } set { _TimeSpan.Hours = value; } } - public int Minute { get { return _TimeSpan.Minutes; } set { _TimeSpan.Minutes = value; } } - public int Second { get { return _TimeSpan.Seconds; } set { _TimeSpan.Seconds = value; } } - public int Millisecond { get { return _TimeSpan.Milliseconds; } set { _TimeSpan.Milliseconds = value; } } - - /// - /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 - /// - public Double UT { - get - { - if (KSPDateTimeStructure.CalendarType== CalendarTypeEnum.Earth) - return _EarthDateTime.Subtract(KSPDateTimeStructure.CustomEpochEarth).TotalSeconds; - else - return _TimeSpan.UT; - } - set { - _TimeSpan = new KSPTimeSpan2(value); - if (KSPDateTimeStructure.CalendarType == CalendarTypeEnum.Earth) - _EarthDateTime = KSPDateTimeStructure.CustomEpochEarth.AddSeconds(value); - } - } - - private KSPTimeSpan2 _TimeSpan; - private DateTime _EarthDateTime; - private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateTimeStructure.EpochYear, 1, KSPDateTimeStructure.EpochDayOfYear); } } - - #region Constructors - public KSPDateTime2() - { - UT = 0; - } - public KSPDateTime2(int year, int dayofyear) - : this() - { - Year = year; DayOfYear = dayofyear; - } - public KSPDateTime2(int year, int day, int hour, int minute, int second) - : this(year, day) - { - Hour = hour; Minute = minute; Second = second; - } - public KSPDateTime2(int year, int day, int hour, int minute, int second, int millisecond) - : this(year, day, hour, minute, second) - { - Millisecond = millisecond; - } - - public KSPDateTime2(Double ut) - : this() - { - UT = ut; - } - #endregion - - - #region Calculated Properties - public KSPDateTime2 Date { get { return new KSPDateTime2(Year, DayOfYear); } } - public KSPTimeSpan2 TimeOfDay { get { return new KSPTimeSpan2(UT % KSPDateTimeStructure.SecondsPerDay); } } - - - public static KSPDateTime2 Now { - get { return new KSPDateTime2(Planetarium.GetUniversalTime()); } - } - public static KSPDateTime2 Today { - get { return new KSPDateTime2(Planetarium.GetUniversalTime()).Date; } - } - #endregion - - - #region Instance Methods - #region Mathematic Methods - public KSPDateTime2 Add(KSPTimeSpan2 value) - { - return new KSPDateTime2(UT + value.UT); - } - public KSPDateTime2 AddYears(Int32 value) - { - KSPDateTime2 dteReturn = new KSPDateTime2(UT); - dteReturn.Year+=value; - return dteReturn; - } - public KSPDateTime2 AddDays(Double value) - { - return new KSPDateTime2(UT + value * KSPDateTimeStructure.SecondsPerDay); - } - public KSPDateTime2 AddHours(Double value) - { - return new KSPDateTime2(UT + value * KSPDateTimeStructure.SecondsPerHour); - } - public KSPDateTime2 AddMinutes(Double value) - { - return new KSPDateTime2(UT + value * KSPDateTimeStructure.SecondsPerMinute); - } - public KSPDateTime2 AddSeconds(Double value) - { - return new KSPDateTime2(UT + value); - } - public KSPDateTime2 AddMilliSeconds(Double value) - { - return new KSPDateTime2(UT + value / 1000); - } - public KSPDateTime2 AddUT(Double value) - { - return new KSPDateTime2(UT + value); - } - - public KSPDateTime2 Subtract(KSPDateTime2 value) - { - return new KSPDateTime2(UT - value.UT); - } - public KSPTimeSpan2 Subtract(KSPTimeSpan2 value) - { - return new KSPTimeSpan2(UT - value.UT); - } - - #endregion - - - #region Comparison Methods - public Int32 CompareTo(KSPDateTime2 value) { - return KSPDateTime2.Compare(this, value); - } - public Int32 CompareTo(System.Object value) { - return this.CompareTo((KSPDateTime2)value); - } - public Boolean Equals(KSPDateTime2 value) { - return KSPDateTime2.Equals(this, value); - } - public override bool Equals(System.Object obj) { - return this.Equals((KSPDateTime2)obj); - } - #endregion - - - public override int GetHashCode() { - return UT.GetHashCode(); - } - - #endregion - - - #region Static Methods - public static Int32 Compare(KSPDateTime2 t1, KSPDateTime2 t2) - { - if (t1.UT < t2.UT) - return -1; - else if (t1.UT > t2.UT) - return 1; - else - return 0; - } - public static Boolean Equals(KSPDateTime2 t1, KSPDateTime2 t2) - { - return t1.UT == t2.UT; - } - - - #endregion - - #region Operators - public static KSPTimeSpan2 operator -(KSPDateTime2 d1, KSPDateTime2 d2) - { - return new KSPTimeSpan2(d1.UT - d2.UT); - } - public static KSPDateTime2 operator -(KSPDateTime2 d, KSPTimeSpan2 t) - { - return new KSPDateTime2(d.UT - t.UT); - } - public static KSPDateTime2 operator +(KSPDateTime2 d, KSPTimeSpan2 t) - { - return new KSPDateTime2(d.UT + t.UT); - } - public static Boolean operator !=(KSPDateTime2 d1, KSPDateTime2 d2) - { - return !(d1 == d2); - } - public static Boolean operator ==(KSPDateTime2 d1, KSPDateTime2 d2) - { - return d1.UT == d2.UT; - } - - - - public static Boolean operator <=(KSPDateTime2 d1, KSPDateTime2 d2) - { - return d1.CompareTo(d2) <= 0; - } - public static Boolean operator <(KSPDateTime2 d1, KSPDateTime2 d2) - { - return d1.CompareTo(d2) < 0; - } - public static Boolean operator >=(KSPDateTime2 d1, KSPDateTime2 d2) - { - return d1.CompareTo(d2) >= 0; - } - public static Boolean operator >(KSPDateTime2 d1, KSPDateTime2 d2) - { - return d1.CompareTo(d2) > 0; - } - #endregion - - //DaysInMonth - //Day - is Day Of Month - //DayOfYear - //IsLeapYear - - - //To String Formats - } - - - - -} diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs b/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs deleted file mode 100644 index f7ebf5a..0000000 --- a/TransferWindowPlanner/SharedStuff/KSPDateTimeSpanAbstract.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace TransferWindowPlanner -{ - public abstract class KSPDateTimeSpanAbstract - { - //Define the Calendar - static public int SecondsPerMinute { get; set; } - static public int MinutesPerHour { get; set; } - static public int HoursPerDay { get; set; } - static public int DaysPerYear { get; set; } - - static public int SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } - static public int SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } - static public int SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } - - static KSPDateTimeSpanAbstract() - { - SecondsPerMinute = 60; - MinutesPerHour = 60; - HoursPerDay = 6; - DaysPerYear = 425; - } - - //Descriptors of DateTime - uses UT as the Root value - private int _Year; - protected virtual int Year - { - get { return (Int32)UT / SecondsPerYear; } - set { - UT = UT - _Year * SecondsPerYear + value * SecondsPerYear; - _Year = value; - } - } - - private int _Day; - protected virtual int Day - { - get { return (Int32)UT / SecondsPerDay % DaysPerYear; } - set { - UT = UT - _Day * SecondsPerDay + value * SecondsPerDay; - _Day = value; - } - } - public int Hour - { - get { return (Int32)UT / SecondsPerHour % HoursPerDay; } - set { UT = UT - Hour * SecondsPerHour + value * SecondsPerHour; } - } - public int Minute - { - get { return (Int32)UT / SecondsPerMinute % MinutesPerHour; } - set { UT = UT - Minute * SecondsPerMinute + value * SecondsPerMinute; } - } - public int Second - { - get { return (Int32)UT % SecondsPerMinute; } - set { UT = UT - Second + value; } - } - public int Millisecond - { - get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } - set { UT = Math.Floor(UT) + ((Double)value / 1000); } - } - - /// - /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 - /// - public Double UT { get { return _UT; } set { _UT = value; } } - - private Double _UT; - } - - - public class KSPDateTime20 : KSPDateTimeSpanAbstract - { - //Define the Epoch - static public int EpochDay { get; set; } - static public int EpochYear { get; set; } - - static KSPDateTime20() - { - EpochYear = 1; - EpochDay = 1; - - } - - new public int Year { - get { return base.Year + EpochYear; } - set { base.Year = value - EpochYear; } - } - new public int Day { - get { return base.Day + EpochDay; } - set { base.Day = value - EpochDay; } - } - new public int Hour { get { return base.Hour; } set { base.Hour = value; } } - new public int Minute { get { return base.Minute; } set { base.Minute = value; } } - new public int Second { get { return base.Second; } set { base.Second = value; } } - new public int Millisecond { get { return base.Millisecond; } set { base.Millisecond = value; } } - - } - public class KSPTimeSpan20 : KSPDateTimeSpanAbstract - { - public int Years { get { return base.Year; } set { base.Year = value; } } - public int Days { get { return base.Day; } set { base.Day = value; } } - public int Hours { get { return base.Hour; } set { base.Hour = value; } } - public int Minutes { get { return base.Minute; } set { base.Minute = value; } } - public int Seconds { get { return base.Second; } set { base.Second = value; } } - public int Milliseconds { get { return base.Millisecond; } set { base.Millisecond = value; } } - - - } - -} diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs index c79e4a0..c43e431 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -9,16 +9,16 @@ public class KSPTimeSpan { //Descriptors of Timespan - uses UT as the Root value public Int32 Days { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerDay; } + get { return (Int32)UT / KSPDateStructure.SecondsPerDay; } } public int Hours { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.HoursPerDay; } + get { return (Int32)UT / KSPDateStructure.SecondsPerHour % KSPDateStructure.HoursPerDay; } } public int Minutes { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.MinutesPerHour; } + get { return (Int32)UT / KSPDateStructure.SecondsPerMinute % KSPDateStructure.MinutesPerHour; } } public int Seconds { - get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } + get { return (Int32)UT % KSPDateStructure.SecondsPerMinute; } } public int Milliseconds { get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } @@ -44,9 +44,9 @@ public KSPTimeSpan(int days, int hours, int minutes, int seconds) } public KSPTimeSpan(int days, int hours, int minutes, int seconds, int milliseconds) { - UT = days * KSPDateTimeStructure.SecondsPerDay + - hours * KSPDateTimeStructure.SecondsPerHour + - minutes * KSPDateTimeStructure.SecondsPerMinute + + UT = days * KSPDateStructure.SecondsPerDay + + hours * KSPDateStructure.SecondsPerHour + + minutes * KSPDateStructure.SecondsPerMinute + seconds + (Double)milliseconds / 1000; } @@ -62,9 +62,9 @@ public KSPTimeSpan(Double ut) #region Calculated Properties public Double TotalMilliseconds { get { return UT * 1000; } } public Double TotalSeconds { get { return UT; } } - public Double TotalMinutes { get { return UT / KSPDateTimeStructure.SecondsPerMinute; } } - public Double TotalHours { get { return UT / KSPDateTimeStructure.SecondsPerHour; } } - public Double TotalDays { get { return UT / KSPDateTimeStructure.SecondsPerDay; } } + public Double TotalMinutes { get { return UT / KSPDateStructure.SecondsPerMinute; } } + public Double TotalHours { get { return UT / KSPDateStructure.SecondsPerHour; } } + public Double TotalDays { get { return UT / KSPDateStructure.SecondsPerDay; } } #endregion #region Instance Methods @@ -121,13 +121,13 @@ public static Boolean Equals(KSPTimeSpan t1, KSPTimeSpan t2) public static KSPTimeSpan FromDays(Double value) { - return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerDay); + return new KSPTimeSpan(value * KSPDateStructure.SecondsPerDay); } public static KSPTimeSpan FromHours(Double value) { - return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerHour); + return new KSPTimeSpan(value * KSPDateStructure.SecondsPerHour); } public static KSPTimeSpan FromMinutes(Double value) { - return new KSPTimeSpan(value * KSPDateTimeStructure.SecondsPerMinute); + return new KSPTimeSpan(value * KSPDateStructure.SecondsPerMinute); } public static KSPTimeSpan FromSeconds(Double value) { return new KSPTimeSpan(value); diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs deleted file mode 100644 index 45af5cf..0000000 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan2.cs +++ /dev/null @@ -1,201 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace KSPPluginFramework -{ - public class KSPTimeSpan2 - { - //Descriptors of Timespan - uses UT as the Root value - public int Days - { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerDay; } - set { UT = UT - Days * KSPDateTimeStructure.SecondsPerDay + value * KSPDateTimeStructure.SecondsPerDay; } - } - public int Hours - { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerHour % KSPDateTimeStructure.SecondsPerDay; } - set { UT = UT - Hours * KSPDateTimeStructure.SecondsPerHour + value * KSPDateTimeStructure.SecondsPerHour; } - } - public int Minutes - { - get { return (Int32)UT / KSPDateTimeStructure.SecondsPerMinute % KSPDateTimeStructure.SecondsPerHour; } - set { UT = UT - Minutes * KSPDateTimeStructure.SecondsPerMinute + value * KSPDateTimeStructure.SecondsPerMinute; } - } - public int Seconds - { - get { return (Int32)UT % KSPDateTimeStructure.SecondsPerMinute; } - set { UT = UT - Seconds + value; } - } - public int Milliseconds - { - get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } - set { UT = Math.Floor(UT) + ((Double)value / 1000); } - } - - /// - /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 - /// - public Double UT { get; set; } - - #region Constructors - public KSPTimeSpan2() - { - UT = 0; - } - public KSPTimeSpan2(int hour, int minute, int second) - : this() - { - Hours = hour; Minutes = minute; Seconds = second; - } - public KSPTimeSpan2(int day, int hour, int minute, int second) - : this(hour, minute,second) - { - Days=day; - } - public KSPTimeSpan2(int day, int hour, int minute, int second, int millisecond) - : this(day, hour, minute, second) - { - Milliseconds = millisecond; - } - - public KSPTimeSpan2(Double ut) - : this() - { - UT = ut; - } - #endregion - - - #region Calculated Properties - public Double TotalMilliseconds { get { return UT * 1000; } } - public Double TotalSeconds { get { return UT; } } - public Double TotalMinutes { get { return UT / KSPDateTimeStructure.SecondsPerMinute; } } - public Double TotalHours { get { return UT / KSPDateTimeStructure.SecondsPerHour; } } - public Double TotalDays { get { return UT / KSPDateTimeStructure.SecondsPerDay; } } - #endregion - - #region Instance Methods - #region Mathematic Methods - public KSPTimeSpan2 Add(KSPTimeSpan2 value) { - return new KSPTimeSpan2(UT + value.UT); - } - public KSPTimeSpan2 Duration() { - return new KSPTimeSpan2(Math.Abs(UT)); - } - public KSPTimeSpan2 Negate() { - return new KSPTimeSpan2(UT*-1); - } - #endregion - - #region Comparison Methods - public Int32 CompareTo(KSPTimeSpan2 value) { - return KSPTimeSpan2.Compare(this, value); - } - public Int32 CompareTo(System.Object value) { - return this.CompareTo((KSPTimeSpan2)value); - } - public Boolean Equals(KSPTimeSpan2 value) { - return KSPTimeSpan2.Equals(this, value); - } - public override bool Equals(System.Object obj) { - return this.Equals((KSPTimeSpan2)obj); - } - #endregion - - - public override int GetHashCode() - { - return UT.GetHashCode(); - } - - #endregion - - - #region Static Methods - public static Int32 Compare(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - if (t1.UT < t2.UT) - return -1; - else if (t1.UT > t2.UT) - return 1; - else - return 0; - } - public static Boolean Equals(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return t1.UT == t2.UT; - } - - - public static KSPTimeSpan2 FromDays(Double value) { - return new KSPTimeSpan2(value * KSPDateTimeStructure.SecondsPerDay); - } - public static KSPTimeSpan2 FromHours(Double value) { - return new KSPTimeSpan2(value * KSPDateTimeStructure.SecondsPerHour); - } - public static KSPTimeSpan2 FromMinutes(Double value) { - return new KSPTimeSpan2(value * KSPDateTimeStructure.SecondsPerMinute); - } - public static KSPTimeSpan2 FromSeconds(Double value) { - return new KSPTimeSpan2(value); - } - public static KSPTimeSpan2 FromMilliseconds(Double value) { - return new KSPTimeSpan2(value / 1000); - } - - #endregion - - #region Operators - public static KSPTimeSpan2 operator -(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return new KSPTimeSpan2(t1.UT - t2.UT); - } - public static KSPTimeSpan2 operator -(KSPTimeSpan2 t) - { - return new KSPTimeSpan2(t.UT).Negate(); - } - public static KSPTimeSpan2 operator +(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return new KSPTimeSpan2(t1.UT + t2.UT); - } - public static KSPTimeSpan2 operator +(KSPTimeSpan2 t) - { - return new KSPTimeSpan2(t.UT); - } - - public static Boolean operator !=(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return !(t1 == t2); - } - public static Boolean operator ==(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return t1.UT == t2.UT; - } - - - - public static Boolean operator <=(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return t1.CompareTo(t2) <= 0; - } - public static Boolean operator <(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return t1.CompareTo(t2) < 0; - } - public static Boolean operator >=(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return t1.CompareTo(t2) >= 0; - } - public static Boolean operator >(KSPTimeSpan2 t1, KSPTimeSpan2 t2) - { - return t1.CompareTo(t2) > 0; - } - #endregion - - - - //To String Formats - } -} diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 117251c..f851924 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -52,12 +52,8 @@ - - - - - + From 4a2dedb95673268f7d68ee6ecf1043d846ecb2f5 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Fri, 21 Nov 2014 18:11:49 +1100 Subject: [PATCH 14/40] Fixed Issue with KAC integration in editor scenes (Fixes #23) --- TransferWindowPlanner/TWPWindowSettings.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/TransferWindowPlanner/TWPWindowSettings.cs b/TransferWindowPlanner/TWPWindowSettings.cs index 85c1cd2..2dd9d56 100644 --- a/TransferWindowPlanner/TWPWindowSettings.cs +++ b/TransferWindowPlanner/TWPWindowSettings.cs @@ -225,7 +225,17 @@ private void DrawWindow_Alarm() { if (GUILayout.Button(new GUIContent("You need a newer version of KAC", "Click to open your browser and download a newer Kerbal Alarm Clock"), Styles.styleTextCenterGreen)) Application.OpenURL("http://forum.kerbalspaceprogram.com/threads/24786"); - + + } + else if (!TWP_KACWrapper.KACWrapper.InstanceExists) + { + GUILayout.Label("KAC is not loaded in this scene, so we can't configure", Styles.styleTextGreen); + GUILayout.Label("the integration options", Styles.styleTextGreen); + GUILayout.Space(10); + GUILayout.Label("You can access these in scenes where KAC is visible", Styles.styleTextGreen); + GUILayout.Space(10); + GUILayout.Label("Go on... Move along... Nothing to see...", Styles.styleTextGreen); + } else { From ab1917d7df9d0b7d13b4501e79db665d009f4727 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Fri, 21 Nov 2014 18:12:19 +1100 Subject: [PATCH 15/40] Updated Extension Methods --- .../FrameworkExt/Extensions.cs | 58 +++++++++++++++++++ .../FrameworkExt/MonoBehaviourWindowPlus.cs | 40 ------------- 2 files changed, 58 insertions(+), 40 deletions(-) create mode 100644 TransferWindowPlanner/FrameworkExt/Extensions.cs diff --git a/TransferWindowPlanner/FrameworkExt/Extensions.cs b/TransferWindowPlanner/FrameworkExt/Extensions.cs new file mode 100644 index 0000000..7648279 --- /dev/null +++ b/TransferWindowPlanner/FrameworkExt/Extensions.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; + + +namespace KSPPluginFramework +{ + public static class EnumExtensions + { + public static String Description(this Enum e) + { + DescriptionAttribute[] desc = (DescriptionAttribute[])e.GetType().GetMember(e.ToString())[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); + if (desc.Length > 0) + return desc[0].Description; + else + return e.ToString(); + } + + //public static List> ToEnumDescriptionsList(TEnum value) + //{ + // return Enum + // .GetValues(typeof(TEnum)) + // .Cast() + // .Select(x => new KeyValuePair(x, ((Enum)((object)x)).Description())) + // .ToList(); + //} + //public static List> ToEnumDescriptionsList() + //{ + // return ToEnumDescriptionsList(default(TEnum)); + //} + + //limit it to accept enums only + public static List ToEnumDescriptions(TEnum value) where TEnum : struct,IConvertible + { + List> temp = Enum + .GetValues(typeof(TEnum)) + .Cast() + .Select(x => new KeyValuePair(x, ((Enum)((System.Object)x)).Description())) + .ToList(); + return temp.Select(x => x.Value).ToList(); + } + public static List ToEnumDescriptions() where TEnum : struct,IConvertible + { + return ToEnumDescriptions(default(TEnum)).ToList(); + } + + + + public static T Clamp(this T val, T min, T max) where T : IComparable + { + if (val.CompareTo(min) < 0) return min; + else if (val.CompareTo(max) > 0) return max; + else return val; + } + } +} diff --git a/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs b/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs index 7e1e170..819fb9c 100644 --- a/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs +++ b/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs @@ -537,46 +537,6 @@ internal Boolean CloseOnOutsideClick() } } - public static class EnumExtensions - { - public static String Description(this Enum e) - { - DescriptionAttribute[] desc = (DescriptionAttribute[])e.GetType().GetMember(e.ToString())[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); - if (desc.Length > 0) - return desc[0].Description; - else - return e.ToString(); - } - - //public static List> ToEnumDescriptionsList(TEnum value) - //{ - // return Enum - // .GetValues(typeof(TEnum)) - // .Cast() - // .Select(x => new KeyValuePair(x, ((Enum)((object)x)).Description())) - // .ToList(); - //} - //public static List> ToEnumDescriptionsList() - //{ - // return ToEnumDescriptionsList(default(TEnum)); - //} - - //limit it to accept enums only - public static List ToEnumDescriptions( TEnum value) where TEnum:struct,IConvertible - { - List> temp = Enum - .GetValues(typeof(TEnum)) - .Cast() - .Select(x => new KeyValuePair(x, ((Enum)((object)x)).Description())) - .ToList(); - return temp.Select(x => x.Value).ToList(); - } - public static List ToEnumDescriptions() where TEnum : struct,IConvertible - { - return ToEnumDescriptions(default(TEnum)).ToList(); - } - } - public class GUIContentWithStyle { internal GUIContentWithStyle(String text, GUIStyle Style) From ae6a8b7bde96eab9898bdcf7aac8061ca297431a Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sat, 22 Nov 2014 22:26:34 +1100 Subject: [PATCH 16/40] Most of the string format done - just the default KSP format to insert --- KSPDateTimeUnitTests/UnitTest1.cs | 22 ++++ .../SharedStuff/KSPDateTime.cs | 123 +++++++++++++++++- TransferWindowPlanner/TWPWindowDebug.cs | 6 + .../TransferWindowPlanner.csproj | 1 + 4 files changed, 145 insertions(+), 7 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index 456b2e7..2ca3bd7 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -1,6 +1,8 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Collections.Generic; + using TransferWindowPlanner; using KSPPluginFramework; @@ -100,6 +102,26 @@ public void TestMonths() dt = dt.AddDays(100); } + [TestMethod] + public void TestFormats() + { + KSPDateTime dt = new KSPDateTime(1, 100); + + KSPDateStructure.Months = new List(); + + Assert.AreEqual("100/00/0001", dt.ToString("dd/MM/yyyy")); + + + KSPDateStructure.SetEarthCalendar(); + Assert.AreEqual("25/01/1951", dt.ToString("dd/MM/yyyy")); + + + dt = new KSPDateTime(1951,100); + Assert.AreEqual("10/04/1951",dt.ToString("dd/MM/yyyy")); + + Assert.AreEqual("Hello there 1951",String.Format("Hello there {0:yyyy}",dt)); + + } } } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index af72068..f0fb534 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -3,9 +3,11 @@ using System.Linq; using System.Text; +using System.Text.RegularExpressions; + namespace KSPPluginFramework { - public class KSPDateTime + public class KSPDateTime : IFormattable { private CalendarTypeEnum CalType { get { return KSPDateStructure.CalendarType; } } @@ -89,20 +91,20 @@ private Int32 DayOfMonth { public Double UT { get { - if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) - return _EarthDateTime.Subtract(KSPDateStructure.CustomEpochEarth).TotalSeconds; - else + //if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) + // return _EarthDateTime.Subtract(KSPDateStructure.CustomEpochEarth).TotalSeconds; + //else return _TimeSpanFromEpoch.UT; } set { _TimeSpanFromEpoch = new KSPTimeSpan(value); - if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) - _EarthDateTime = KSPDateStructure.CustomEpochEarth.AddSeconds(value); + //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) + // _EarthDateTime = KSPDateStructure.CustomEpochEarth.AddSeconds(value); } } private KSPTimeSpan _TimeSpanFromEpoch; - private DateTime _EarthDateTime; + private DateTime _EarthDateTime { get { return KSPDateStructure.CustomEpochEarth.AddSeconds(UT); } } private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateStructure.EpochYear, 1, KSPDateStructure.EpochDayOfYear); } } #region Constructors @@ -153,6 +155,112 @@ public static KSPDateTime Today { #endregion + #region String Formatter + + private AMPMEnum AMPM { + get { + if (KSPDateStructure.HoursPerDay % 2 == 0) + { + if (Hour < (KSPDateStructure.HoursPerDay / 2)) + return AMPMEnum.AM; + else + return AMPMEnum.PM; + } + else + return AMPMEnum.OddHoursPerDay; + } + } + private enum AMPMEnum { + AM,PM,OddHoursPerDay + } + + + public override String ToString() + { + if (KSPDateStructure.CalendarType ==CalendarTypeEnum.Earth) { + return ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern); + } else { + return ToString("d M y H:mm:ss", null); + } + } + public String ToString(String format) + { + return ToString(format, null); + } + public String ToString(String format, IFormatProvider provider) + { + //parse and replace the format stuff + MatchCollection matches = Regex.Matches(format, "([a-zA-z])\\1{0,}"); + for (int i = matches.Count-1; i >=0; i--) + { + Match m = matches[i]; + //if (format[m.Index - 1] == '\\') continue; + switch (m.Value[0]) + { + case 'y': + format = format.Substring(0, m.Index) + Year.ToString("D" + m.Length) + format.Substring(m.Index + m.Length); + break; + case 'M': + String input2 = Month.ToString("D" + m.Length); + + //test for more than 2 M's + format = format.Substring(0, m.Index) + input2 + format.Substring(m.Index + m.Length); + break; + case 'd': + format = format.Substring(0, m.Index) + Day.ToString("D" + m.Length) + format.Substring(m.Index + m.Length); + break; + case 'h': + //how to do this one AM/PM Hours + String HalfDayTime=""; + switch (AMPM) + { + case AMPMEnum.AM: + HalfDayTime = Hour.ToString("D" + m.Length.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); + break; + case AMPMEnum.PM: + HalfDayTime = (Hour - (KSPDateStructure.HoursPerDay / 2)).ToString("D" + m.Length.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); + break; + case AMPMEnum.OddHoursPerDay: + default: + HalfDayTime = Hour.ToString("D" + m.Length.Clamp(1, KSPDateStructure.HoursPerDay.ToString().Length)); + break; + } + + format = format.Substring(0, m.Index) + HalfDayTime + format.Substring(m.Index + m.Length); + break; + case 't': + if (AMPM != AMPMEnum.OddHoursPerDay) + format = format.Substring(0, m.Index) + AMPM.ToString().ToLower() + format.Substring(m.Index + m.Length); + break; + case 'T': + if (AMPM != AMPMEnum.OddHoursPerDay) + format = format.Substring(0, m.Index) + AMPM.ToString().ToUpper() + format.Substring(m.Index + m.Length); + break; + case 'H': + format = format.Substring(0, m.Index) + Hour.ToString("D" + m.Length.Clamp(1,KSPDateStructure.HoursPerDay.ToString().Length)) + format.Substring(m.Index + m.Length); + break; + case 'm': + format = format.Substring(0, m.Index) + Minute.ToString("D" + m.Length.Clamp(1,KSPDateStructure.MinutesPerHour.ToString().Length)) + format.Substring(m.Index + m.Length); + break; + case 's': + format = format.Substring(0, m.Index) + Second.ToString("D" + m.Length.Clamp(1,KSPDateStructure.SecondsPerMinute.ToString().Length)) + format.Substring(m.Index + m.Length); + break; + + default: + break; + } + } + + return format; + //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) + // return String.Format(format, _EarthDateTime); + //else + // return String.Format(format, this); //"TEST"; + } + + #endregion + + #region Instance Methods #region Mathematic Methods public KSPDateTime Add(KSPTimeSpan value) @@ -241,6 +349,7 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) #endregion + #region Operators public static KSPTimeSpan operator -(KSPDateTime d1, KSPDateTime d2) { diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index f187508..3473ebc 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -151,6 +151,12 @@ internal override void DrawWindow(int id) DrawLabel("Selected:{0}", mbTWP.windowMain.vectSelected); DrawLabel("Departure:{0:0}, Travel:{1:0}", mbTWP.windowMain.DepartureSelected/KSPTime.SecondsPerDay,mbTWP.windowMain.TravelSelected/KSPTime.SecondsPerDay); + + + DrawLabel("Ass:{0}", KACWrapper.AssemblyExists); + DrawLabel("Ins:{0}", KACWrapper.InstanceExists); + DrawLabel("API:{0}", KACWrapper.APIReady); + //if (mbTWP.windowMain.TransferSelected != null) //{ // DrawLabel("D:{0:0} T:{0:0}", mbTWP.windowMain.TransferSelected.DepartureTime, mbTWP.windowMain.TransferSelected.TravelTime); diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index f851924..1b768f3 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -44,6 +44,7 @@ + From 4f8737a8c16c9ba48e64b8c292164fbf1cfa2d18 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sun, 23 Nov 2014 09:03:59 +1100 Subject: [PATCH 17/40] KSPDateTime Done --- KSPDateTimeUnitTests/UnitTest1.cs | 5 +++ .../SharedStuff/KSPDateTime.cs | 43 ++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index 2ca3bd7..a8adbab 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -111,6 +111,8 @@ public void TestFormats() Assert.AreEqual("100/00/0001", dt.ToString("dd/MM/yyyy")); + Assert.AreEqual("Year 1, Day 100, 0:00:00",dt.ToString()); + KSPDateStructure.SetEarthCalendar(); Assert.AreEqual("25/01/1951", dt.ToString("dd/MM/yyyy")); @@ -121,6 +123,9 @@ public void TestFormats() Assert.AreEqual("Hello there 1951",String.Format("Hello there {0:yyyy}",dt)); + + + } } diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index f0fb534..7c77eac 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -180,7 +180,7 @@ public override String ToString() if (KSPDateStructure.CalendarType ==CalendarTypeEnum.Earth) { return ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern); } else { - return ToString("d M y H:mm:ss", null); + return ToString("Year y, Da\\y d, HH:mm:ss", null); } } public String ToString(String format) @@ -194,20 +194,30 @@ public String ToString(String format, IFormatProvider provider) for (int i = matches.Count-1; i >=0; i--) { Match m = matches[i]; - //if (format[m.Index - 1] == '\\') continue; + Int32 mIndex = m.Index,mLength = m.Length; + + if (mIndex>0 && format[m.Index - 1] == '\\') + { + if (m.Length == 1) + continue; + else { + mIndex++; + mLength--; + } + } switch (m.Value[0]) { case 'y': - format = format.Substring(0, m.Index) + Year.ToString("D" + m.Length) + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + Year.ToString("D" + mLength) + format.Substring(mIndex + mLength); break; case 'M': - String input2 = Month.ToString("D" + m.Length); + String input2 = Month.ToString("D" + mLength); //test for more than 2 M's - format = format.Substring(0, m.Index) + input2 + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + input2 + format.Substring(mIndex + mLength); break; case 'd': - format = format.Substring(0, m.Index) + Day.ToString("D" + m.Length) + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + Day.ToString("D" + mLength) + format.Substring(mIndex + mLength); break; case 'h': //how to do this one AM/PM Hours @@ -215,35 +225,35 @@ public String ToString(String format, IFormatProvider provider) switch (AMPM) { case AMPMEnum.AM: - HalfDayTime = Hour.ToString("D" + m.Length.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); + HalfDayTime = Hour.ToString("D" + mLength.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); break; case AMPMEnum.PM: - HalfDayTime = (Hour - (KSPDateStructure.HoursPerDay / 2)).ToString("D" + m.Length.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); + HalfDayTime = (Hour - (KSPDateStructure.HoursPerDay / 2)).ToString("D" + mLength.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); break; case AMPMEnum.OddHoursPerDay: default: - HalfDayTime = Hour.ToString("D" + m.Length.Clamp(1, KSPDateStructure.HoursPerDay.ToString().Length)); + HalfDayTime = Hour.ToString("D" + mLength.Clamp(1, KSPDateStructure.HoursPerDay.ToString().Length)); break; } - format = format.Substring(0, m.Index) + HalfDayTime + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + HalfDayTime + format.Substring(mIndex + mLength); break; case 't': if (AMPM != AMPMEnum.OddHoursPerDay) - format = format.Substring(0, m.Index) + AMPM.ToString().ToLower() + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + AMPM.ToString().ToLower() + format.Substring(mIndex + mLength); break; case 'T': if (AMPM != AMPMEnum.OddHoursPerDay) - format = format.Substring(0, m.Index) + AMPM.ToString().ToUpper() + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + AMPM.ToString().ToUpper() + format.Substring(mIndex + mLength); break; case 'H': - format = format.Substring(0, m.Index) + Hour.ToString("D" + m.Length.Clamp(1,KSPDateStructure.HoursPerDay.ToString().Length)) + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + Hour.ToString("D" + mLength.Clamp(1,KSPDateStructure.HoursPerDay.ToString().Length)) + format.Substring(mIndex + mLength); break; case 'm': - format = format.Substring(0, m.Index) + Minute.ToString("D" + m.Length.Clamp(1,KSPDateStructure.MinutesPerHour.ToString().Length)) + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + Minute.ToString("D" + mLength.Clamp(1,KSPDateStructure.MinutesPerHour.ToString().Length)) + format.Substring(mIndex + mLength); break; case 's': - format = format.Substring(0, m.Index) + Second.ToString("D" + m.Length.Clamp(1,KSPDateStructure.SecondsPerMinute.ToString().Length)) + format.Substring(m.Index + m.Length); + format = format.Substring(0, mIndex) + Second.ToString("D" + mLength.Clamp(1,KSPDateStructure.SecondsPerMinute.ToString().Length)) + format.Substring(mIndex + mLength); break; default: @@ -251,6 +261,9 @@ public String ToString(String format, IFormatProvider provider) } } + //Now strip out the \ , but not multiple \\ + format = Regex.Replace(format, "\\\\(?=[a-z])", ""); + return format; //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) // return String.Format(format, _EarthDateTime); From c2abbf2e2a4357e4a6984b3c1eeac1da0577cc2d Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sun, 23 Nov 2014 22:43:11 +1100 Subject: [PATCH 18/40] Time Library is done - now to replace the current time class --- KSPDateTimeUnitTests/UnitTest1.cs | 2 +- .../SharedStuff/KSPDateTime.cs | 2 +- .../SharedStuff/KSPTimeSpan.cs | 96 ++++++++++++++++++- 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/KSPDateTimeUnitTests/UnitTest1.cs b/KSPDateTimeUnitTests/UnitTest1.cs index a8adbab..38175fb 100644 --- a/KSPDateTimeUnitTests/UnitTest1.cs +++ b/KSPDateTimeUnitTests/UnitTest1.cs @@ -111,7 +111,7 @@ public void TestFormats() Assert.AreEqual("100/00/0001", dt.ToString("dd/MM/yyyy")); - Assert.AreEqual("Year 1, Day 100, 0:00:00",dt.ToString()); + Assert.AreEqual("Year 1, Day 100 - 0h, 0m",dt.ToString()); KSPDateStructure.SetEarthCalendar(); diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs index 7c77eac..ac57cb7 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/SharedStuff/KSPDateTime.cs @@ -180,7 +180,7 @@ public override String ToString() if (KSPDateStructure.CalendarType ==CalendarTypeEnum.Earth) { return ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern); } else { - return ToString("Year y, Da\\y d, HH:mm:ss", null); + return ToString("Year y, Da\\y d - H\\h, m\\m", null); } } public String ToString(String format) diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs index c43e431..3845813 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs @@ -3,9 +3,11 @@ using System.Linq; using System.Text; +using System.Text.RegularExpressions; + namespace KSPPluginFramework { - public class KSPTimeSpan + public class KSPTimeSpan : IFormattable { //Descriptors of Timespan - uses UT as the Root value public Int32 Days { @@ -67,6 +69,98 @@ public KSPTimeSpan(Double ut) public Double TotalDays { get { return UT / KSPDateStructure.SecondsPerDay; } } #endregion + #region String Formatter + + public override String ToString() + { + return ToString(3); + } + + public String ToString(Int32 Precision) + { + Int32 Displayed = 0; + String format = ""; + + if(Precision > 3 || Days>0 ){ + format = "d\\d,"; + Displayed++; + } + if ((Hours>0 && Displayed0 && Displayed0 && Displayed= 0; i--) + { + Match m = matches[i]; + Int32 mIndex = m.Index, mLength = m.Length; + + if (mIndex > 0 && format[m.Index - 1] == '\\') + { + if (m.Length == 1) + continue; + else + { + mIndex++; + mLength--; + } + } + switch (m.Value[0]) + { + case 'd': + format = format.Substring(0, mIndex) + Days.ToString("D" + mLength) + format.Substring(mIndex + mLength); + break; + case 'h': + format = format.Substring(0, mIndex) + Hours.ToString("D" + mLength.Clamp(1, KSPDateStructure.HoursPerDay.ToString().Length)) + format.Substring(mIndex + mLength); + break; + case 'm': + format = format.Substring(0, mIndex) + Minutes.ToString("D" + mLength.Clamp(1, KSPDateStructure.MinutesPerHour.ToString().Length)) + format.Substring(mIndex + mLength); + break; + case 's': + format = format.Substring(0, mIndex) + Seconds.ToString("D" + mLength.Clamp(1, KSPDateStructure.SecondsPerMinute.ToString().Length)) + format.Substring(mIndex + mLength); + break; + + default: + break; + } + } + + //Now strip out the \ , but not multiple \\ + format = Regex.Replace(format, "\\\\(?=[a-z])", ""); + + return format; + //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) + // return String.Format(format, _EarthDateTime); + //else + // return String.Format(format, this); //"TEST"; + } + + #endregion + #region Instance Methods #region Mathematic Methods public KSPTimeSpan Add(KSPTimeSpan value) { From e7c4cc5c957da5a54275e9ee45bfd22f436b31db Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sun, 23 Nov 2014 23:12:07 +1100 Subject: [PATCH 19/40] Changed all working references - now to sort out the "Print" functions --- .../KSPDateStructure.cs | 0 .../KSPDateTime.cs | 5 +- .../KSPTimeSpan.cs | 4 + TransferWindowPlanner/TWPWindow.cs | 14 +- TransferWindowPlanner/TWPWindowWorkers.cs | 14 +- TransferWindowPlanner/TimeObjects.cs | 630 +++++++++--------- .../TransferWindowPlanner.csproj | 6 +- 7 files changed, 340 insertions(+), 333 deletions(-) rename TransferWindowPlanner/{SharedStuff => FrameworkExt}/KSPDateStructure.cs (100%) rename TransferWindowPlanner/{SharedStuff => FrameworkExt}/KSPDateTime.cs (98%) rename TransferWindowPlanner/{SharedStuff => FrameworkExt}/KSPTimeSpan.cs (97%) diff --git a/TransferWindowPlanner/SharedStuff/KSPDateStructure.cs b/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs similarity index 100% rename from TransferWindowPlanner/SharedStuff/KSPDateStructure.cs rename to TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs diff --git a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs similarity index 98% rename from TransferWindowPlanner/SharedStuff/KSPDateTime.cs rename to TransferWindowPlanner/FrameworkExt/KSPDateTime.cs index ac57cb7..85ac0b2 100644 --- a/TransferWindowPlanner/SharedStuff/KSPDateTime.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs @@ -116,9 +116,12 @@ public KSPDateTime(int year, int dayofyear) { UT = new KSPDateTime(year, dayofyear, 0, 0, 0).UT; } + public KSPDateTime(String year, String day, String hour, String minute, String second) + { + UT = new KSPDateTime(Convert.ToInt32(year), Convert.ToInt32(day), Convert.ToInt32(hour), Convert.ToInt32(minute), Convert.ToInt32(second), 0).UT; + } public KSPDateTime(int year, int day, int hour, int minute, int second) { - UT = new KSPDateTime(year, day, hour, minute, second, 0).UT; } public KSPDateTime(int year, int day, int hour, int minute, int second, int millisecond) diff --git a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs similarity index 97% rename from TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs rename to TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs index 3845813..fad5267 100644 --- a/TransferWindowPlanner/SharedStuff/KSPTimeSpan.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs @@ -40,6 +40,10 @@ public KSPTimeSpan(int hours, int minutes, int seconds) { UT = new KSPTimeSpan(0, hours, minutes, seconds, 0).UT; } + public KSPTimeSpan(String days, String hours, String minutes, String seconds) + { + UT = new KSPTimeSpan(Convert.ToInt32(days), Convert.ToInt32(hours), Convert.ToInt32(minutes), Convert.ToInt32(seconds), 0).UT; + } public KSPTimeSpan(int days, int hours, int minutes, int seconds) { UT = new KSPTimeSpan(days, hours, minutes, seconds, 0).UT; diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index 5cd72c7..dbd30e3 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -88,11 +88,11 @@ void TWPWindow_WindowVisibleChanged(MonoBehaviourWindow sender, bool NewVisibleS private void SetDepartureMinToYesterday() { //Set the Departure min to be yesterday - KSPTime timeYesterday = new KSPTime(Planetarium.GetUniversalTime() - KSPTime.SecondsPerDay); - LogFormatted("Setting to y{0} d{1}", timeYesterday.Year, timeYesterday.Day); - strDepartureMinYear = (timeYesterday.Year+1).ToString(); - strDepartureMinDay = (timeYesterday.Day+1).ToString(); - DepartureMin = KSPTime.BuildUTFromRaw(strDepartureMinYear, strDepartureMinDay, "0", "0", "0") - KSPTime.SecondsPerYear - KSPTime.SecondsPerDay; + KSPDateTime dateYesterday = new KSPDateTime(Planetarium.GetUniversalTime() - KSPDateStructure.SecondsPerDay); + LogFormatted("Setting to y{0} d{1}", dateYesterday.Year, dateYesterday.Day); + strDepartureMinYear = (dateYesterday.Year+1).ToString(); + strDepartureMinDay = (dateYesterday.Day+1).ToString(); + DepartureMin = new KSPDateTime(dateYesterday.Year+1, dateYesterday.Day+1, 0, 0, 0).UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; } void ddlOrigin_OnSelectionChanged(MonoBehaviourWindowPlus.DropDownList sender, int OldIndex, int NewIndex) @@ -520,13 +520,13 @@ private void DrawTransferPlot() GUI.matrix = matrixBackup; //Y Axis for (Double i = 0; i <= 1; i += 0.25) { - GUI.Label(new Rect((Single)(PlotPosition.x - 50), (Single)(PlotPosition.y + (i * (PlotHeight - 3)) - 5), 40, 15), String.Format("{0:0}", (TransferSpecs.TravelMin + (1 - i) * TransferSpecs.TravelRange) / (KSPTime.SecondsPerDay)), Styles.stylePlotYText); + GUI.Label(new Rect((Single)(PlotPosition.x - 50), (Single)(PlotPosition.y + (i * (PlotHeight - 3)) - 5), 40, 15), String.Format("{0:0}", (TransferSpecs.TravelMin + (1 - i) * TransferSpecs.TravelRange) / (KSPDateStructure.SecondsPerDay)), Styles.stylePlotYText); } //XAxis GUI.Label(new Rect((Single)(PlotPosition.x), (Single)(PlotPosition.y + PlotHeight + 20), PlotWidth, 15), "Departure Date", Styles.stylePlotXLabel); for (Double i = 0; i <= 1; i += 0.25) { - GUI.Label(new Rect((Single)(PlotPosition.x + (i * PlotWidth) - 22), (Single)(PlotPosition.y + PlotHeight + 5), 40, 15), String.Format("{0:0}", (TransferSpecs.DepartureMin + i * TransferSpecs.DepartureRange) / (KSPTime.SecondsPerDay)), Styles.stylePlotXText); + GUI.Label(new Rect((Single)(PlotPosition.x + (i * PlotWidth) - 22), (Single)(PlotPosition.y + PlotHeight + 5), 40, 15), String.Format("{0:0}", (TransferSpecs.DepartureMin + i * TransferSpecs.DepartureRange) / (KSPDateStructure.SecondsPerDay)), Styles.stylePlotXText); } //Draw the DeltaV Legend diff --git a/TransferWindowPlanner/TWPWindowWorkers.cs b/TransferWindowPlanner/TWPWindowWorkers.cs index 865413e..4614071 100644 --- a/TransferWindowPlanner/TWPWindowWorkers.cs +++ b/TransferWindowPlanner/TWPWindowWorkers.cs @@ -72,7 +72,7 @@ private void SetWindowStrings() strOrigin = cbOrigin.bodyName; strDestination = cbDestination.bodyName; - KSPTime kTime = new KSPTime(DepartureMin); + KSPDateTime kTime = new KSPDateTime(DepartureMin); strDepartureMinYear = (kTime.Year + 1).ToString(); strDepartureMinDay = (kTime.Day + 1).ToString(); @@ -81,10 +81,10 @@ private void SetWindowStrings() strDepartureMaxDay = (kTime.Day + 1).ToString(); kTime.UT = TravelMin; - strTravelMinDays = (kTime.Year * KSPTime.DaysPerYear + kTime.Day).ToString(); + strTravelMinDays = (kTime.Year * KSPDateStructure.DaysPerYear + kTime.Day).ToString(); kTime.UT = TravelMax; - strTravelMaxDays = (kTime.Year * KSPTime.DaysPerYear + kTime.Day).ToString(); + strTravelMaxDays = (kTime.Year * KSPDateStructure.DaysPerYear + kTime.Day).ToString(); strArrivalAltitude = (InitialOrbitAltitude / 1000).ToString(); strDepartureAltitude = (FinalOrbitAltitude / 1000).ToString(); @@ -116,12 +116,12 @@ internal class TransferWorkerDetails private void SetWorkerVariables() { - DepartureMin = KSPTime.BuildUTFromRaw(strDepartureMinYear, strDepartureMinDay, "0", "0", "0") - KSPTime.SecondsPerYear - KSPTime.SecondsPerDay; - DepartureMax = KSPTime.BuildUTFromRaw(strDepartureMaxYear, strDepartureMaxDay, "0", "0", "0") - KSPTime.SecondsPerYear - KSPTime.SecondsPerDay; + DepartureMin = new KSPDateTime(strDepartureMinYear, strDepartureMinDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; + DepartureMax = new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; DepartureRange = DepartureMax - DepartureMin; DepartureSelected = -1; - TravelMin = KSPTime.BuildUTFromRaw("0", strTravelMinDays, "0", "0", "0"); - TravelMax = KSPTime.BuildUTFromRaw("0", strTravelMaxDays, "0", "0", "0"); + TravelMin = new KSPTimeSpan(strTravelMinDays, "0", "0", "0").UT; + TravelMax = new KSPTimeSpan(strTravelMaxDays, "0", "0", "0").UT; TravelRange = TravelMax - TravelMin; TravelSelected = -1; FinalOrbitAltitude = Convert.ToDouble(strArrivalAltitude) * 1000; diff --git a/TransferWindowPlanner/TimeObjects.cs b/TransferWindowPlanner/TimeObjects.cs index a0953fb..995c21c 100644 --- a/TransferWindowPlanner/TimeObjects.cs +++ b/TransferWindowPlanner/TimeObjects.cs @@ -12,320 +12,320 @@ namespace TransferWindowPlanner /// /// A class to store the UT of events and get back useful data /// - public class KSPTime - { - - //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math - // to go to years and get back to full days isnt confusing - have to sort this out some day though. - //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo - const double HoursPerDayEarth = 24; - const double HoursPerYearEarth = 365 * HoursPerDayEarth; - - const double HoursPerDayKerbin = 6; - const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; - - #region "Constructors" - public KSPTime() - { } - public KSPTime(double NewUT) - { - UT = NewUT; - } - public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) - { - UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); - } - #endregion - - /// - /// Build the UT from raw values - /// - /// - /// - /// - /// - /// - public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) - { - UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); - } - - public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) - { - BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); - } - - #region "Properties" - - //Stores the Universal Time in game seconds - public double UT; - - //readonly props that resolve from UT - public long Second - { - get { return Convert.ToInt64(Math.Truncate(UT % 60)); } - } - public long Minute - { - get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } - } - - private double HourRaw { get { return UT / 60 / 60; } } - - public long Hour - { - get - { - if (GameSettings.KERBIN_TIME) { - return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); - } - else { - return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); - } - } - } - - public long Day - { - get - { - if (GameSettings.KERBIN_TIME) { - return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); - } - else { - return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); - } - } - } - - public long Year - { - get - { - if (GameSettings.KERBIN_TIME) { - return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); - } - else { - return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); - } - } - } - //public long HourKerbin - //{ - // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } - //} - - //public long DayKerbin - //{ - // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } - //} - - //public long YearKerbin - //{ - // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } - //} - #endregion - - #region "String Formatting" - public String IntervalString() - { - return IntervalString(6); - } - public String IntervalString(int segments) - { - String strReturn = ""; - - if (UT < 0) strReturn += "+ "; - - int intUsed = 0; - - if (intUsed < segments && Year != 0) { - strReturn += String.Format("{0}y", Math.Abs(Year)); - intUsed++; - } - - if (intUsed < segments && (Day != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}d", Math.Abs(Day)); - intUsed++; - } - - if (intUsed < segments && (Hour != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}h", Math.Abs(Hour)); - intUsed++; - } - if (intUsed < segments && (Minute != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}m", Math.Abs(Minute)); - intUsed++; - } - if (intUsed < segments)// && (Second != 0 || intUsed > 0)) - { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}s", Math.Abs(Second)); - intUsed++; - } - - - return strReturn; - } - - public String IntervalDateTimeString() - { - return IntervalDateTimeString(6); - } - public String IntervalDateTimeString(int segments) - { - String strReturn = ""; - - if (UT < 0) strReturn += "+ "; - - int intUsed = 0; - - if (intUsed < segments && Year != 0) { - strReturn += String.Format("{0}y", Math.Abs(Year)); - intUsed++; - } - - if (intUsed < segments && (Day != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}d", Math.Abs(Day)); - intUsed++; - } - - if (intUsed < segments && (Hour != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0:00}", Math.Abs(Hour)); - intUsed++; - } - if (intUsed < segments && (Minute != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ":"; - strReturn += String.Format("{0:00}", Math.Abs(Minute)); - intUsed++; - } - if (intUsed < segments)// && (Second != 0 || intUsed > 0)) - { - if (intUsed > 0) strReturn += ":"; - strReturn += String.Format("{0:00}", Math.Abs(Second)); - intUsed++; - } - - - return strReturn; - } - - public String DateString() - { - return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); - } - - public String DateTimeString() - { - return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); - } - - public String IntervalStringLong() - { - String strReturn = ""; - if (UT < 0) strReturn += "+ "; - strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); - return strReturn; - } - public String IntervalStringLongTrimYears() - { - String strReturn = IntervalStringLong(); - return strReturn.Replace("0 Years, ", ""); - } - - public String UTString() - { - String strReturn = ""; - if (UT < 0) strReturn += "+ "; - strReturn += String.Format("{0:N0}s", Math.Abs(UT)); - return strReturn; - } - #endregion - - public override String ToString() - { - return IntervalStringLong(); - } - - #region Static Properties - public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } - public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } - public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } - public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } - public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } - #endregion - - #region "Static Functions" - //fudging for dates - public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); - - public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) - { - return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); - } - public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) - { - if (GameSettings.KERBIN_TIME) { - return Seconds + - Minutes * 60 + - Hours * 60 * 60 + - Days * HoursPerDayKerbin * 60 * 60 + - Years * HoursPerYearKerbin * 60 * 60; - } - else { - return Seconds + - Minutes * 60 + - Hours * 60 * 60 + - Days * HoursPerDayEarth * 60 * 60 + - Years * HoursPerYearEarth * 60 * 60; - } - } - - public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) - { - return PrintInterval(timeTemp, 3, TimeFormat); - } - - public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) - { - switch (TimeFormat) { - case PrintTimeFormat.TimeAsUT: - return timeTemp.UTString(); - case PrintTimeFormat.KSPString: - return timeTemp.IntervalString(Segments); - case PrintTimeFormat.DateTimeString: - return timeTemp.IntervalDateTimeString(Segments); - default: - return timeTemp.IntervalString(Segments); - } - } - - public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) - { - switch (TimeFormat) { - case PrintTimeFormat.TimeAsUT: - return timeTemp.UTString(); - case PrintTimeFormat.KSPString: - return timeTemp.DateString(); - case PrintTimeFormat.DateTimeString: - return timeTemp.DateTimeString(); - default: - return timeTemp.DateTimeString(); - } - } - - public enum PrintTimeFormat - { - TimeAsUT, - KSPString, - DateTimeString - } - #endregion - } + //public class KSPTime + //{ + + // //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math + // // to go to years and get back to full days isnt confusing - have to sort this out some day though. + // //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo + // const double HoursPerDayEarth = 24; + // const double HoursPerYearEarth = 365 * HoursPerDayEarth; + + // const double HoursPerDayKerbin = 6; + // const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; + + // #region "Constructors" + // public KSPTime() + // { } + // public KSPTime(double NewUT) + // { + // UT = NewUT; + // } + // public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) + // { + // UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); + // } + // #endregion + + // /// + // /// Build the UT from raw values + // /// + // /// + // /// + // /// + // /// + // /// + // public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) + // { + // UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); + // } + + // public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) + // { + // BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); + // } + + // #region "Properties" + + // //Stores the Universal Time in game seconds + // public double UT; + + // //readonly props that resolve from UT + // public long Second + // { + // get { return Convert.ToInt64(Math.Truncate(UT % 60)); } + // } + // public long Minute + // { + // get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } + // } + + // private double HourRaw { get { return UT / 60 / 60; } } + + // public long Hour + // { + // get + // { + // if (GameSettings.KERBIN_TIME) { + // return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); + // } + // else { + // return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); + // } + // } + // } + + // public long Day + // { + // get + // { + // if (GameSettings.KERBIN_TIME) { + // return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); + // } + // else { + // return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); + // } + // } + // } + + // public long Year + // { + // get + // { + // if (GameSettings.KERBIN_TIME) { + // return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); + // } + // else { + // return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); + // } + // } + // } + // //public long HourKerbin + // //{ + // // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } + // //} + + // //public long DayKerbin + // //{ + // // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } + // //} + + // //public long YearKerbin + // //{ + // // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } + // //} + // #endregion + + // #region "String Formatting" + // public String IntervalString() + // { + // return IntervalString(6); + // } + // public String IntervalString(int segments) + // { + // String strReturn = ""; + + // if (UT < 0) strReturn += "+ "; + + // int intUsed = 0; + + // if (intUsed < segments && Year != 0) { + // strReturn += String.Format("{0}y", Math.Abs(Year)); + // intUsed++; + // } + + // if (intUsed < segments && (Day != 0 || intUsed > 0)) { + // if (intUsed > 0) strReturn += ", "; + // strReturn += String.Format("{0}d", Math.Abs(Day)); + // intUsed++; + // } + + // if (intUsed < segments && (Hour != 0 || intUsed > 0)) { + // if (intUsed > 0) strReturn += ", "; + // strReturn += String.Format("{0}h", Math.Abs(Hour)); + // intUsed++; + // } + // if (intUsed < segments && (Minute != 0 || intUsed > 0)) { + // if (intUsed > 0) strReturn += ", "; + // strReturn += String.Format("{0}m", Math.Abs(Minute)); + // intUsed++; + // } + // if (intUsed < segments)// && (Second != 0 || intUsed > 0)) + // { + // if (intUsed > 0) strReturn += ", "; + // strReturn += String.Format("{0}s", Math.Abs(Second)); + // intUsed++; + // } + + + // return strReturn; + // } + + // public String IntervalDateTimeString() + // { + // return IntervalDateTimeString(6); + // } + // public String IntervalDateTimeString(int segments) + // { + // String strReturn = ""; + + // if (UT < 0) strReturn += "+ "; + + // int intUsed = 0; + + // if (intUsed < segments && Year != 0) { + // strReturn += String.Format("{0}y", Math.Abs(Year)); + // intUsed++; + // } + + // if (intUsed < segments && (Day != 0 || intUsed > 0)) { + // if (intUsed > 0) strReturn += ", "; + // strReturn += String.Format("{0}d", Math.Abs(Day)); + // intUsed++; + // } + + // if (intUsed < segments && (Hour != 0 || intUsed > 0)) { + // if (intUsed > 0) strReturn += ", "; + // strReturn += String.Format("{0:00}", Math.Abs(Hour)); + // intUsed++; + // } + // if (intUsed < segments && (Minute != 0 || intUsed > 0)) { + // if (intUsed > 0) strReturn += ":"; + // strReturn += String.Format("{0:00}", Math.Abs(Minute)); + // intUsed++; + // } + // if (intUsed < segments)// && (Second != 0 || intUsed > 0)) + // { + // if (intUsed > 0) strReturn += ":"; + // strReturn += String.Format("{0:00}", Math.Abs(Second)); + // intUsed++; + // } + + + // return strReturn; + // } + + // public String DateString() + // { + // return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); + // } + + // public String DateTimeString() + // { + // return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); + // } + + // public String IntervalStringLong() + // { + // String strReturn = ""; + // if (UT < 0) strReturn += "+ "; + // strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); + // return strReturn; + // } + // public String IntervalStringLongTrimYears() + // { + // String strReturn = IntervalStringLong(); + // return strReturn.Replace("0 Years, ", ""); + // } + + // public String UTString() + // { + // String strReturn = ""; + // if (UT < 0) strReturn += "+ "; + // strReturn += String.Format("{0:N0}s", Math.Abs(UT)); + // return strReturn; + // } + // #endregion + + // public override String ToString() + // { + // return IntervalStringLong(); + // } + + // #region Static Properties + // public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } + // public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } + // public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } + // public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } + // public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } + // #endregion + + // #region "Static Functions" + // //fudging for dates + // public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); + + // public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) + // { + // return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); + // } + // public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) + // { + // if (GameSettings.KERBIN_TIME) { + // return Seconds + + // Minutes * 60 + + // Hours * 60 * 60 + + // Days * HoursPerDayKerbin * 60 * 60 + + // Years * HoursPerYearKerbin * 60 * 60; + // } + // else { + // return Seconds + + // Minutes * 60 + + // Hours * 60 * 60 + + // Days * HoursPerDayEarth * 60 * 60 + + // Years * HoursPerYearEarth * 60 * 60; + // } + // } + + // public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) + // { + // return PrintInterval(timeTemp, 3, TimeFormat); + // } + + // public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) + // { + // switch (TimeFormat) { + // case PrintTimeFormat.TimeAsUT: + // return timeTemp.UTString(); + // case PrintTimeFormat.KSPString: + // return timeTemp.IntervalString(Segments); + // case PrintTimeFormat.DateTimeString: + // return timeTemp.IntervalDateTimeString(Segments); + // default: + // return timeTemp.IntervalString(Segments); + // } + // } + + // public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) + // { + // switch (TimeFormat) { + // case PrintTimeFormat.TimeAsUT: + // return timeTemp.UTString(); + // case PrintTimeFormat.KSPString: + // return timeTemp.DateString(); + // case PrintTimeFormat.DateTimeString: + // return timeTemp.DateTimeString(); + // default: + // return timeTemp.DateTimeString(); + // } + // } + + // public enum PrintTimeFormat + // { + // TimeAsUT, + // KSPString, + // DateTimeString + // } + // #endregion + //} } \ No newline at end of file diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 1b768f3..255b8bc 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -53,9 +53,9 @@ - - - + + + From 08aa1aa347964d8020e2ed9414bcd309df929fdf Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 24 Nov 2014 18:17:20 +1100 Subject: [PATCH 20/40] Added Standard date formats and resolved errors --- .../FrameworkExt/KSPDateTime.cs | 33 + .../FrameworkExt/KSPTimeSpan.cs | 11 + TransferWindowPlanner/TWPWindow.cs | 24 +- TransferWindowPlanner/TimeObjects.cs | 662 +++++++++--------- 4 files changed, 390 insertions(+), 340 deletions(-) diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs index 85ac0b2..6d833cd 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs @@ -177,6 +177,39 @@ private enum AMPMEnum { AM,PM,OddHoursPerDay } + public enum DateStringFormatsEnum{ + TimeAsUT, + DateFormat, + DateFormatWithSecs, + DateTimeFormat + } + public String ToStringStandard(DateStringFormatsEnum DateFormat){ + switch (DateFormat) + { + case DateStringFormatsEnum.TimeAsUT: + String strReturn = ""; + if (UT < 0) strReturn += "+ "; + strReturn += String.Format("{0:N0}s", Math.Abs(UT)); + return strReturn; + case DateStringFormatsEnum.DateFormat: + return ToString(); + case DateStringFormatsEnum.DateFormatWithSecs: + return ToString("Year y, Da\\y d - H\\h, m\\m, s\\s"); + case DateStringFormatsEnum.DateTimeFormat: + return ToString("Year y,Da\\y d, HH:mm:ss"); + default: + return ToString(); + } + } + public String DateString() + { + return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); + } + + public String DateTimeString() + { + return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); + } public override String ToString() { diff --git a/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs index fad5267..a6101ca 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs @@ -26,6 +26,17 @@ public int Milliseconds { get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } } + //public Int32 Years { + // get { + // if (KSPDateStructure.CalendarType != CalendarTypeEnum.Earth) { + // return (Int32)UT / KSPDateStructure.SecondsPerYear; + // } else { + + // } + // } + //} + + /// /// Replaces the normal "Ticks" function. This is Seconds of UT /// diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index dbd30e3..be4507f 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -314,8 +314,10 @@ private void DrawTransferDetailsMinimal() GUILayout.BeginVertical(); GUILayout.Label(String.Format("{0} (@{1:0}km)", TransferSpecs.OriginName, TransferSpecs.InitialOrbitAltitude / 1000), Styles.styleTextYellow); GUILayout.Label(String.Format("{0} (@{1:0}km)", TransferSpecs.DestinationName, TransferSpecs.FinalOrbitAltitude / 1000), Styles.styleTextYellow); - GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); - GUILayout.Label(String.Format("{0:0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()), Styles.styleTextYellow); + //GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); + GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); + //GUILayout.Label(String.Format("{0:0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()), Styles.styleTextYellow); + GUILayout.Label(new KSPTimeSpan(TransferSelected.DepartureTime).ToString(), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVEjection), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVInjection), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVTotal), Styles.styleTextYellow); @@ -348,8 +350,8 @@ private void DrawTransferDetails() GUILayout.EndVertical(); GUILayout.BeginVertical(); GUILayout.BeginHorizontal(); - GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); - + //GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); + GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); GUIStyle styleCopyButton = new GUIStyle(SkinsLibrary.CurrentSkin.button); styleCopyButton.fixedHeight = 18; styleCopyButton.padding.top = styleCopyButton.padding.bottom = 0; @@ -409,7 +411,8 @@ private void DrawTransferDetails() GUILayout.Label("Insertion Inclination:", Styles.styleTextDetailsLabel); GUILayout.EndVertical(); GUILayout.BeginVertical(); - GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime + TransferSelected.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); + //GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime + TransferSelected.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); + GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime + TransferSelected.TravelTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0.00}°", TransferSelected.EjectionAngle * LambertSolver.Rad2Deg), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0.00}°", TransferSelected.EjectionInclination * LambertSolver.Rad2Deg), Styles.styleTextYellow); if (ShowEjectionDetails) { @@ -428,7 +431,7 @@ private void DrawTransferDetails() GUILayout.Label("Insertion Δv:", Styles.styleTextDetailsLabel); GUILayout.EndVertical(); GUILayout.BeginVertical(); - GUILayout.Label(String.Format("{0:0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()), Styles.styleTextYellow); + GUILayout.Label(String.Format("{0:0}", new KSPTimeSpan(TransferSelected.TravelTime).ToString()), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVTotal), Styles.styleTextYellow); GUILayout.BeginHorizontal(); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVEjection), Styles.styleTextYellow); @@ -461,11 +464,14 @@ private void CopyAllDetailsToClipboard() internal string GenerateTransferDetailsText() { String Message = String.Format("{0} (@{2:0}km) -> {1} (@{3:0}km)", TransferSpecs.OriginName, TransferSpecs.DestinationName, TransferSpecs.InitialOrbitAltitude / 1000, TransferSpecs.FinalOrbitAltitude / 1000); - Message = Message.AppendLine("Depart at: {0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)); + //Message = Message.AppendLine("Depart at: {0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)); + Message = Message.AppendLine("Depart at: {0}", new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat)); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.DepartureTime); - Message = Message.AppendLine(" Travel: {0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()); + //Message = Message.AppendLine(" Travel: {0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()); + Message = Message.AppendLine(" Travel: {0}", new KSPTimeSpan(TransferSelected.TravelTime).ToString()); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.TravelTime); - Message = Message.AppendLine("Arrive at: {0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime + TransferSelected.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)); + //Message = Message.AppendLine("Arrive at: {0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime + TransferSelected.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)); + Message = Message.AppendLine("Arrive at: {0}", new KSPDateTime(TransferSelected.DepartureTime + TransferSelected.TravelTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat)); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.DepartureTime + TransferSelected.TravelTime); Message = Message.AppendLine("Phase Angle: {0:0.00}°", TransferSelected.PhaseAngle * LambertSolver.Rad2Deg); Message = Message.AppendLine("Ejection Angle: {0:0.00}°", TransferSelected.EjectionAngle * LambertSolver.Rad2Deg); diff --git a/TransferWindowPlanner/TimeObjects.cs b/TransferWindowPlanner/TimeObjects.cs index 995c21c..b13cbf4 100644 --- a/TransferWindowPlanner/TimeObjects.cs +++ b/TransferWindowPlanner/TimeObjects.cs @@ -1,331 +1,331 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -using KSP; -using UnityEngine; -using KSPPluginFramework; - -namespace TransferWindowPlanner -{ - /// - /// A class to store the UT of events and get back useful data - /// - //public class KSPTime - //{ - - // //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math - // // to go to years and get back to full days isnt confusing - have to sort this out some day though. - // //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo - // const double HoursPerDayEarth = 24; - // const double HoursPerYearEarth = 365 * HoursPerDayEarth; - - // const double HoursPerDayKerbin = 6; - // const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; - - // #region "Constructors" - // public KSPTime() - // { } - // public KSPTime(double NewUT) - // { - // UT = NewUT; - // } - // public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) - // { - // UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); - // } - // #endregion - - // /// - // /// Build the UT from raw values - // /// - // /// - // /// - // /// - // /// - // /// - // public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) - // { - // UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); - // } - - // public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) - // { - // BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); - // } - - // #region "Properties" - - // //Stores the Universal Time in game seconds - // public double UT; - - // //readonly props that resolve from UT - // public long Second - // { - // get { return Convert.ToInt64(Math.Truncate(UT % 60)); } - // } - // public long Minute - // { - // get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } - // } - - // private double HourRaw { get { return UT / 60 / 60; } } - - // public long Hour - // { - // get - // { - // if (GameSettings.KERBIN_TIME) { - // return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); - // } - // else { - // return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); - // } - // } - // } - - // public long Day - // { - // get - // { - // if (GameSettings.KERBIN_TIME) { - // return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); - // } - // else { - // return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); - // } - // } - // } - - // public long Year - // { - // get - // { - // if (GameSettings.KERBIN_TIME) { - // return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); - // } - // else { - // return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); - // } - // } - // } - // //public long HourKerbin - // //{ - // // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } - // //} - - // //public long DayKerbin - // //{ - // // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } - // //} - - // //public long YearKerbin - // //{ - // // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } - // //} - // #endregion - - // #region "String Formatting" - // public String IntervalString() - // { - // return IntervalString(6); - // } - // public String IntervalString(int segments) - // { - // String strReturn = ""; - - // if (UT < 0) strReturn += "+ "; - - // int intUsed = 0; - - // if (intUsed < segments && Year != 0) { - // strReturn += String.Format("{0}y", Math.Abs(Year)); - // intUsed++; - // } - - // if (intUsed < segments && (Day != 0 || intUsed > 0)) { - // if (intUsed > 0) strReturn += ", "; - // strReturn += String.Format("{0}d", Math.Abs(Day)); - // intUsed++; - // } - - // if (intUsed < segments && (Hour != 0 || intUsed > 0)) { - // if (intUsed > 0) strReturn += ", "; - // strReturn += String.Format("{0}h", Math.Abs(Hour)); - // intUsed++; - // } - // if (intUsed < segments && (Minute != 0 || intUsed > 0)) { - // if (intUsed > 0) strReturn += ", "; - // strReturn += String.Format("{0}m", Math.Abs(Minute)); - // intUsed++; - // } - // if (intUsed < segments)// && (Second != 0 || intUsed > 0)) - // { - // if (intUsed > 0) strReturn += ", "; - // strReturn += String.Format("{0}s", Math.Abs(Second)); - // intUsed++; - // } - - - // return strReturn; - // } - - // public String IntervalDateTimeString() - // { - // return IntervalDateTimeString(6); - // } - // public String IntervalDateTimeString(int segments) - // { - // String strReturn = ""; - - // if (UT < 0) strReturn += "+ "; - - // int intUsed = 0; - - // if (intUsed < segments && Year != 0) { - // strReturn += String.Format("{0}y", Math.Abs(Year)); - // intUsed++; - // } - - // if (intUsed < segments && (Day != 0 || intUsed > 0)) { - // if (intUsed > 0) strReturn += ", "; - // strReturn += String.Format("{0}d", Math.Abs(Day)); - // intUsed++; - // } - - // if (intUsed < segments && (Hour != 0 || intUsed > 0)) { - // if (intUsed > 0) strReturn += ", "; - // strReturn += String.Format("{0:00}", Math.Abs(Hour)); - // intUsed++; - // } - // if (intUsed < segments && (Minute != 0 || intUsed > 0)) { - // if (intUsed > 0) strReturn += ":"; - // strReturn += String.Format("{0:00}", Math.Abs(Minute)); - // intUsed++; - // } - // if (intUsed < segments)// && (Second != 0 || intUsed > 0)) - // { - // if (intUsed > 0) strReturn += ":"; - // strReturn += String.Format("{0:00}", Math.Abs(Second)); - // intUsed++; - // } - - - // return strReturn; - // } - - // public String DateString() - // { - // return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); - // } - - // public String DateTimeString() - // { - // return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); - // } - - // public String IntervalStringLong() - // { - // String strReturn = ""; - // if (UT < 0) strReturn += "+ "; - // strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); - // return strReturn; - // } - // public String IntervalStringLongTrimYears() - // { - // String strReturn = IntervalStringLong(); - // return strReturn.Replace("0 Years, ", ""); - // } - - // public String UTString() - // { - // String strReturn = ""; - // if (UT < 0) strReturn += "+ "; - // strReturn += String.Format("{0:N0}s", Math.Abs(UT)); - // return strReturn; - // } - // #endregion - - // public override String ToString() - // { - // return IntervalStringLong(); - // } - - // #region Static Properties - // public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } - // public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } - // public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } - // public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } - // public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } - // #endregion - - // #region "Static Functions" - // //fudging for dates - // public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); - - // public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) - // { - // return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); - // } - // public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) - // { - // if (GameSettings.KERBIN_TIME) { - // return Seconds + - // Minutes * 60 + - // Hours * 60 * 60 + - // Days * HoursPerDayKerbin * 60 * 60 + - // Years * HoursPerYearKerbin * 60 * 60; - // } - // else { - // return Seconds + - // Minutes * 60 + - // Hours * 60 * 60 + - // Days * HoursPerDayEarth * 60 * 60 + - // Years * HoursPerYearEarth * 60 * 60; - // } - // } - - // public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) - // { - // return PrintInterval(timeTemp, 3, TimeFormat); - // } - - // public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) - // { - // switch (TimeFormat) { - // case PrintTimeFormat.TimeAsUT: - // return timeTemp.UTString(); - // case PrintTimeFormat.KSPString: - // return timeTemp.IntervalString(Segments); - // case PrintTimeFormat.DateTimeString: - // return timeTemp.IntervalDateTimeString(Segments); - // default: - // return timeTemp.IntervalString(Segments); - // } - // } - - // public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) - // { - // switch (TimeFormat) { - // case PrintTimeFormat.TimeAsUT: - // return timeTemp.UTString(); - // case PrintTimeFormat.KSPString: - // return timeTemp.DateString(); - // case PrintTimeFormat.DateTimeString: - // return timeTemp.DateTimeString(); - // default: - // return timeTemp.DateTimeString(); - // } - // } - - // public enum PrintTimeFormat - // { - // TimeAsUT, - // KSPString, - // DateTimeString - // } - // #endregion - //} - -} \ No newline at end of file +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; + +//using KSP; +//using UnityEngine; +//using KSPPluginFramework; + +//namespace TransferWindowPlanner +//{ +// +// A class to store the UT of events and get back useful data +// +// public class KSPTime +// { + +// //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math +// // to go to years and get back to full days isnt confusing - have to sort this out some day though. +// //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo +// const double HoursPerDayEarth = 24; +// const double HoursPerYearEarth = 365 * HoursPerDayEarth; + +// const double HoursPerDayKerbin = 6; +// const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; + +// #region "Constructors" +// public KSPTime() +// { } +// public KSPTime(double NewUT) +// { +// UT = NewUT; +// } +// public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) +// { +// UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); +// } +// #endregion + +// /// +// /// Build the UT from raw values +// /// +// /// +// /// +// /// +// /// +// /// +// public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) +// { +// UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); +// } + +// public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) +// { +// BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); +// } + +// #region "Properties" + +// //Stores the Universal Time in game seconds +// public double UT; + +// //readonly props that resolve from UT +// public long Second +// { +// get { return Convert.ToInt64(Math.Truncate(UT % 60)); } +// } +// public long Minute +// { +// get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } +// } + +// private double HourRaw { get { return UT / 60 / 60; } } + +// public long Hour +// { +// get +// { +// if (GameSettings.KERBIN_TIME) { +// return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); +// } +// else { +// return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); +// } +// } +// } + +// public long Day +// { +// get +// { +// if (GameSettings.KERBIN_TIME) { +// return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); +// } +// else { +// return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); +// } +// } +// } + +// public long Year +// { +// get +// { +// if (GameSettings.KERBIN_TIME) { +// return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); +// } +// else { +// return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); +// } +// } +// } +// //public long HourKerbin +// //{ +// // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } +// //} + +// //public long DayKerbin +// //{ +// // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } +// //} + +// //public long YearKerbin +// //{ +// // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } +// //} +// #endregion + +// #region "String Formatting" +// public String IntervalString() +// { +// return IntervalString(6); +// } +// public String IntervalString(int segments) +// { +// String strReturn = ""; + +// if (UT < 0) strReturn += "+ "; + +// int intUsed = 0; + +// if (intUsed < segments && Year != 0) { +// strReturn += String.Format("{0}y", Math.Abs(Year)); +// intUsed++; +// } + +// if (intUsed < segments && (Day != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}d", Math.Abs(Day)); +// intUsed++; +// } + +// if (intUsed < segments && (Hour != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}h", Math.Abs(Hour)); +// intUsed++; +// } +// if (intUsed < segments && (Minute != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}m", Math.Abs(Minute)); +// intUsed++; +// } +// if (intUsed < segments)// && (Second != 0 || intUsed > 0)) +// { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}s", Math.Abs(Second)); +// intUsed++; +// } + + +// return strReturn; +// } + +// public String IntervalDateTimeString() +// { +// return IntervalDateTimeString(6); +// } +// public String IntervalDateTimeString(int segments) +// { +// String strReturn = ""; + +// if (UT < 0) strReturn += "+ "; + +// int intUsed = 0; + +// if (intUsed < segments && Year != 0) { +// strReturn += String.Format("{0}y", Math.Abs(Year)); +// intUsed++; +// } + +// if (intUsed < segments && (Day != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}d", Math.Abs(Day)); +// intUsed++; +// } + +// if (intUsed < segments && (Hour != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0:00}", Math.Abs(Hour)); +// intUsed++; +// } +// if (intUsed < segments && (Minute != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ":"; +// strReturn += String.Format("{0:00}", Math.Abs(Minute)); +// intUsed++; +// } +// if (intUsed < segments)// && (Second != 0 || intUsed > 0)) +// { +// if (intUsed > 0) strReturn += ":"; +// strReturn += String.Format("{0:00}", Math.Abs(Second)); +// intUsed++; +// } + + +// return strReturn; +// } + +// public String DateString() +// { +// return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); +// } + +// public String DateTimeString() +// { +// return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); +// } + +// public String IntervalStringLong() +// { +// String strReturn = ""; +// if (UT < 0) strReturn += "+ "; +// strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); +// return strReturn; +// } +// public String IntervalStringLongTrimYears() +// { +// String strReturn = IntervalStringLong(); +// return strReturn.Replace("0 Years, ", ""); +// } + +// public String UTString() +// { +// String strReturn = ""; +// if (UT < 0) strReturn += "+ "; +// strReturn += String.Format("{0:N0}s", Math.Abs(UT)); +// return strReturn; +// } +// #endregion + +// public override String ToString() +// { +// return IntervalStringLong(); +// } + +// #region Static Properties +// public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } +// public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } +// public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } +// public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } +// public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } +// #endregion + +// #region "Static Functions" +// //fudging for dates +// public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); + +// public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) +// { +// return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); +// } +// public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) +// { +// if (GameSettings.KERBIN_TIME) { +// return Seconds + +// Minutes * 60 + +// Hours * 60 * 60 + +// Days * HoursPerDayKerbin * 60 * 60 + +// Years * HoursPerYearKerbin * 60 * 60; +// } +// else { +// return Seconds + +// Minutes * 60 + +// Hours * 60 * 60 + +// Days * HoursPerDayEarth * 60 * 60 + +// Years * HoursPerYearEarth * 60 * 60; +// } +// } + +// public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) +// { +// return PrintInterval(timeTemp, 3, TimeFormat); +// } + +// public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) +// { +// switch (TimeFormat) { +// case PrintTimeFormat.TimeAsUT: +// return timeTemp.UTString(); +// case PrintTimeFormat.KSPString: +// return timeTemp.IntervalString(Segments); +// case PrintTimeFormat.DateTimeString: +// return timeTemp.IntervalDateTimeString(Segments); +// default: +// return timeTemp.IntervalString(Segments); +// } +// } + +// public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) +// { +// switch (TimeFormat) { +// case PrintTimeFormat.TimeAsUT: +// return timeTemp.UTString(); +// case PrintTimeFormat.KSPString: +// return timeTemp.DateString(); +// case PrintTimeFormat.DateTimeString: +// return timeTemp.DateTimeString(); +// default: +// return timeTemp.DateTimeString(); +// } +// } + +// public enum PrintTimeFormat +// { +// TimeAsUT, +// KSPString, +// DateTimeString +// } +// #endregion +// } + +//} \ No newline at end of file From 3b2d29ddc06421896404bc20d2ab41379bc53e1f Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 24 Nov 2014 22:54:15 +1100 Subject: [PATCH 21/40] Time Library Done - Confirm File location and test in game --- .../FrameworkExt/KSPDateTime.cs | 242 ++++++- .../FrameworkExt/KSPTimeSpan.cs | 287 +++++++- TransferWindowPlanner/TWPWindow.cs | 10 +- TransferWindowPlanner/TimeObjects.cs | 662 +++++++++--------- 4 files changed, 799 insertions(+), 402 deletions(-) diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs index 6d833cd..336d5b8 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs @@ -7,12 +7,16 @@ namespace KSPPluginFramework { + /// Represents an instant in time, typically expressed as a date and time of day. public class KSPDateTime : IFormattable { + //Shortcut to the Calendar Type private CalendarTypeEnum CalType { get { return KSPDateStructure.CalendarType; } } //Descriptors of DateTime - uses UT as the Root value + + /// Gets the year component of the date represented by this instance. public int Year { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Year; @@ -20,6 +24,9 @@ public int Year { return KSPDateStructure.EpochYear + (Int32)UT / KSPDateStructure.SecondsPerYear; } } + + /// Gets the day of the year represented by this instance. + /// The day of the year, expressed as a value between 1 and . public int DayOfYear { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.DayOfYear; @@ -27,6 +34,9 @@ public int DayOfYear { return KSPDateStructure.EpochDayOfYear + (Int32)UT / KSPDateStructure.SecondsPerDay % KSPDateStructure.DaysPerYear; } } + + /// Gets the day of the month represented by this instance. + /// The day component, expressed as a value between 1 and the number of days in the month. public int Day { get @@ -37,6 +47,13 @@ public int Day return DayOfMonth; } } + + + /// Gets the month component of the date represented by this instance. + /// + /// The day component, expressed as a value between 1 and the months in the year. + /// If the Defined calendar has no months then this will be 0 + /// public int Month { get { @@ -80,15 +97,23 @@ private Int32 DayOfMonth { } + /// Gets the hour component of the date represented by this instance. + /// The hour component, expressed as a value between 1 and . public int Hour { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Hour; else return _TimeSpanFromEpoch.Hours; } } + /// Gets the minute Component of the date represented by this instance. + /// The minute component, expressed as a value between 1 and . public int Minute { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Minute; else return _TimeSpanFromEpoch.Minutes; } } + /// Gets the second component of the date represented by this instance. + /// The second component, expressed as a value between 1 and . public int Second { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Second; else return _TimeSpanFromEpoch.Seconds; } } + /// Gets the millisecond component of the date represented by this instance. + /// The hour component, expressed as a value between 1 and 999. public int Millisecond { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Millisecond; else return _TimeSpanFromEpoch.Milliseconds; } } - /// - /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 - /// - public Double UT { + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// The number of seconds of game UT since Epoch + public Double UT + { get { //if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) @@ -108,22 +133,46 @@ public Double UT { private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateStructure.EpochYear, 1, KSPDateStructure.EpochDayOfYear); } } #region Constructors - public KSPDateTime() - { - UT = 0; - } + //public KSPDateTime() + //{ + // UT = 0; + //} + + /// Initializes a new instance of the System.DateTime structure to the specified year and day. + /// The year + /// The day of the year public KSPDateTime(int year, int dayofyear) { UT = new KSPDateTime(year, dayofyear, 0, 0, 0).UT; } + + /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. + /// The year + /// The day of the year + /// The hour + /// The minute + /// The second public KSPDateTime(String year, String day, String hour, String minute, String second) { UT = new KSPDateTime(Convert.ToInt32(year), Convert.ToInt32(day), Convert.ToInt32(hour), Convert.ToInt32(minute), Convert.ToInt32(second), 0).UT; } + /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. + /// The year + /// The day of the year + /// The hour + /// The minute + /// The second public KSPDateTime(int year, int day, int hour, int minute, int second) { UT = new KSPDateTime(year, day, hour, minute, second, 0).UT; } + /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. + /// The year + /// The day of the year + /// The hour + /// The minute + /// The second + /// The milliseconds public KSPDateTime(int year, int day, int hour, int minute, int second, int millisecond) { //Test for entering values outside the norm - eg 25 hours, day 600 @@ -137,6 +186,8 @@ public KSPDateTime(int year, int day, int hour, int minute, int second, int mill ).UT; } + /// Initializes a new instance of the System.DateTime structure to a specified number to the specified number of seconds of Game UT. + /// a time period expressed in seconds public KSPDateTime(Double ut) { UT = ut; @@ -145,14 +196,26 @@ public KSPDateTime(Double ut) #region Calculated Properties + /// Gets the date component of this instance. + /// A new System.DateTime with the same date as this instance, and the time value set to 00:00:00. public KSPDateTime Date { get { return new KSPDateTime(Year, DayOfYear); } } + + + /// Gets the time of day for this instance. + /// A System.TimeSpan that represents the fraction of the day that has elapsed since midnight. public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateStructure.SecondsPerDay); } } - public static KSPDateTime Now { + /// Gets a System.DateTime object that is set to the current date and time of the game. + /// A System.DateTime whose value is the current game date and time. + public static KSPDateTime Now + { get { return new KSPDateTime(Planetarium.GetUniversalTime()); } } - public static KSPDateTime Today { + /// Gets the current date. + /// A System.DateTime set to today's date, with the time component set to 00:00:00. + public static KSPDateTime Today + { get { return new KSPDateTime(Planetarium.GetUniversalTime()).Date; } } #endregion @@ -177,12 +240,9 @@ private enum AMPMEnum { AM,PM,OddHoursPerDay } - public enum DateStringFormatsEnum{ - TimeAsUT, - DateFormat, - DateFormatWithSecs, - DateTimeFormat - } + /// Generates some standard Templated versions of output + /// Enum of some common formats + /// A string that represents the value of this instance. public String ToStringStandard(DateStringFormatsEnum DateFormat){ switch (DateFormat) { @@ -191,9 +251,9 @@ public String ToStringStandard(DateStringFormatsEnum DateFormat){ if (UT < 0) strReturn += "+ "; strReturn += String.Format("{0:N0}s", Math.Abs(UT)); return strReturn; - case DateStringFormatsEnum.DateFormat: + case DateStringFormatsEnum.KSPFormat: return ToString(); - case DateStringFormatsEnum.DateFormatWithSecs: + case DateStringFormatsEnum.KSPFormatWithSecs: return ToString("Year y, Da\\y d - H\\h, m\\m, s\\s"); case DateStringFormatsEnum.DateTimeFormat: return ToString("Year y,Da\\y d, HH:mm:ss"); @@ -201,28 +261,27 @@ public String ToStringStandard(DateStringFormatsEnum DateFormat){ return ToString(); } } - public String DateString() - { - return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); - } - - public String DateTimeString() - { - return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); - } + /// Returns the string representation of the value of this instance. + /// A string that represents the value of this instance. public override String ToString() { - if (KSPDateStructure.CalendarType ==CalendarTypeEnum.Earth) { + if (CalType ==CalendarTypeEnum.Earth) { return ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern); } else { return ToString("Year y, Da\\y d - H\\h, m\\m", null); } } + /// Returns the string representation of the value of this instance. + /// Format string using the usual characters to interpret custom datetime - as per standard DateTime custom formats + /// A string that represents the value of this instance. public String ToString(String format) { return ToString(format, null); } + /// Returns the string representation of the value of this instance. + /// Format string using the usual characters to interpret custom datetime - as per standard DateTime custom formats + /// A string that represents the value of this instance. public String ToString(String format, IFormatProvider provider) { //parse and replace the format stuff @@ -312,43 +371,79 @@ public String ToString(String format, IFormatProvider provider) #region Instance Methods #region Mathematic Methods + /// Returns a new KSPPluginFramework.KSPDateTime object whose value is the sum of the specified KSPPluginFramework.KSPTimeSpan object and this instance. + /// A KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the time interval represented by value. public KSPDateTime Add(KSPTimeSpan value) { return new KSPDateTime(UT + value.UT); } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified years to the value of this instance. + /// a number of whole or fractional years. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of years represented by value. public KSPDateTime AddYears(Int32 value) { - return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerYear); + if (CalType != CalendarTypeEnum.Earth) + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerYear); + else { + DateTime newDate = _EarthDateTime.AddYears(value); + return new KSPDateTime(newDate.Subtract(_EarthDateTimeEpoch).TotalSeconds); + } } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified days to the value of this instance. + /// a number of whole or fractional days. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of days represented by value. public KSPDateTime AddDays(Double value) { return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerDay); } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified hours to the value of this instance. + /// a number of whole or fractional hours. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of hours represented by value. public KSPDateTime AddHours(Double value) { return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerHour); } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified minutes to the value of this instance. + /// a number of whole or fractional minutes. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of minutes represented by value. public KSPDateTime AddMinutes(Double value) { return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerMinute); } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified seconds to the value of this instance. + /// a number of whole or fractional seconds. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value. public KSPDateTime AddSeconds(Double value) { return new KSPDateTime(UT + value); } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified milliseconds to the value of this instance. + /// a number of whole or fractional milliseconds. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by value. public KSPDateTime AddMilliSeconds(Double value) { return new KSPDateTime(UT + value / 1000); } + + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified seconds to the value of this instance. + /// a number of whole or fractional seconds. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value. public KSPDateTime AddUT(Double value) { return new KSPDateTime(UT + value); } + /// Subtracts the specified date and time from this instance. + /// An instance of System.DateTime. + /// A System.DateTime equal to the date and time represented by this instance minus the date and time represented by value. public KSPDateTime Subtract(KSPDateTime value) { return new KSPDateTime(UT - value.UT); } + /// Subtracts the specified duration from this instance. + /// An instance of System.TimeSpan. + /// A System.DateTime equal to the date and time represented by this instance minus the time interval represented by value. public KSPTimeSpan Subtract(KSPTimeSpan value) { return new KSPTimeSpan(UT - value.UT); @@ -358,22 +453,42 @@ public KSPTimeSpan Subtract(KSPTimeSpan value) #region Comparison Methods - public Int32 CompareTo(KSPDateTime value) { + /// Compares this instance to a specified KSPPluginFramework.KSPDateTime object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the KSPPluginFramework.KSPDateTime object. + /// A KSPPluginFramework.KSPDateTime object to compare to this instance. + /// A signed number indicating the relative values of this instance and value.Value Description A negative integer This instance is shorter than value. Zero This instance is equal to value. A positive integer This instance is longer than value. + public Int32 CompareTo(KSPDateTime value) + { return KSPDateTime.Compare(this, value); } - public Int32 CompareTo(System.Object value) { + /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. + /// An object to compare, or null. + /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. + public Int32 CompareTo(System.Object value) + { + if (value == null) return 1; return this.CompareTo((KSPDateTime)value); } - public Boolean Equals(KSPDateTime value) { + /// Returns a value indicating whether this instance is equal to a specified KSPPluginFramework.KSPDateTime object. + /// An KSPPluginFramework.KSPDateTime object to compare with this instance. + /// true if obj represents the same time interval as this instance; otherwise, false. + public Boolean Equals(KSPDateTime value) + { return KSPDateTime.Equals(this, value); } - public override bool Equals(System.Object obj) { - return this.Equals((KSPDateTime)obj); + /// Returns a value indicating whether this instance is equal to a specified object. + /// An object to compare with this instance + /// true if value is a KSPPluginFramework.KSPDateTime object that represents the same time interval as the current KSPPluginFramework.KSPDateTime structure; otherwise, false. + public override bool Equals(System.Object value) + { + return (value.GetType() == this.GetType()) && this.Equals((KSPDateTime)value); } #endregion - public override int GetHashCode() { + /// Returns a hash code for this instance. + /// A 32-bit signed integer hash code. + public override int GetHashCode() + { return UT.GetHashCode(); } @@ -381,6 +496,10 @@ public override int GetHashCode() { #region Static Methods + /// Compares two KSPPluginFramework.KSPDateTime values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// Value Condition -1 t1 is shorter than t20 t1 is equal to t21 t1 is longer than t2 public static Int32 Compare(KSPDateTime t1, KSPDateTime t2) { if (t1.UT < t2.UT) @@ -390,6 +509,10 @@ public static Int32 Compare(KSPDateTime t1, KSPDateTime t2) else return 0; } + /// Returns a value indicating whether two specified instances of KSPPluginFramework.KSPDateTime are equal. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the values of t1 and t2 are equal; otherwise, false. public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) { return t1.UT == t2.UT; @@ -400,22 +523,43 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) #region Operators + /// Subtracts a specified date and time from another specified date and time and returns a time interval. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime whose value is the result of the value of d1 minus the value of d2. public static KSPTimeSpan operator -(KSPDateTime d1, KSPDateTime d2) { return new KSPTimeSpan(d1.UT - d2.UT); } + /// Subtracts a specified duration from another specified date and time and returns a time interval. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPTimeSpan. + /// A DateTime whose value is the result of the value of d minus the value of t. public static KSPDateTime operator -(KSPDateTime d, KSPTimeSpan t) { return new KSPDateTime(d.UT - t.UT); } + /// Adds a specified duration from another specified date and time and returns a time interval. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPTimeSpan. + /// A DateTime whose value is the result of the value of d plus the value of t. public static KSPDateTime operator +(KSPDateTime d, KSPTimeSpan t) { return new KSPDateTime(d.UT + t.UT); } + + /// Indicates whether two KSPPluginFramework.KSPDateTime instances are not equal. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the values of d1 and d2 are not equal; otherwise, false. public static Boolean operator !=(KSPDateTime d1, KSPDateTime d2) { return !(d1 == d2); } + /// Indicates whether two KSPPluginFramework.KSPDateTime instances are equal. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the values of d1 and d2 are equal; otherwise, false. public static Boolean operator ==(KSPDateTime d1, KSPDateTime d2) { return d1.UT == d2.UT; @@ -423,18 +567,34 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is less than or equal to another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is less than or equal to the value of d2; otherwise, false. public static Boolean operator <=(KSPDateTime d1, KSPDateTime d2) { return d1.CompareTo(d2) <= 0; } + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is less than another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is less than the value of d2; otherwise, false. public static Boolean operator <(KSPDateTime d1, KSPDateTime d2) { return d1.CompareTo(d2) < 0; } + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is greater than or equal to another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is greater than or equal to the value of d2; otherwise, false. public static Boolean operator >=(KSPDateTime d1, KSPDateTime d2) { return d1.CompareTo(d2) >= 0; } + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is greater than another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is greater than the value of d2; otherwise, false. public static Boolean operator >(KSPDateTime d1, KSPDateTime d2) { return d1.CompareTo(d2) > 0; @@ -442,4 +602,16 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) #endregion } + + + /// + /// Enum of standardised outputs for DateTimes as strings + /// + public enum DateStringFormatsEnum + { + TimeAsUT, + KSPFormat, + KSPFormatWithSecs, + DateTimeFormat + } } diff --git a/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs index a6101ca..c7c022b 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs @@ -7,58 +7,127 @@ namespace KSPPluginFramework { + /// Represents a time interval. public class KSPTimeSpan : IFormattable { + //Shortcut to the Calendar Type + private CalendarTypeEnum CalType { get { return KSPDateStructure.CalendarType; } } + //Descriptors of Timespan - uses UT as the Root value + + + /// Gets the years component of the time interval represented by the current KSPPluginFramework.KSPTimeSpan structure + /// + /// Returns 0 if the == + /// otherwise + /// Returns the year component of this instance. The return value can be positive or negative. + /// + public Int32 Years + { + get + { + if (CalType != CalendarTypeEnum.Earth) + { + return (Int32)UT / KSPDateStructure.SecondsPerYear; + } + else + { + return 0; + } + } + } + + /// Gets the days component of the time interval represented by the current KSPPluginFramework.KSPTimeSpan structure. + /// + /// Returns Total Number of Days for the current component if the == + /// otherwise + /// The day component of the current KSPPluginFramework.KSPTimeSpan structure. The return value ranges between +/- + /// public Int32 Days { - get { return (Int32)UT / KSPDateStructure.SecondsPerDay; } + get { + if (CalType != CalendarTypeEnum.Earth) { + return (Int32)UT / KSPDateStructure.SecondsPerDay % KSPDateStructure.DaysPerYear; + } else { + return (Int32)UT / KSPDateStructure.SecondsPerDay; + } + } } + + /// Gets the hours component of the time interval represented by the current KSPPluginFramework.KSPTimeSpan structure. + /// The hour component of the current KSPPluginFramework.KSPTimeSpan structure. The return value ranges between +/- public int Hours { get { return (Int32)UT / KSPDateStructure.SecondsPerHour % KSPDateStructure.HoursPerDay; } } + + /// Gets the minutes component of the time interval represented by the current KSPPluginFramework.KSPTimeSpan structure. + /// + /// The minute component of the current KSPPluginFramework.KSPTimeSpan structure. The return value ranges between +/- + /// public int Minutes { get { return (Int32)UT / KSPDateStructure.SecondsPerMinute % KSPDateStructure.MinutesPerHour; } } + + /// Gets the seconds component of the time interval represented by the current KSPPluginFramework.KSPTimeSpan structure. + /// + /// The second component of the current KSPPluginFramework.KSPTimeSpan structure. The return value ranges between +/- + /// public int Seconds { get { return (Int32)UT % KSPDateStructure.SecondsPerMinute; } } + + /// Gets the milliseconds component of the time interval represented by the current KSPPluginFramework.KSPTimeSpan structure. + /// The millisecond component of the current KSPPluginFramework.KSPTimeSpan structure. The return value ranges from -999 through 999. public int Milliseconds { get { return (Int32)(Math.Round(UT - Math.Floor(UT), 3) * 1000); } } - //public Int32 Years { - // get { - // if (KSPDateStructure.CalendarType != CalendarTypeEnum.Earth) { - // return (Int32)UT / KSPDateStructure.SecondsPerYear; - // } else { - // } - // } - //} - /// - /// Replaces the normal "Ticks" function. This is Seconds of UT - /// + /// Replaces the normal "Ticks" function. This is Seconds of UT + /// The number of seconds of game UT in this instance public Double UT { get; set; } #region Constructors - public KSPTimeSpan() - { - UT = 0; - } + //public KSPTimeSpan() + //{ + // UT = 0; + //} + + /// Initializes a new KSPPluginFramework.KSPTimeSpan to a specified number of hours, minutes, and seconds. + /// Number of hours. + /// Number of minutes. + /// Number of seconds. public KSPTimeSpan(int hours, int minutes, int seconds) { UT = new KSPTimeSpan(0, hours, minutes, seconds, 0).UT; } + + /// Initializes a new KSPPluginFramework.KSPTimeSpan to a specified number of days, hours, minutes, and seconds. + /// Number of days. + /// Number of hours. + /// Number of minutes. + /// Number of seconds. public KSPTimeSpan(String days, String hours, String minutes, String seconds) { UT = new KSPTimeSpan(Convert.ToInt32(days), Convert.ToInt32(hours), Convert.ToInt32(minutes), Convert.ToInt32(seconds), 0).UT; } + /// Initializes a new KSPPluginFramework.KSPTimeSpan to a specified number of days, hours, minutes, and seconds. + /// Number of days. + /// Number of hours. + /// Number of minutes. + /// Number of seconds. public KSPTimeSpan(int days, int hours, int minutes, int seconds) { UT = new KSPTimeSpan(days, hours, minutes, seconds, 0).UT; } + + /// Initializes a new KSPPluginFramework.KSPTimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds. + /// Number of days. + /// Number of hours. + /// Number of minutes. + /// Number of seconds. + /// Number of milliseconds. public KSPTimeSpan(int days, int hours, int minutes, int seconds, int milliseconds) { UT = days * KSPDateStructure.SecondsPerDay + @@ -68,8 +137,9 @@ public KSPTimeSpan(int days, int hours, int minutes, int seconds, int millisecon (Double)milliseconds / 1000; } + /// Initialises a new KSPPluginFramework.KSPTimeSpan to the specified number of seconds of Game UT + /// a time period expressed in seconds public KSPTimeSpan(Double ut) - : this() { UT = ut; } @@ -77,40 +147,94 @@ public KSPTimeSpan(Double ut) #region Calculated Properties + /// Gets the value of the current KSPPluginFramework.KSPTimeSpan structure expressed in whole and fractional milliseconds. + /// The total number of milliseconds represented by this instance. public Double TotalMilliseconds { get { return UT * 1000; } } + /// Gets the value of the current KSPPluginFramework.KSPTimeSpan structure expressed in whole and fractional seconds. + /// The total number of seconds represented by this instance. public Double TotalSeconds { get { return UT; } } + /// Gets the value of the current KSPPluginFramework.KSPTimeSpan structure expressed in whole and fractional minutes. + /// The total number of minutes represented by this instance. public Double TotalMinutes { get { return UT / KSPDateStructure.SecondsPerMinute; } } + /// Gets the value of the current KSPPluginFramework.KSPTimeSpan structure expressed in whole and fractional hours. + /// The total number of hours represented by this instance. public Double TotalHours { get { return UT / KSPDateStructure.SecondsPerHour; } } + /// Gets the value of the current KSPPluginFramework.KSPTimeSpan structure expressed in whole and fractional days. + /// The total number of days represented by this instance. public Double TotalDays { get { return UT / KSPDateStructure.SecondsPerDay; } } #endregion #region String Formatter + /// Generates some standard Templated versions of output + /// Enum of some common formats + /// A string that represents the value of this instance. + public String ToStringStandard(TimeSpanStringFormatsEnum TimeSpanFormat) + { + switch (TimeSpanFormat) + { + case TimeSpanStringFormatsEnum.TimeAsUT: + String strReturn = ""; + if (UT < 0) strReturn += "+ "; + strReturn += String.Format("{0:N0}s", Math.Abs(UT)); + return strReturn; + case TimeSpanStringFormatsEnum.KSPFormat: + return ToString(5); + case TimeSpanStringFormatsEnum.DateTimeFormat: + String strFormat = ""; + if (Years > 0) strFormat += "y\\y"; + if (Days > 0) strFormat += (strFormat.EndsWith("y") ? ", ":"") + "d\\d"; + if (strFormat!="") strFormat += " "; + strFormat += "hh:mm:ss"; + + if (UT < 0) strFormat = "+ " + strFormat; + + return ToString(strFormat); + default: + return ToString(); + } + } + + /// Returns the string representation of the value of this instance. + /// A string that represents the value of this instance. public override String ToString() { - return ToString(3); + return ToString(1); } + /// Returns the string representation of the value of this instance. + /// How many parts of the timespane to return (of year, Day, hour, minute, second) + /// A string that represents the value of this instance. public String ToString(Int32 Precision) { Int32 Displayed = 0; String format = ""; - if(Precision > 3 || Days>0 ){ + if (UT < 0) format += "+"; + + + if (CalType != CalendarTypeEnum.Earth) { + if ((Years > 0 || Precision > 4) && Displayed < Precision) { + format = "y\\y,"; + Displayed++; + } + } + + if((Days>0 || Precision > 3) && Displayed0 && Displayed0 || Precision > 2) && Displayed0 && Displayed0 || Precision > 1) && Displayed0 && Displayed0 || Precision > 0) && DisplayedReturns the string representation of the value of this instance. + /// Format string using the usual characters to interpret custom datetime - as per standard Timespan custom formats + /// A string that represents the value of this instance. public String ToString(String format) { return ToString(format, null); } + /// Returns the string representation of the value of this instance. + /// Format string using the usual characters to interpret custom datetime - as per standard Timespan custom formats + /// A string that represents the value of this instance. public String ToString(String format, IFormatProvider provider) { //parse and replace the format stuff @@ -178,33 +308,56 @@ public String ToString(String format, IFormatProvider provider) #region Instance Methods #region Mathematic Methods + /// Returns a new KSPPluginFramework.KSPTimeSpan object whose value is the sum of the specified KSPPluginFramework.KSPTimeSpan object and this instance. + /// A KSPPluginFramework.KSPTimeSpan. + /// A new object that represents the value of this instance plus the value of the timespan supplied. public KSPTimeSpan Add(KSPTimeSpan value) { return new KSPTimeSpan(UT + value.UT); } + /// Returns a new KSPPluginFramework.KSPTimeSpan object whose value is the absolute value of the current KSPPluginFramework.KSPTimeSpan object. + /// A new object whose value is the absolute value of the current KSPPluginFramework.KSPTimeSpan object. public KSPTimeSpan Duration() { return new KSPTimeSpan(Math.Abs(UT)); } + /// Returns a new KSPPluginFramework.KSPTimeSpan object whose value is the negated value of this instance. + /// A new object with the same numeric value as this instance, but with the opposite sign. public KSPTimeSpan Negate() { return new KSPTimeSpan(UT*-1); } #endregion #region Comparison Methods + /// Compares this instance to a specified KSPPluginFramework.KSPTimeSpan object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the KSPPluginFramework.KSPTimeSpan object. + /// A KSPPluginFramework.KSPTimeSpan object to compare to this instance. + /// A signed number indicating the relative values of this instance and value.Value Description A negative integer This instance is shorter than value. Zero This instance is equal to value. A positive integer This instance is longer than value. public Int32 CompareTo(KSPTimeSpan value) { return KSPTimeSpan.Compare(this, value); } + /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. + /// An object to compare, or null. + /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. public Int32 CompareTo(System.Object value) { + if (value == null) return 1; + return this.CompareTo((KSPTimeSpan)value); } + /// Returns a value indicating whether this instance is equal to a specified KSPPluginFramework.KSPTimeSpan object. + /// An KSPPluginFramework.KSPTimeSpan object to compare with this instance. + /// true if obj represents the same time interval as this instance; otherwise, false. public Boolean Equals(KSPTimeSpan value) { return KSPTimeSpan.Equals(this, value); } - public override bool Equals(System.Object obj) { - return this.Equals((KSPTimeSpan)obj); + /// Returns a value indicating whether this instance is equal to a specified object. + /// An object to compare with this instance + /// true if value is a KSPPluginFramework.KSPTimeSpan object that represents the same time interval as the current KSPPluginFramework.KSPTimeSpan structure; otherwise, false. + public override bool Equals(System.Object value) { + return (value.GetType() == this.GetType()) && this.Equals((KSPTimeSpan)value); } #endregion + /// Returns a hash code for this instance. + /// A 32-bit signed integer hash code. public override int GetHashCode() { return UT.GetHashCode(); @@ -214,6 +367,10 @@ public override int GetHashCode() #region Static Methods + /// Compares two KSPPluginFramework.KSPTimeSpan values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. + /// A KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// Value Condition -1 t1 is shorter than t20 t1 is equal to t21 t1 is longer than t2 public static Int32 Compare(KSPTimeSpan t1, KSPTimeSpan t2) { if (t1.UT < t2.UT) @@ -223,52 +380,98 @@ public static Int32 Compare(KSPTimeSpan t1, KSPTimeSpan t2) else return 0; } + /// Returns a value indicating whether two specified instances of KSPPluginFramework.KSPTimeSpan are equal. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// true if the values of t1 and t2 are equal; otherwise, false. public static Boolean Equals(KSPTimeSpan t1, KSPTimeSpan t2) { return t1.UT == t2.UT; } + /// Returns a KSPPluginFramework.KSPTimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond. + /// A number of days, accurate to the nearest millisecond. + /// A KSPPluginFramework.KSPTimeSpan that represents value. public static KSPTimeSpan FromDays(Double value) { return new KSPTimeSpan(value * KSPDateStructure.SecondsPerDay); } - public static KSPTimeSpan FromHours(Double value) { + /// Returns a KSPPluginFramework.KSPTimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond. + /// A number of hours, accurate to the nearest millisecond. + /// A KSPPluginFramework.KSPTimeSpan that represents value. + public static KSPTimeSpan FromHours(Double value) + { return new KSPTimeSpan(value * KSPDateStructure.SecondsPerHour); } - public static KSPTimeSpan FromMinutes(Double value) { + /// Returns a KSPPluginFramework.KSPTimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond. + /// A number of minutes, accurate to the nearest millisecond. + /// A KSPPluginFramework.KSPTimeSpan that represents value. + public static KSPTimeSpan FromMinutes(Double value) + { return new KSPTimeSpan(value * KSPDateStructure.SecondsPerMinute); } - public static KSPTimeSpan FromSeconds(Double value) { + /// Returns a KSPPluginFramework.KSPTimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond. + /// A number of seconds, accurate to the nearest millisecond. + /// A KSPPluginFramework.KSPTimeSpan that represents value. + public static KSPTimeSpan FromSeconds(Double value) + { return new KSPTimeSpan(value); } - public static KSPTimeSpan FromMilliseconds(Double value) { + /// Returns a KSPPluginFramework.KSPTimeSpan that represents a specified number of milliseconds. + /// A number of milliseconds. + /// A KSPPluginFramework.KSPTimeSpan that represents value. + public static KSPTimeSpan FromMilliseconds(Double value) + { return new KSPTimeSpan(value / 1000); } #endregion + #region Operators + /// Subtracts a specified KSPPluginFramework.KSPTimeSpan from another specified KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// A TimeSpan whose value is the result of the value of t1 minus the value of t2. public static KSPTimeSpan operator -(KSPTimeSpan t1, KSPTimeSpan t2) { return new KSPTimeSpan(t1.UT - t2.UT); } + /// Returns a KSPPluginFramework.KSPTimeSpan whose value is the negated value of the specified instance. + /// A KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan with the same numeric value as this instance, but the opposite sign. public static KSPTimeSpan operator -(KSPTimeSpan t) { return new KSPTimeSpan(t.UT).Negate(); } + /// Adds two specified KSPPluginFramework.KSPTimeSpan instances. + /// A KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan whose value is the sum of the values of t1 and t2. public static KSPTimeSpan operator +(KSPTimeSpan t1, KSPTimeSpan t2) { return new KSPTimeSpan(t1.UT + t2.UT); } + /// Returns the specified instance of KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// Returns t. public static KSPTimeSpan operator +(KSPTimeSpan t) { return new KSPTimeSpan(t.UT); } + /// Indicates whether two KSPPluginFramework.KSPTimeSpan instances are not equal. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// true if the values of t1 and t2 are not equal; otherwise, false. public static Boolean operator !=(KSPTimeSpan t1, KSPTimeSpan t2) { return !(t1 == t2); } + /// Indicates whether two KSPPluginFramework.KSPTimeSpan instances are equal. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// true if the values of t1 and t2 are equal; otherwise, false. public static Boolean operator ==(KSPTimeSpan t1, KSPTimeSpan t2) { return t1.UT == t2.UT; @@ -276,26 +479,48 @@ public static KSPTimeSpan FromMilliseconds(Double value) { + /// Indicates whether a specified KSPPluginFramework.KSPTimeSpan is less than another specified KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// true if the value of t1 is less than the value of t2; otherwise, false. public static Boolean operator <=(KSPTimeSpan t1, KSPTimeSpan t2) { return t1.CompareTo(t2) <= 0; } + /// Indicates whether a specified KSPPluginFramework.KSPTimeSpan is less than or equal to another specified KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// true if the value of t1 is less than or equal to the value of t2; otherwise, false. public static Boolean operator <(KSPTimeSpan t1, KSPTimeSpan t2) { return t1.CompareTo(t2) < 0; } + /// Indicates whether a specified KSPPluginFramework.KSPTimeSpan is greater than or equal to another specified KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// true if the value of t1 is greater than or equal to the value of t2; otherwise, false. public static Boolean operator >=(KSPTimeSpan t1, KSPTimeSpan t2) { return t1.CompareTo(t2) >= 0; } + /// Indicates whether a specified KSPPluginFramework.KSPTimeSpan is greater than another specified KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPTimeSpan. + /// A TimeSpan. + /// true if the value of t1 is greater than the value of t2; otherwise, false. public static Boolean operator >(KSPTimeSpan t1, KSPTimeSpan t2) { return t1.CompareTo(t2) > 0; } #endregion + } - - - //To String Formats + /// + /// Enum of standardised outputs for Timespans as strings + /// + public enum TimeSpanStringFormatsEnum + { + TimeAsUT, + KSPFormat, + DateTimeFormat } } diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index be4507f..5c6f2ff 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -315,7 +315,7 @@ private void DrawTransferDetailsMinimal() GUILayout.Label(String.Format("{0} (@{1:0}km)", TransferSpecs.OriginName, TransferSpecs.InitialOrbitAltitude / 1000), Styles.styleTextYellow); GUILayout.Label(String.Format("{0} (@{1:0}km)", TransferSpecs.DestinationName, TransferSpecs.FinalOrbitAltitude / 1000), Styles.styleTextYellow); //GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); - GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); + GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); //GUILayout.Label(String.Format("{0:0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()), Styles.styleTextYellow); GUILayout.Label(new KSPTimeSpan(TransferSelected.DepartureTime).ToString(), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVEjection), Styles.styleTextYellow); @@ -351,7 +351,7 @@ private void DrawTransferDetails() GUILayout.BeginVertical(); GUILayout.BeginHorizontal(); //GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); - GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); + GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); GUIStyle styleCopyButton = new GUIStyle(SkinsLibrary.CurrentSkin.button); styleCopyButton.fixedHeight = 18; styleCopyButton.padding.top = styleCopyButton.padding.bottom = 0; @@ -412,7 +412,7 @@ private void DrawTransferDetails() GUILayout.EndVertical(); GUILayout.BeginVertical(); //GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime + TransferSelected.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); - GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime + TransferSelected.TravelTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); + GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime + TransferSelected.TravelTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0.00}°", TransferSelected.EjectionAngle * LambertSolver.Rad2Deg), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0.00}°", TransferSelected.EjectionInclination * LambertSolver.Rad2Deg), Styles.styleTextYellow); if (ShowEjectionDetails) { @@ -465,13 +465,13 @@ internal string GenerateTransferDetailsText() { String Message = String.Format("{0} (@{2:0}km) -> {1} (@{3:0}km)", TransferSpecs.OriginName, TransferSpecs.DestinationName, TransferSpecs.InitialOrbitAltitude / 1000, TransferSpecs.FinalOrbitAltitude / 1000); //Message = Message.AppendLine("Depart at: {0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)); - Message = Message.AppendLine("Depart at: {0}", new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat)); + Message = Message.AppendLine("Depart at: {0}", new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat)); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.DepartureTime); //Message = Message.AppendLine(" Travel: {0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()); Message = Message.AppendLine(" Travel: {0}", new KSPTimeSpan(TransferSelected.TravelTime).ToString()); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.TravelTime); //Message = Message.AppendLine("Arrive at: {0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime + TransferSelected.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)); - Message = Message.AppendLine("Arrive at: {0}", new KSPDateTime(TransferSelected.DepartureTime + TransferSelected.TravelTime).ToStringStandard(KSPDateTime.DateStringFormatsEnum.DateTimeFormat)); + Message = Message.AppendLine("Arrive at: {0}", new KSPDateTime(TransferSelected.DepartureTime + TransferSelected.TravelTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat)); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.DepartureTime + TransferSelected.TravelTime); Message = Message.AppendLine("Phase Angle: {0:0.00}°", TransferSelected.PhaseAngle * LambertSolver.Rad2Deg); Message = Message.AppendLine("Ejection Angle: {0:0.00}°", TransferSelected.EjectionAngle * LambertSolver.Rad2Deg); diff --git a/TransferWindowPlanner/TimeObjects.cs b/TransferWindowPlanner/TimeObjects.cs index b13cbf4..d62b5d6 100644 --- a/TransferWindowPlanner/TimeObjects.cs +++ b/TransferWindowPlanner/TimeObjects.cs @@ -1,331 +1,331 @@ -//using System; -//using System.Collections.Generic; -//using System.Linq; -//using System.Text; - -//using KSP; -//using UnityEngine; -//using KSPPluginFramework; - -//namespace TransferWindowPlanner -//{ -// -// A class to store the UT of events and get back useful data -// -// public class KSPTime -// { - -// //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math -// // to go to years and get back to full days isnt confusing - have to sort this out some day though. -// //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo -// const double HoursPerDayEarth = 24; -// const double HoursPerYearEarth = 365 * HoursPerDayEarth; - -// const double HoursPerDayKerbin = 6; -// const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; - -// #region "Constructors" -// public KSPTime() -// { } -// public KSPTime(double NewUT) -// { -// UT = NewUT; -// } -// public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) -// { -// UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); -// } -// #endregion - -// /// -// /// Build the UT from raw values -// /// -// /// -// /// -// /// -// /// -// /// -// public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) -// { -// UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); -// } - -// public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) -// { -// BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); -// } - -// #region "Properties" - -// //Stores the Universal Time in game seconds -// public double UT; - -// //readonly props that resolve from UT -// public long Second -// { -// get { return Convert.ToInt64(Math.Truncate(UT % 60)); } -// } -// public long Minute -// { -// get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } -// } - -// private double HourRaw { get { return UT / 60 / 60; } } - -// public long Hour -// { -// get -// { -// if (GameSettings.KERBIN_TIME) { -// return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); -// } -// else { -// return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); -// } -// } -// } - -// public long Day -// { -// get -// { -// if (GameSettings.KERBIN_TIME) { -// return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); -// } -// else { -// return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); -// } -// } -// } - -// public long Year -// { -// get -// { -// if (GameSettings.KERBIN_TIME) { -// return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); -// } -// else { -// return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); -// } -// } -// } -// //public long HourKerbin -// //{ -// // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } -// //} - -// //public long DayKerbin -// //{ -// // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } -// //} - -// //public long YearKerbin -// //{ -// // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } -// //} -// #endregion - -// #region "String Formatting" -// public String IntervalString() -// { -// return IntervalString(6); -// } -// public String IntervalString(int segments) -// { -// String strReturn = ""; - -// if (UT < 0) strReturn += "+ "; - -// int intUsed = 0; - -// if (intUsed < segments && Year != 0) { -// strReturn += String.Format("{0}y", Math.Abs(Year)); -// intUsed++; -// } - -// if (intUsed < segments && (Day != 0 || intUsed > 0)) { -// if (intUsed > 0) strReturn += ", "; -// strReturn += String.Format("{0}d", Math.Abs(Day)); -// intUsed++; -// } - -// if (intUsed < segments && (Hour != 0 || intUsed > 0)) { -// if (intUsed > 0) strReturn += ", "; -// strReturn += String.Format("{0}h", Math.Abs(Hour)); -// intUsed++; -// } -// if (intUsed < segments && (Minute != 0 || intUsed > 0)) { -// if (intUsed > 0) strReturn += ", "; -// strReturn += String.Format("{0}m", Math.Abs(Minute)); -// intUsed++; -// } -// if (intUsed < segments)// && (Second != 0 || intUsed > 0)) -// { -// if (intUsed > 0) strReturn += ", "; -// strReturn += String.Format("{0}s", Math.Abs(Second)); -// intUsed++; -// } - - -// return strReturn; -// } - -// public String IntervalDateTimeString() -// { -// return IntervalDateTimeString(6); -// } -// public String IntervalDateTimeString(int segments) -// { -// String strReturn = ""; - -// if (UT < 0) strReturn += "+ "; - -// int intUsed = 0; - -// if (intUsed < segments && Year != 0) { -// strReturn += String.Format("{0}y", Math.Abs(Year)); -// intUsed++; -// } - -// if (intUsed < segments && (Day != 0 || intUsed > 0)) { -// if (intUsed > 0) strReturn += ", "; -// strReturn += String.Format("{0}d", Math.Abs(Day)); -// intUsed++; -// } - -// if (intUsed < segments && (Hour != 0 || intUsed > 0)) { -// if (intUsed > 0) strReturn += ", "; -// strReturn += String.Format("{0:00}", Math.Abs(Hour)); -// intUsed++; -// } -// if (intUsed < segments && (Minute != 0 || intUsed > 0)) { -// if (intUsed > 0) strReturn += ":"; -// strReturn += String.Format("{0:00}", Math.Abs(Minute)); -// intUsed++; -// } -// if (intUsed < segments)// && (Second != 0 || intUsed > 0)) -// { -// if (intUsed > 0) strReturn += ":"; -// strReturn += String.Format("{0:00}", Math.Abs(Second)); -// intUsed++; -// } - - -// return strReturn; -// } - -// public String DateString() -// { -// return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); -// } - -// public String DateTimeString() -// { -// return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); -// } - -// public String IntervalStringLong() -// { -// String strReturn = ""; -// if (UT < 0) strReturn += "+ "; -// strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); -// return strReturn; -// } -// public String IntervalStringLongTrimYears() -// { -// String strReturn = IntervalStringLong(); -// return strReturn.Replace("0 Years, ", ""); -// } - -// public String UTString() -// { -// String strReturn = ""; -// if (UT < 0) strReturn += "+ "; -// strReturn += String.Format("{0:N0}s", Math.Abs(UT)); -// return strReturn; -// } -// #endregion - -// public override String ToString() -// { -// return IntervalStringLong(); -// } - -// #region Static Properties -// public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } -// public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } -// public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } -// public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } -// public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } -// #endregion - -// #region "Static Functions" -// //fudging for dates -// public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); - -// public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) -// { -// return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); -// } -// public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) -// { -// if (GameSettings.KERBIN_TIME) { -// return Seconds + -// Minutes * 60 + -// Hours * 60 * 60 + -// Days * HoursPerDayKerbin * 60 * 60 + -// Years * HoursPerYearKerbin * 60 * 60; -// } -// else { -// return Seconds + -// Minutes * 60 + -// Hours * 60 * 60 + -// Days * HoursPerDayEarth * 60 * 60 + -// Years * HoursPerYearEarth * 60 * 60; -// } -// } - -// public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) -// { -// return PrintInterval(timeTemp, 3, TimeFormat); -// } - -// public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) -// { -// switch (TimeFormat) { -// case PrintTimeFormat.TimeAsUT: -// return timeTemp.UTString(); -// case PrintTimeFormat.KSPString: -// return timeTemp.IntervalString(Segments); -// case PrintTimeFormat.DateTimeString: -// return timeTemp.IntervalDateTimeString(Segments); -// default: -// return timeTemp.IntervalString(Segments); -// } -// } - -// public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) -// { -// switch (TimeFormat) { -// case PrintTimeFormat.TimeAsUT: -// return timeTemp.UTString(); -// case PrintTimeFormat.KSPString: -// return timeTemp.DateString(); -// case PrintTimeFormat.DateTimeString: -// return timeTemp.DateTimeString(); -// default: -// return timeTemp.DateTimeString(); -// } -// } - -// public enum PrintTimeFormat -// { -// TimeAsUT, -// KSPString, -// DateTimeString -// } -// #endregion -// } - -//} \ No newline at end of file +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using KSP; +using UnityEngine; +using KSPPluginFramework; + +namespace TransferWindowPlanner +{ + // + //A class to store the UT of events and get back useful data + // + public class KSPTime + { + + //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math + // to go to years and get back to full days isnt confusing - have to sort this out some day though. + //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo + const double HoursPerDayEarth = 24; + const double HoursPerYearEarth = 365 * HoursPerDayEarth; + + const double HoursPerDayKerbin = 6; + const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; + + #region "Constructors" + public KSPTime() + { } + public KSPTime(double NewUT) + { + UT = NewUT; + } + public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) + { + UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); + } + #endregion + + /// + /// Build the UT from raw values + /// + /// + /// + /// + /// + /// + public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) + { + UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); + } + + public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) + { + BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); + } + + #region "Properties" + + //Stores the Universal Time in game seconds + public double UT; + + //readonly props that resolve from UT + public long Second + { + get { return Convert.ToInt64(Math.Truncate(UT % 60)); } + } + public long Minute + { + get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } + } + + private double HourRaw { get { return UT / 60 / 60; } } + + public long Hour + { + get + { + if (GameSettings.KERBIN_TIME) { + return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); + } + else { + return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); + } + } + } + + public long Day + { + get + { + if (GameSettings.KERBIN_TIME) { + return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); + } + else { + return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); + } + } + } + + public long Year + { + get + { + if (GameSettings.KERBIN_TIME) { + return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); + } + else { + return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); + } + } + } + //public long HourKerbin + //{ + // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } + //} + + //public long DayKerbin + //{ + // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } + //} + + //public long YearKerbin + //{ + // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } + //} + #endregion + + #region "String Formatting" + public String IntervalString() + { + return IntervalString(6); + } + public String IntervalString(int segments) + { + String strReturn = ""; + + if (UT < 0) strReturn += "+ "; + + int intUsed = 0; + + if (intUsed < segments && Year != 0) { + strReturn += String.Format("{0}y", Math.Abs(Year)); + intUsed++; + } + + if (intUsed < segments && (Day != 0 || intUsed > 0)) { + if (intUsed > 0) strReturn += ", "; + strReturn += String.Format("{0}d", Math.Abs(Day)); + intUsed++; + } + + if (intUsed < segments && (Hour != 0 || intUsed > 0)) { + if (intUsed > 0) strReturn += ", "; + strReturn += String.Format("{0}h", Math.Abs(Hour)); + intUsed++; + } + if (intUsed < segments && (Minute != 0 || intUsed > 0)) { + if (intUsed > 0) strReturn += ", "; + strReturn += String.Format("{0}m", Math.Abs(Minute)); + intUsed++; + } + if (intUsed < segments)// && (Second != 0 || intUsed > 0)) + { + if (intUsed > 0) strReturn += ", "; + strReturn += String.Format("{0}s", Math.Abs(Second)); + intUsed++; + } + + + return strReturn; + } + + public String IntervalDateTimeString() + { + return IntervalDateTimeString(6); + } + public String IntervalDateTimeString(int segments) + { + String strReturn = ""; + + if (UT < 0) strReturn += "+ "; + + int intUsed = 0; + + if (intUsed < segments && Year != 0) { + strReturn += String.Format("{0}y", Math.Abs(Year)); + intUsed++; + } + + if (intUsed < segments && (Day != 0 || intUsed > 0)) { + if (intUsed > 0) strReturn += ", "; + strReturn += String.Format("{0}d", Math.Abs(Day)); + intUsed++; + } + + if (intUsed < segments && (Hour != 0 || intUsed > 0)) { + if (intUsed > 0) strReturn += ", "; + strReturn += String.Format("{0:00}", Math.Abs(Hour)); + intUsed++; + } + if (intUsed < segments && (Minute != 0 || intUsed > 0)) { + if (intUsed > 0) strReturn += ":"; + strReturn += String.Format("{0:00}", Math.Abs(Minute)); + intUsed++; + } + if (intUsed < segments)// && (Second != 0 || intUsed > 0)) + { + if (intUsed > 0) strReturn += ":"; + strReturn += String.Format("{0:00}", Math.Abs(Second)); + intUsed++; + } + + + return strReturn; + } + + public String DateString() + { + return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); + } + + public String DateTimeString() + { + return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); + } + + public String IntervalStringLong() + { + String strReturn = ""; + if (UT < 0) strReturn += "+ "; + strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); + return strReturn; + } + public String IntervalStringLongTrimYears() + { + String strReturn = IntervalStringLong(); + return strReturn.Replace("0 Years, ", ""); + } + + public String UTString() + { + String strReturn = ""; + if (UT < 0) strReturn += "+ "; + strReturn += String.Format("{0:N0}s", Math.Abs(UT)); + return strReturn; + } + #endregion + + public override String ToString() + { + return IntervalStringLong(); + } + + #region Static Properties + public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } + public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } + public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } + public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } + public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } + #endregion + + #region "Static Functions" + //fudging for dates + public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); + + public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) + { + return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); + } + public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) + { + if (GameSettings.KERBIN_TIME) { + return Seconds + + Minutes * 60 + + Hours * 60 * 60 + + Days * HoursPerDayKerbin * 60 * 60 + + Years * HoursPerYearKerbin * 60 * 60; + } + else { + return Seconds + + Minutes * 60 + + Hours * 60 * 60 + + Days * HoursPerDayEarth * 60 * 60 + + Years * HoursPerYearEarth * 60 * 60; + } + } + + public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) + { + return PrintInterval(timeTemp, 3, TimeFormat); + } + + public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) + { + switch (TimeFormat) { + case PrintTimeFormat.TimeAsUT: + return timeTemp.UTString(); + case PrintTimeFormat.KSPString: + return timeTemp.IntervalString(Segments); + case PrintTimeFormat.DateTimeString: + return timeTemp.IntervalDateTimeString(Segments); + default: + return timeTemp.IntervalString(Segments); + } + } + + public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) + { + switch (TimeFormat) { + case PrintTimeFormat.TimeAsUT: + return timeTemp.UTString(); + case PrintTimeFormat.KSPString: + return timeTemp.DateString(); + case PrintTimeFormat.DateTimeString: + return timeTemp.DateTimeString(); + default: + return timeTemp.DateTimeString(); + } + } + + public enum PrintTimeFormat + { + TimeAsUT, + KSPString, + DateTimeString + } + #endregion + } + +} \ No newline at end of file From 4a2c9c2cc2cbad4b8473fae72d3440c9b901dc82 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 24 Nov 2014 22:56:48 +1100 Subject: [PATCH 22/40] tweaking --- .../FrameworkExt/KSPDateStructure.cs | 4 +- .../FrameworkExt/KSPDateTime.cs | 1214 ++++++++--------- 2 files changed, 608 insertions(+), 610 deletions(-) diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs b/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs index 5635e55..4f4c83a 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs @@ -24,9 +24,7 @@ public static class KSPDateStructure static public DateTime CustomEpochEarth { get; private set; } static public CalendarTypeEnum CalendarType {get; private set;} - - - + static public void SetKSPStockCalendar() { CalendarType = CalendarTypeEnum.KSPStock; diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs index 336d5b8..09b5c89 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs @@ -7,611 +7,611 @@ namespace KSPPluginFramework { - /// Represents an instant in time, typically expressed as a date and time of day. - public class KSPDateTime : IFormattable - { - //Shortcut to the Calendar Type - private CalendarTypeEnum CalType { get { return KSPDateStructure.CalendarType; } } - - - //Descriptors of DateTime - uses UT as the Root value - - /// Gets the year component of the date represented by this instance. - public int Year { - get { if (CalType == CalendarTypeEnum.Earth) - return _EarthDateTime.Year; - else - return KSPDateStructure.EpochYear + (Int32)UT / KSPDateStructure.SecondsPerYear; - } - } - - /// Gets the day of the year represented by this instance. - /// The day of the year, expressed as a value between 1 and . - public int DayOfYear { - get { if (CalType == CalendarTypeEnum.Earth) - return _EarthDateTime.DayOfYear; - else - return KSPDateStructure.EpochDayOfYear + (Int32)UT / KSPDateStructure.SecondsPerDay % KSPDateStructure.DaysPerYear; - } - } - - /// Gets the day of the month represented by this instance. - /// The day component, expressed as a value between 1 and the number of days in the month. - public int Day - { - get - { - if (CalType == CalendarTypeEnum.Earth) - return _EarthDateTime.Day; - else - return DayOfMonth; - } - } - - - /// Gets the month component of the date represented by this instance. - /// - /// The day component, expressed as a value between 1 and the months in the year. - /// If the Defined calendar has no months then this will be 0 - /// - public int Month - { - get { - if (CalType == CalendarTypeEnum.Earth) - return _EarthDateTime.Month; - else - { - if (KSPDateStructure.MonthCount < 1) - return 0; - else - return KSPDateStructure.Months.IndexOf(MonthObj)+1; - } - } - } - - private KSPMonth MonthObj { - get { - if (KSPDateStructure.MonthCount < 1) - return null; - Int32 monthMaxDay=0; - for (int i = 0; i < KSPDateStructure.MonthCount; i++){ - if (DayOfYear <= monthMaxDay + KSPDateStructure.Months[i].Days) - return KSPDateStructure.Months[i]; - } - return KSPDateStructure.Months.Last(); - } - } - private Int32 DayOfMonth { - get { - if (KSPDateStructure.MonthCount < 1) - return DayOfYear; - - Int32 monthMaxDay = 0; - for (int i = 0; i < KSPDateStructure.MonthCount; i++) - { - if (DayOfYear <= monthMaxDay + KSPDateStructure.Months[i].Days) - return DayOfYear - monthMaxDay; - } - return DayOfYear; - } - } - - - /// Gets the hour component of the date represented by this instance. - /// The hour component, expressed as a value between 1 and . - public int Hour { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Hour; else return _TimeSpanFromEpoch.Hours; } } - /// Gets the minute Component of the date represented by this instance. - /// The minute component, expressed as a value between 1 and . - public int Minute { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Minute; else return _TimeSpanFromEpoch.Minutes; } } - /// Gets the second component of the date represented by this instance. - /// The second component, expressed as a value between 1 and . - public int Second { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Second; else return _TimeSpanFromEpoch.Seconds; } } - /// Gets the millisecond component of the date represented by this instance. - /// The hour component, expressed as a value between 1 and 999. - public int Millisecond { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Millisecond; else return _TimeSpanFromEpoch.Milliseconds; } } - - /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 - /// The number of seconds of game UT since Epoch - public Double UT - { - get - { - //if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) - // return _EarthDateTime.Subtract(KSPDateStructure.CustomEpochEarth).TotalSeconds; - //else - return _TimeSpanFromEpoch.UT; - } - set { - _TimeSpanFromEpoch = new KSPTimeSpan(value); - //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) - // _EarthDateTime = KSPDateStructure.CustomEpochEarth.AddSeconds(value); - } - } - - private KSPTimeSpan _TimeSpanFromEpoch; - private DateTime _EarthDateTime { get { return KSPDateStructure.CustomEpochEarth.AddSeconds(UT); } } - private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateStructure.EpochYear, 1, KSPDateStructure.EpochDayOfYear); } } - - #region Constructors - //public KSPDateTime() - //{ - // UT = 0; - //} - - /// Initializes a new instance of the System.DateTime structure to the specified year and day. - /// The year - /// The day of the year - public KSPDateTime(int year, int dayofyear) - { - UT = new KSPDateTime(year, dayofyear, 0, 0, 0).UT; - } - - /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. - /// The year - /// The day of the year - /// The hour - /// The minute - /// The second - public KSPDateTime(String year, String day, String hour, String minute, String second) - { - UT = new KSPDateTime(Convert.ToInt32(year), Convert.ToInt32(day), Convert.ToInt32(hour), Convert.ToInt32(minute), Convert.ToInt32(second), 0).UT; - } - /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. - /// The year - /// The day of the year - /// The hour - /// The minute - /// The second - public KSPDateTime(int year, int day, int hour, int minute, int second) - { - UT = new KSPDateTime(year, day, hour, minute, second, 0).UT; - } - /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. - /// The year - /// The day of the year - /// The hour - /// The minute - /// The second - /// The milliseconds - public KSPDateTime(int year, int day, int hour, int minute, int second, int millisecond) - { - //Test for entering values outside the norm - eg 25 hours, day 600 - - UT = new KSPTimeSpan((year - KSPDateStructure.EpochYear) * KSPDateStructure.DaysPerYear + - (day - KSPDateStructure.EpochDayOfYear), - hour, - minute, - second, - millisecond - ).UT; - } - - /// Initializes a new instance of the System.DateTime structure to a specified number to the specified number of seconds of Game UT. - /// a time period expressed in seconds - public KSPDateTime(Double ut) - { - UT = ut; - } - #endregion - - - #region Calculated Properties - /// Gets the date component of this instance. - /// A new System.DateTime with the same date as this instance, and the time value set to 00:00:00. - public KSPDateTime Date { get { return new KSPDateTime(Year, DayOfYear); } } - - - /// Gets the time of day for this instance. - /// A System.TimeSpan that represents the fraction of the day that has elapsed since midnight. - public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateStructure.SecondsPerDay); } } - - - /// Gets a System.DateTime object that is set to the current date and time of the game. - /// A System.DateTime whose value is the current game date and time. - public static KSPDateTime Now - { - get { return new KSPDateTime(Planetarium.GetUniversalTime()); } - } - /// Gets the current date. - /// A System.DateTime set to today's date, with the time component set to 00:00:00. - public static KSPDateTime Today - { - get { return new KSPDateTime(Planetarium.GetUniversalTime()).Date; } - } - #endregion - - - #region String Formatter - - private AMPMEnum AMPM { - get { - if (KSPDateStructure.HoursPerDay % 2 == 0) - { - if (Hour < (KSPDateStructure.HoursPerDay / 2)) - return AMPMEnum.AM; - else - return AMPMEnum.PM; - } - else - return AMPMEnum.OddHoursPerDay; - } - } - private enum AMPMEnum { - AM,PM,OddHoursPerDay - } - - /// Generates some standard Templated versions of output - /// Enum of some common formats - /// A string that represents the value of this instance. - public String ToStringStandard(DateStringFormatsEnum DateFormat){ - switch (DateFormat) - { - case DateStringFormatsEnum.TimeAsUT: - String strReturn = ""; - if (UT < 0) strReturn += "+ "; - strReturn += String.Format("{0:N0}s", Math.Abs(UT)); - return strReturn; - case DateStringFormatsEnum.KSPFormat: - return ToString(); - case DateStringFormatsEnum.KSPFormatWithSecs: - return ToString("Year y, Da\\y d - H\\h, m\\m, s\\s"); - case DateStringFormatsEnum.DateTimeFormat: - return ToString("Year y,Da\\y d, HH:mm:ss"); - default: - return ToString(); - } - } - - /// Returns the string representation of the value of this instance. - /// A string that represents the value of this instance. - public override String ToString() - { - if (CalType ==CalendarTypeEnum.Earth) { - return ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern); - } else { - return ToString("Year y, Da\\y d - H\\h, m\\m", null); - } - } - /// Returns the string representation of the value of this instance. - /// Format string using the usual characters to interpret custom datetime - as per standard DateTime custom formats - /// A string that represents the value of this instance. - public String ToString(String format) - { - return ToString(format, null); - } - /// Returns the string representation of the value of this instance. - /// Format string using the usual characters to interpret custom datetime - as per standard DateTime custom formats - /// A string that represents the value of this instance. - public String ToString(String format, IFormatProvider provider) - { - //parse and replace the format stuff - MatchCollection matches = Regex.Matches(format, "([a-zA-z])\\1{0,}"); - for (int i = matches.Count-1; i >=0; i--) - { - Match m = matches[i]; - Int32 mIndex = m.Index,mLength = m.Length; - - if (mIndex>0 && format[m.Index - 1] == '\\') - { - if (m.Length == 1) - continue; - else { - mIndex++; - mLength--; - } - } - switch (m.Value[0]) - { - case 'y': - format = format.Substring(0, mIndex) + Year.ToString("D" + mLength) + format.Substring(mIndex + mLength); - break; - case 'M': - String input2 = Month.ToString("D" + mLength); - - //test for more than 2 M's - format = format.Substring(0, mIndex) + input2 + format.Substring(mIndex + mLength); - break; - case 'd': - format = format.Substring(0, mIndex) + Day.ToString("D" + mLength) + format.Substring(mIndex + mLength); - break; - case 'h': - //how to do this one AM/PM Hours - String HalfDayTime=""; - switch (AMPM) - { - case AMPMEnum.AM: - HalfDayTime = Hour.ToString("D" + mLength.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); - break; - case AMPMEnum.PM: - HalfDayTime = (Hour - (KSPDateStructure.HoursPerDay / 2)).ToString("D" + mLength.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); - break; - case AMPMEnum.OddHoursPerDay: - default: - HalfDayTime = Hour.ToString("D" + mLength.Clamp(1, KSPDateStructure.HoursPerDay.ToString().Length)); - break; - } - - format = format.Substring(0, mIndex) + HalfDayTime + format.Substring(mIndex + mLength); - break; - case 't': - if (AMPM != AMPMEnum.OddHoursPerDay) - format = format.Substring(0, mIndex) + AMPM.ToString().ToLower() + format.Substring(mIndex + mLength); - break; - case 'T': - if (AMPM != AMPMEnum.OddHoursPerDay) - format = format.Substring(0, mIndex) + AMPM.ToString().ToUpper() + format.Substring(mIndex + mLength); - break; - case 'H': - format = format.Substring(0, mIndex) + Hour.ToString("D" + mLength.Clamp(1,KSPDateStructure.HoursPerDay.ToString().Length)) + format.Substring(mIndex + mLength); - break; - case 'm': - format = format.Substring(0, mIndex) + Minute.ToString("D" + mLength.Clamp(1,KSPDateStructure.MinutesPerHour.ToString().Length)) + format.Substring(mIndex + mLength); - break; - case 's': - format = format.Substring(0, mIndex) + Second.ToString("D" + mLength.Clamp(1,KSPDateStructure.SecondsPerMinute.ToString().Length)) + format.Substring(mIndex + mLength); - break; - - default: - break; - } - } - - //Now strip out the \ , but not multiple \\ - format = Regex.Replace(format, "\\\\(?=[a-z])", ""); - - return format; - //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) - // return String.Format(format, _EarthDateTime); - //else - // return String.Format(format, this); //"TEST"; - } - - #endregion - - - #region Instance Methods - #region Mathematic Methods - /// Returns a new KSPPluginFramework.KSPDateTime object whose value is the sum of the specified KSPPluginFramework.KSPTimeSpan object and this instance. - /// A KSPPluginFramework.KSPTimeSpan. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the time interval represented by value. - public KSPDateTime Add(KSPTimeSpan value) - { - return new KSPDateTime(UT + value.UT); - } - /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified years to the value of this instance. - /// a number of whole or fractional years. Can be positive or negative. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of years represented by value. - public KSPDateTime AddYears(Int32 value) - { - if (CalType != CalendarTypeEnum.Earth) - return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerYear); - else { - DateTime newDate = _EarthDateTime.AddYears(value); - return new KSPDateTime(newDate.Subtract(_EarthDateTimeEpoch).TotalSeconds); - } - } - /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified days to the value of this instance. - /// a number of whole or fractional days. Can be positive or negative. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of days represented by value. - public KSPDateTime AddDays(Double value) - { - return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerDay); - } - /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified hours to the value of this instance. - /// a number of whole or fractional hours. Can be positive or negative. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of hours represented by value. - public KSPDateTime AddHours(Double value) - { - return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerHour); - } - /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified minutes to the value of this instance. - /// a number of whole or fractional minutes. Can be positive or negative. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of minutes represented by value. - public KSPDateTime AddMinutes(Double value) - { - return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerMinute); - } - /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified seconds to the value of this instance. - /// a number of whole or fractional seconds. Can be positive or negative. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value. - public KSPDateTime AddSeconds(Double value) - { - return new KSPDateTime(UT + value); - } - /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified milliseconds to the value of this instance. - /// a number of whole or fractional milliseconds. Can be positive or negative. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by value. - public KSPDateTime AddMilliSeconds(Double value) - { - return new KSPDateTime(UT + value / 1000); - } - - /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified seconds to the value of this instance. - /// a number of whole or fractional seconds. Can be positive or negative. - /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value. - public KSPDateTime AddUT(Double value) - { - return new KSPDateTime(UT + value); - } - - /// Subtracts the specified date and time from this instance. - /// An instance of System.DateTime. - /// A System.DateTime equal to the date and time represented by this instance minus the date and time represented by value. - public KSPDateTime Subtract(KSPDateTime value) - { - return new KSPDateTime(UT - value.UT); - } - /// Subtracts the specified duration from this instance. - /// An instance of System.TimeSpan. - /// A System.DateTime equal to the date and time represented by this instance minus the time interval represented by value. - public KSPTimeSpan Subtract(KSPTimeSpan value) - { - return new KSPTimeSpan(UT - value.UT); - } - - #endregion - - - #region Comparison Methods - /// Compares this instance to a specified KSPPluginFramework.KSPDateTime object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the KSPPluginFramework.KSPDateTime object. - /// A KSPPluginFramework.KSPDateTime object to compare to this instance. - /// A signed number indicating the relative values of this instance and value.Value Description A negative integer This instance is shorter than value. Zero This instance is equal to value. A positive integer This instance is longer than value. - public Int32 CompareTo(KSPDateTime value) - { - return KSPDateTime.Compare(this, value); - } - /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. - /// An object to compare, or null. - /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. - public Int32 CompareTo(System.Object value) - { - if (value == null) return 1; - return this.CompareTo((KSPDateTime)value); - } - /// Returns a value indicating whether this instance is equal to a specified KSPPluginFramework.KSPDateTime object. - /// An KSPPluginFramework.KSPDateTime object to compare with this instance. - /// true if obj represents the same time interval as this instance; otherwise, false. - public Boolean Equals(KSPDateTime value) - { - return KSPDateTime.Equals(this, value); - } - /// Returns a value indicating whether this instance is equal to a specified object. - /// An object to compare with this instance - /// true if value is a KSPPluginFramework.KSPDateTime object that represents the same time interval as the current KSPPluginFramework.KSPDateTime structure; otherwise, false. - public override bool Equals(System.Object value) - { - return (value.GetType() == this.GetType()) && this.Equals((KSPDateTime)value); - } - #endregion - - - /// Returns a hash code for this instance. - /// A 32-bit signed integer hash code. - public override int GetHashCode() - { - return UT.GetHashCode(); - } - - #endregion - - - #region Static Methods - /// Compares two KSPPluginFramework.KSPDateTime values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. - /// A KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPDateTime. - /// Value Condition -1 t1 is shorter than t20 t1 is equal to t21 t1 is longer than t2 - public static Int32 Compare(KSPDateTime t1, KSPDateTime t2) - { - if (t1.UT < t2.UT) - return -1; - else if (t1.UT > t2.UT) - return 1; - else - return 0; - } - /// Returns a value indicating whether two specified instances of KSPPluginFramework.KSPDateTime are equal. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime. - /// true if the values of t1 and t2 are equal; otherwise, false. - public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) - { - return t1.UT == t2.UT; - } - - - #endregion - - - #region Operators - /// Subtracts a specified date and time from another specified date and time and returns a time interval. - /// A KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime whose value is the result of the value of d1 minus the value of d2. - public static KSPTimeSpan operator -(KSPDateTime d1, KSPDateTime d2) - { - return new KSPTimeSpan(d1.UT - d2.UT); - } - /// Subtracts a specified duration from another specified date and time and returns a time interval. - /// A KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPTimeSpan. - /// A DateTime whose value is the result of the value of d minus the value of t. - public static KSPDateTime operator -(KSPDateTime d, KSPTimeSpan t) - { - return new KSPDateTime(d.UT - t.UT); - } - /// Adds a specified duration from another specified date and time and returns a time interval. - /// A KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPTimeSpan. - /// A DateTime whose value is the result of the value of d plus the value of t. - public static KSPDateTime operator +(KSPDateTime d, KSPTimeSpan t) - { - return new KSPDateTime(d.UT + t.UT); - } - - /// Indicates whether two KSPPluginFramework.KSPDateTime instances are not equal. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime. - /// true if the values of d1 and d2 are not equal; otherwise, false. - public static Boolean operator !=(KSPDateTime d1, KSPDateTime d2) - { - return !(d1 == d2); - } - /// Indicates whether two KSPPluginFramework.KSPDateTime instances are equal. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime. - /// true if the values of d1 and d2 are equal; otherwise, false. - public static Boolean operator ==(KSPDateTime d1, KSPDateTime d2) - { - return d1.UT == d2.UT; - } - - - - /// Indicates whether a specified KSPPluginFramework.KSPDateTime is less than or equal to another specified KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime. - /// true if the value of d1 is less than or equal to the value of d2; otherwise, false. - public static Boolean operator <=(KSPDateTime d1, KSPDateTime d2) - { - return d1.CompareTo(d2) <= 0; - } - /// Indicates whether a specified KSPPluginFramework.KSPDateTime is less than another specified KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime. - /// true if the value of d1 is less than the value of d2; otherwise, false. - public static Boolean operator <(KSPDateTime d1, KSPDateTime d2) - { - return d1.CompareTo(d2) < 0; - } - /// Indicates whether a specified KSPPluginFramework.KSPDateTime is greater than or equal to another specified KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime. - /// true if the value of d1 is greater than or equal to the value of d2; otherwise, false. - public static Boolean operator >=(KSPDateTime d1, KSPDateTime d2) - { - return d1.CompareTo(d2) >= 0; - } - /// Indicates whether a specified KSPPluginFramework.KSPDateTime is greater than another specified KSPPluginFramework.KSPDateTime. - /// A KSPPluginFramework.KSPDateTime. - /// A DateTime. - /// true if the value of d1 is greater than the value of d2; otherwise, false. - public static Boolean operator >(KSPDateTime d1, KSPDateTime d2) - { - return d1.CompareTo(d2) > 0; - } - #endregion - - } - - - /// - /// Enum of standardised outputs for DateTimes as strings - /// - public enum DateStringFormatsEnum - { - TimeAsUT, - KSPFormat, - KSPFormatWithSecs, - DateTimeFormat - } + /// Represents an instant in time, typically expressed as a date and time of day. + public class KSPDateTime : IFormattable + { + //Shortcut to the Calendar Type + private CalendarTypeEnum CalType { get { return KSPDateStructure.CalendarType; } } + + + //Descriptors of DateTime - uses UT as the Root value + + /// Gets the year component of the date represented by this instance. + public int Year { + get { if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Year; + else + return KSPDateStructure.EpochYear + (Int32)UT / KSPDateStructure.SecondsPerYear; + } + } + + /// Gets the day of the year represented by this instance. + /// The day of the year, expressed as a value between 1 and . + public int DayOfYear { + get { if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.DayOfYear; + else + return KSPDateStructure.EpochDayOfYear + (Int32)UT / KSPDateStructure.SecondsPerDay % KSPDateStructure.DaysPerYear; + } + } + + /// Gets the day of the month represented by this instance. + /// The day component, expressed as a value between 1 and the number of days in the month. + public int Day + { + get + { + if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Day; + else + return DayOfMonth; + } + } + + + /// Gets the month component of the date represented by this instance. + /// + /// The day component, expressed as a value between 1 and the months in the year. + /// If the Defined calendar has no months then this will be 0 + /// + public int Month + { + get { + if (CalType == CalendarTypeEnum.Earth) + return _EarthDateTime.Month; + else + { + if (KSPDateStructure.MonthCount < 1) + return 0; + else + return KSPDateStructure.Months.IndexOf(MonthObj)+1; + } + } + } + + private KSPMonth MonthObj { + get { + if (KSPDateStructure.MonthCount < 1) + return null; + Int32 monthMaxDay=0; + for (int i = 0; i < KSPDateStructure.MonthCount; i++){ + if (DayOfYear <= monthMaxDay + KSPDateStructure.Months[i].Days) + return KSPDateStructure.Months[i]; + } + return KSPDateStructure.Months.Last(); + } + } + private Int32 DayOfMonth { + get { + if (KSPDateStructure.MonthCount < 1) + return DayOfYear; + + Int32 monthMaxDay = 0; + for (int i = 0; i < KSPDateStructure.MonthCount; i++) + { + if (DayOfYear <= monthMaxDay + KSPDateStructure.Months[i].Days) + return DayOfYear - monthMaxDay; + } + return DayOfYear; + } + } + + + /// Gets the hour component of the date represented by this instance. + /// The hour component, expressed as a value between 1 and . + public int Hour { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Hour; else return _TimeSpanFromEpoch.Hours; } } + /// Gets the minute Component of the date represented by this instance. + /// The minute component, expressed as a value between 1 and . + public int Minute { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Minute; else return _TimeSpanFromEpoch.Minutes; } } + /// Gets the second component of the date represented by this instance. + /// The second component, expressed as a value between 1 and . + public int Second { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Second; else return _TimeSpanFromEpoch.Seconds; } } + /// Gets the millisecond component of the date represented by this instance. + /// The hour component, expressed as a value between 1 and 999. + public int Millisecond { get { if (CalType == CalendarTypeEnum.Earth) return _EarthDateTime.Millisecond; else return _TimeSpanFromEpoch.Milliseconds; } } + + /// Replaces the normal "Ticks" function. This is Seconds of UT since game time 0 + /// The number of seconds of game UT since Epoch + public Double UT + { + get + { + //if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) + // return _EarthDateTime.Subtract(KSPDateStructure.CustomEpochEarth).TotalSeconds; + //else + return _TimeSpanFromEpoch.UT; + } + set { + _TimeSpanFromEpoch = new KSPTimeSpan(value); + //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) + // _EarthDateTime = KSPDateStructure.CustomEpochEarth.AddSeconds(value); + } + } + + private KSPTimeSpan _TimeSpanFromEpoch; + private DateTime _EarthDateTime { get { return KSPDateStructure.CustomEpochEarth.AddSeconds(UT); } } + private DateTime _EarthDateTimeEpoch { get { return new DateTime(KSPDateStructure.EpochYear, 1, KSPDateStructure.EpochDayOfYear); } } + + #region Constructors + //public KSPDateTime() + //{ + // UT = 0; + //} + + /// Initializes a new instance of the System.DateTime structure to the specified year and day. + /// The year + /// The day of the year + public KSPDateTime(int year, int dayofyear) + { + UT = new KSPDateTime(year, dayofyear, 0, 0, 0).UT; + } + + /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. + /// The year + /// The day of the year + /// The hour + /// The minute + /// The second + public KSPDateTime(String year, String day, String hour, String minute, String second) + { + UT = new KSPDateTime(Convert.ToInt32(year), Convert.ToInt32(day), Convert.ToInt32(hour), Convert.ToInt32(minute), Convert.ToInt32(second), 0).UT; + } + /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. + /// The year + /// The day of the year + /// The hour + /// The minute + /// The second + public KSPDateTime(int year, int day, int hour, int minute, int second) + { + UT = new KSPDateTime(year, day, hour, minute, second, 0).UT; + } + /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. + /// The year + /// The day of the year + /// The hour + /// The minute + /// The second + /// The milliseconds + public KSPDateTime(int year, int day, int hour, int minute, int second, int millisecond) + { + //Test for entering values outside the norm - eg 25 hours, day 600 + + UT = new KSPTimeSpan((year - KSPDateStructure.EpochYear) * KSPDateStructure.DaysPerYear + + (day - KSPDateStructure.EpochDayOfYear), + hour, + minute, + second, + millisecond + ).UT; + } + + /// Initializes a new instance of the System.DateTime structure to a specified number to the specified number of seconds of Game UT. + /// a time period expressed in seconds + public KSPDateTime(Double ut) + { + UT = ut; + } + #endregion + + + #region Calculated Properties + /// Gets the date component of this instance. + /// A new System.DateTime with the same date as this instance, and the time value set to 00:00:00. + public KSPDateTime Date { get { return new KSPDateTime(Year, DayOfYear); } } + + + /// Gets the time of day for this instance. + /// A System.TimeSpan that represents the fraction of the day that has elapsed since midnight. + public KSPTimeSpan TimeOfDay { get { return new KSPTimeSpan(UT % KSPDateStructure.SecondsPerDay); } } + + + /// Gets a System.DateTime object that is set to the current date and time of the game. + /// A System.DateTime whose value is the current game date and time. + public static KSPDateTime Now + { + get { return new KSPDateTime(Planetarium.GetUniversalTime()); } + } + /// Gets the current date. + /// A System.DateTime set to today's date, with the time component set to 00:00:00. + public static KSPDateTime Today + { + get { return new KSPDateTime(Planetarium.GetUniversalTime()).Date; } + } + #endregion + + + #region String Formatter + + private AMPMEnum AMPM { + get { + if (KSPDateStructure.HoursPerDay % 2 == 0) + { + if (Hour < (KSPDateStructure.HoursPerDay / 2)) + return AMPMEnum.AM; + else + return AMPMEnum.PM; + } + else + return AMPMEnum.OddHoursPerDay; + } + } + private enum AMPMEnum { + AM,PM,OddHoursPerDay + } + + /// Generates some standard Templated versions of output + /// Enum of some common formats + /// A string that represents the value of this instance. + public String ToStringStandard(DateStringFormatsEnum DateFormat){ + switch (DateFormat) + { + case DateStringFormatsEnum.TimeAsUT: + String strReturn = ""; + if (UT < 0) strReturn += "+ "; + strReturn += String.Format("{0:N0}s", Math.Abs(UT)); + return strReturn; + case DateStringFormatsEnum.KSPFormat: + return ToString(); + case DateStringFormatsEnum.KSPFormatWithSecs: + return ToString("Year y, Da\\y d - H\\h, m\\m, s\\s"); + case DateStringFormatsEnum.DateTimeFormat: + return ToString("Year y,Da\\y d, HH:mm:ss"); + default: + return ToString(); + } + } + + /// Returns the string representation of the value of this instance. + /// A string that represents the value of this instance. + public override String ToString() + { + if (CalType ==CalendarTypeEnum.Earth) { + return ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern); + } else { + return ToString("Year y, Da\\y d - H\\h, m\\m", null); + } + } + /// Returns the string representation of the value of this instance. + /// Format string using the usual characters to interpret custom datetime - as per standard DateTime custom formats + /// A string that represents the value of this instance. + public String ToString(String format) + { + return ToString(format, null); + } + /// Returns the string representation of the value of this instance. + /// Format string using the usual characters to interpret custom datetime - as per standard DateTime custom formats + /// A string that represents the value of this instance. + public String ToString(String format, IFormatProvider provider) + { + //parse and replace the format stuff + MatchCollection matches = Regex.Matches(format, "([a-zA-z])\\1{0,}"); + for (int i = matches.Count-1; i >=0; i--) + { + Match m = matches[i]; + Int32 mIndex = m.Index,mLength = m.Length; + + if (mIndex>0 && format[m.Index - 1] == '\\') + { + if (m.Length == 1) + continue; + else { + mIndex++; + mLength--; + } + } + switch (m.Value[0]) + { + case 'y': + format = format.Substring(0, mIndex) + Year.ToString("D" + mLength) + format.Substring(mIndex + mLength); + break; + case 'M': + String input2 = Month.ToString("D" + mLength); + + //test for more than 2 M's + format = format.Substring(0, mIndex) + input2 + format.Substring(mIndex + mLength); + break; + case 'd': + format = format.Substring(0, mIndex) + Day.ToString("D" + mLength) + format.Substring(mIndex + mLength); + break; + case 'h': + //how to do this one AM/PM Hours + String HalfDayTime=""; + switch (AMPM) + { + case AMPMEnum.AM: + HalfDayTime = Hour.ToString("D" + mLength.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); + break; + case AMPMEnum.PM: + HalfDayTime = (Hour - (KSPDateStructure.HoursPerDay / 2)).ToString("D" + mLength.Clamp(1, (KSPDateStructure.HoursPerDay / 2).ToString().Length)); + break; + case AMPMEnum.OddHoursPerDay: + default: + HalfDayTime = Hour.ToString("D" + mLength.Clamp(1, KSPDateStructure.HoursPerDay.ToString().Length)); + break; + } + + format = format.Substring(0, mIndex) + HalfDayTime + format.Substring(mIndex + mLength); + break; + case 't': + if (AMPM != AMPMEnum.OddHoursPerDay) + format = format.Substring(0, mIndex) + AMPM.ToString().ToLower() + format.Substring(mIndex + mLength); + break; + case 'T': + if (AMPM != AMPMEnum.OddHoursPerDay) + format = format.Substring(0, mIndex) + AMPM.ToString().ToUpper() + format.Substring(mIndex + mLength); + break; + case 'H': + format = format.Substring(0, mIndex) + Hour.ToString("D" + mLength.Clamp(1,KSPDateStructure.HoursPerDay.ToString().Length)) + format.Substring(mIndex + mLength); + break; + case 'm': + format = format.Substring(0, mIndex) + Minute.ToString("D" + mLength.Clamp(1,KSPDateStructure.MinutesPerHour.ToString().Length)) + format.Substring(mIndex + mLength); + break; + case 's': + format = format.Substring(0, mIndex) + Second.ToString("D" + mLength.Clamp(1,KSPDateStructure.SecondsPerMinute.ToString().Length)) + format.Substring(mIndex + mLength); + break; + + default: + break; + } + } + + //Now strip out the \ , but not multiple \\ + format = Regex.Replace(format, "\\\\(?=[a-z])", ""); + + return format; + //if (KSPDateStructure.CalendarType == CalendarTypeEnum.Earth) + // return String.Format(format, _EarthDateTime); + //else + // return String.Format(format, this); //"TEST"; + } + + #endregion + + + #region Instance Methods + #region Mathematic Methods + /// Returns a new KSPPluginFramework.KSPDateTime object whose value is the sum of the specified KSPPluginFramework.KSPTimeSpan object and this instance. + /// A KSPPluginFramework.KSPTimeSpan. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the time interval represented by value. + public KSPDateTime Add(KSPTimeSpan value) + { + return new KSPDateTime(UT + value.UT); + } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified years to the value of this instance. + /// a number of whole or fractional years. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of years represented by value. + public KSPDateTime AddYears(Int32 value) + { + if (CalType != CalendarTypeEnum.Earth) + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerYear); + else { + DateTime newDate = _EarthDateTime.AddYears(value); + return new KSPDateTime(newDate.Subtract(_EarthDateTimeEpoch).TotalSeconds); + } + } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified days to the value of this instance. + /// a number of whole or fractional days. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of days represented by value. + public KSPDateTime AddDays(Double value) + { + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerDay); + } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified hours to the value of this instance. + /// a number of whole or fractional hours. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of hours represented by value. + public KSPDateTime AddHours(Double value) + { + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerHour); + } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified minutes to the value of this instance. + /// a number of whole or fractional minutes. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of minutes represented by value. + public KSPDateTime AddMinutes(Double value) + { + return new KSPDateTime(UT + value * KSPDateStructure.SecondsPerMinute); + } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified seconds to the value of this instance. + /// a number of whole or fractional seconds. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value. + public KSPDateTime AddSeconds(Double value) + { + return new KSPDateTime(UT + value); + } + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified milliseconds to the value of this instance. + /// a number of whole or fractional milliseconds. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of milliseconds represented by value. + public KSPDateTime AddMilliSeconds(Double value) + { + return new KSPDateTime(UT + value / 1000); + } + + /// Returns a new KSPPluginFramework.KSPDateTime that adds the specified seconds to the value of this instance. + /// a number of whole or fractional seconds. Can be positive or negative. + /// A KSPPluginFramework.KSPDateTime whose value is the sum of the date and time represented by this instance and the number of seconds represented by value. + public KSPDateTime AddUT(Double value) + { + return new KSPDateTime(UT + value); + } + + /// Subtracts the specified date and time from this instance. + /// An instance of System.DateTime. + /// A System.DateTime equal to the date and time represented by this instance minus the date and time represented by value. + public KSPDateTime Subtract(KSPDateTime value) + { + return new KSPDateTime(UT - value.UT); + } + /// Subtracts the specified duration from this instance. + /// An instance of System.TimeSpan. + /// A System.DateTime equal to the date and time represented by this instance minus the time interval represented by value. + public KSPTimeSpan Subtract(KSPTimeSpan value) + { + return new KSPTimeSpan(UT - value.UT); + } + + #endregion + + + #region Comparison Methods + /// Compares this instance to a specified KSPPluginFramework.KSPDateTime object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the KSPPluginFramework.KSPDateTime object. + /// A KSPPluginFramework.KSPDateTime object to compare to this instance. + /// A signed number indicating the relative values of this instance and value.Value Description A negative integer This instance is shorter than value. Zero This instance is equal to value. A positive integer This instance is longer than value. + public Int32 CompareTo(KSPDateTime value) + { + return KSPDateTime.Compare(this, value); + } + /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. + /// An object to compare, or null. + /// Value Condition -1 This instance is shorter than value. 0 This instance is equal to value. 1 This instance is longer than value.-or- value is null. + public Int32 CompareTo(System.Object value) + { + if (value == null) return 1; + return this.CompareTo((KSPDateTime)value); + } + /// Returns a value indicating whether this instance is equal to a specified KSPPluginFramework.KSPDateTime object. + /// An KSPPluginFramework.KSPDateTime object to compare with this instance. + /// true if obj represents the same time interval as this instance; otherwise, false. + public Boolean Equals(KSPDateTime value) + { + return KSPDateTime.Equals(this, value); + } + /// Returns a value indicating whether this instance is equal to a specified object. + /// An object to compare with this instance + /// true if value is a KSPPluginFramework.KSPDateTime object that represents the same time interval as the current KSPPluginFramework.KSPDateTime structure; otherwise, false. + public override bool Equals(System.Object value) + { + return (value.GetType() == this.GetType()) && this.Equals((KSPDateTime)value); + } + #endregion + + + /// Returns a hash code for this instance. + /// A 32-bit signed integer hash code. + public override int GetHashCode() + { + return UT.GetHashCode(); + } + + #endregion + + + #region Static Methods + /// Compares two KSPPluginFramework.KSPDateTime values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// Value Condition -1 t1 is shorter than t20 t1 is equal to t21 t1 is longer than t2 + public static Int32 Compare(KSPDateTime t1, KSPDateTime t2) + { + if (t1.UT < t2.UT) + return -1; + else if (t1.UT > t2.UT) + return 1; + else + return 0; + } + /// Returns a value indicating whether two specified instances of KSPPluginFramework.KSPDateTime are equal. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the values of t1 and t2 are equal; otherwise, false. + public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) + { + return t1.UT == t2.UT; + } + + + #endregion + + + #region Operators + /// Subtracts a specified date and time from another specified date and time and returns a time interval. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime whose value is the result of the value of d1 minus the value of d2. + public static KSPTimeSpan operator -(KSPDateTime d1, KSPDateTime d2) + { + return new KSPTimeSpan(d1.UT - d2.UT); + } + /// Subtracts a specified duration from another specified date and time and returns a time interval. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPTimeSpan. + /// A DateTime whose value is the result of the value of d minus the value of t. + public static KSPDateTime operator -(KSPDateTime d, KSPTimeSpan t) + { + return new KSPDateTime(d.UT - t.UT); + } + /// Adds a specified duration from another specified date and time and returns a time interval. + /// A KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPTimeSpan. + /// A DateTime whose value is the result of the value of d plus the value of t. + public static KSPDateTime operator +(KSPDateTime d, KSPTimeSpan t) + { + return new KSPDateTime(d.UT + t.UT); + } + + /// Indicates whether two KSPPluginFramework.KSPDateTime instances are not equal. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the values of d1 and d2 are not equal; otherwise, false. + public static Boolean operator !=(KSPDateTime d1, KSPDateTime d2) + { + return !(d1 == d2); + } + /// Indicates whether two KSPPluginFramework.KSPDateTime instances are equal. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the values of d1 and d2 are equal; otherwise, false. + public static Boolean operator ==(KSPDateTime d1, KSPDateTime d2) + { + return d1.UT == d2.UT; + } + + + + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is less than or equal to another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is less than or equal to the value of d2; otherwise, false. + public static Boolean operator <=(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) <= 0; + } + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is less than another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is less than the value of d2; otherwise, false. + public static Boolean operator <(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) < 0; + } + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is greater than or equal to another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is greater than or equal to the value of d2; otherwise, false. + public static Boolean operator >=(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) >= 0; + } + /// Indicates whether a specified KSPPluginFramework.KSPDateTime is greater than another specified KSPPluginFramework.KSPDateTime. + /// A KSPPluginFramework.KSPDateTime. + /// A DateTime. + /// true if the value of d1 is greater than the value of d2; otherwise, false. + public static Boolean operator >(KSPDateTime d1, KSPDateTime d2) + { + return d1.CompareTo(d2) > 0; + } + #endregion + + } + + + /// + /// Enum of standardised outputs for DateTimes as strings + /// + public enum DateStringFormatsEnum + { + TimeAsUT, + KSPFormat, + KSPFormatWithSecs, + DateTimeFormat + } } From 92776e7bbc59a23da7a4f9e474364412348bc9cc Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Tue, 25 Nov 2014 18:53:37 +1100 Subject: [PATCH 23/40] KSPDateTime Done --- .../FrameworkExt/KSPDateStructure.cs | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs b/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs index 4f4c83a..c91f002 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs @@ -1,30 +1,47 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; namespace KSPPluginFramework { + + /// + /// Static class to control the Calendar used by KSPDateTime and KSPTimeSpan + /// public static class KSPDateStructure { //Define the Epoch + /// What Day does UT 0 represent static public Int32 EpochDayOfYear { get; private set; } + /// What Year does UT 0 represent static public Int32 EpochYear { get; private set; } //Define the Calendar + /// How many seconds (game UT) make up a minute static public Int32 SecondsPerMinute { get; private set; } + /// How many minutes make up an hour static public Int32 MinutesPerHour { get; private set; } + /// How many hours make up a day static public Int32 HoursPerDay { get; private set; } + /// How many days make up a year static public Int32 DaysPerYear { get; private set; } + /// How many seconds (game UT) make up an hour static public Int32 SecondsPerHour { get { return SecondsPerMinute * MinutesPerHour; } } + /// How many seconds (game UT) make up a day static public Int32 SecondsPerDay { get { return SecondsPerHour * HoursPerDay; } } + /// How many seconds (game UT) make up a year - not relevant for Earth time static public Int32 SecondsPerYear { get { return SecondsPerDay * DaysPerYear; } } + /// What Earth date does UT 0 represent static public DateTime CustomEpochEarth { get; private set; } + /// What type of Calendar is being used - KSPStock, Earth, or custom static public CalendarTypeEnum CalendarType {get; private set;} + /// Sets the Date Structure to be stock KSP static public void SetKSPStockCalendar() { CalendarType = CalendarTypeEnum.KSPStock; @@ -38,10 +55,15 @@ static public void SetKSPStockCalendar() DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; } + /// Sets the Date Structure to be Earth based - Epoch of 1/1/1951 (RSS default) static public void SetEarthCalendar() { SetEarthCalendar(1951, 1, 1); } + /// Sets the Date Structure to be Earth based - With an epoch date supplied + /// year represented by UT0 + /// month represented by UT0 + /// day represented by UT0 static public void SetEarthCalendar(Int32 epochyear, Int32 epochmonth, Int32 epochday) { CalendarType = CalendarTypeEnum.Earth; @@ -58,12 +80,20 @@ static public void SetEarthCalendar(Int32 epochyear, Int32 epochmonth, Int32 epo } + /// Set Calendar type to be a custom type static public void SetCustomCalendar() { SetKSPStockCalendar(); CalendarType = CalendarTypeEnum.Custom; } + /// Set Calendar type be a custom type with the supplied values + /// Year represented by UT 0 + /// DayOfYear represented by UT 0 + /// How many days per year in this calendar + /// How many hours per day in this calendar + /// How many minutes per hour in this calendar + /// How many seconds per minute in this calendar static public void SetCustomCalendar(Int32 CustomEpochYear, Int32 CustomEpochDayOfYear, Int32 CustomDaysPerYear, Int32 CustomHoursPerDay, Int32 CustomMinutesPerHour, Int32 CustomSecondsPerMinute) { CalendarType = CalendarTypeEnum.Custom; @@ -77,7 +107,7 @@ static public void SetCustomCalendar(Int32 CustomEpochYear, Int32 CustomEpochDay } - + /// Default Constructor static KSPDateStructure() { SetKSPStockCalendar(); @@ -86,25 +116,39 @@ static KSPDateStructure() //LeapDays = new List(); } + /// List of KSPMonth objects representing the months in the year static public List Months { get; set; } + /// How many months have been defined static public Int32 MonthCount { get { return Months.Count; } } //static public List LeapDays { get; set; } //static public Int32 LeapDaysCount { get { return LeapDays.Count; } } } + /// + /// options for KSPDateStructure Calendar Type + /// public enum CalendarTypeEnum { - KSPStock, - Earth, - Custom + [Description("KSP Stock Calendar")] KSPStock, + [Description("Earth Calendar")] Earth, + [Description("Custom Calendar")] Custom } + /// + /// Definition of a calendar month + /// public class KSPMonth { public KSPMonth(String name, Int32 days) { Name = name; Days = days; } + /// + /// Name of the month + /// public String Name { get; set; } + /// + /// How many days in this month + /// public Int32 Days { get; set; } } From a247d0bf97c3607b33f6bdba59e18e10a5bc7165 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Tue, 25 Nov 2014 18:53:50 +1100 Subject: [PATCH 24/40] Custom in progress --- TransferWindowPlanner/Settings.cs | 3 +++ TransferWindowPlanner/TWPWindowSettings.cs | 28 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/TransferWindowPlanner/Settings.cs b/TransferWindowPlanner/Settings.cs index 2861d0a..9a4d94e 100644 --- a/TransferWindowPlanner/Settings.cs +++ b/TransferWindowPlanner/Settings.cs @@ -39,6 +39,8 @@ internal ButtonStyleEnum ButtonStyleToDisplay { } [Persistent] internal ButtonStyleEnum ButtonStyleChosen = ButtonStyleEnum.Launcher; + [Persistent] internal CalendarTypeEnum SelectedCalendar = CalendarTypeEnum.KSPStock; + internal enum ButtonStyleEnum { //[Description("Basic button")] Basic, @@ -54,6 +56,7 @@ internal enum DisplaySkin [Description("Unity/KSP Buttons")] UnityWKSPButtons } + [Persistent] internal TWP_KACWrapper.KACWrapper.KACAPI.AlarmActionEnum KACAlarmAction = TWP_KACWrapper.KACWrapper.KACAPI.AlarmActionEnum.KillWarp; [Persistent] internal Double KACMargin = 24; diff --git a/TransferWindowPlanner/TWPWindowSettings.cs b/TransferWindowPlanner/TWPWindowSettings.cs index 2dd9d56..db03a2c 100644 --- a/TransferWindowPlanner/TWPWindowSettings.cs +++ b/TransferWindowPlanner/TWPWindowSettings.cs @@ -21,6 +21,7 @@ class TWPWindowSettings:MonoBehaviourWindowPlus internal DropDownList ddlSettingsTab; private DropDownList ddlSettingsSkin; private DropDownList ddlSettingsButtonStyle; + private DropDownList ddlSettingsCalendar; internal Int32 WindowWidth = 320; internal Int32 WindowHeight = 200; @@ -29,6 +30,7 @@ internal enum SettingsTabs { [Description("General Properties")] General, [Description("Alarm Clock Integration")] AlarmIntegration, + [Description("Calendar Control")] Calendar, //[Description("Styling/Visuals")] Styling, [Description("About...")] About, } @@ -47,7 +49,9 @@ internal override void Awake() ddlSettingsSkin.OnSelectionChanged += ddlSettingsSkin_SelectionChanged; ddlSettingsButtonStyle = new DropDownList(EnumExtensions.ToEnumDescriptions(), (Int32)settings.ButtonStyleChosen, this); ddlSettingsButtonStyle.OnSelectionChanged += ddlSettingsButtonStyle_OnSelectionChanged; + ddlSettingsCalendar = new DropDownList(EnumExtensions.ToEnumDescriptions(), this); + ddlManager.AddDDL(ddlSettingsCalendar); ddlManager.AddDDL(ddlSettingsButtonStyle); ddlManager.AddDDL(ddlSettingsSkin); ddlManager.AddDDL(ddlSettingsTab); @@ -137,6 +141,10 @@ internal override void DrawWindow(int id) DrawWindow_Alarm(); WindowHeight = 206; break; + case SettingsTabs.Calendar: + DrawWindow_Calendar(); + WindowHeight = mbTWP.windowDebug.intTest1; + break; case SettingsTabs.About: DrawWindow_About(); WindowHeight = 285; @@ -259,7 +267,27 @@ private void DrawWindow_Alarm() } } + private void DrawWindow_Calendar() + { + //Update Check Area + GUILayout.Label("Selected Calendar", Styles.styleTextHeading); + + GUILayout.BeginHorizontal(Styles.styleSettingsArea); + + GUILayout.BeginVertical(GUILayout.Width(60)); + GUILayout.Space(2); //to even up the text + GUILayout.Label("Calendar:", Styles.styleTextHeading); + GUILayout.EndVertical(); + + GUILayout.BeginVertical(); + ddlSettingsCalendar.DrawButton(); + GUILayout.EndVertical(); + GUILayout.EndHorizontal(); + + //if RSS not installed and RSS chosen... + ///section for custom stuff + } private void DrawWindow_About() { //Update Check Area From 770a838e6a3af0189f9002e9327766451cf303a6 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Wed, 26 Nov 2014 22:27:02 +1100 Subject: [PATCH 25/40] Time Library is done! and implemented in TWP Need to tweak stuff to handle Earth times correctly --- .../FrameworkExt/KSPDateStructure.cs | 2 +- .../FrameworkExt/KSPDateTime.cs | 9 ++++++- .../FrameworkExt/KSPTimeSpan.cs | 9 +++++++ TransferWindowPlanner/TWPWindow.cs | 6 ++--- TransferWindowPlanner/TWPWindowDebug.cs | 26 ++++++++++--------- TransferWindowPlanner/TWPWindowSettings.cs | 21 ++++++++++++++- TransferWindowPlanner/TWPWindowWorkers.cs | 21 ++++++++------- 7 files changed, 66 insertions(+), 28 deletions(-) diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs b/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs index c91f002..9131e3e 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateStructure.cs @@ -52,7 +52,7 @@ static public void SetKSPStockCalendar() MinutesPerHour = 60; HoursPerDay = GameSettings.KERBIN_TIME ? 6 : 24; - DaysPerYear = GameSettings.KERBIN_TIME ? 425 : 365; + DaysPerYear = GameSettings.KERBIN_TIME ? 426 : 365; } /// Sets the Date Structure to be Earth based - Epoch of 1/1/1951 (RSS default) diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs index 09b5c89..57e56d4 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs @@ -145,6 +145,13 @@ public KSPDateTime(int year, int dayofyear) { UT = new KSPDateTime(year, dayofyear, 0, 0, 0).UT; } + /// Initializes a new instance of the System.DateTime structure to the specified year and day. + /// The year + /// The day of the year + public KSPDateTime(String year, String dayofyear) + { + UT = new KSPDateTime(year, dayofyear, "0", "0", "0").UT; + } /// Initializes a new instance of the System.DateTime structure to the specified year, day, hour, minute, and second. /// The year @@ -256,7 +263,7 @@ public String ToStringStandard(DateStringFormatsEnum DateFormat){ case DateStringFormatsEnum.KSPFormatWithSecs: return ToString("Year y, Da\\y d - H\\h, m\\m, s\\s"); case DateStringFormatsEnum.DateTimeFormat: - return ToString("Year y,Da\\y d, HH:mm:ss"); + return ToString("Year y, Da\\y d, HH:mm:ss"); default: return ToString(); } diff --git a/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs index c7c022b..03ed05a 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPTimeSpan.cs @@ -180,6 +180,10 @@ public String ToStringStandard(TimeSpanStringFormatsEnum TimeSpanFormat) return strReturn; case TimeSpanStringFormatsEnum.KSPFormat: return ToString(5); + case TimeSpanStringFormatsEnum.IntervalLong: + return ToString("y Year\\s, d Da\\y\\s, hh:mm:ss"); + case TimeSpanStringFormatsEnum.IntervalLongTrimYears: + return ToString("y Year\\s, d Da\\y\\s, hh:mm:ss").Replace("0 Years, ",""); case TimeSpanStringFormatsEnum.DateTimeFormat: String strFormat = ""; if (Years > 0) strFormat += "y\\y"; @@ -276,6 +280,9 @@ public String ToString(String format, IFormatProvider provider) } switch (m.Value[0]) { + case 'y': + format = format.Substring(0, mIndex) + Years.ToString("D" + mLength) + format.Substring(mIndex + mLength); + break; case 'd': format = format.Substring(0, mIndex) + Days.ToString("D" + mLength) + format.Substring(mIndex + mLength); break; @@ -521,6 +528,8 @@ public enum TimeSpanStringFormatsEnum { TimeAsUT, KSPFormat, + IntervalLong, + IntervalLongTrimYears, DateTimeFormat } } diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index 5c6f2ff..2aca4ec 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -317,7 +317,7 @@ private void DrawTransferDetailsMinimal() //GUILayout.Label(String.Format("{0:0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)), Styles.styleTextYellow); GUILayout.Label(new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat), Styles.styleTextYellow); //GUILayout.Label(String.Format("{0:0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()), Styles.styleTextYellow); - GUILayout.Label(new KSPTimeSpan(TransferSelected.DepartureTime).ToString(), Styles.styleTextYellow); + GUILayout.Label(new KSPTimeSpan(TransferSelected.TravelTime).ToStringStandard(TimeSpanStringFormatsEnum.IntervalLongTrimYears), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVEjection), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVInjection), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVTotal), Styles.styleTextYellow); @@ -431,7 +431,7 @@ private void DrawTransferDetails() GUILayout.Label("Insertion Δv:", Styles.styleTextDetailsLabel); GUILayout.EndVertical(); GUILayout.BeginVertical(); - GUILayout.Label(String.Format("{0:0}", new KSPTimeSpan(TransferSelected.TravelTime).ToString()), Styles.styleTextYellow); + GUILayout.Label(String.Format("{0:0}", new KSPTimeSpan(TransferSelected.TravelTime).ToStringStandard(TimeSpanStringFormatsEnum.IntervalLongTrimYears)), Styles.styleTextYellow); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVTotal), Styles.styleTextYellow); GUILayout.BeginHorizontal(); GUILayout.Label(String.Format("{0:0} m/s", TransferSelected.DVEjection), Styles.styleTextYellow); @@ -468,7 +468,7 @@ internal string GenerateTransferDetailsText() Message = Message.AppendLine("Depart at: {0}", new KSPDateTime(TransferSelected.DepartureTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat)); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.DepartureTime); //Message = Message.AppendLine(" Travel: {0}", new KSPTime(TransferSelected.TravelTime).IntervalStringLongTrimYears()); - Message = Message.AppendLine(" Travel: {0}", new KSPTimeSpan(TransferSelected.TravelTime).ToString()); + Message = Message.AppendLine(" Travel: {0}", new KSPTimeSpan(TransferSelected.TravelTime).ToStringStandard(TimeSpanStringFormatsEnum.IntervalLongTrimYears)); Message = Message.AppendLine(" UT: {0:0}", TransferSelected.TravelTime); //Message = Message.AppendLine("Arrive at: {0}", KSPTime.PrintDate(new KSPTime(TransferSelected.DepartureTime + TransferSelected.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)); Message = Message.AppendLine("Arrive at: {0}", new KSPDateTime(TransferSelected.DepartureTime + TransferSelected.TravelTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat)); diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index 3473ebc..f14b35e 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -72,7 +72,7 @@ class TWPWindowDebug : MonoBehaviourWindowPlus internal TransferWindowPlanner mbTWP; internal Settings settings; - public Int32 intTest1 = 0; + public Int32 intTest1 = 200; public Int32 intTest2 = 0; public Int32 intTest3 = 0; public Int32 intTest4 = 0; @@ -96,19 +96,21 @@ internal override void DrawWindow(int id) //if (GUILayout.Button("Unity")) SkinsLibrary.SetCurrent("Unity"); //if (GUILayout.Button("UnityWKSPButtons")) SkinsLibrary.SetCurrent("UnityWKSPButtons"); - if (GUILayout.Button("Make Date")) - { - LogFormatted("a"); - //KSPDateTimeStructure.CalendarType = CalendarTypeEnum.Earth; - KSPDateTime dt = new KSPDateTime(301.123); + DrawLabel("{0}", KSPDateStructure.CalendarType); - LogFormatted("1:{0}", dt.Minute); - LogFormatted("2:{0}", dt.UT); - LogFormatted("3:{0}", dt.Year); - //LogFormatted("4:{0}", KSPDateTimeStructure.CalendarType); - //LogFormatted("5:{0}", dt.Day); + //if (GUILayout.Button("Make Date")) + //{ + // LogFormatted("a"); + // //KSPDateTimeStructure.CalendarType = CalendarTypeEnum.Earth; + // KSPDateTime dt = new KSPDateTime(301.123); + + // LogFormatted("1:{0}", dt.Minute); + // LogFormatted("2:{0}", dt.UT); + // LogFormatted("3:{0}", dt.Year); + // //LogFormatted("4:{0}", KSPDateTimeStructure.CalendarType); + // //LogFormatted("5:{0}", dt.Day); - } + //} //if (GUILayout.Button("CreateAlarm")) //{ // String tmpID = KACWrapper.KAC.CreateAlarm(KACWrapper.KACAPI.AlarmTypeEnum.TransferModelled, diff --git a/TransferWindowPlanner/TWPWindowSettings.cs b/TransferWindowPlanner/TWPWindowSettings.cs index db03a2c..14c0084 100644 --- a/TransferWindowPlanner/TWPWindowSettings.cs +++ b/TransferWindowPlanner/TWPWindowSettings.cs @@ -50,7 +50,8 @@ internal override void Awake() ddlSettingsButtonStyle = new DropDownList(EnumExtensions.ToEnumDescriptions(), (Int32)settings.ButtonStyleChosen, this); ddlSettingsButtonStyle.OnSelectionChanged += ddlSettingsButtonStyle_OnSelectionChanged; ddlSettingsCalendar = new DropDownList(EnumExtensions.ToEnumDescriptions(), this); - + ddlSettingsCalendar.OnSelectionChanged += ddlSettingsCalendar_OnSelectionChanged; + ddlManager.AddDDL(ddlSettingsCalendar); ddlManager.AddDDL(ddlSettingsButtonStyle); ddlManager.AddDDL(ddlSettingsSkin); @@ -59,6 +60,7 @@ internal override void Awake() onWindowVisibleChanged += TWPWindowSettings_onWindowVisibleChanged; } + void TWPWindowSettings_onWindowVisibleChanged(MonoBehaviourWindow sender, bool NewVisibleState) { if (NewVisibleState) @@ -86,6 +88,23 @@ void ddlSettingsSkin_SelectionChanged(DropDownList sender, int OldIndex, int New settings.Save(); } + void ddlSettingsCalendar_OnSelectionChanged(DropDownList sender, int OldIndex, int NewIndex) + { + settings.SelectedCalendar = (CalendarTypeEnum)NewIndex; + switch (settings.SelectedCalendar) + { + case CalendarTypeEnum.KSPStock: KSPDateStructure.SetKSPStockCalendar(); break; + case CalendarTypeEnum.Earth: + KSPDateStructure.SetEarthCalendar(); + break; + case CalendarTypeEnum.Custom: + KSPDateStructure.SetCustomCalendar(); + break; + default: KSPDateStructure.SetKSPStockCalendar(); break; + } + } + + void ddlSettingsButtonStyle_OnSelectionChanged(MonoBehaviourWindowPlus.DropDownList sender, int OldIndex, int NewIndex) { settings.ButtonStyleChosen = (Settings.ButtonStyleEnum)NewIndex; diff --git a/TransferWindowPlanner/TWPWindowWorkers.cs b/TransferWindowPlanner/TWPWindowWorkers.cs index 4614071..570da98 100644 --- a/TransferWindowPlanner/TWPWindowWorkers.cs +++ b/TransferWindowPlanner/TWPWindowWorkers.cs @@ -73,18 +73,19 @@ private void SetWindowStrings() strDestination = cbDestination.bodyName; KSPDateTime kTime = new KSPDateTime(DepartureMin); - strDepartureMinYear = (kTime.Year + 1).ToString(); - strDepartureMinDay = (kTime.Day + 1).ToString(); + strDepartureMinYear = (kTime.Year ).ToString(); + strDepartureMinDay = (kTime.Day).ToString(); kTime.UT = DepartureMax; - strDepartureMaxYear = (kTime.Year + 1).ToString(); - strDepartureMaxDay = (kTime.Day + 1).ToString(); + strDepartureMaxYear = (kTime.Year).ToString(); + strDepartureMaxDay = (kTime.Day).ToString(); - kTime.UT = TravelMin; - strTravelMinDays = (kTime.Year * KSPDateStructure.DaysPerYear + kTime.Day).ToString(); + KSPTimeSpan kSpan = new KSPTimeSpan(0); + kSpan.UT = TravelMin; + strTravelMinDays = ((Int32)kSpan.TotalDays).ToString(); - kTime.UT = TravelMax; - strTravelMaxDays = (kTime.Year * KSPDateStructure.DaysPerYear + kTime.Day).ToString(); + kSpan.UT = TravelMax; + strTravelMaxDays = ((Int32)kSpan.TotalDays).ToString(); strArrivalAltitude = (InitialOrbitAltitude / 1000).ToString(); strDepartureAltitude = (FinalOrbitAltitude / 1000).ToString(); @@ -116,8 +117,8 @@ internal class TransferWorkerDetails private void SetWorkerVariables() { - DepartureMin = new KSPDateTime(strDepartureMinYear, strDepartureMinDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; - DepartureMax = new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; + DepartureMin = new KSPDateTime(strDepartureMinYear, strDepartureMinDay).UT; // new KSPDateTime(strDepartureMinYear, strDepartureMinDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; + DepartureMax = new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay).UT; // new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; DepartureRange = DepartureMax - DepartureMin; DepartureSelected = -1; TravelMin = new KSPTimeSpan(strTravelMinDays, "0", "0", "0").UT; From f6b3bf9c94a06f281a0051a061367030a7e6f66a Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 1 Dec 2014 06:35:51 +1100 Subject: [PATCH 26/40] WIP-Mannode stuff --- TransferWindowPlanner/TWPWindowDebug.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index f14b35e..37593a2 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -154,10 +154,17 @@ internal override void DrawWindow(int id) DrawLabel("Departure:{0:0}, Travel:{1:0}", mbTWP.windowMain.DepartureSelected/KSPTime.SecondsPerDay,mbTWP.windowMain.TravelSelected/KSPTime.SecondsPerDay); + if (mbTWP.windowMain.TransferSelected != null && FlightGlobals.ActiveVessel!=null) + { + Vector3d vecPlanetDirection = FlightGlobals.ActiveVessel.orbit.getOrbitalVelocityAtUT(mbTWP.windowMain.TransferSelected.DepartureTime); + Vector3d vecVesselPosition = FlightGlobals.ActiveVessel.orbit.getRelativePositionAtUT(mbTWP.windowMain.TransferSelected.DepartureTime); - DrawLabel("Ass:{0}", KACWrapper.AssemblyExists); - DrawLabel("Ins:{0}", KACWrapper.InstanceExists); - DrawLabel("API:{0}", KACWrapper.APIReady); + + + } + //DrawLabel("Ass:{0}", KACWrapper.AssemblyExists); + //DrawLabel("Ins:{0}", KACWrapper.InstanceExists); + //DrawLabel("API:{0}", KACWrapper.APIReady); //if (mbTWP.windowMain.TransferSelected != null) //{ From 1a6b5003889f026d829320eefd6e7f0d9d3cd4f1 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 1 Dec 2014 18:00:49 +1100 Subject: [PATCH 27/40] Added EjectionFinder --- .../FrameworkExt/Extensions.cs | 12 ++++ .../SharedStuff/Utilities.cs | 57 +++++++++++++++++++ TransferWindowPlanner/TWPWindowDebug.cs | 10 +++- TransferWindowPlanner/TWPWindowWorkers.cs | 2 - 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/TransferWindowPlanner/FrameworkExt/Extensions.cs b/TransferWindowPlanner/FrameworkExt/Extensions.cs index 7648279..1fa100c 100644 --- a/TransferWindowPlanner/FrameworkExt/Extensions.cs +++ b/TransferWindowPlanner/FrameworkExt/Extensions.cs @@ -54,5 +54,17 @@ public static T Clamp(this T val, T min, T max) where T : IComparable else if (val.CompareTo(max) > 0) return max; else return val; } + + public static Int32 NormalizeAngle360(this Int32 val) { + return (Int32)Convert.ToDouble(val).NormalizeAngle360(); + } + + public static Double NormalizeAngle360(this Double val) + { + val %= 360; + if (val < 0) + val += 360; + return val; + } } } diff --git a/TransferWindowPlanner/SharedStuff/Utilities.cs b/TransferWindowPlanner/SharedStuff/Utilities.cs index 864b05c..df8c192 100644 --- a/TransferWindowPlanner/SharedStuff/Utilities.cs +++ b/TransferWindowPlanner/SharedStuff/Utilities.cs @@ -5,6 +5,8 @@ using UnityEngine; +using KSPPluginFramework; + namespace TransferWindowPlanner { internal static class Utilities @@ -24,5 +26,60 @@ public static String AppendLine(this String s, String LineToAdd, params object[] s += String.Format(LineToAdd,args); return s; } + + + internal static double getEjectionAngleAtUT(this Orbit o, double UT) + { + //What planet is the orbit around + CelestialBody body = o.referenceBody; + + + //get the bodies prograde vector + Vector3d bodyPrograde = body.orbit.getOrbitalVelocityAtUT(UT); + //get the vessels position vector relative to the body + Vector3d vesselPosition = o.getRelativePositionAtUT(UT); + //now get the angle between em + double returnEjectAngle = ((Math.Atan2(bodyPrograde.y, bodyPrograde.x) - Math.Atan2(vesselPosition.y, vesselPosition.x)) * 180.0 / Math.PI); + + //clamp to 360 + + return returnEjectAngle.NormalizeAngle360(); + } + + internal static double timeOfEjectionAngle(Orbit orbitVessel, double timeStart, double angleEjection, double numDivisions) + { + //Min and max is back and forward half an orbit + double minTime = timeStart - orbitVessel.period / 2; + double maxTime = timeStart + orbitVessel.period; + + double returnTime = minTime; + + //iterate it 8 times + for (int iter = 0; iter < 8; iter++) { + double dt = (maxTime - minTime) / numDivisions; + for (int i = 0; i < numDivisions; i++) { + //for each division work out the max and min ejection angles + double t1 = minTime + i * dt; + double t2 = minTime + (i + 1) * dt; + + double ejectT1 = orbitVessel.getEjectionAngleAtUT(t1); + double ejectT2 = orbitVessel.getEjectionAngleAtUT(t2); + + //what to do if the ejection angle wraps the 0 degree line + dasdas + + if ( (ejectT1 < angleEjection && angleEjection <= ejectT2) || + (ejectT1 > angleEjection && angleEjection >= ejectT2)) { + + minTime = t1; + maxTime = t2; + break; + } + } + returnTime = minTime; + } + + return returnTime; + } } } diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index 37593a2..2532f76 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -78,6 +78,9 @@ class TWPWindowDebug : MonoBehaviourWindowPlus public Int32 intTest4 = 0; public Int32 intTest5 = 300; + + public Double dblEjectAt = 0; + internal override void DrawWindow(int id) { try @@ -156,9 +159,10 @@ internal override void DrawWindow(int id) if (mbTWP.windowMain.TransferSelected != null && FlightGlobals.ActiveVessel!=null) { - Vector3d vecPlanetDirection = FlightGlobals.ActiveVessel.orbit.getOrbitalVelocityAtUT(mbTWP.windowMain.TransferSelected.DepartureTime); - Vector3d vecVesselPosition = FlightGlobals.ActiveVessel.orbit.getRelativePositionAtUT(mbTWP.windowMain.TransferSelected.DepartureTime); - + if (GUILayout.Button("FindUT")) { + dblEjectAt = Utilities.timeOfEjectionAngle(FlightGlobals.ActiveVessel.orbit, mbTWP.windowMain.TransferSelected.DepartureTime, mbTWP.windowMain.TransferSelected.EjectionAngle, 20); + } + DrawLabel("{0}", dblEjectAt); } diff --git a/TransferWindowPlanner/TWPWindowWorkers.cs b/TransferWindowPlanner/TWPWindowWorkers.cs index 570da98..535bdf4 100644 --- a/TransferWindowPlanner/TWPWindowWorkers.cs +++ b/TransferWindowPlanner/TWPWindowWorkers.cs @@ -39,9 +39,7 @@ void SetupDestinationControls() ddlDestination.SelectedIndex = 0; ddlDestination.Items = lstDestinations.Select(x => x.Name).ToList(); - LogFormatted("B:Setting to y{0} d{1}", strDepartureMinYear, strDepartureMinDay); SetupTransferParams(); - LogFormatted("C:Setting to y{0} d{1}", strDepartureMinYear, strDepartureMinDay); } void SetupTransferParams() From 3290b499cc6ba70f9ce36ec0170ad55198b3ac01 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Tue, 2 Dec 2014 22:30:42 +1100 Subject: [PATCH 28/40] WIP-Find Closest Ejectino Angle IDEA --- TransferWindowPlanner/TWP.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TransferWindowPlanner/TWP.cs b/TransferWindowPlanner/TWP.cs index 2b5762b..8e0106d 100644 --- a/TransferWindowPlanner/TWP.cs +++ b/TransferWindowPlanner/TWP.cs @@ -319,7 +319,7 @@ private Boolean MouseOverWindow(Rect WindowRect, Boolean WindowVisible) #if DEBUG //This will kick us into the save called default and set the first vessel active - //[KSPAddon(KSPAddon.Startup.MainMenu, false)] + [KSPAddon(KSPAddon.Startup.MainMenu, false)] public class Debug_AutoLoadPersistentSaveOnStartup : MonoBehaviour { //use this variable for first run to avoid the issue with when this is true and multiple addons use it From 17f0981ce7da358350e55e77d46448532eb5db2e Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Wed, 3 Dec 2014 07:31:20 +1100 Subject: [PATCH 29/40] Sorted timeOfEjectionAngle --- .../SharedStuff/Utilities.cs | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/TransferWindowPlanner/SharedStuff/Utilities.cs b/TransferWindowPlanner/SharedStuff/Utilities.cs index df8c192..a3a98e2 100644 --- a/TransferWindowPlanner/SharedStuff/Utilities.cs +++ b/TransferWindowPlanner/SharedStuff/Utilities.cs @@ -27,7 +27,12 @@ public static String AppendLine(this String s, String LineToAdd, params object[] return s; } - + /// + /// Return the ejection angle of the vessel/body on this orbit for a given UT + /// + /// + /// Te UT at which to calculate the angle + /// internal static double getEjectionAngleAtUT(this Orbit o, double UT) { //What planet is the orbit around @@ -46,40 +51,45 @@ internal static double getEjectionAngleAtUT(this Orbit o, double UT) return returnEjectAngle.NormalizeAngle360(); } - internal static double timeOfEjectionAngle(Orbit orbitVessel, double timeStart, double angleEjection, double numDivisions) + /// + /// Find the point on the orbit that includes the initial time where the ejection angle is closest to the suplied one + /// + /// Orbit of the vessel/body + /// The UT you want to search around for the angle - will search 1/2 an orbit back and 1/2 forward + /// Higher thisnumber the more precise the answer - and the longer it will take + /// The output of the closest angle the method could find + /// The ejection angle we are looking for + /// + internal static double timeOfEjectionAngle(Orbit oObject, double timeInitial, double targetAngle, double numDivisions, out double closestAngle) { - //Min and max is back and forward half an orbit - double minTime = timeStart - orbitVessel.period / 2; - double maxTime = timeStart + orbitVessel.period; + double timeStart = timeInitial; + double periodtoscan = oObject.period; + + double closestAngleTime = timeStart; + double closestAngleValue = 361; + double minTime = timeStart; + double maxTime = timeStart + periodtoscan; - double returnTime = minTime; + //work out iterations for precision - we only really need to within a second - so how many iterations do we actually need + //Each iteration gets us 1/10th of the period to scan - //iterate it 8 times for (int iter = 0; iter < 8; iter++) { double dt = (maxTime - minTime) / numDivisions; for (int i = 0; i < numDivisions; i++) { - //for each division work out the max and min ejection angles - double t1 = minTime + i * dt; - double t2 = minTime + (i + 1) * dt; - - double ejectT1 = orbitVessel.getEjectionAngleAtUT(t1); - double ejectT2 = orbitVessel.getEjectionAngleAtUT(t2); - - //what to do if the ejection angle wraps the 0 degree line - dasdas - - if ( (ejectT1 < angleEjection && angleEjection <= ejectT2) || - (ejectT1 > angleEjection && angleEjection >= ejectT2)) { - - minTime = t1; - maxTime = t2; - break; + double t = minTime + i * dt; + double angle = oObject.getEjectionAngleAtUT(t); + if (Math.Abs(angle - targetAngle) < closestAngleValue) { + closestAngleValue = Math.Abs(angle - targetAngle); + closestAngleTime = t; } } - returnTime = minTime; + minTime = (closestAngleTime - dt).Clamp(timeStart, timeStart + periodtoscan); + maxTime = (closestAngleTime + dt).Clamp(timeStart, timeStart + periodtoscan); } - return returnTime; + closestAngle = closestAngleValue + targetAngle; + return closestAngleTime; } + } } From 4dcc62c970bd9bd8223e7f6b79fa2d1e3195f1b0 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sat, 6 Dec 2014 10:09:03 +1100 Subject: [PATCH 30/40] WIP-Orbit maths --- .../SharedStuff/LambertSolverHelpers.cs | 33 +++++++++++++++++ .../SharedStuff/Utilities.cs | 4 +-- TransferWindowPlanner/TWPWindow.cs | 2 +- TransferWindowPlanner/TWPWindowDebug.cs | 35 +++++++++++++++++-- TransferWindowPlanner/TWPWindowWorkers.cs | 4 +-- 5 files changed, 70 insertions(+), 8 deletions(-) diff --git a/TransferWindowPlanner/SharedStuff/LambertSolverHelpers.cs b/TransferWindowPlanner/SharedStuff/LambertSolverHelpers.cs index 64f4b33..c814029 100644 --- a/TransferWindowPlanner/SharedStuff/LambertSolverHelpers.cs +++ b/TransferWindowPlanner/SharedStuff/LambertSolverHelpers.cs @@ -6,6 +6,8 @@ using KSP; using UnityEngine; +using KSPPluginFramework; + namespace TransferWindowPlanner { public class TransferDetails @@ -205,5 +207,36 @@ private Double PhaseAngleCalc(Orbit o1, Orbit o2, Double UT) return LambertSolver.Deg2Rad * ((phaseAngle + 360) % 360); } + + + internal string TransferDetailsText + { + get + { + + String Message = ""; //String.Format("{0} (@{2:0}km) -> {1} (@{3:0}km)", this.OriginName, TransferSpecs.DestinationName, TransferSpecs.InitialOrbitAltitude / 1000, TransferSpecs.FinalOrbitAltitude / 1000); + //Message = Message.AppendLine("Depart at: {0}", KSPTime.PrintDate(new KSPTime(this.DepartureTime), KSPTime.PrintTimeFormat.DateTimeString)); + Message = Message.AppendLine("Depart at: {0}", new KSPDateTime(this.DepartureTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat)); + Message = Message.AppendLine(" UT: {0:0}", this.DepartureTime); + //Message = Message.AppendLine(" Travel: {0}", new KSPTime(this.TravelTime).IntervalStringLongTrimYears()); + Message = Message.AppendLine(" Travel: {0}", new KSPTimeSpan(this.TravelTime).ToStringStandard(TimeSpanStringFormatsEnum.IntervalLongTrimYears)); + Message = Message.AppendLine(" UT: {0:0}", this.TravelTime); + //Message = Message.AppendLine("Arrive at: {0}", KSPTime.PrintDate(new KSPTime(this.DepartureTime + this.TravelTime), KSPTime.PrintTimeFormat.DateTimeString)); + Message = Message.AppendLine("Arrive at: {0}", new KSPDateTime(this.DepartureTime + this.TravelTime).ToStringStandard(DateStringFormatsEnum.DateTimeFormat)); + Message = Message.AppendLine(" UT: {0:0}", this.DepartureTime + this.TravelTime); + Message = Message.AppendLine("Phase Angle: {0:0.00}°", this.PhaseAngle * LambertSolver.Rad2Deg); + Message = Message.AppendLine("Ejection Angle: {0:0.00}°", this.EjectionAngle * LambertSolver.Rad2Deg); + Message = Message.AppendLine("Ejection Inc.: {0:0.00}°", this.EjectionInclination * LambertSolver.Rad2Deg); + Message = Message.AppendLine("Ejection Δv: {0:0} m/s", this.DVEjection); + Message = Message.AppendLine("Prograde Δv: {0:0.0} m/s", this.EjectionDVPrograde); + Message = Message.AppendLine("Normal Δv: {0:0.0} m/s", this.EjectionDVNormal); + Message = Message.AppendLine("Heading: {0:0.00}°", this.EjectionHeading * LambertSolver.Rad2Deg); + Message = Message.AppendLine("Insertion Inc.: {0:0.00}°", this.InsertionInclination * LambertSolver.Rad2Deg); + Message = Message.AppendLine("Insertion Δv: {0:0} m/s", this.DVInjection); + Message = Message.AppendLine("Total Δv: {0:0} m/s", this.DVTotal); + return Message; + } + } + } } diff --git a/TransferWindowPlanner/SharedStuff/Utilities.cs b/TransferWindowPlanner/SharedStuff/Utilities.cs index a3a98e2..08de674 100644 --- a/TransferWindowPlanner/SharedStuff/Utilities.cs +++ b/TransferWindowPlanner/SharedStuff/Utilities.cs @@ -62,11 +62,11 @@ internal static double getEjectionAngleAtUT(this Orbit o, double UT) /// internal static double timeOfEjectionAngle(Orbit oObject, double timeInitial, double targetAngle, double numDivisions, out double closestAngle) { - double timeStart = timeInitial; + double timeStart = timeInitial - oObject.period/2; double periodtoscan = oObject.period; double closestAngleTime = timeStart; - double closestAngleValue = 361; + double closestAngleValue = Double.MaxValue; double minTime = timeStart; double maxTime = timeStart + periodtoscan; diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index 2aca4ec..f9a44aa 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -18,7 +18,7 @@ namespace TransferWindowPlanner DragEnabled=true, TooltipsEnabled=true, WindowMoveEventsEnabled=true)] - internal partial class TWPWindow:MonoBehaviourWindowPlus + public partial class TWPWindow:MonoBehaviourWindowPlus { internal TransferWindowPlanner mbTWP; internal Settings settings; diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index 2532f76..0ec0390 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -79,7 +79,8 @@ class TWPWindowDebug : MonoBehaviourWindowPlus public Int32 intTest5 = 300; - public Double dblEjectAt = 0; + public Double dblEjectAt = 0, dblOutAngle=0; + TransferDetails transTemp; internal override void DrawWindow(int id) { @@ -160,12 +161,40 @@ internal override void DrawWindow(int id) if (mbTWP.windowMain.TransferSelected != null && FlightGlobals.ActiveVessel!=null) { if (GUILayout.Button("FindUT")) { - dblEjectAt = Utilities.timeOfEjectionAngle(FlightGlobals.ActiveVessel.orbit, mbTWP.windowMain.TransferSelected.DepartureTime, mbTWP.windowMain.TransferSelected.EjectionAngle, 20); + dblEjectAt = Utilities.timeOfEjectionAngle(FlightGlobals.ActiveVessel.orbit, mbTWP.windowMain.TransferSelected.DepartureTime, mbTWP.windowMain.TransferSelected.EjectionAngle * LambertSolver.Rad2Deg, 20, out dblOutAngle); + intTest5 = (Int32)dblEjectAt; } - DrawLabel("{0}", dblEjectAt); + DrawLabel("UT: {0:0}", dblEjectAt); + DrawLabel("Angle: {0:0.000}", dblOutAngle); + DrawLabel("UTSelect: {0:0}", mbTWP.windowMain.TransferSelected.DepartureTime); + DrawLabel("OrbitPeriod: {0:0}", FlightGlobals.ActiveVessel.orbit.period); + DrawLabel("Scan: {0:0}->{1:0}", mbTWP.windowMain.TransferSelected.DepartureTime - FlightGlobals.ActiveVessel.orbit.period / 2, mbTWP.windowMain.TransferSelected.DepartureTime + FlightGlobals.ActiveVessel.orbit.period / 2); + + + if (GUILayout.Button("NewTransfer")) + { + transTemp = new TransferDetails(); + LambertSolver.TransferDeltaV(mbTWP.windowMain.TransferSelected.Origin, mbTWP.windowMain.TransferSelected.Destination, intTest5, mbTWP.windowMain.TransferSelected.TravelTime, FlightGlobals.ActiveVessel.orbit.getRelativePositionAtUT(intTest5).magnitude - FlightGlobals.ActiveVessel.orbit.referenceBody.Radius, mbTWP.windowMain.TransferSpecs.FinalOrbitAltitude, out transTemp); + transTemp.CalcEjectionValues(); + } + if (transTemp != null) + { + GUILayout.Label(transTemp.TransferDetailsText); + if (GUILayout.Button("Newnode")) + { + FlightGlobals.ActiveVessel.patchedConicSolver.maneuverNodes.Clear(); + ManeuverNode mNode = FlightGlobals.ActiveVessel.patchedConicSolver.AddManeuverNode(transTemp.DepartureTime); + mNode.DeltaV = new Vector3d(0, transTemp.EjectionDVNormal, transTemp.EjectionDVPrograde); + FlightGlobals.ActiveVessel.patchedConicSolver.UpdateFlightPlan(); + + } + + } } + + //DrawLabel("Ass:{0}", KACWrapper.AssemblyExists); //DrawLabel("Ins:{0}", KACWrapper.InstanceExists); //DrawLabel("API:{0}", KACWrapper.APIReady); diff --git a/TransferWindowPlanner/TWPWindowWorkers.cs b/TransferWindowPlanner/TWPWindowWorkers.cs index 535bdf4..8ed78f5 100644 --- a/TransferWindowPlanner/TWPWindowWorkers.cs +++ b/TransferWindowPlanner/TWPWindowWorkers.cs @@ -11,7 +11,7 @@ namespace TransferWindowPlanner { - internal partial class TWPWindow + public partial class TWPWindow { List lstPlanets = new List(); List lstDestinations = new List(); @@ -96,7 +96,7 @@ private void SetWindowStrings() internal BackgroundWorker bw; - internal class TransferWorkerDetails + public class TransferWorkerDetails { public Double DepartureMin { get; set; } public Double DepartureMax { get; set; } From ab38199933e4cf6b103e917c4e372e6934bbe4d1 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 8 Dec 2014 00:00:48 +1100 Subject: [PATCH 31/40] Still working on the lambert stuff --- TransferWindowPlanner/SharedStuff/LambertSolver.cs | 5 +++++ TransferWindowPlanner/TWPWindowDebug.cs | 3 +++ 2 files changed, 8 insertions(+) diff --git a/TransferWindowPlanner/SharedStuff/LambertSolver.cs b/TransferWindowPlanner/SharedStuff/LambertSolver.cs index 367e8b0..4c129ca 100644 --- a/TransferWindowPlanner/SharedStuff/LambertSolver.cs +++ b/TransferWindowPlanner/SharedStuff/LambertSolver.cs @@ -62,6 +62,9 @@ public static double TransferDeltaV(CelestialBody origin, CelestialBody destinat return TransferDeltaV(origin, destination, ut, dt, initialOrbitAltitude, finalOrbitAltitude, out tmp); } + + public static Double v1out, vedvout; + /// /// Find the delta-v required for a ballistic transfer from to , /// departing at UT, with a time of flight of seconds, starting from a circular @@ -101,6 +104,8 @@ public static double TransferDeltaV(CelestialBody origin, CelestialBody destinat double v0 = Math.Sqrt(origin.gravParameter / r0); // Initial circular orbit velocity double v1 = Math.Sqrt(ejectionDeltaV * ejectionDeltaV + 2 * v0 * v0 - 2 * mu / rsoi); // Velocity at periapsis + v1out = v1; vedvout = ejectionDeltaV; + vesselOriginOrbitalSpeed = v0; //Store this for later double e = r0 * v1 * v1 / mu - 1; // Ejection orbit eccentricity diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index 0ec0390..c748e3c 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -178,6 +178,9 @@ internal override void DrawWindow(int id) LambertSolver.TransferDeltaV(mbTWP.windowMain.TransferSelected.Origin, mbTWP.windowMain.TransferSelected.Destination, intTest5, mbTWP.windowMain.TransferSelected.TravelTime, FlightGlobals.ActiveVessel.orbit.getRelativePositionAtUT(intTest5).magnitude - FlightGlobals.ActiveVessel.orbit.referenceBody.Radius, mbTWP.windowMain.TransferSpecs.FinalOrbitAltitude, out transTemp); transTemp.CalcEjectionValues(); } + + DrawLabel("v1:{0:0.000} edv:{1:0.000}", LambertSolver.v1out, LambertSolver.vedvout); + if (transTemp != null) { GUILayout.Label(transTemp.TransferDetailsText); From 0761e1f89120fc9e47c93df9153a01e48109e126 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sat, 20 Dec 2014 21:59:34 +1100 Subject: [PATCH 32/40] WIP stuff --- TransferWindowPlanner/SharedStuff/LambertSolver.cs | 2 +- TransferWindowPlanner/TransferWindowPlanner.csproj | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/TransferWindowPlanner/SharedStuff/LambertSolver.cs b/TransferWindowPlanner/SharedStuff/LambertSolver.cs index 4c129ca..accd110 100644 --- a/TransferWindowPlanner/SharedStuff/LambertSolver.cs +++ b/TransferWindowPlanner/SharedStuff/LambertSolver.cs @@ -111,7 +111,7 @@ public static double TransferDeltaV(CelestialBody origin, CelestialBody destinat double e = r0 * v1 * v1 / mu - 1; // Ejection orbit eccentricity double ap = r0 * (1 + e) / (1 - e); // Ejection orbit apoapsis - if (ap > 0 && ap <= rsoi) { + if (ap > 0 && ap <= rsoi) { oTransfer = null; //Nuke this if we have no result return Double.NaN; // There is no orbit that leaves the SoI with a velocity of ejectionDeltaV } diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index 255b8bc..a672de1 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -33,14 +33,14 @@ False - ..\..\..\..\~Games\KSP_win_PluginTest_Minimal\KSP_Data\Managed\Assembly-CSharp.dll + ..\..\..\..\~Games\KSP_win_PluginTest_Prerelease\KSP_Data\Managed\Assembly-CSharp.dll False - ..\..\..\..\~Games\KSP_win_PluginTest_Minimal\KSP_Data\Managed\UnityEngine.dll + ..\..\..\..\~Games\KSP_win_PluginTest_Prerelease\KSP_Data\Managed\UnityEngine.dll @@ -76,7 +76,9 @@ rem Set the Variables we need echo Finding KSP -if exist "D:\~Games\KSP_Win_PlugInTest_Minimal\KSP.exe" ( +if exist "D:\~Games\KSP_Win_PlugInTest_PreRelease\KSP.exe" ( + set GAMEPATH="D:\~Games\KSP_Win_PlugInTest_PreRelease" +) else if exist "D:\~Games\KSP_Win_PlugInTest_Minimal\KSP.exe" ( set GAMEPATH="D:\~Games\KSP_Win_PlugInTest_Minimal" ) else if exist "C:\~Games\KSP_Win_PlugInTest_Minimal\KSP.exe" ( set GAMEPATH="C:\~Games\KSP_Win_PlugInTest_Minimal From f65e51c8b1aaa331e13d584e0346d528f664cc76 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Sun, 21 Dec 2014 20:55:04 +1100 Subject: [PATCH 33/40] KerbalStuff Version String Fix --- PluginBuilder/PublishKSPTWP.ps1 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/PluginBuilder/PublishKSPTWP.ps1 b/PluginBuilder/PublishKSPTWP.ps1 index 64529db..9e410bb 100644 --- a/PluginBuilder/PublishKSPTWP.ps1 +++ b/PluginBuilder/PublishKSPTWP.ps1 @@ -99,7 +99,7 @@ function CreateKerbalStuffRelease() { { "Updating Mod at KerbalStuff" $File = get-item "$($UploadDir)\v$($Version)\$($pluginname)_$($Version).zip" - & $KerbalStuffWrapper updatemod /m:$KerbalStuffModID /u:"$KerbalStuffLogin" /p:"$KerbalStuffPW" /k:"$KSPVersion" /v:"$Version" /f:"$($File.FullName)" /l:"$relKStuff" /n:true + & $KerbalStuffWrapper updatemod /m:$KerbalStuffModID /u:"$KerbalStuffLogin" /p:"$KerbalStuffPW" /k:"$KerbalStuffKSPVersion" /v:"$Version" /f:"$($File.FullName)" /l:"$relKStuff" /n:true } } @@ -234,6 +234,10 @@ if($ChoiceRtn -eq 0) $relKStuff = $reldescr.Replace("\r\n","`r`n") + $KerbalStuffKSPVersion = $KSPVersion.Split(":")[1].Trim(" ") + if ($KerbalStuffKSPVersion.EndsWith(".0")){ + $KerbalStuffKSPVersion = $KerbalStuffKSPVersion.Substring(0,$KerbalStuffKSPVersion.Length-2) + } "GitHub Description:" "-------------------" @@ -243,6 +247,10 @@ if($ChoiceRtn -eq 0) "-------------------" "$($relKStuff)`r`n" + "KStuff KSPVersion:" + "-------------------" + "$($KerbalStuffKSPVersion)`r`n" + "Forum Info:" "-------------------" "$($ForumHeader)`r`n" From 03dc4861698316e05382c7c0fb9f76f0ebda6bbe Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 22 Dec 2014 22:32:53 +1100 Subject: [PATCH 34/40] 0.90 updates --- TransferWindowPlanner/TWP.cs | 20 +- TransferWindowPlanner/TWPWindowSettings.cs | 2 +- TransferWindowPlanner/TimeObjects.cs | 662 +++++++++--------- .../TransferWindowPlanner.csproj | 4 +- 4 files changed, 339 insertions(+), 349 deletions(-) diff --git a/TransferWindowPlanner/TWP.cs b/TransferWindowPlanner/TWP.cs index 8e0106d..e93a28c 100644 --- a/TransferWindowPlanner/TWP.cs +++ b/TransferWindowPlanner/TWP.cs @@ -260,10 +260,9 @@ void DrawGUI() if (MouseOverAnyWindow && !InputLockExists) { Boolean AddLock = false; switch (HighLogic.LoadedScene) { - case GameScenes.SPACECENTER: AddLock = settings.ClickThroughProtect_KSC && !(InputLockManager.GetControlLock("TWPControlLock") == ControlTypes.KSC_FACILITIES); break; - case GameScenes.EDITOR: - case GameScenes.SPH: AddLock = settings.ClickThroughProtect_Editor && !(InputLockManager.GetControlLock("TWPControlLock") == ControlTypes.EDITOR_LOCK); break; - case GameScenes.FLIGHT: AddLock = settings.ClickThroughProtect_Flight && !(InputLockManager.GetControlLock("TWPControlLock") == ControlTypes.All); break; + case GameScenes.SPACECENTER: AddLock = settings.ClickThroughProtect_KSC && !(InputLockManager.GetControlLock("TWPControlLock") != ControlTypes.None); break; + case GameScenes.EDITOR: AddLock = settings.ClickThroughProtect_Editor && !(InputLockManager.GetControlLock("TWPControlLock") != ControlTypes.None); break; + case GameScenes.FLIGHT: AddLock = settings.ClickThroughProtect_Flight && !(InputLockManager.GetControlLock("TWPControlLock") != ControlTypes.None); break; case GameScenes.TRACKSTATION: break; default: @@ -274,13 +273,8 @@ void DrawGUI() switch (HighLogic.LoadedScene) { case GameScenes.SPACECENTER: InputLockManager.SetControlLock(ControlTypes.KSC_FACILITIES, "TWPControlLock"); break; - case GameScenes.EDITOR: - case GameScenes.SPH: - InputLockManager.SetControlLock(ControlTypes.EDITOR_LOCK, "TWPControlLock"); - break; - case GameScenes.FLIGHT: - InputLockManager.SetControlLock(ControlTypes.All, "TWPControlLock"); - break; + case GameScenes.EDITOR: InputLockManager.SetControlLock(ControlTypes.EDITOR_LOCK, "TWPControlLock"); break; + case GameScenes.FLIGHT: InputLockManager.SetControlLock(ControlTypes.All, "TWPControlLock"); break; case GameScenes.TRACKSTATION: break; default: @@ -298,9 +292,7 @@ void DrawGUI() internal void RemoveInputLock() { - if (InputLockManager.GetControlLock("TWPControlLock") == ControlTypes.KSC_FACILITIES || - InputLockManager.GetControlLock("TWPControlLock") == ControlTypes.EDITOR_LOCK || - InputLockManager.GetControlLock("TWPControlLock") == ControlTypes.All) + if (InputLockManager.GetControlLock("TWPControlLock") != ControlTypes.None) { LogFormatted_DebugOnly("Removing-{0}", "TWPControlLock"); InputLockManager.RemoveControlLock("TWPControlLock"); diff --git a/TransferWindowPlanner/TWPWindowSettings.cs b/TransferWindowPlanner/TWPWindowSettings.cs index 14c0084..83a6a94 100644 --- a/TransferWindowPlanner/TWPWindowSettings.cs +++ b/TransferWindowPlanner/TWPWindowSettings.cs @@ -225,7 +225,7 @@ private void DrawWindow_General() settings.Save(); } if (DrawToggle(ref settings.ClickThroughProtect_Editor, "Prevent in Editors", Styles.styleToggle)) { - if (!settings.ClickThroughProtect_KSC && (HighLogic.LoadedScene == GameScenes.EDITOR ||HighLogic.LoadedScene == GameScenes.SPH)) + if (!settings.ClickThroughProtect_KSC && (HighLogic.LoadedScene == GameScenes.EDITOR)) mbTWP.RemoveInputLock(); settings.Save(); } diff --git a/TransferWindowPlanner/TimeObjects.cs b/TransferWindowPlanner/TimeObjects.cs index d62b5d6..d20cd9f 100644 --- a/TransferWindowPlanner/TimeObjects.cs +++ b/TransferWindowPlanner/TimeObjects.cs @@ -1,331 +1,331 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -using KSP; -using UnityEngine; -using KSPPluginFramework; - -namespace TransferWindowPlanner -{ - // - //A class to store the UT of events and get back useful data - // - public class KSPTime - { - - //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math - // to go to years and get back to full days isnt confusing - have to sort this out some day though. - //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo - const double HoursPerDayEarth = 24; - const double HoursPerYearEarth = 365 * HoursPerDayEarth; - - const double HoursPerDayKerbin = 6; - const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; - - #region "Constructors" - public KSPTime() - { } - public KSPTime(double NewUT) - { - UT = NewUT; - } - public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) - { - UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); - } - #endregion - - /// - /// Build the UT from raw values - /// - /// - /// - /// - /// - /// - public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) - { - UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); - } - - public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) - { - BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); - } - - #region "Properties" - - //Stores the Universal Time in game seconds - public double UT; - - //readonly props that resolve from UT - public long Second - { - get { return Convert.ToInt64(Math.Truncate(UT % 60)); } - } - public long Minute - { - get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } - } - - private double HourRaw { get { return UT / 60 / 60; } } - - public long Hour - { - get - { - if (GameSettings.KERBIN_TIME) { - return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); - } - else { - return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); - } - } - } - - public long Day - { - get - { - if (GameSettings.KERBIN_TIME) { - return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); - } - else { - return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); - } - } - } - - public long Year - { - get - { - if (GameSettings.KERBIN_TIME) { - return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); - } - else { - return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); - } - } - } - //public long HourKerbin - //{ - // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } - //} - - //public long DayKerbin - //{ - // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } - //} - - //public long YearKerbin - //{ - // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } - //} - #endregion - - #region "String Formatting" - public String IntervalString() - { - return IntervalString(6); - } - public String IntervalString(int segments) - { - String strReturn = ""; - - if (UT < 0) strReturn += "+ "; - - int intUsed = 0; - - if (intUsed < segments && Year != 0) { - strReturn += String.Format("{0}y", Math.Abs(Year)); - intUsed++; - } - - if (intUsed < segments && (Day != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}d", Math.Abs(Day)); - intUsed++; - } - - if (intUsed < segments && (Hour != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}h", Math.Abs(Hour)); - intUsed++; - } - if (intUsed < segments && (Minute != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}m", Math.Abs(Minute)); - intUsed++; - } - if (intUsed < segments)// && (Second != 0 || intUsed > 0)) - { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}s", Math.Abs(Second)); - intUsed++; - } - - - return strReturn; - } - - public String IntervalDateTimeString() - { - return IntervalDateTimeString(6); - } - public String IntervalDateTimeString(int segments) - { - String strReturn = ""; - - if (UT < 0) strReturn += "+ "; - - int intUsed = 0; - - if (intUsed < segments && Year != 0) { - strReturn += String.Format("{0}y", Math.Abs(Year)); - intUsed++; - } - - if (intUsed < segments && (Day != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0}d", Math.Abs(Day)); - intUsed++; - } - - if (intUsed < segments && (Hour != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ", "; - strReturn += String.Format("{0:00}", Math.Abs(Hour)); - intUsed++; - } - if (intUsed < segments && (Minute != 0 || intUsed > 0)) { - if (intUsed > 0) strReturn += ":"; - strReturn += String.Format("{0:00}", Math.Abs(Minute)); - intUsed++; - } - if (intUsed < segments)// && (Second != 0 || intUsed > 0)) - { - if (intUsed > 0) strReturn += ":"; - strReturn += String.Format("{0:00}", Math.Abs(Second)); - intUsed++; - } - - - return strReturn; - } - - public String DateString() - { - return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); - } - - public String DateTimeString() - { - return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); - } - - public String IntervalStringLong() - { - String strReturn = ""; - if (UT < 0) strReturn += "+ "; - strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); - return strReturn; - } - public String IntervalStringLongTrimYears() - { - String strReturn = IntervalStringLong(); - return strReturn.Replace("0 Years, ", ""); - } - - public String UTString() - { - String strReturn = ""; - if (UT < 0) strReturn += "+ "; - strReturn += String.Format("{0:N0}s", Math.Abs(UT)); - return strReturn; - } - #endregion - - public override String ToString() - { - return IntervalStringLong(); - } - - #region Static Properties - public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } - public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } - public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } - public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } - public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } - #endregion - - #region "Static Functions" - //fudging for dates - public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); - - public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) - { - return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); - } - public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) - { - if (GameSettings.KERBIN_TIME) { - return Seconds + - Minutes * 60 + - Hours * 60 * 60 + - Days * HoursPerDayKerbin * 60 * 60 + - Years * HoursPerYearKerbin * 60 * 60; - } - else { - return Seconds + - Minutes * 60 + - Hours * 60 * 60 + - Days * HoursPerDayEarth * 60 * 60 + - Years * HoursPerYearEarth * 60 * 60; - } - } - - public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) - { - return PrintInterval(timeTemp, 3, TimeFormat); - } - - public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) - { - switch (TimeFormat) { - case PrintTimeFormat.TimeAsUT: - return timeTemp.UTString(); - case PrintTimeFormat.KSPString: - return timeTemp.IntervalString(Segments); - case PrintTimeFormat.DateTimeString: - return timeTemp.IntervalDateTimeString(Segments); - default: - return timeTemp.IntervalString(Segments); - } - } - - public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) - { - switch (TimeFormat) { - case PrintTimeFormat.TimeAsUT: - return timeTemp.UTString(); - case PrintTimeFormat.KSPString: - return timeTemp.DateString(); - case PrintTimeFormat.DateTimeString: - return timeTemp.DateTimeString(); - default: - return timeTemp.DateTimeString(); - } - } - - public enum PrintTimeFormat - { - TimeAsUT, - KSPString, - DateTimeString - } - #endregion - } - -} \ No newline at end of file +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; + +//using KSP; +//using UnityEngine; +//using KSPPluginFramework; + +//namespace TransferWindowPlanner +//{ +// // +// //A class to store the UT of events and get back useful data +// // +// public class KSPTime +// { + +// //really there are 31,446,925.9936 seconds in a year, use 365*24 so the reciprocal math +// // to go to years and get back to full days isnt confusing - have to sort this out some day though. +// //NOTE: KSP Dates appear to all be 365 * 24 as well - no fractions - woohoo +// const double HoursPerDayEarth = 24; +// const double HoursPerYearEarth = 365 * HoursPerDayEarth; + +// const double HoursPerDayKerbin = 6; +// const double HoursPerYearKerbin = 426 * HoursPerDayKerbin; + +// #region "Constructors" +// public KSPTime() +// { } +// public KSPTime(double NewUT) +// { +// UT = NewUT; +// } +// public KSPTime(double Years, double Days, double Hours, double Minutes, double Seconds) +// { +// UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); +// } +// #endregion + +// /// +// /// Build the UT from raw values +// /// +// /// +// /// +// /// +// /// +// /// +// public void BuildUT(double Years, double Days, double Hours, double Minutes, double Seconds) +// { +// UT = KSPTime.BuildUTFromRaw(Years, Days, Hours, Minutes, Seconds); +// } + +// public void BuildUT(String Years, String Days, String Hours, String Minutes, String Seconds) +// { +// BuildUT(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); +// } + +// #region "Properties" + +// //Stores the Universal Time in game seconds +// public double UT; + +// //readonly props that resolve from UT +// public long Second +// { +// get { return Convert.ToInt64(Math.Truncate(UT % 60)); } +// } +// public long Minute +// { +// get { return Convert.ToInt64(Math.Truncate((UT / 60) % 60)); } +// } + +// private double HourRaw { get { return UT / 60 / 60; } } + +// public long Hour +// { +// get +// { +// if (GameSettings.KERBIN_TIME) { +// return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); +// } +// else { +// return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayEarth)); +// } +// } +// } + +// public long Day +// { +// get +// { +// if (GameSettings.KERBIN_TIME) { +// return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); +// } +// else { +// return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearEarth) / HoursPerDayEarth))); +// } +// } +// } + +// public long Year +// { +// get +// { +// if (GameSettings.KERBIN_TIME) { +// return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); +// } +// else { +// return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearEarth))); +// } +// } +// } +// //public long HourKerbin +// //{ +// // get { return Convert.ToInt64(Math.Truncate(HourRaw % HoursPerDayKerbin)); } +// //} + +// //public long DayKerbin +// //{ +// // get { return Convert.ToInt64(Math.Truncate(((HourRaw % HoursPerYearKerbin) / HoursPerDayKerbin))); } +// //} + +// //public long YearKerbin +// //{ +// // get { return Convert.ToInt64(Math.Truncate((HourRaw / HoursPerYearKerbin))); } +// //} +// #endregion + +// #region "String Formatting" +// public String IntervalString() +// { +// return IntervalString(6); +// } +// public String IntervalString(int segments) +// { +// String strReturn = ""; + +// if (UT < 0) strReturn += "+ "; + +// int intUsed = 0; + +// if (intUsed < segments && Year != 0) { +// strReturn += String.Format("{0}y", Math.Abs(Year)); +// intUsed++; +// } + +// if (intUsed < segments && (Day != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}d", Math.Abs(Day)); +// intUsed++; +// } + +// if (intUsed < segments && (Hour != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}h", Math.Abs(Hour)); +// intUsed++; +// } +// if (intUsed < segments && (Minute != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}m", Math.Abs(Minute)); +// intUsed++; +// } +// if (intUsed < segments)// && (Second != 0 || intUsed > 0)) +// { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}s", Math.Abs(Second)); +// intUsed++; +// } + + +// return strReturn; +// } + +// public String IntervalDateTimeString() +// { +// return IntervalDateTimeString(6); +// } +// public String IntervalDateTimeString(int segments) +// { +// String strReturn = ""; + +// if (UT < 0) strReturn += "+ "; + +// int intUsed = 0; + +// if (intUsed < segments && Year != 0) { +// strReturn += String.Format("{0}y", Math.Abs(Year)); +// intUsed++; +// } + +// if (intUsed < segments && (Day != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0}d", Math.Abs(Day)); +// intUsed++; +// } + +// if (intUsed < segments && (Hour != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ", "; +// strReturn += String.Format("{0:00}", Math.Abs(Hour)); +// intUsed++; +// } +// if (intUsed < segments && (Minute != 0 || intUsed > 0)) { +// if (intUsed > 0) strReturn += ":"; +// strReturn += String.Format("{0:00}", Math.Abs(Minute)); +// intUsed++; +// } +// if (intUsed < segments)// && (Second != 0 || intUsed > 0)) +// { +// if (intUsed > 0) strReturn += ":"; +// strReturn += String.Format("{0:00}", Math.Abs(Second)); +// intUsed++; +// } + + +// return strReturn; +// } + +// public String DateString() +// { +// return String.Format("Year {0},Day {1}, {2}h, {3}m, {4}s", Year + 1, Day + 1, Hour, Minute, Second); +// } + +// public String DateTimeString() +// { +// return String.Format("Year {0},Day {1}, {2:00}:{3:00}:{4:00}", Year + 1, Day + 1, Hour, Minute, Second); +// } + +// public String IntervalStringLong() +// { +// String strReturn = ""; +// if (UT < 0) strReturn += "+ "; +// strReturn += String.Format("{0} Years, {1} Days, {2:00}:{3:00}:{4:00}", Math.Abs(Year), Math.Abs(Day), Math.Abs(Hour), Math.Abs(Minute), Math.Abs(Second)); +// return strReturn; +// } +// public String IntervalStringLongTrimYears() +// { +// String strReturn = IntervalStringLong(); +// return strReturn.Replace("0 Years, ", ""); +// } + +// public String UTString() +// { +// String strReturn = ""; +// if (UT < 0) strReturn += "+ "; +// strReturn += String.Format("{0:N0}s", Math.Abs(UT)); +// return strReturn; +// } +// #endregion + +// public override String ToString() +// { +// return IntervalStringLong(); +// } + +// #region Static Properties +// public static Double HoursPerDay { get { return GameSettings.KERBIN_TIME ? HoursPerDayKerbin : HoursPerDayEarth; } } +// public static Double SecondsPerDay { get { return HoursPerDay * 60 * 60; } } +// public static Double HoursPerYear { get { return GameSettings.KERBIN_TIME ? HoursPerYearKerbin : HoursPerYearEarth; } } +// public static Double DaysPerYear { get { return HoursPerYear / HoursPerDay; } } +// public static Double SecondsPerYear { get { return HoursPerYear * 60 * 60; } } +// #endregion + +// #region "Static Functions" +// //fudging for dates +// public static KSPTime timeDateOffest = new KSPTime(1, 1, 0, 0, 0); + +// public static Double BuildUTFromRaw(String Years, String Days, String Hours, String Minutes, String Seconds) +// { +// return BuildUTFromRaw(Convert.ToDouble(Years), Convert.ToDouble(Days), Convert.ToDouble(Hours), Convert.ToDouble(Minutes), Convert.ToDouble(Seconds)); +// } +// public static Double BuildUTFromRaw(double Years, double Days, double Hours, double Minutes, double Seconds) +// { +// if (GameSettings.KERBIN_TIME) { +// return Seconds + +// Minutes * 60 + +// Hours * 60 * 60 + +// Days * HoursPerDayKerbin * 60 * 60 + +// Years * HoursPerYearKerbin * 60 * 60; +// } +// else { +// return Seconds + +// Minutes * 60 + +// Hours * 60 * 60 + +// Days * HoursPerDayEarth * 60 * 60 + +// Years * HoursPerYearEarth * 60 * 60; +// } +// } + +// public static String PrintInterval(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) +// { +// return PrintInterval(timeTemp, 3, TimeFormat); +// } + +// public static String PrintInterval(KSPTime timeTemp, int Segments, KSPTime.PrintTimeFormat TimeFormat) +// { +// switch (TimeFormat) { +// case PrintTimeFormat.TimeAsUT: +// return timeTemp.UTString(); +// case PrintTimeFormat.KSPString: +// return timeTemp.IntervalString(Segments); +// case PrintTimeFormat.DateTimeString: +// return timeTemp.IntervalDateTimeString(Segments); +// default: +// return timeTemp.IntervalString(Segments); +// } +// } + +// public static String PrintDate(KSPTime timeTemp, KSPTime.PrintTimeFormat TimeFormat) +// { +// switch (TimeFormat) { +// case PrintTimeFormat.TimeAsUT: +// return timeTemp.UTString(); +// case PrintTimeFormat.KSPString: +// return timeTemp.DateString(); +// case PrintTimeFormat.DateTimeString: +// return timeTemp.DateTimeString(); +// default: +// return timeTemp.DateTimeString(); +// } +// } + +// public enum PrintTimeFormat +// { +// TimeAsUT, +// KSPString, +// DateTimeString +// } +// #endregion +// } + +//} \ No newline at end of file diff --git a/TransferWindowPlanner/TransferWindowPlanner.csproj b/TransferWindowPlanner/TransferWindowPlanner.csproj index a672de1..384a8ea 100644 --- a/TransferWindowPlanner/TransferWindowPlanner.csproj +++ b/TransferWindowPlanner/TransferWindowPlanner.csproj @@ -76,9 +76,7 @@ rem Set the Variables we need echo Finding KSP -if exist "D:\~Games\KSP_Win_PlugInTest_PreRelease\KSP.exe" ( - set GAMEPATH="D:\~Games\KSP_Win_PlugInTest_PreRelease" -) else if exist "D:\~Games\KSP_Win_PlugInTest_Minimal\KSP.exe" ( +if exist "D:\~Games\KSP_Win_PlugInTest_Minimal\KSP.exe" ( set GAMEPATH="D:\~Games\KSP_Win_PlugInTest_Minimal" ) else if exist "C:\~Games\KSP_Win_PlugInTest_Minimal\KSP.exe" ( set GAMEPATH="C:\~Games\KSP_Win_PlugInTest_Minimal From 201cf421e5d9aacb5919e28c2d02736f8bd46769 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Mon, 22 Dec 2014 22:35:26 +1100 Subject: [PATCH 35/40] Adjustments for Inputs and Other files to handle new DateTime Library and display correct stuff Relates to #25 and #24 --- .../FrameworkExt/KSPDateTime.cs | 49 +++++++++-- .../FrameworkExt/MonoBehaviourWindowPlus.cs | 30 +++---- TransferWindowPlanner/Settings.cs | 1 + .../SharedStuff/Utilities.cs | 5 ++ TransferWindowPlanner/TWP.cs | 14 ++- TransferWindowPlanner/TWPWindow.cs | 88 ++++++++++++++----- TransferWindowPlanner/TWPWindowDebug.cs | 8 +- TransferWindowPlanner/TWPWindowSettings.cs | 46 +++++++++- TransferWindowPlanner/TWPWindowWorkers.cs | 20 ++--- 9 files changed, 199 insertions(+), 62 deletions(-) diff --git a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs index 57e56d4..55e6e2f 100644 --- a/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs +++ b/TransferWindowPlanner/FrameworkExt/KSPDateTime.cs @@ -263,7 +263,10 @@ public String ToStringStandard(DateStringFormatsEnum DateFormat){ case DateStringFormatsEnum.KSPFormatWithSecs: return ToString("Year y, Da\\y d - H\\h, m\\m, s\\s"); case DateStringFormatsEnum.DateTimeFormat: - return ToString("Year y, Da\\y d, HH:mm:ss"); + if (KSPDateStructure.CalendarType==CalendarTypeEnum.Earth) + return ToString("d MMM yyyy, HH:mm:ss"); + else + return ToString("Year y, Da\\y d, HH:mm:ss"); default: return ToString(); } @@ -313,10 +316,38 @@ public String ToString(String format, IFormatProvider provider) format = format.Substring(0, mIndex) + Year.ToString("D" + mLength) + format.Substring(mIndex + mLength); break; case 'M': - String input2 = Month.ToString("D" + mLength); - - //test for more than 2 M's - format = format.Substring(0, mIndex) + input2 + format.Substring(mIndex + mLength); + if (mLength < 3) + { + String input2 = Month.ToString("D" + mLength); + format = format.Substring(0, mIndex) + input2 + format.Substring(mIndex + mLength); + } + else if (mLength == 3) + { + if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) + format = format.Substring(0, mIndex) + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(Month) + format.Substring(mIndex + mLength); + else + if (KSPDateStructure.MonthCount < 1) + { + String input2 = Month.ToString("D" + mLength); + format = format.Substring(0, mIndex) + input2 + format.Substring(mIndex + mLength); + } + else + { + format = format.Substring(0, mIndex) + KSPDateStructure.Months[Month].ToString().Substring(0, 3) + format.Substring(mIndex + mLength); + } + } + else + { + if (KSPDateStructure.CalendarType== CalendarTypeEnum.Earth) + format = format.Substring(0, mIndex) + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(Month) + format.Substring(mIndex + mLength); + else + if (KSPDateStructure.MonthCount<1){ + String input2 = Month.ToString("D" + mLength); + format = format.Substring(0, mIndex) + input2 + format.Substring(mIndex + mLength); + } else { + format = format.Substring(0, mIndex) + KSPDateStructure.Months[Month] + format.Substring(mIndex + mLength); + } + } break; case 'd': format = format.Substring(0, mIndex) + Day.ToString("D" + mLength) + format.Substring(mIndex + mLength); @@ -525,6 +556,14 @@ public static Boolean Equals(KSPDateTime t1, KSPDateTime t2) return t1.UT == t2.UT; } + public static KSPDateTime FromEarthValues(Int32 Year, Int32 Month, Int32 Day) + { + return new KSPDateTime(new DateTime(Year, Month, Day).Subtract(KSPDateStructure.CustomEpochEarth).TotalSeconds); + } + public static KSPDateTime FromEarthValues(String Year, String Month, String Day) + { + return FromEarthValues(Convert.ToInt32(Year), Convert.ToInt32(Month), Convert.ToInt32(Day)); + } #endregion diff --git a/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs b/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs index 819fb9c..cde01dc 100644 --- a/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs +++ b/TransferWindowPlanner/FrameworkExt/MonoBehaviourWindowPlus.cs @@ -14,20 +14,20 @@ public abstract class MonoBehaviourWindowPlus : MonoBehaviourWindow { #region Draw Control Wrappers - internal Boolean DrawButton(String text, params GUILayoutOption[] options) + internal static Boolean DrawButton(String text, params GUILayoutOption[] options) { return GUILayout.Button(text,options); } - internal Boolean DrawButton(String text, GUIStyle style, params GUILayoutOption[] options) + internal static Boolean DrawButton(String text, GUIStyle style, params GUILayoutOption[] options) { return GUILayout.Button(text,style,options); } - internal Boolean DrawTextBox(ref String strVar, params GUILayoutOption[] options) + internal static Boolean DrawTextBox(ref String strVar, params GUILayoutOption[] options) { return DrawTextBox(ref strVar,SkinsLibrary.CurrentSkin.textField,options); } - internal Boolean DrawTextBox(ref String strVar, GUIStyle style, params GUILayoutOption[] options) + internal static Boolean DrawTextBox(ref String strVar, GUIStyle style, params GUILayoutOption[] options) { String strOld = strVar; strVar = GUILayout.TextField(strVar, style, options); @@ -35,11 +35,11 @@ internal Boolean DrawTextBox(ref String strVar, GUIStyle style, params GUILayout return DrawResultChanged(strOld, strVar, "Text String"); } - internal Boolean DrawTextBox(ref Int32 intVar, params GUILayoutOption[] options) + internal static Boolean DrawTextBox(ref Int32 intVar, params GUILayoutOption[] options) { return DrawTextBox(ref intVar, SkinsLibrary.CurrentSkin.textField, options); } - internal Boolean DrawTextBox(ref Int32 intVar, GUIStyle style, params GUILayoutOption[] options) + internal static Boolean DrawTextBox(ref Int32 intVar, GUIStyle style, params GUILayoutOption[] options) { String strRef = intVar.ToString(); DrawTextBox(ref strRef, style, options); @@ -48,11 +48,11 @@ internal Boolean DrawTextBox(ref Int32 intVar, GUIStyle style, params GUILayoutO return DrawResultChanged(intOld, intVar, "Integer Changed"); } - internal Boolean DrawTextBox(ref Double dblVar, params GUILayoutOption[] options) + internal static Boolean DrawTextBox(ref Double dblVar, params GUILayoutOption[] options) { return DrawTextBox(ref dblVar, SkinsLibrary.CurrentSkin.textField, options); } - internal Boolean DrawTextBox(ref Double dblVar, GUIStyle style, params GUILayoutOption[] options) + internal static Boolean DrawTextBox(ref Double dblVar, GUIStyle style, params GUILayoutOption[] options) { String strRef = dblVar.ToString(); DrawTextBox(ref strRef, style, options); @@ -62,14 +62,14 @@ internal Boolean DrawTextBox(ref Double dblVar, GUIStyle style, params GUILayout } - internal Boolean DrawHorizontalSlider(ref Int32 intVar, Int32 leftValue, Int32 rightValue, params GUILayoutOption[] options) + internal static Boolean DrawHorizontalSlider(ref Int32 intVar, Int32 leftValue, Int32 rightValue, params GUILayoutOption[] options) { Int32 intOld = intVar; intVar = (Int32)GUILayout.HorizontalSlider((Single)intVar, (Single)leftValue, (Single)rightValue, options); return DrawResultChanged(intOld, intVar, "Integer HorizSlider"); } - internal Boolean DrawHorizontalSlider(ref Single dblVar, Single leftValue, Single rightValue, params GUILayoutOption[] options) + internal static Boolean DrawHorizontalSlider(ref Single dblVar, Single leftValue, Single rightValue, params GUILayoutOption[] options) { Single intOld = dblVar; @@ -85,7 +85,7 @@ internal Boolean DrawHorizontalSlider(ref Single dblVar, Single leftValue, Singl /// /// /// True when the button state has changed - internal Boolean DrawToggle(ref Boolean blnVar, String ButtonText, GUIStyle style, params GUILayoutOption[] options) + internal static Boolean DrawToggle(ref Boolean blnVar, String ButtonText, GUIStyle style, params GUILayoutOption[] options) { Boolean blnOld = blnVar; blnVar = GUILayout.Toggle(blnVar, ButtonText, style, options); @@ -93,7 +93,7 @@ internal Boolean DrawToggle(ref Boolean blnVar, String ButtonText, GUIStyle styl return DrawResultChanged(blnOld, blnVar, "Toggle"); } - internal Boolean DrawToggle(ref Boolean blnVar, Texture image, GUIStyle style, params GUILayoutOption[] options) + internal static Boolean DrawToggle(ref Boolean blnVar, Texture image, GUIStyle style, params GUILayoutOption[] options) { Boolean blnOld = blnVar; blnVar = GUILayout.Toggle(blnVar, image, style, options); @@ -101,7 +101,7 @@ internal Boolean DrawToggle(ref Boolean blnVar, Texture image, GUIStyle style, p return DrawResultChanged(blnOld, blnVar, "Toggle"); } - internal Boolean DrawToggle(ref Boolean blnVar, GUIContent content, GUIStyle style, params GUILayoutOption[] options) + internal static Boolean DrawToggle(ref Boolean blnVar, GUIContent content, GUIStyle style, params GUILayoutOption[] options) { Boolean blnOld = blnVar; blnVar = GUILayout.Toggle(blnVar, content, style, options); @@ -110,13 +110,13 @@ internal Boolean DrawToggle(ref Boolean blnVar, GUIContent content, GUIStyle sty } - internal void DrawLabel(String Message, params object[] args) + internal static void DrawLabel(String Message, params object[] args) { GUILayout.Label(String.Format(Message, args)); } - private Boolean DrawResultChanged(T Original, T New,String Message) + private static Boolean DrawResultChanged(T Original, T New, String Message) { if (Original.Equals(New)) { return false; diff --git a/TransferWindowPlanner/Settings.cs b/TransferWindowPlanner/Settings.cs index 9a4d94e..39ffa68 100644 --- a/TransferWindowPlanner/Settings.cs +++ b/TransferWindowPlanner/Settings.cs @@ -40,6 +40,7 @@ internal ButtonStyleEnum ButtonStyleToDisplay { [Persistent] internal ButtonStyleEnum ButtonStyleChosen = ButtonStyleEnum.Launcher; [Persistent] internal CalendarTypeEnum SelectedCalendar = CalendarTypeEnum.KSPStock; + [Persistent] internal String EarthEpoch = "1951-01-01"; internal enum ButtonStyleEnum { diff --git a/TransferWindowPlanner/SharedStuff/Utilities.cs b/TransferWindowPlanner/SharedStuff/Utilities.cs index 08de674..24287f0 100644 --- a/TransferWindowPlanner/SharedStuff/Utilities.cs +++ b/TransferWindowPlanner/SharedStuff/Utilities.cs @@ -27,6 +27,11 @@ public static String AppendLine(this String s, String LineToAdd, params object[] return s; } + public static Int32 ToInt32(this String s) + { + return Convert.ToInt32(s); + } + /// /// Return the ejection angle of the vessel/body on this orbit for a given UT /// diff --git a/TransferWindowPlanner/TWP.cs b/TransferWindowPlanner/TWP.cs index e93a28c..63df2fc 100644 --- a/TransferWindowPlanner/TWP.cs +++ b/TransferWindowPlanner/TWP.cs @@ -46,6 +46,13 @@ internal override void Awake() InitWindows(); + if(settings.SelectedCalendar==CalendarTypeEnum.Earth) { + KSPDateStructure.SetEarthCalendar(settings.EarthEpoch.Split('-')[0].ToInt32(), + settings.EarthEpoch.Split('-')[1].ToInt32(), + settings.EarthEpoch.Split('-')[2].ToInt32()); + windowSettings.ddlSettingsCalendar.SelectedIndex = (Int32)settings.SelectedCalendar; + } + //plug us in to the draw queue and start the worker RenderingManager.AddToPostDrawQueue(1, DrawGUI); @@ -88,17 +95,22 @@ internal override void OnDestroy() internal override void Start() { + //Init the KAC Integration KACWrapper.InitKACWrapper(); - if (KACWrapper.APIReady) { LogFormatted("Successfully Hooked the KAC"); KACWrapper.KAC.onAlarmStateChanged += KAC_onAlarmStateChanged; } + + //Ensure no lagging locks + RemoveInputLock(); } + private void DestroyAPIHooks() { + //clean up the integration events if (KACWrapper.APIReady) { KACWrapper.KAC.onAlarmStateChanged -= KAC_onAlarmStateChanged; diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index f9a44aa..c0b14a2 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -44,7 +44,7 @@ internal override void Awake() cbStar = FlightGlobals.Bodies.FirstOrDefault(x => x.referenceBody == x.referenceBody); if (cbStar == null) { - //RuRo + //RuRo!! LogFormatted("Error: Couldn't detect a Star (ref body is itself)"); } else @@ -88,11 +88,8 @@ void TWPWindow_WindowVisibleChanged(MonoBehaviourWindow sender, bool NewVisibleS private void SetDepartureMinToYesterday() { //Set the Departure min to be yesterday - KSPDateTime dateYesterday = new KSPDateTime(Planetarium.GetUniversalTime() - KSPDateStructure.SecondsPerDay); - LogFormatted("Setting to y{0} d{1}", dateYesterday.Year, dateYesterday.Day); - strDepartureMinYear = (dateYesterday.Year+1).ToString(); - strDepartureMinDay = (dateYesterday.Day+1).ToString(); - DepartureMin = new KSPDateTime(dateYesterday.Year+1, dateYesterday.Day+1, 0, 0, 0).UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; + dateMinDeparture = new KSPDateTime(Planetarium.GetUniversalTime()).Date; + DepartureMin = dateMinDeparture.UT; } void ddlOrigin_OnSelectionChanged(MonoBehaviourWindowPlus.DropDownList sender, int OldIndex, int NewIndex) @@ -147,7 +144,7 @@ internal override void OnGUIOnceOnly() // return blnReturn; //} - internal Boolean DrawTextField(ref String Value, String RegexValidator, Boolean RegexFailOnMatch, String LabelText="", Int32 FieldWidth=0, Int32 LabelWidth=0) + internal static Boolean DrawTextField(ref String Value, String RegexValidator, Boolean RegexFailOnMatch, String LabelText = "", Int32 FieldWidth = 0, Int32 LabelWidth = 0) { GUIStyle styleTextBox = Styles.styleTextField; if ((RegexFailOnMatch && System.Text.RegularExpressions.Regex.IsMatch(Value, RegexValidator, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) || @@ -169,7 +166,32 @@ internal Boolean DrawTextField(ref String Value, String RegexValidator, Boolean return blnReturn; } - internal Boolean DrawYearDay(ref String strYear,ref String strDay) + internal static Boolean DrawYearDay(ref KSPDateTime dateToDraw) + { + String strYear = dateToDraw.Year.ToString(); + String strMonth = dateToDraw.Month.ToString(); + String strDay = dateToDraw.Day.ToString(); + + //If the value changed + Boolean blnReturn = false; + + if (KSPDateStructure.CalendarType==CalendarTypeEnum.Earth) + { + blnReturn = DrawYearMonthDay(ref strYear, ref strMonth, ref strDay); + if (blnReturn) { + dateToDraw = KSPDateTime.FromEarthValues(strYear, strMonth, strDay); + } + } + else { + blnReturn = DrawYearDay(ref strYear, ref strDay); + if (blnReturn) { + dateToDraw = new KSPDateTime(strYear, strDay); + } + } + return blnReturn; + } + + internal static Boolean DrawYearDay(ref String strYear, ref String strDay) { Boolean blnReturn = false; GUILayout.BeginHorizontal(); @@ -179,9 +201,22 @@ internal Boolean DrawYearDay(ref String strYear,ref String strDay) return blnReturn; } - String strOrigin, strDestination; - String strDepartureAltitude,strArrivalAltitude; - String strDepartureMinYear, strDepartureMinDay, strDepartureMaxYear, strDepartureMaxDay; + internal static Boolean DrawYearMonthDay(ref String strYear, ref String strMonth, ref String strDay) + { + Boolean blnReturn = false; + GUILayout.BeginHorizontal(); + blnReturn = blnReturn || DrawTextField(ref strYear, "[^\\d\\.]+", true, "Y:", 40, 20); + blnReturn = blnReturn || DrawTextField(ref strMonth, "[^\\d\\.]+", true, "M:", 30, 20); + blnReturn = blnReturn || DrawTextField(ref strDay, "[^\\d\\.]+", true, "D:", 30, 20); + GUILayout.EndHorizontal(); + return blnReturn; + } + + + + //String strDepartureMinYear, strDepartureMinDay, strDepartureMaxYear, strDepartureMaxDay; + internal KSPDateTime dateMinDeparture, dateMaxDeparture; + String strDepartureAltitude, strArrivalAltitude; String strTravelMinDays, strTravelMaxDays; internal Vector2 vectMouse; @@ -633,9 +668,9 @@ private void DrawTransferEntry() GUILayout.Label("km", GUILayout.Width(20)); GUILayout.EndHorizontal(); - DrawYearDay(ref strDepartureMinYear, ref strDepartureMinDay); + DrawYearDay(ref dateMinDeparture); - DrawYearDay(ref strDepartureMaxYear, ref strDepartureMaxDay); + DrawYearDay(ref dateMaxDeparture); GUILayout.BeginHorizontal(); DrawTextField(ref strTravelMinDays, "[^\\d\\.]+", true, FieldWidth: 60); @@ -650,26 +685,31 @@ private void DrawTransferEntry() GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("Reset",Resources.btnReset), "ButtonSettings")) { - Done = false; - if (bw!=null && bw.IsBusy) - bw.CancelAsync(); - Running = false; - TransferSelected = null; - WindowRect.height = 400; - - SetDepartureMinToYesterday(); - LogFormatted("Setting to y{0} d{1}", strDepartureMinYear, strDepartureMinDay); - SetupDestinationControls(); - LogFormatted("Setting to y{0} d{1}", strDepartureMinYear, strDepartureMinDay); + mbTWP.windowSettings.Visible = false; + ResetWindow(); } if (GUILayout.Button("Plot It!")) { + mbTWP.windowSettings.Visible = false; StartWorker(); WindowRect.height = 400; ShowEjectionDetails = false; } GUILayout.EndHorizontal(); } + + internal void ResetWindow() + { + Done = false; + if (bw != null && bw.IsBusy) + bw.CancelAsync(); + Running = false; + TransferSelected = null; + WindowRect.height = 400; + + SetDepartureMinToYesterday(); + SetupDestinationControls(); + } internal override void OnGUIEvery() { diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index c748e3c..5634723 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -155,8 +155,7 @@ internal override void DrawWindow(int id) DrawLabel("Mouse:{0}", mbTWP.windowMain.vectMouse); DrawLabel("Plot:{0}", new Rect(mbTWP.windowMain.PlotPosition.x, mbTWP.windowMain.PlotPosition.y, mbTWP.windowMain.PlotWidth, mbTWP.windowMain.PlotHeight)); DrawLabel("Selected:{0}", mbTWP.windowMain.vectSelected); - DrawLabel("Departure:{0:0}, Travel:{1:0}", mbTWP.windowMain.DepartureSelected/KSPTime.SecondsPerDay,mbTWP.windowMain.TravelSelected/KSPTime.SecondsPerDay); - + DrawLabel("Departure:{0:0}, Travel:{1:0}", mbTWP.windowMain.DepartureSelected / KSPDateStructure.SecondsPerDay, mbTWP.windowMain.TravelSelected / KSPDateStructure.SecondsPerDay); if (mbTWP.windowMain.TransferSelected != null && FlightGlobals.ActiveVessel!=null) { @@ -271,9 +270,10 @@ internal override void DrawWindow(int id) //DrawLabel("AbsPos at firstUT:{0}", cbD.orbit.getPositionAtUT(intTest1)); - //DrawLabel("DepartureMin:{0}", DepartureMin); + + DrawLabel("DepartureMin:{0}", mbTWP.windowMain.dateMinDeparture.UT); //DrawLabel("DepartureRange:{0}", DepartureRange); - //DrawLabel("DepartureMax:{0}", DepartureMax); + DrawLabel("DepartureMax:{0}", mbTWP.windowMain.dateMaxDeparture.UT); //DrawLabel("TravelMin:{0}", TravelMin); //DrawLabel("TravelMax:{0}", TravelMax); diff --git a/TransferWindowPlanner/TWPWindowSettings.cs b/TransferWindowPlanner/TWPWindowSettings.cs index 83a6a94..1557190 100644 --- a/TransferWindowPlanner/TWPWindowSettings.cs +++ b/TransferWindowPlanner/TWPWindowSettings.cs @@ -21,7 +21,7 @@ class TWPWindowSettings:MonoBehaviourWindowPlus internal DropDownList ddlSettingsTab; private DropDownList ddlSettingsSkin; private DropDownList ddlSettingsButtonStyle; - private DropDownList ddlSettingsCalendar; + internal DropDownList ddlSettingsCalendar; internal Int32 WindowWidth = 320; internal Int32 WindowHeight = 200; @@ -91,17 +91,21 @@ void ddlSettingsSkin_SelectionChanged(DropDownList sender, int OldIndex, int New void ddlSettingsCalendar_OnSelectionChanged(DropDownList sender, int OldIndex, int NewIndex) { settings.SelectedCalendar = (CalendarTypeEnum)NewIndex; + settings.Save(); switch (settings.SelectedCalendar) { case CalendarTypeEnum.KSPStock: KSPDateStructure.SetKSPStockCalendar(); break; - case CalendarTypeEnum.Earth: - KSPDateStructure.SetEarthCalendar(); + case CalendarTypeEnum.Earth: + KSPDateStructure.SetEarthCalendar(settings.EarthEpoch.Split('-')[0].ToInt32(), + settings.EarthEpoch.Split('-')[1].ToInt32(), + settings.EarthEpoch.Split('-')[2].ToInt32()); break; case CalendarTypeEnum.Custom: KSPDateStructure.SetCustomCalendar(); break; default: KSPDateStructure.SetKSPStockCalendar(); break; } + mbTWP.windowMain.ResetWindow(); } @@ -303,6 +307,42 @@ private void DrawWindow_Calendar() GUILayout.EndVertical(); GUILayout.EndHorizontal(); + if (settings.SelectedCalendar == CalendarTypeEnum.Earth) + { + GUILayout.BeginHorizontal(); + GUILayout.Label("Earth Epoch:"); + + String strYear, strMonth, strDay; + strYear = KSPDateStructure.CustomEpochEarth.Year.ToString(); + strMonth = KSPDateStructure.CustomEpochEarth.Month.ToString(); + strDay = KSPDateStructure.CustomEpochEarth.Day.ToString(); + if (TWPWindow.DrawYearMonthDay(ref strYear, ref strMonth, ref strDay)) + { + try + { + KSPDateStructure.SetEarthCalendar(strYear.ToInt32(), strMonth.ToInt32(), strDay.ToInt32()); + settings.EarthEpoch = KSPDateStructure.CustomEpochEarth.ToString("yyyy-MM-dd"); + settings.Save(); + mbTWP.windowMain.ResetWindow(); + } + catch (Exception) + { + LogFormatted("Unable to set the Epoch date using the values provided-{0}-{1}-{2}", strYear, strMonth, strDay); + } + } + + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + GUILayout.FlexibleSpace(); + if (GUILayout.Button("Reset Earth Epoch")) + { + KSPDateStructure.SetEarthCalendar(); + settings.EarthEpoch = KSPDateStructure.CustomEpochEarth.ToString("1951-01-01"); + settings.Save(); + } + GUILayout.EndHorizontal(); + } + //if RSS not installed and RSS chosen... ///section for custom stuff diff --git a/TransferWindowPlanner/TWPWindowWorkers.cs b/TransferWindowPlanner/TWPWindowWorkers.cs index 8ed78f5..ddbb22b 100644 --- a/TransferWindowPlanner/TWPWindowWorkers.cs +++ b/TransferWindowPlanner/TWPWindowWorkers.cs @@ -67,16 +67,16 @@ void SetupTransferParams() private void SetWindowStrings() { - strOrigin = cbOrigin.bodyName; - strDestination = cbDestination.bodyName; + //KSPDateTime kTime = new KSPDateTime(DepartureMin); + //strDepartureMinYear = (kTime.Year ).ToString(); + //strDepartureMinDay = (kTime.Day).ToString(); - KSPDateTime kTime = new KSPDateTime(DepartureMin); - strDepartureMinYear = (kTime.Year ).ToString(); - strDepartureMinDay = (kTime.Day).ToString(); + //kTime.UT = DepartureMax; + //strDepartureMaxYear = (kTime.Year).ToString(); + //strDepartureMaxDay = (kTime.Day).ToString(); - kTime.UT = DepartureMax; - strDepartureMaxYear = (kTime.Year).ToString(); - strDepartureMaxDay = (kTime.Day).ToString(); + dateMinDeparture = new KSPDateTime(DepartureMin); + dateMaxDeparture = new KSPDateTime(DepartureMax); KSPTimeSpan kSpan = new KSPTimeSpan(0); kSpan.UT = TravelMin; @@ -115,8 +115,8 @@ public class TransferWorkerDetails private void SetWorkerVariables() { - DepartureMin = new KSPDateTime(strDepartureMinYear, strDepartureMinDay).UT; // new KSPDateTime(strDepartureMinYear, strDepartureMinDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; - DepartureMax = new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay).UT; // new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; + DepartureMin = dateMinDeparture.UT; // new KSPDateTime(strDepartureMinYear, strDepartureMinDay).UT; // new KSPDateTime(strDepartureMinYear, strDepartureMinDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; + DepartureMax = dateMaxDeparture.UT; // new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay).UT; // new KSPDateTime(strDepartureMaxYear, strDepartureMaxDay, "0", "0", "0").UT - KSPDateStructure.SecondsPerYear - KSPDateStructure.SecondsPerDay; DepartureRange = DepartureMax - DepartureMin; DepartureSelected = -1; TravelMin = new KSPTimeSpan(strTravelMinDays, "0", "0", "0").UT; From 45f45926972142d5af1b02f014cb6d49c3b296ef Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Wed, 24 Dec 2014 08:51:31 +1100 Subject: [PATCH 36/40] Finalised DateTime additions (Fixes #24) --- Images/PNG/img_buttonCalendar.png | Bin 0 -> 319 bytes .../FrameworkExt/KSPDateTime.cs | 1 - TransferWindowPlanner/Resources.cs | 2 + TransferWindowPlanner/Settings.cs | 3 ++ TransferWindowPlanner/StylesAndSkins.cs | 6 ++- TransferWindowPlanner/TWP.cs | 6 +++ TransferWindowPlanner/TWPWindow.cs | 42 +++++++++++++++--- TransferWindowPlanner/TWPWindowDebug.cs | 7 +-- TransferWindowPlanner/TWPWindowSettings.cs | 14 +++++- 9 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 Images/PNG/img_buttonCalendar.png diff --git a/Images/PNG/img_buttonCalendar.png b/Images/PNG/img_buttonCalendar.png new file mode 100644 index 0000000000000000000000000000000000000000..a7220ba693c9acb5ab2a4c75a1d77aa2efa4b218 GIT binary patch literal 319 zcmeAS@N?(olHy`uVBq!ia0vp^fNn{1`ISV`@iy0XB4ude`@%$AjKtYKT*NBqf{Irtt#G+J&g2c?c61}|C5(N`I z13lxOXVU|KYBqSfIEGmG-<^1n^N@pxOTDJjwFL2(hnv|qvic|KH%-`_z^K&pZGkzz zS_Q|8GoS2z#EJrxI=K5~)6AUzt;;Q|PUX`oU^o8Wq?&Oc%EsH9H=y~@!fS6BU)Uc! zus7_agbyQsk4#pTW6B-z1g5sAB4(b?ljhvnpV7&t?QVNEDlYtF^b^%Tp;_nF>j!tG ziCOi2n>tDN0lRuZneVzqGh3hil$TpReUkmI$;!R_7Bgn a.assembly.GetExportedTypes()) + .SelectMany(t => t) + .Any(t => t.FullName.ToLower().EndsWith(".realsolarsystem"))) + settings.RSSActive = true; + //Init the KAC Integration KACWrapper.InitKACWrapper(); if (KACWrapper.APIReady) diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index c0b14a2..6e9de0c 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -144,13 +144,16 @@ internal override void OnGUIOnceOnly() // return blnReturn; //} - internal static Boolean DrawTextField(ref String Value, String RegexValidator, Boolean RegexFailOnMatch, String LabelText = "", Int32 FieldWidth = 0, Int32 LabelWidth = 0) + internal static Boolean DrawTextField(ref String Value, String RegexValidator, Boolean RegexFailOnMatch, String LabelText = "", Int32 FieldWidth = 0, Int32 LabelWidth = 0, Boolean Locked = false) { GUIStyle styleTextBox = Styles.styleTextField; - if ((RegexFailOnMatch && System.Text.RegularExpressions.Regex.IsMatch(Value, RegexValidator, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) || + if (Locked) + styleTextBox = Styles.styleTextFieldLocked; + else if ((RegexFailOnMatch && System.Text.RegularExpressions.Regex.IsMatch(Value, RegexValidator, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) || (!RegexFailOnMatch && !System.Text.RegularExpressions.Regex.IsMatch(Value, RegexValidator, System.Text.RegularExpressions.RegexOptions.IgnoreCase))) styleTextBox = Styles.styleTextFieldError; + if(LabelText!="") { if (LabelWidth==0) GUILayout.Label(LabelText, Styles.styleTextFieldLabel); @@ -158,11 +161,15 @@ internal static Boolean DrawTextField(ref String Value, String RegexValidator, B GUILayout.Label(LabelText, Styles.styleTextFieldLabel, GUILayout.Width(LabelWidth)); } + + String textValue = Value; Boolean blnReturn = false; if (FieldWidth == 0) - blnReturn = DrawTextBox(ref Value, styleTextBox); + blnReturn = DrawTextBox(ref textValue, styleTextBox); else - blnReturn = DrawTextBox(ref Value, styleTextBox, GUILayout.Width(FieldWidth)); + blnReturn = DrawTextBox(ref textValue, styleTextBox, GUILayout.Width(FieldWidth)); + + if (!Locked) Value = textValue; return blnReturn; } @@ -230,6 +237,27 @@ internal static Boolean DrawYearMonthDay(ref String strYear, ref String strMonth internal override void DrawWindow(int id) { + //Calendar toggle + if (settings.ShowCalendarToggle || settings.RSSActive) + { + if (GUI.Button(new Rect(WindowRect.width - 122, 2, 30, 20), new GUIContent(Resources.btnCalendar, "Toggle Calendar"), "ButtonSettings")) + { + if (settings.SelectedCalendar == CalendarTypeEnum.Earth) + { + settings.SelectedCalendar = CalendarTypeEnum.KSPStock; + KSPDateStructure.SetKSPStockCalendar(); + } + else + { + settings.SelectedCalendar = CalendarTypeEnum.Earth; + KSPDateStructure.SetEarthCalendar(settings.EarthEpoch.Split('-')[0].ToInt32(), + settings.EarthEpoch.Split('-')[1].ToInt32(), + settings.EarthEpoch.Split('-')[2].ToInt32()); + } + settings.Save(); + } + } + //Settings toggle GUIContent contSettings = new GUIContent(Resources.GetSettingsButtonIcon(TransferWindowPlanner.settings.VersionAttentionFlag), "Settings..."); if (TransferWindowPlanner.settings.VersionAvailable) contSettings.tooltip = "Updated Version Available - Settings..."; @@ -634,6 +662,7 @@ private void DrawSingleInstruction(String Num, String Text) GUILayout.EndHorizontal(); } + Boolean blnFlyby = false; private void DrawTransferEntry() { GUILayout.Label("Enter Parameters", Styles.styleTextYellowBold); @@ -644,6 +673,7 @@ private void DrawTransferEntry() GUILayout.Label("Initial Orbit:", Styles.styleTextFieldLabel); GUILayout.Label("Destination:", Styles.styleTextFieldLabel); GUILayout.Label("Final Orbit:", Styles.styleTextFieldLabel); + GUILayout.Label("", Styles.styleTextFieldLabel); //Checkbox re insertion burn GUILayout.Label("Earliest Departure:", Styles.styleTextFieldLabel); @@ -664,10 +694,12 @@ private void DrawTransferEntry() ddlDestination.DrawButton(); GUILayout.BeginHorizontal(); - DrawTextField(ref strArrivalAltitude, "[^\\d\\.]+", true, FieldWidth: 172); + DrawTextField(ref strArrivalAltitude, "[^\\d\\.]+", true, FieldWidth: 172, Locked:blnFlyby); GUILayout.Label("km", GUILayout.Width(20)); GUILayout.EndHorizontal(); + DrawToggle(ref blnFlyby, new GUIContent("No Insertion Burn (eg. fly-by)"), Styles.styleToggle); + DrawYearDay(ref dateMinDeparture); DrawYearDay(ref dateMaxDeparture); diff --git a/TransferWindowPlanner/TWPWindowDebug.cs b/TransferWindowPlanner/TWPWindowDebug.cs index 5634723..2fd1d4d 100644 --- a/TransferWindowPlanner/TWPWindowDebug.cs +++ b/TransferWindowPlanner/TWPWindowDebug.cs @@ -174,7 +174,8 @@ internal override void DrawWindow(int id) if (GUILayout.Button("NewTransfer")) { transTemp = new TransferDetails(); - LambertSolver.TransferDeltaV(mbTWP.windowMain.TransferSelected.Origin, mbTWP.windowMain.TransferSelected.Destination, intTest5, mbTWP.windowMain.TransferSelected.TravelTime, FlightGlobals.ActiveVessel.orbit.getRelativePositionAtUT(intTest5).magnitude - FlightGlobals.ActiveVessel.orbit.referenceBody.Radius, mbTWP.windowMain.TransferSpecs.FinalOrbitAltitude, out transTemp); + LambertSolver.TransferDeltaV(mbTWP.windowMain.TransferSelected.Origin, mbTWP.windowMain.TransferSelected.Destination, + intTest5, mbTWP.windowMain.TransferSelected.TravelTime, FlightGlobals.ActiveVessel.orbit.getRelativePositionAtUT(intTest5).magnitude - FlightGlobals.ActiveVessel.orbit.referenceBody.Radius, mbTWP.windowMain.TransferSpecs.FinalOrbitAltitude, out transTemp); transTemp.CalcEjectionValues(); } @@ -271,9 +272,9 @@ internal override void DrawWindow(int id) //DrawLabel("AbsPos at firstUT:{0}", cbD.orbit.getPositionAtUT(intTest1)); - DrawLabel("DepartureMin:{0}", mbTWP.windowMain.dateMinDeparture.UT); + //DrawLabel("DepartureMin:{0}", mbTWP.windowMain.dateMinDeparture.UT); //DrawLabel("DepartureRange:{0}", DepartureRange); - DrawLabel("DepartureMax:{0}", mbTWP.windowMain.dateMaxDeparture.UT); + //DrawLabel("DepartureMax:{0}", mbTWP.windowMain.dateMaxDeparture.UT); //DrawLabel("TravelMin:{0}", TravelMin); //DrawLabel("TravelMax:{0}", TravelMax); diff --git a/TransferWindowPlanner/TWPWindowSettings.cs b/TransferWindowPlanner/TWPWindowSettings.cs index 1557190..ab11048 100644 --- a/TransferWindowPlanner/TWPWindowSettings.cs +++ b/TransferWindowPlanner/TWPWindowSettings.cs @@ -34,7 +34,7 @@ internal enum SettingsTabs //[Description("Styling/Visuals")] Styling, [Description("About...")] About, } - + internal override void Awake() { //WindowRect = new Rect(mbTWP.windowMain.WindowRect.x + mbTWP.windowMain.WindowRect.width, mbTWP.windowMain.WindowRect.y, 300, 200); @@ -50,6 +50,8 @@ internal override void Awake() ddlSettingsButtonStyle = new DropDownList(EnumExtensions.ToEnumDescriptions(), (Int32)settings.ButtonStyleChosen, this); ddlSettingsButtonStyle.OnSelectionChanged += ddlSettingsButtonStyle_OnSelectionChanged; ddlSettingsCalendar = new DropDownList(EnumExtensions.ToEnumDescriptions(), this); + //NOTE:Pull out the custom option for now + ddlSettingsCalendar.Items.Remove(CalendarTypeEnum.Custom.ToString()); ddlSettingsCalendar.OnSelectionChanged += ddlSettingsCalendar_OnSelectionChanged; ddlManager.AddDDL(ddlSettingsCalendar); @@ -293,7 +295,7 @@ private void DrawWindow_Alarm() private void DrawWindow_Calendar() { //Update Check Area - GUILayout.Label("Selected Calendar", Styles.styleTextHeading); + GUILayout.Label("General Settings", Styles.styleTextHeading); GUILayout.BeginHorizontal(Styles.styleSettingsArea); @@ -306,9 +308,15 @@ private void DrawWindow_Calendar() ddlSettingsCalendar.DrawButton(); GUILayout.EndVertical(); GUILayout.EndHorizontal(); + if (DrawToggle(ref settings.ShowCalendarToggle, "Show Calendar Toggle in Main Window", Styles.styleToggle)) + settings.Save(); + if (settings.SelectedCalendar == CalendarTypeEnum.Earth) { + GUILayout.Label("Earth Settings", Styles.styleTextHeading); + GUILayout.BeginVertical(Styles.styleSettingsArea); + GUILayout.BeginHorizontal(); GUILayout.Label("Earth Epoch:"); @@ -341,6 +349,8 @@ private void DrawWindow_Calendar() settings.Save(); } GUILayout.EndHorizontal(); + + GUILayout.EndVertical(); } //if RSS not installed and RSS chosen... From 7e201c7a71dc63aa4f339085dfc8ded9f8626969 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Wed, 24 Dec 2014 09:18:40 +1100 Subject: [PATCH 37/40] Version 1.2.0.0 ready --- PluginFiles/ReadMe-TransferWindowPlanner.txt | 9 +++++++++ TransferWindowPlanner/Properties/AssemblyInfo.cs | 4 ++-- TransferWindowPlanner/TWPWindow.cs | 2 ++ TransferWindowPlanner/TWPWindowSettings.cs | 2 +- TransferWindowPlanner/TWPWindowWorkers.cs | 5 ++++- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/PluginFiles/ReadMe-TransferWindowPlanner.txt b/PluginFiles/ReadMe-TransferWindowPlanner.txt index ee8ed7b..c17422a 100644 --- a/PluginFiles/ReadMe-TransferWindowPlanner.txt +++ b/PluginFiles/ReadMe-TransferWindowPlanner.txt @@ -27,6 +27,15 @@ LICENSE This work is licensed under an MIT license as outlined at the OSI site. Visit the documentation site for more details and Attribution VERSION HISTORY +Version 1.2.0.0 - KSP Version: 0.90 +- Reworked the storage of input values to remove issues (Issue #25) +- Redid Input Locks to resolve interface lockups (Issue #27) +- Added Calendar options so RSS can show Earth Dates for things +- Added Fly-By checkbox +- Restructured Zip File +- Bunch of other small stuff +- Built and included KSPDateTime Library (Issue #24) + Version 1.1.3.0 - KSP Version: 0.90 - Recompiled for 0.90 and checked code stuff - Fixed some issues with KAC Integration (Issue #23) diff --git a/TransferWindowPlanner/Properties/AssemblyInfo.cs b/TransferWindowPlanner/Properties/AssemblyInfo.cs index f0934a1..869ebd7 100644 --- a/TransferWindowPlanner/Properties/AssemblyInfo.cs +++ b/TransferWindowPlanner/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.3.0")] -[assembly: AssemblyFileVersion("1.1.3.0")] +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")] diff --git a/TransferWindowPlanner/TWPWindow.cs b/TransferWindowPlanner/TWPWindow.cs index c4248c6..dc28028 100644 --- a/TransferWindowPlanner/TWPWindow.cs +++ b/TransferWindowPlanner/TWPWindow.cs @@ -713,6 +713,8 @@ private void DrawTransferEntry() DrawToggle(ref blnFlyby, new GUIContent("No Insertion Burn (eg. fly-by)"), Styles.styleToggle); + GUILayout.Space(8); + DrawYearDay(ref dateMinDeparture); DrawYearDay(ref dateMaxDeparture); diff --git a/TransferWindowPlanner/TWPWindowSettings.cs b/TransferWindowPlanner/TWPWindowSettings.cs index ab11048..72582ab 100644 --- a/TransferWindowPlanner/TWPWindowSettings.cs +++ b/TransferWindowPlanner/TWPWindowSettings.cs @@ -168,7 +168,7 @@ internal override void DrawWindow(int id) break; case SettingsTabs.Calendar: DrawWindow_Calendar(); - WindowHeight = mbTWP.windowDebug.intTest1; + WindowHeight = 206; break; case SettingsTabs.About: DrawWindow_About(); diff --git a/TransferWindowPlanner/TWPWindowWorkers.cs b/TransferWindowPlanner/TWPWindowWorkers.cs index ddbb22b..20a023f 100644 --- a/TransferWindowPlanner/TWPWindowWorkers.cs +++ b/TransferWindowPlanner/TWPWindowWorkers.cs @@ -123,8 +123,11 @@ private void SetWorkerVariables() TravelMax = new KSPTimeSpan(strTravelMaxDays, "0", "0", "0").UT; TravelRange = TravelMax - TravelMin; TravelSelected = -1; - FinalOrbitAltitude = Convert.ToDouble(strArrivalAltitude) * 1000; InitialOrbitAltitude = Convert.ToDouble(strDepartureAltitude) * 1000; + if (blnFlyby) + FinalOrbitAltitude = 0; + else + FinalOrbitAltitude = Convert.ToDouble(strArrivalAltitude) * 1000; //Store the transfer Specs for display purposes TransferSpecs = new TransferWorkerDetails(); From ca94e35c29ee5e0b006e1e1a107b11514cf5de82 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Wed, 24 Dec 2014 10:39:38 +1100 Subject: [PATCH 38/40] Made Toolbar Texture relative (Fixes #28) --- TransferWindowPlanner/Resources.cs | 2 +- TransferWindowPlanner/TWP.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/TransferWindowPlanner/Resources.cs b/TransferWindowPlanner/Resources.cs index 2746208..ce7f298 100644 --- a/TransferWindowPlanner/Resources.cs +++ b/TransferWindowPlanner/Resources.cs @@ -13,7 +13,7 @@ namespace TransferWindowPlanner { internal class Resources { - internal static String PathPlugin = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + internal static String PathPlugin = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Replace("\\", "/"); internal static String PathPluginToolbarIcons = string.Format("{0}/ToolbarIcons", PathPlugin); internal static String PathPluginTextures = string.Format("{0}/Textures", PathPlugin); //internal static String PathPluginData = string.Format("{0}/Data", PathPlugin); diff --git a/TransferWindowPlanner/TWP.cs b/TransferWindowPlanner/TWP.cs index deebe36..50c63b3 100644 --- a/TransferWindowPlanner/TWP.cs +++ b/TransferWindowPlanner/TWP.cs @@ -182,7 +182,8 @@ private static void SetToolbarIcon(IButton btnReturn) //if (settings.ToggleOn) //btnReturn.TexturePath = "TriggerTech/KSPAlternateResourcePanel/ToolbarIcons/KSPARPa_On"; //else - btnReturn.TexturePath = "TriggerTech/TransferWindowPlanner/ToolbarIcons/TWPIcon"; + String strToolbarIcon = Resources.PathPluginToolbarIcons.Substring(Resources.PathPluginToolbarIcons.ToLower().IndexOf("/gamedata/")+10) + "/TWPIcon"; + btnReturn.TexturePath = strToolbarIcon;// "TriggerTech/TransferWindowPlanner/ToolbarIcons/TWPIcon"; } void btnReturn_OnMouseLeave(MouseLeaveEvent e) From f9e1541b438fa5002eba2b959cac5cd110762920 Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Wed, 24 Dec 2014 10:39:53 +1100 Subject: [PATCH 39/40] Version 1.2.1.0 ready --- PluginFiles/ReadMe-TransferWindowPlanner.txt | 3 ++- TransferWindowPlanner/Properties/AssemblyInfo.cs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/PluginFiles/ReadMe-TransferWindowPlanner.txt b/PluginFiles/ReadMe-TransferWindowPlanner.txt index c17422a..7d8c33a 100644 --- a/PluginFiles/ReadMe-TransferWindowPlanner.txt +++ b/PluginFiles/ReadMe-TransferWindowPlanner.txt @@ -27,12 +27,13 @@ LICENSE This work is licensed under an MIT license as outlined at the OSI site. Visit the documentation site for more details and Attribution VERSION HISTORY -Version 1.2.0.0 - KSP Version: 0.90 +Version 1.2.1.0 - KSP Version: 0.90 - Reworked the storage of input values to remove issues (Issue #25) - Redid Input Locks to resolve interface lockups (Issue #27) - Added Calendar options so RSS can show Earth Dates for things - Added Fly-By checkbox - Restructured Zip File +- Made ToolbarIcon relative (Issue #28) - Bunch of other small stuff - Built and included KSPDateTime Library (Issue #24) diff --git a/TransferWindowPlanner/Properties/AssemblyInfo.cs b/TransferWindowPlanner/Properties/AssemblyInfo.cs index 869ebd7..4e75e3a 100644 --- a/TransferWindowPlanner/Properties/AssemblyInfo.cs +++ b/TransferWindowPlanner/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.0.0")] -[assembly: AssemblyFileVersion("1.2.0.0")] +[assembly: AssemblyVersion("1.2.1.0")] +[assembly: AssemblyFileVersion("1.2.1.0")] From 1d2a68f66c152096bd2a6647f95d4b6791bc2f0d Mon Sep 17 00:00:00 2001 From: TriggerAu Date: Wed, 24 Dec 2014 11:57:54 +1100 Subject: [PATCH 40/40] Updated zip builder to drop the plugin_version name folder (Fixes #26) --- PluginBuilder/BuildKSPTWPFiles.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/PluginBuilder/BuildKSPTWPFiles.ps1 b/PluginBuilder/BuildKSPTWPFiles.ps1 index d455b07..24c55ce 100644 --- a/PluginBuilder/BuildKSPTWPFiles.ps1 +++ b/PluginBuilder/BuildKSPTWPFiles.ps1 @@ -63,7 +63,7 @@ if($ChoiceRtn -eq 0) (Get-Content "$($DestFullPath)\$($PluginName)_$($VersionString)\ReadMe-$($PluginName).txt") | ForEach-Object {$_ -replace "%VERSIONSTRING%",$VersionString} | Set-Content "$($DestFullPath)\$($PluginName)_$($VersionString)\ReadMe-$($PluginName).txt" - Move-Item "$($DestFullPath)\$($PluginName)_$($VersionString)\ReadMe-$($PluginName).txt" "$($DestFullPath)\$($PluginName)_$($VersionString)\GameData\TriggerTech\$($PluginName)\" + Move-Item "$($DestFullPath)\$($PluginName)_$($VersionString)\ReadMe-$($PluginName).txt" "$($DestFullPath)\$($PluginName)_$($VersionString)\GameData\TriggerTech\" #Copy the source files "Copying Source..." @@ -75,9 +75,9 @@ if($ChoiceRtn -eq 0) # Now Zip it up - & "$($7ZipPath)" a "$($DestFullPath)\$($PluginName)_$($VersionString).zip" "$($DestFullPath)\$($PluginName)_$($VersionString)" -xr!"info.txt" + & "$($7ZipPath)" a "$($DestFullPath)\$($PluginName)_$($VersionString).zip" "$($DestFullPath)\$($PluginName)_$($VersionString)\GameData" -xr!"info.txt" & "$($7ZipPath)" a "$($DestFullPath)\$($PluginName)_$($VersionString).zip" "$($DestFullPath)\$($PluginName)_$($VersionString)\info.txt" - & "$($7ZipPath)" a "$($DestFullPath)\$($PluginName)_$($VersionString).zip" "$($DestFullPath)\$($PluginName)_$($VersionString)\GameData\TriggerTech\ReadMe-$($PluginName).txt" + #& "$($7ZipPath)" a "$($DestFullPath)\$($PluginName)_$($VersionString).zip" "$($DestFullPath)\$($PluginName)_$($VersionString)\GameData\TriggerTech\ReadMe-$($PluginName).txt" & "$($7ZipPath)" a "$($DestFullPath)\$($PluginName)Source_$($VersionString).zip" "$($DestFullPath)\$($PluginName)Source_$($VersionString)" }