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 | // // 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; using System.Collections.Generic; using KerbalEngineer.Flight.Sections; using UnityEngine; #endregion namespace KerbalEngineer.Flight { public class ActionMenuGui : MonoBehaviour { #region Fields private int numberOfSections; private Rect position = new Rect(Screen.width, 38.0f, 300.0f, 0); #endregion #region Properties public bool StayOpen { get; set; } public bool Hovering { get; set; } public bool Hidden { get; set; } #endregion #region Initialisation private void Awake() { try { this.enabled = false; Logger.Log("ActionMenuGui was created."); } catch (Exception ex) { Logger.Exception(ex); } } private void Start() { try { this.InitialiseStyles(); } catch (Exception ex) { Logger.Exception(ex); } } #endregion #region GUIStyles private GUIStyle buttonStyle; private GUIStyle windowStyle; /// <summary> /// Initialises all the styles required for this object. /// </summary> private void InitialiseStyles() { try { this.windowStyle = new GUIStyle { border = new RectOffset(10, 0, 20, 10), margin = new RectOffset(0, 0, 3, 0), padding = new RectOffset(5, 5, 26, 5), normal = { background = GameDatabase.Instance.GetTexture("KerbalEngineer/Textures/ToolbarBackground", false) } }; this.buttonStyle = new GUIStyle(HighLogic.Skin.button) { normal = { textColor = Color.white, }, margin = new RectOffset(), padding = new RectOffset(), alignment = TextAnchor.MiddleCenter, fontSize = 11, fontStyle = FontStyle.Bold, fixedHeight = 20.0f, }; } catch (Exception ex) { Logger.Exception(ex, "ActionMenu->InitialiseStyles"); } } #endregion #region Drawing /// <summary> /// Called to draw the menu when the UI is enabled. /// </summary> private void OnGUI() { try { if (this.Hidden || !FlightEngineerCore.IsDisplayable) { return; } if (!this.position.Contains(Event.current.mousePosition) && !this.StayOpen && !this.Hovering) { this.enabled = false; return; } if (this.numberOfSections < SectionLibrary.NumberOfSections) { this.numberOfSections = SectionLibrary.NumberOfSections; } else if (this.numberOfSections > SectionLibrary.NumberOfSections) { this.numberOfSections = SectionLibrary.NumberOfSections; this.position.height = 0; } GUI.skin = null; this.position.x = Mathf.Clamp(Screen.width * 0.5f + this.transform.parent.position.x - 19.0f, Screen.width * 0.5f, Screen.width - this.position.width); this.position = GUILayout.Window(this.GetInstanceID(), this.position, this.Window, string.Empty, this.windowStyle); } catch (Exception ex) { Logger.Exception(ex); } } /// <summary> /// Draws the menu window. /// </summary> private void Window(int windowId) { try { GUILayout.BeginVertical(); this.DrawControlBarButton(); GUILayout.Space(5.0f); this.DrawSections(SectionLibrary.StockSections); this.DrawSections(SectionLibrary.CustomSections); GUILayout.Space(5.0f); this.DrawNewButton(); GUILayout.EndVertical(); } catch (Exception ex) { Logger.Exception(ex); } } /// <summary> /// Draws and performs the control bar button action. /// </summary> private void DrawControlBarButton() { try { GUILayout.BeginHorizontal(); DisplayStack.Instance.Hidden = !GUILayout.Toggle(!DisplayStack.Instance.Hidden, "SHOW ENGINEER", this.buttonStyle); if (GUILayout.Toggle(DisplayStack.Instance.ShowControlBar, "CONTROL BAR", this.buttonStyle) != DisplayStack.Instance.ShowControlBar) { DisplayStack.Instance.ShowControlBar = !DisplayStack.Instance.ShowControlBar; DisplayStack.Instance.RequestResize(); } GUILayout.EndHorizontal(); } catch (Exception ex) { Logger.Exception(ex); } } /// <summary> /// Draws an action list for the supplied sections. /// </summary> private void DrawSections(IEnumerable<SectionModule> sections) { try { foreach (var section in sections) { GUILayout.BeginHorizontal(); section.IsVisible = GUILayout.Toggle(section.IsVisible, section.Name.ToUpper(), this.buttonStyle); section.IsEditorVisible = GUILayout.Toggle(section.IsEditorVisible, "EDIT", this.buttonStyle, GUILayout.Width(50.0f)); GUILayout.EndHorizontal(); } } catch (Exception ex) { Logger.Exception(ex); } } /// <summary> /// Draws and performs the new section button action. /// </summary> private void DrawNewButton() { try { GUILayout.BeginHorizontal(); if (GUILayout.Button("NEW CUSTOM SECTION", this.buttonStyle)) { SectionLibrary.CustomSections.Add(new SectionModule { Name = "Custom " + (SectionLibrary.CustomSections.Count + 1), Abbreviation = "CUST " + (SectionLibrary.CustomSections.Count + 1), IsVisible = true, IsCustom = true, IsEditorVisible = true }); } GUILayout.EndHorizontal(); } catch (Exception ex) { Logger.Exception(ex); } } #endregion #region Destruction private void OnDestroy() { try { Logger.Log("ActionMenuGui was destroyed."); } catch (Exception ex) { Logger.Exception(ex); } } #endregion } } |