-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTCAEngineInfo.cs
180 lines (161 loc) · 5.73 KB
/
TCAEngineInfo.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
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2015 Allis Tauri
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/
// or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
using System;
using AT_Utils;
namespace ThrottleControlledAvionics
{
public enum TCARole
{
MAIN,
MANEUVER,
MANUAL,
BALANCE,
UNBALANCE
}
[Flags]
public enum ManeuverMode { ALL = TORQUE | TRANSLATION, TORQUE = 1, TRANSLATION = 1 << 1 }
public class TCAEngineInfo : PartModule
{
public static readonly OrderedDict<TCARole, string> Roles =
new OrderedDict<TCARole, string>
{
{ TCARole.MAIN, "Thrust & Maneuver" },
{ TCARole.BALANCE, "Thrust" },
{ TCARole.MANEUVER, "Maneuver" },
{ TCARole.UNBALANCE, "UnBalanced Thrust" },
{ TCARole.MANUAL, "Manual Control" }
};
public static readonly OrderedDict<ManeuverMode, string> Modes =
new OrderedDict<ManeuverMode, string>
{
{ ManeuverMode.ALL, "Rotation & Translation" },
{ ManeuverMode.TORQUE, "Rotation" },
{ ManeuverMode.TRANSLATION, "Translation" }
};
[KSPField(isPersistant = true)] public TCARole Role = TCARole.MAIN;
[KSPField(isPersistant = true)] public ManeuverMode Mode = ManeuverMode.ALL;
[UI_ChooseOption]
[KSPField(isPersistant = true,
guiActive = true,
guiActiveEditor = true,
guiName = "Engine Group:",
groupName = "TCAEngineInfo",
groupDisplayName = "TCA")]
public int group;
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
// legacy config conversion
if(node.HasValue("role")
&& int.TryParse(node.GetValue("role"), out var role))
Role = (TCARole)role;
update_events();
}
private static void setup_groups(UI_ChooseOption chooser)
{
if(chooser == null)
return;
chooser.options = new string[Globals.Instance.MaxManualGroups];
chooser.options[0] = "OFF";
for(var i = 1; i < Globals.Instance.MaxManualGroups; i++)
chooser.options[i] = $"G{i:D}";
}
public override void OnStart(StartState state)
{
var f = Fields[nameof(group)];
setup_groups(f.uiControlEditor as UI_ChooseOption);
setup_groups(f.uiControlFlight as UI_ChooseOption);
update_events();
}
public void SetGroup(int newGroup)
{
group = newGroup >= 0 ? newGroup : 0;
}
public void SetRole(TCARole role)
{
Role = role;
update_events();
}
public void SetMode(ManeuverMode mode)
{
Mode = mode;
update_events();
}
public void SetRoleAndMode(TCARole role, ManeuverMode mode)
{
Role = role;
Mode = mode;
update_events();
}
[KSPEvent(guiActive = true,
guiActiveEditor = true,
guiName = "TCA Role",
groupName = "TCAEngineInfo",
groupDisplayName = "TCA",
active = true)]
public void SwitchRole()
{
if(!HighLogic.LoadedSceneIsEditor && group > 0)
{
Utils.Message("Cannot change the role of an engine belonging to a group.\n"
+ "Use in-flight group controls instead.");
return;
}
Role = Roles.Next(Role);
update_events();
applyToCounterparts(e => e.SetRole(Role));
}
[KSPEvent(guiActive = true,
guiActiveEditor = true,
guiName = "TCA Maneuver Mode",
groupName = "TCAEngineInfo",
groupDisplayName = "TCA",
active = false)]
public void SwitchMode()
{
if(!HighLogic.LoadedSceneIsEditor && group > 0)
{
Utils.Message("Cannot change the mode of an engine belonging to a group.\n"
+ "Use in-flight group controls instead.");
return;
}
Mode = Modes.Next(Mode);
update_events();
applyToCounterparts(e => e.SetMode(Mode));
}
private void applyToCounterparts(Action<TCAEngineInfo> action)
{
if(!Globals.Instance.RoleSymmetryInFlight
&& HighLogic.LoadedSceneIsFlight)
return;
foreach(var cp in part.symmetryCounterparts)
{
var info = cp.Modules.GetModule<TCAEngineInfo>();
if(info != null
&& (HighLogic.LoadedSceneIsEditor
|| info.group == 0))
action(info);
}
}
private void update_events()
{
Events[nameof(SwitchRole)].guiName = $"Role: {Roles[Role]}";
var modeEvent = Events[nameof(SwitchMode)];
modeEvent.guiName = $"Mode: {Modes[Mode]}";
var enableModeEvent = Role == TCARole.MANEUVER;
if(modeEvent.active != enableModeEvent)
{
modeEvent.active = enableModeEvent;
part.UpdatePartMenu();
}
if(HighLogic.LoadedSceneIsEditor)
GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
}
}
}