Updated simulation logic.
[VesselSimulator.git] / KerbalEngineer / Flight / ActionMenu.cs
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
// 
//     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 KerbalEngineer.Settings;
using KerbalEngineer.VesselSimulator;
 
using UnityEngine;
 
#endregion
 
namespace KerbalEngineer.Flight
{
    /// <summary>
    ///     Graphical controller for section interaction in the form of a menu system.
    /// </summary>
    public class ActionMenu : MonoBehaviour
    {
        #region Constants
 
        private const float ScrollSpeed = 2.0f;
 
        #endregion
 
        #region Fields
 
        private readonly int windowId = new Guid().GetHashCode();
 
        private bool isOpen = true;
        private int numberOfSections;
        private float scrollPercent;
        private Rect windowPosition = new Rect((Screen.width * 0.25f) - 100.0f, 0, 200.0f, 0);
 
        #endregion
 
        #region Constructors
 
        /// <summary>
        ///     Initialises object's state on creation.
        /// </summary>
        private void Start()
        {
            this.InitialiseStyles();
            this.Load();
            RenderingManager.AddToPostDrawQueue(0, this.Draw);
        }
 
        #endregion
 
        #region GUIStyles
 
        private GUIStyle boxStyle;
        private GUIStyle buttonStyle;
        private GUIStyle windowStyle;
 
        /// <summary>
        ///     Initialises all the styles required for this object.
        /// </summary>
        private void InitialiseStyles()
        {
            this.windowStyle = new GUIStyle();
 
            this.boxStyle = new GUIStyle(HighLogic.Skin.window)
            {
                margin = new RectOffset(),
                padding = new RectOffset(3, 3, 3, 3)
            };
 
            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,
            };
        }
 
        #endregion
 
        #region Drawing
 
        /// <summary>
        ///     Called to draw the menu when the UI is enabled.
        /// </summary>
        private void Draw()
        {
            if (this.numberOfSections != SectionLibrary.Instance.NumberOfSections)
            {
                this.numberOfSections = SectionLibrary.Instance.NumberOfSections;
                this.windowPosition.height = 0;
            }
            this.windowPosition = GUILayout.Window(this.windowId, this.windowPosition, this.Window, string.Empty, this.windowStyle);
            this.ScrollMechanism();
        }
 
        /// <summary>
        ///     Draws the menu window.
        /// </summary>
        private void Window(int windowId)
        {
            GUILayout.BeginVertical(this.boxStyle);
 
            this.DrawControlBarButton();
            this.DrawSections(SectionLibrary.Instance.StockSections);
            this.DrawSections(SectionLibrary.Instance.CustomSections);
            this.DrawNewButton();
 
            GUILayout.EndVertical();
 
            if (GUILayout.Button("FLIGHT ENGINEER", this.buttonStyle))
            {
                this.isOpen = !this.isOpen;
            }
        }
 
        /// <summary>
        ///     Draws and performs the control bar button action.
        /// </summary>
        private void DrawControlBarButton()
        {
            if (GUILayout.Toggle(DisplayStack.Instance.ShowControlBar, "CONTROL BAR", this.buttonStyle) != DisplayStack.Instance.ShowControlBar)
            {
                DisplayStack.Instance.ShowControlBar = !DisplayStack.Instance.ShowControlBar;
                DisplayStack.Instance.RequestResize();
            }
        }
 
        /// <summary>
        ///     Draws an action list for the supplied sections.
        /// </summary>
        private void DrawSections(IEnumerable<SectionModule> sections)
        {
            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();
            }
        }
 
        /// <summary>
        ///     Draws and performs the new section button action.
        /// </summary>
        private void DrawNewButton()
        {
            GUILayout.BeginHorizontal();
            GUI.skin = HighLogic.Skin;
            SimManager.minSimTime = (long)GUILayout.HorizontalSlider(SimManager.minSimTime, 0, 1000.0f);
            GUI.skin = null;
 
            if (GUILayout.Button("NEW", this.buttonStyle, GUILayout.Width(50.0f)))
            {
                SectionLibrary.Instance.CustomSections.Add(new SectionModule
                {
                    Name = "Custom " + (SectionLibrary.Instance.CustomSections.Count + 1),
                    Abbreviation = "CUST " + (SectionLibrary.Instance.CustomSections.Count + 1),
                    IsVisible = true,
                    IsCustom = true,
                    IsEditorVisible = true
                });
            }
            GUILayout.EndHorizontal();
        }
 
        /// <summary>
        ///     Controls the dynamics of the scrolling mechanism.
        /// </summary>
        private void ScrollMechanism()
        {
            if (this.isOpen && this.windowPosition.y != 0)
            {
                this.scrollPercent += Time.deltaTime * ScrollSpeed;
                this.windowPosition.y = Mathf.Lerp(this.windowPosition.y, 0, this.scrollPercent);
            }
            else if (!this.isOpen && this.windowPosition.y != 20.0f - this.windowPosition.height)
            {
                this.scrollPercent += Time.deltaTime * ScrollSpeed;
                this.windowPosition.y = Mathf.Lerp(this.windowPosition.y, 20.0f - this.windowPosition.height, this.scrollPercent);
            }
            else
            {
                this.scrollPercent = 0;
            }
        }
 
        #endregion
 
        #region Destruction
 
        /// <summary>
        ///     Runs when the object is destroyed.
        /// </summary>
        private void OnDestroy()
        {
            this.Save();
 
            RenderingManager.RemoveFromPostDrawQueue(0, this.Draw);
        }
 
        #endregion
 
        #region Saving and Loading
 
        /// <summary>
        ///     Saves the menu's state.
        /// </summary>
        private void Save()
        {
            var handler = new SettingHandler();
            handler.Set("isOpen", this.isOpen);
            handler.Set("windowPositionY", this.windowPosition.y);
            handler.Set("windowPositionHeight", this.windowPosition.height);
            handler.Save("ActionMenu.xml");
        }
 
        /// <summary>
        ///     Loads the menu's state.
        /// </summary>
        private void Load()
        {
            var handler = SettingHandler.Load("ActionMenu.xml");
            handler.Get("isOpen", ref this.isOpen);
            this.windowPosition.y = handler.Get("windowPositionY", this.windowPosition.y);
            this.windowPosition.height = handler.Get("windowPositionHeight", this.windowPosition.height);
        }
 
        #endregion
    }
}