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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | // // Kerbal Engineer Redux // // Copyright (C) 2014 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 using System.Collections.Generic; using System.Linq; using System.Xml.Serialization; using KerbalEngineer.Flight.Readouts; using UnityEngine; #endregion namespace KerbalEngineer.Flight.Sections { using Unity.Flight; /// <summary> /// Object for management and display of readout modules. /// </summary> public class SectionModule : ISectionModule { #region Fields private SectionEditor editor; private bool isHud; private int numberOfReadouts; #endregion #region Constructors /// <summary> /// Creates a new section module. /// </summary> public SectionModule() { this.FloatingPositionX = Screen.width * 0.5f - 125.0f; this.FloatingPositionY = 100.0f; this.EditorPositionX = Screen.width * 0.5f - SectionEditor.Width * 0.5f; this.EditorPositionY = Screen.height * 0.5f - SectionEditor.Height * 0.5f; this.ReadoutModules = new List<ReadoutModule>(); this.InitialiseStyles(); GuiDisplaySize.OnSizeChanged += this.OnSizeChanged; } #endregion #region Properties /// <summary> /// Gets and sets the abbreviation of the section. /// </summary> public string Abbreviation { get; set; } /// <summary> /// Gets and sets the X position of the editor window. (Only used for serialisation.) /// </summary> public float EditorPositionX { get; set; } /// <summary> /// Gets and sets the Y position of the editor window. (Only used for serialisation.) /// </summary> public float EditorPositionY { get; set; } /// <summary> /// Gets and sets the X position of the floating window. (Only used for serialisation.) /// </summary> public float FloatingPositionX { get; set; } /// <summary> /// Gets and sets the Y position of the floating window. (Only used for serialisation.) /// </summary> public float FloatingPositionY { get; set; } /// <summary> /// Gets and sets whether the section is custom. /// </summary> public bool IsCustom { get; set; } /// <summary> /// Gets and sets whether the section editor is visible. /// </summary> public bool IsEditorVisible { get { return this.editor != null; } set { if (value && this.editor == null) { this.editor = FlightEngineerCore.Instance.AddSectionEditor(this); } else if (!value && this.editor != null) { Object.Destroy(this.editor); } } } /// <summary> /// Gets and sets whether the section is in a floating state. /// </summary> public bool IsFloating { get { return this.Window != null; } set { if (value && this.Window == null) { this.Window = FlightEngineerCore.Instance.AddSectionWindow(this); } else if (!value && this.Window != null) { Object.Destroy(this.Window); } } } /// <summary> /// Gets and sets whether the section module is a HUD. /// </summary> public bool IsHud { get { return this.isHud; } set { if (this.isHud == value) { return; } this.isHud = value; if (this.isHud) { this.IsFloating = true; } if (this.Window != null) { this.Window.RequestResize(); } } } /// <summary> /// Gets and sets whether the section module has been deleted. /// </summary> public bool IsDeleted { get; set; } /// <summary> /// Gets and sets whether the section module has a background as a HUD. /// </summary> public bool IsHudBackground { get; set; } /// <summary> /// Gets and sets the visibility of the section. /// </summary> public bool IsVisible { get; set; } /// <summary> /// Gets the number of drawn readout lines. /// </summary> public int LineCount { get; private set; } /// <summary> /// Gets and sets the name of the section. /// </summary> public string Name { get; set; } /// <summary> /// Gets and sets the names of the installed readout modules. (Only used with serialisation.) /// </summary> public string[] ReadoutModuleNames { get { return this.ReadoutModules.Select(r => r.Category + "." + r.GetType().Name).ToArray(); } set { this.ReadoutModules = value.Select(ReadoutLibrary.GetReadout).ToList(); } } /// <summary> /// Gets and sets the list of readout modules. /// </summary> [XmlIgnore] public List<ReadoutModule> ReadoutModules { get; set; } /// <summary> /// Gets and sets the floating window. /// </summary> [XmlIgnore] public SectionWindow Window { get; set; } #endregion #region GUIStyles #region Fields private GUIStyle boxStyle; private GUIStyle buttonStyle; private GUIStyle messageStyle; private GUIStyle titleStyle; #endregion /// <summary> /// Initialises all the styles required for this object. /// </summary> private void InitialiseStyles() { this.boxStyle = new GUIStyle(HighLogic.Skin.box) { margin = new RectOffset(), padding = new RectOffset(5, 5, 5, 5) }; this.titleStyle = new GUIStyle(HighLogic.Skin.label) { normal = { textColor = Color.white }, margin = new RectOffset(), padding = new RectOffset(2, 0, 5, 2), fontSize = (int)(13 * GuiDisplaySize.Offset), fontStyle = FontStyle.Bold, stretchWidth = true }; this.buttonStyle = new GUIStyle(HighLogic.Skin.button) { normal = { textColor = Color.white }, margin = new RectOffset(0, 0, 5, 3), padding = new RectOffset(), fontSize = (int)(10 * GuiDisplaySize.Offset), stretchHeight = true, fixedWidth = 60.0f * GuiDisplaySize.Offset }; this.messageStyle = new GUIStyle(HighLogic.Skin.label) { normal = { textColor = Color.white }, margin = new RectOffset(), padding = new RectOffset(), alignment = TextAnchor.MiddleCenter, fontSize = (int)(12 * GuiDisplaySize.Offset), fontStyle = FontStyle.Bold, fixedWidth = 220.0f * GuiDisplaySize.Offset, fixedHeight = 20.0f * GuiDisplaySize.Offset }; } private void OnSizeChanged() { this.InitialiseStyles(); } #endregion #region Updating /// <summary> /// Updates all of the internal readout modules at fixed time intervals. /// </summary> public void FixedUpdate() { if (!this.IsVisible) { return; } foreach (var readout in this.ReadoutModules) { readout.FixedUpdate(); } } /// <summary> /// Updates all of the internal readout modules. /// </summary> public void Update() { if (!this.IsVisible) { return; } foreach (var readout in this.ReadoutModules) { readout.Update(); } if (this.numberOfReadouts != this.ReadoutModules.Count) { this.numberOfReadouts = this.ReadoutModules.Count; if (!this.IsFloating) { DisplayStack.Instance.RequestResize(); } else { this.Window.RequestResize(); } } } #endregion #region Drawing #region Methods: public /// <summary> /// Draws the section and all of the internal readout modules. /// </summary> public void Draw() { if (!this.IsVisible) { return; } if (!this.IsHud) { this.DrawSectionTitleBar(); } this.DrawReadoutModules(); } #endregion #region Methods: private /// <summary> /// Draws all the readout modules. /// </summary> private void DrawReadoutModules() { if (!this.IsHud) { GUILayout.BeginVertical(this.boxStyle); } this.LineCount = 0; if (this.ReadoutModules.Count > 0) { foreach (var readout in this.ReadoutModules) { readout.LineCountStart(); readout.Draw(this); readout.LineCountEnd(); this.LineCount += readout.LineCount; } } else { GUILayout.Label("No readouts are installed.", this.messageStyle); this.LineCount = 1; } if (!this.IsHud) { GUILayout.EndVertical(); } } /// <summary> /// Draws the section title and action buttons. /// </summary> private void DrawSectionTitleBar() { GUILayout.BeginHorizontal(); GUILayout.Label(this.Name.ToUpper(), this.titleStyle); this.IsEditorVisible = GUILayout.Toggle(this.IsEditorVisible, "EDIT", this.buttonStyle); this.IsFloating = GUILayout.Toggle(this.IsFloating, "FLOAT", this.buttonStyle); GUILayout.EndHorizontal(); } #endregion #endregion #region Public Methods public void ClearNullReadouts() { this.ReadoutModules.RemoveAll(r => r == null); } #endregion } } |