-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRnDButton.cs
248 lines (219 loc) · 8.99 KB
/
RnDButton.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
using UnityEngine;
using System.Collections.Generic;
using KSP.UI;
using KSP.UI.Screens;
using UnityEngine.UI;
namespace SciencePlus
{
[KSPAddon(KSPAddon.Startup.SpaceCentre, true)]
class ConditionalResearchButtonSetup : MonoBehaviour
{
private void Start()
{
RDController.OnRDTreeSpawn.Add(i => AddButton(i));
Destroy(gameObject);
}
private static void AddButton(RDController instance)
{
instance.actionButton.gameObject.AddComponent<ConditionalResearchButton>();
}
}
class ConditionalResearchButton : MonoBehaviour
{
public class Request
{
public RDNode node;
public bool allowed;
public void Deny() { allowed = false; }
}
public static EventData<Request> OnResearchButtonPresented = new EventData<ConditionalResearchButton.Request>("OnResearchButtonPresented");
private Image _buttonImage;
private Button _button;
private Button.ButtonClickedEvent _originalEvent;
private void Awake()
{
_buttonImage = GetComponent<Image>();
_button = GetComponent<Button>();
_originalEvent = _button.onClick;
_button.onClick = new Button.ButtonClickedEvent();
_button.onClick.AddListener(OnButtonClicked);
GetComponent<UIStateButton>().onValueChanged.AddListener(_ => UpdateButtonStatus());
}
private void OnEnable()
{
UpdateButtonStatus();
}
private void UpdateButtonStatus()
{
ToggleButton(!IsNodeResearchable() || AllowResearch());
}
private void ToggleButton(bool enable)
{
_button.enabled = enable;
_buttonImage.color = enable ? Color.white : Color.clear;
}
private void OnButtonClicked()
{
if (IsNodeResearchable())
{
List<string> sciTypes = new List<string>();
ConfigNode TechNode = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/ModuleManager.TechTree");
TechNode = TechNode.GetNode("TechTree");
foreach (ConfigNode RDNode in TechNode.GetNodes("RDNode"))
{
if (RDNode.GetValue("id") == RDController.Instance.node_selected.tech.techID)
{
string[] sciTypesArray = RDNode.GetValues("scitype");
foreach (string scitype in sciTypesArray)
{
sciTypes.Add(scitype);
}
}
}
string sciTypesDialog = "";
foreach (string scitype in sciTypes)
{
sciTypesDialog = sciTypesDialog + "\n" + scitype + " Science";
}
PopupDialog.SpawnPopupDialog(
new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f),
new MultiOptionDialog(
"ConfirmResearchDialog", $"Are you sure you want to purchase {RDController.Instance.node_selected.tech.title}? This node requires: ",
"Science+",
UISkinManager.GetSkin("KSP window 2"),
new DialogGUILabel(sciTypesDialog),
new DialogGUIFlexibleSpace(),
new DialogGUIButton("Yes", UserConfirmedPurchase, true),
new DialogGUIButton("No", () => { }, true)),
false,
UISkinManager.GetSkin("KSP window 2"));
}
else
{
_originalEvent.Invoke();
}
}
private void UserConfirmedPurchase()
{
_originalEvent.Invoke();
}
private bool IsNodeResearchable()
{
var node = RDController.Instance.node_selected;
return node != null && node.state == RDNode.State.RESEARCHABLE;
}
private bool AllowResearch()
{
var request = new Request { node = RDController.Instance.node_selected, allowed = true };
List<string> sciTypes = new List<string>();
ConfigNode TechNode = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/ModuleManager.TechTree");
TechNode = TechNode.GetNode("TechTree");
foreach (ConfigNode RDNode in TechNode.GetNodes("RDNode"))
{
if (RDNode.GetValue("id") == RDController.Instance.node_selected.tech.techID)
{
string[] sciTypesArray = RDNode.GetValues("scitype");
foreach (string scitype in sciTypesArray)
{
sciTypes.Add(scitype);
}
}
}
string sciTypesDialog = "";
foreach (string scitype in sciTypes)
{
sciTypesDialog = sciTypesDialog + ", " + scitype;
}
if (sciTypesDialog.StartsWith(", "))
{
sciTypesDialog = sciTypesDialog.TrimStart(',');
}
string sciTypesDialogButton = "Research: " + RDController.Instance.node_selected.tech.scienceCost + sciTypesDialog;
string sciTypesDialogDescription = RDController.Instance.requiresCaption.text + "Required Science: " + sciTypesDialog;
RDController.Instance.actionButtonText.text = sciTypesDialogButton;
RDController.Instance.requiresCaption.text = sciTypesDialog;
OnResearchButtonPresented.Fire(request);
return request.allowed;
}
}
[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
class RnDScienceTypes : MonoBehaviour
{
private void Start()
{
ConditionalResearchButton.OnResearchButtonPresented.Add(CanAffordScienceTypes);
GameEvents.OnTechnologyResearched.Add(SpendScienceTypes);
}
private void OnDestroy()
{
ConditionalResearchButton.OnResearchButtonPresented.Remove(CanAffordScienceTypes);
GameEvents.OnTechnologyResearched.Remove(SpendScienceTypes);
}
private void CanAffordScienceTypes(ConditionalResearchButton.Request request)
{
List<string> sciTypes = new List<string>();
ConfigNode TechNode = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/ModuleManager.TechTree");
TechNode = TechNode.GetNode("TechTree");
foreach (ConfigNode RDNode in TechNode.GetNodes("RDNode"))
{
if (RDNode.GetValue("id") == request.node.tech.techID)
{
string[] sciTypesArray = RDNode.GetValues("scitype");
foreach (string scitype in sciTypesArray)
{
sciTypes.Add(scitype);
}
}
}
bool typecheck = true;
foreach (ScienceCounter.ScienceType scienceType in ScienceCounter.instance.allScienceTypes)
{
foreach (string scitype in sciTypes)
{
if (typecheck)
{
if (scitype == scienceType.type)
{
if ((scienceType.scienceBank < request.node.tech.scienceCost) | ((scienceType.scienceBank + scienceType.scienceCache) < request.node.tech.scienceCost))
{
typecheck = false;
}
}
}
}
}
if (!typecheck)
{
request.Deny();
}
}
private void SpendScienceTypes(GameEvents.HostTargetAction<RDTech, RDTech.OperationResult> targetAction)
{
List<string> sciTypes = new List<string>();
ConfigNode TechNode = ConfigNode.Load(KSPUtil.ApplicationRootPath + "GameData/ModuleManager.TechTree");
TechNode = TechNode.GetNode("TechTree");
foreach (ConfigNode RDNode in TechNode.GetNodes("RDNode"))
{
if (RDNode.GetValue("id") == targetAction.host.techID)
{
string[] sciTypesArray = RDNode.GetValues("scitype");
foreach (string scitype in sciTypesArray)
{
sciTypes.Add(scitype);
}
}
}
foreach (ScienceCounter.ScienceType scienceType in ScienceCounter.instance.allScienceTypes)
{
foreach (string scitype in sciTypes)
{
if (scitype == scienceType.type)
{
scienceType.scienceCache = scienceType.scienceCache - targetAction.host.scienceCost;
}
}
}
}
}
}