1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | // // Kerbal Engineer Redux // // Copyright (C) 2015 CYBUTEK // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. // #region Using Directives #endregion namespace KerbalEngineer.Flight.Sections { using System.Collections.Generic; using System.Linq; using Readouts; using Settings; using UnityEngine; public static class SectionLibrary { #region Constructors /// <summary> /// Sets up and populates the library with the stock sections on creation. /// </summary> static SectionLibrary() { StockSections = new List<SectionModule>(); CustomSections = new List<SectionModule>(); StockSections.Add(new SectionModule { Name = "ORBITAL", Abbreviation = "ORBT", ReadoutModules = ReadoutLibrary.GetCategory(ReadoutCategory.GetCategory("Orbital")).Where(r => r.IsDefault).ToList() }); StockSections.Add(new SectionModule { Name = "SURFACE", Abbreviation = "SURF", ReadoutModules = ReadoutLibrary.GetCategory(ReadoutCategory.GetCategory("Surface")).Where(r => r.IsDefault).ToList() }); StockSections.Add(new SectionModule { Name = "VESSEL", Abbreviation = "VESL", ReadoutModules = ReadoutLibrary.GetCategory(ReadoutCategory.GetCategory("Vessel")).Where(r => r.IsDefault).ToList() }); StockSections.Add(new SectionModule { Name = "RENDEZVOUS", Abbreviation = "RDZV", ReadoutModules = ReadoutLibrary.GetCategory(ReadoutCategory.GetCategory("Rendezvous")).Where(r => r.IsDefault).ToList() }); CustomSections.Add(new SectionModule { Name = "THERMAL", Abbreviation = "HEAT", ReadoutModules = ReadoutLibrary.GetCategory(ReadoutCategory.GetCategory("Thermal")).Where(r => r.IsDefault).ToList() }); SectionModule hud1 = new SectionModule { Name = "HUD 1", Abbreviation = "HUD 1", IsCustom = true, IsVisible = true, ReadoutModules = new List<ReadoutModule> { ReadoutLibrary.GetReadout("ApoapsisHeight"), ReadoutLibrary.GetReadout("TimeToApoapsis"), ReadoutLibrary.GetReadout("PeriapsisHeight"), ReadoutLibrary.GetReadout("TimeToPeriapsis") }, }; hud1.FloatingPositionX = Screen.width * 0.25f - (hud1.ReadoutModules.First().ContentWidth * 0.5f); hud1.FloatingPositionY = 0.0f; hud1.IsHud = true; CustomSections.Add(hud1); SectionModule hud2 = new SectionModule { Name = "HUD 2", Abbreviation = "HUD 2", IsCustom = true, IsVisible = true, ReadoutModules = new List<ReadoutModule> { ReadoutLibrary.GetReadout("AltitudeTerrain"), ReadoutLibrary.GetReadout("VerticalSpeed"), ReadoutLibrary.GetReadout("HorizontalSpeed"), ReadoutLibrary.GetReadout("Biome"), ReadoutLibrary.GetReadout("MachNumber") }, }; hud2.FloatingPositionX = Screen.width * 0.75f - (hud2.ReadoutModules.First().ContentWidth * 0.5f); hud2.FloatingPositionY = 0.0f; hud2.IsHud = true; CustomSections.Add(hud2); } #endregion #region Properties /// <summary> /// Gets and sets a list of custom sections. /// </summary> public static List<SectionModule> CustomSections { get; set; } /// <summary> /// Gets the number of total sections that are stored in the library. /// </summary> public static int NumberOfSections { get; private set; } /// <summary> /// Gets the number of sections that are being drawn on the display stack. /// </summary> public static int NumberOfStackSections { get; private set; } /// <summary> /// Gets and sets a list of stock sections /// </summary> public static List<SectionModule> StockSections { get; set; } #endregion #region Updating #region Methods: public /// <summary> /// Fixed update all of the sections. /// </summary> public static void FixedUpdate() { FixedUpdateSections(StockSections); FixedUpdateSections(CustomSections); } /// <summary> /// Update all of the sections and process section counts. /// </summary> public static void Update() { NumberOfStackSections = 0; NumberOfSections = 0; UpdateSections(StockSections); UpdateSections(CustomSections); } #endregion #region Methods: private /// <summary> /// Fixed updates a list of sections. /// </summary> private static void FixedUpdateSections(IEnumerable<SectionModule> sections) { foreach (SectionModule section in sections) { if (section.IsVisible) { section.FixedUpdate(); } } } /// <summary> /// Updates a list of sections and increments the section counts. /// </summary> private static void UpdateSections(IEnumerable<SectionModule> sections) { foreach (SectionModule section in sections) { if (section.IsVisible) { if (!section.IsFloating) { foreach (ReadoutModule readout in section.ReadoutModules) { if (readout.ResizeRequested) { DisplayStack.Instance.RequestResize(); readout.ResizeRequested = false; } } NumberOfStackSections++; } else { foreach (ReadoutModule readout in section.ReadoutModules) { if (readout.ResizeRequested) { section.Window.RequestResize(); readout.ResizeRequested = false; } } } section.Update(); } NumberOfSections++; } } #endregion #endregion #region Saving and Loading /// <summary> /// Loads the state of all stored sections. /// </summary> public static void Load() { if (!SettingHandler.Exists("SectionLibrary.xml")) { return; } GetAllSections().ForEach(s => { if (s.Window != null) { Object.Destroy(s.Window); } }); SettingHandler handler = SettingHandler.Load("SectionLibrary.xml", new[] { typeof(List<SectionModule>) }); StockSections = handler.Get("StockSections", StockSections); CustomSections = handler.Get("CustomSections", CustomSections); foreach (SectionModule section in GetAllSections()) { section.ClearNullReadouts(); } } /// <summary> /// Saves the state of all the stored sections. /// </summary> public static void Save() { SettingHandler handler = new SettingHandler(); handler.Set("StockSections", StockSections); handler.Set("CustomSections", CustomSections); handler.Save("SectionLibrary.xml"); } #endregion #region Methods /// <summary> /// Gets a list containing all section modules. /// </summary> public static List<SectionModule> GetAllSections() { List<SectionModule> sections = new List<SectionModule>(); sections.AddRange(StockSections); sections.AddRange(CustomSections); return sections; } /// <summary> /// Gets a custom section that has the specified name. /// </summary> public static SectionModule GetCustomSection(string name) { return CustomSections.FirstOrDefault(s => s.Name == name); } /// <summary> /// Gets a section that has the specified name. /// </summary> public static SectionModule GetSection(string name) { return GetStockSection(name) ?? GetCustomSection(name); } /// <summary> /// Gets a stock section that has the specified name. /// </summary> public static SectionModule GetStockSection(string name) { return StockSections.FirstOrDefault(s => s.Name == name); } /// <summary> /// Removes a custom section witht he specified name. /// </summary> public static bool RemoveCustomSection(string name) { return CustomSections.Remove(GetCustomSection(name)); } /// <summary> /// Removes a section with the specified name. /// </summary> public static bool RemoveSection(string name) { return RemoveStockSection(name) || RemoveCustomSection(name); } /// <summary> /// Removes a stock section with the specified name. /// </summary> public static bool RemoveStockSection(string name) { return StockSections.Remove(GetStockSection(name)); } #endregion } } |