forked from ihsoft/KAS
/
KASModuleContainer.cs
731 lines (662 loc) · 29.2 KB
/
KASModuleContainer.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using UnityEngine;
namespace KAS
{
public class KASModuleContainer : PartModule
{
[KSPField] public float maxSize = 10f;
[KSPField] public float maxOpenDistance = 2f;
[KSPField] public string sndStorePath = "KAS/Sounds/hookBayStore";
[KSPField] public string sndOpenPath = "KAS/Sounds/containerOpen";
[KSPField] public string sndClosePath = "KAS/Sounds/containerClose";
[KSPField] public string bipWrongSndPath = "KAS/Sounds/bipwrong";
public FXGroup fxSndStore, fxSndOpen, fxSndClose, fxSndBipWrong;
public Dictionary<AvailablePart, int> availableContents = new Dictionary<AvailablePart, int>();
public Dictionary<AvailablePart, int> contents = new Dictionary<AvailablePart, int>();
private KASModuleContainer exchangeContainer = null;
public float totalSize = 0;
private bool waitAndGrabRunning = false;
private float orgMass;
private GUIStyle guiButtonStyle, guiDataboxStyle, guiCenterStyle, guiBoldCyanCenterStyle, guiTooltipStyle;
public Rect guiMainWindowPos;
private Vector2 scrollPos1 = Vector2.zero;
private Vector2 scrollPos2 = Vector2.zero;
private EditTab activeEditTab = EditTab.All;
public enum EditTab
{
None = -1,
All = 0,
Propulsion = 1,
Control = 2,
Structural = 3,
Aero = 4,
Utility = 5,
Science = 6,
Pods = 7,
}
private guiMode showGUI = guiMode.None;
public enum guiMode
{
None = 0,
Edit = 1,
Take = 2,
Exchange = 3,
View = 4,
}
public enum ShowButton
{
None = 0,
Add = 1,
Remove = 2,
Take = 3,
Move = 4,
}
public override string GetInfo()
{
string info = base.GetInfo();
info += "---- Container ----";
info += "\n";
info += "Max size : " + maxSize;
return info;
}
public override void OnSave(ConfigNode node)
{
base.OnSave(node);
foreach (KeyValuePair<AvailablePart, int> ct in contents)
{
ConfigNode nodeD = node.AddNode("CONTENT");
nodeD.AddValue("name", ct.Key.name);
nodeD.AddValue("qty", ct.Value);
}
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
if (node.HasNode("CONTENT"))
{
contents.Clear();
}
foreach (ConfigNode cn in node.GetNodes("CONTENT"))
{
if (cn.HasValue("name") && cn.HasValue("qty"))
{
string AvPartName = cn.GetValue("name").ToString();
AvailablePart avPart = null;
avPart = PartLoader.getPartInfoByName(AvPartName);
int qty = int.Parse(cn.GetValue("qty"));
if (avPart != null)
{
contents.Add(avPart, qty);
}
else
{
KAS_Shared.DebugError("Load(Container) - Cannot retrieve " + AvPartName + " from PartLoader !");
}
}
}
}
public override void OnStart(StartState state)
{
base.OnStart(state);
if (state == StartState.None) return;
KAS_Shared.createFXSound(this.part, fxSndStore, sndStorePath, false);
KAS_Shared.createFXSound(this.part, fxSndOpen, sndOpenPath, false);
KAS_Shared.createFXSound(this.part, fxSndClose, sndClosePath, false);
KAS_Shared.createFXSound(this.part, fxSndBipWrong, bipWrongSndPath, false);
GameEvents.onVesselChange.Add(new EventData<Vessel>.OnEvent(this.OnVesselChange));
orgMass = this.part.partInfo.partPrefab.mass;
RefreshTotalSize();
}
void OnVesselChange(Vessel vess)
{
if (showGUI != guiMode.None)
{
CloseAllGUI();
}
}
void OnDestroy()
{
GameEvents.onVesselChange.Remove(new EventData<Vessel>.OnEvent(this.OnVesselChange));
}
public override void OnUpdate()
{
base.OnUpdate();
if (!HighLogic.LoadedSceneIsFlight) return;
if (showGUI == guiMode.Take)
{
float distEvaToContainer = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position, this.part.transform.position);
if (distEvaToContainer > maxOpenDistance)
{
CloseAllGUI();
}
}
}
public Dictionary<AvailablePart, int> GetContent()
{
Dictionary<AvailablePart, int> returnContents = new Dictionary<AvailablePart, int>();
foreach (KeyValuePair<AvailablePart, int> ct in contents)
{
returnContents.Add(ct.Key,ct.Value);
}
return returnContents;
}
private Dictionary<AvailablePart, int> GetAvailableContents(PartCategories category)
{
Dictionary<AvailablePart, int> edct = new Dictionary<AvailablePart, int>();
foreach (AvailablePart avPart in PartLoader.LoadedPartsList)
{
KASModuleGrab grabModule = avPart.partPrefab.GetComponent<KASModuleGrab>();
if (grabModule)
{
if (grabModule.storable)
{
if (avPart.category == category)
{
edct.Add(avPart, 0);
}
}
}
}
return edct;
}
private Dictionary<AvailablePart, int> GetAvailableContents()
{
Dictionary<AvailablePart, int> edct = new Dictionary<AvailablePart, int>();
foreach (AvailablePart avPart in PartLoader.LoadedPartsList)
{
KASModuleGrab grabModule = avPart.partPrefab.GetComponent<KASModuleGrab>();
if (grabModule)
{
if (grabModule.storable)
{
edct.Add(avPart, 0);
}
}
}
return edct;
}
private void Add(AvailablePart avPart, int qty)
{
if (contents.ContainsKey(avPart))
{
contents[avPart] += qty;
}
else
{
contents.Add(avPart,qty);
}
RefreshTotalSize();
}
private void Remove(AvailablePart avPart, int qty)
{
if (contents.ContainsKey(avPart))
{
if (contents[avPart] - 1 > 0)
{
contents[avPart] -= qty;
}
else
{
contents.Remove(avPart);
}
RefreshTotalSize();
}
else
{
KAS_Shared.DebugLog("Remove(Container) - Nothing to remove");
}
}
private void RefreshTotalSize()
{
totalSize = 0;
this.part.mass = orgMass;
foreach (KeyValuePair<AvailablePart, int> contentPart in contents)
{
KASModuleGrab grabModule = contentPart.Key.partPrefab.GetComponent<KASModuleGrab>();
if (grabModule)
{
totalSize += contentPart.Value * grabModule.storedSize;
this.part.mass += contentPart.Value * contentPart.Key.partPrefab.mass;
}
}
}
private bool MaxSizeReached(AvailablePart avPart, int qty)
{
KASModuleGrab moduleGrab = avPart.partPrefab.GetComponent<KASModuleGrab>();
if (moduleGrab)
{
if (totalSize + (moduleGrab.storedSize * qty) > maxSize)
{
return true;
}
}
return false;
}
private float GetPartSize(AvailablePart avPart)
{
KASModuleGrab grabModule = avPart.partPrefab.GetComponent<KASModuleGrab>();
if (grabModule)
{
return grabModule.storedSize;
}
else
{
KAS_Shared.DebugError("Cannot retrieve part size, grab module not found !");
return 0;
}
}
private void Take(AvailablePart avPart)
{
if (waitAndGrabRunning)
{
KAS_Shared.DebugError("Take(Container) Take action is already running, please wait !");
return;
}
KASModuleGrab prefabGrabModule = avPart.partPrefab.GetComponent<KASModuleGrab>();
if (!prefabGrabModule)
{
KAS_Shared.DebugError("Take(Container) Can't find the prefab grab module !");
return;
}
// get grabbed position and rotation
Vector3 pos = FlightGlobals.ActiveVessel.rootPart.transform.TransformPoint(prefabGrabModule.evaPartPos);
Quaternion rot = FlightGlobals.ActiveVessel.rootPart.transform.rotation * Quaternion.Euler(prefabGrabModule.evaPartDir);
//Move away the part at creation
pos += new Vector3(0f, 0f, 100);
//Part newPart = KAS_Shared.CreatePart(avPart, pos, rot, this.part);
Part newPart = KAS_Shared.CreatePart(avPart.name, pos, rot, this.part);
if (!newPart)
{
KAS_Shared.DebugError("Take(Container) failed to create the part !");
return;
}
KASModuleGrab moduleGrab = newPart.GetComponent<KASModuleGrab>();
if (!moduleGrab)
{
KAS_Shared.DebugError("Take(Container) Cannot grab the part taken, no grab module found !");
return;
}
Remove(avPart, 1);
StartCoroutine(WaitAndGrab(moduleGrab, FlightGlobals.ActiveVessel));
}
private IEnumerator WaitAndGrab(KASModuleGrab moduleGrab, Vessel vess)
{
waitAndGrabRunning = true;
while (!moduleGrab.part.rigidbody)
{
KAS_Shared.DebugLog("WaitAndGrab(Container) - Waiting rigidbody to initialize...");
yield return new WaitForFixedUpdate();
}
KAS_Shared.DebugLog("WaitAndGrab(Container) - Rigidbody initialized, setting velocity...");
moduleGrab.part.rigidbody.velocity = vess.rootPart.rigidbody.velocity;
moduleGrab.part.rigidbody.angularVelocity = vess.rootPart.rigidbody.angularVelocity;
KAS_Shared.DebugLog("WaitAndGrab(Container) - Waiting velocity to apply by waiting 0.1 seconds...");
yield return new WaitForSeconds(0.1f);
KAS_Shared.DebugLog("WaitAndGrab(Container) - Grab part...");
if (!moduleGrab.evaHolderPart) moduleGrab.Grab(vess);
KAS_Shared.DebugLog("WaitAndGrab(Container) - End of coroutine...");
waitAndGrabRunning = false;
}
private void StoreGrabbedPart()
{
KASModuleGrab moduleGrab = KAS_Shared.GetGrabbedPartModule(FlightGlobals.ActiveVessel);
if (!moduleGrab)
{
fxSndBipWrong.audio.Play();
ScreenMessages.PostScreenMessage("You didn't grab anything to store !", 5, ScreenMessageStyle.UPPER_CENTER);
return;
}
if (!moduleGrab.storable)
{
fxSndBipWrong.audio.Play();
ScreenMessages.PostScreenMessage("This part cannot be stored !", 5, ScreenMessageStyle.UPPER_CENTER);
return;
}
if (MaxSizeReached(moduleGrab.part.partInfo,1))
{
fxSndBipWrong.audio.Play();
ScreenMessages.PostScreenMessage("Max size of the container reached !", 5, ScreenMessageStyle.UPPER_CENTER);
return;
}
Add(moduleGrab.part.partInfo, 1);
moduleGrab.Drop();
moduleGrab.part.Die();
fxSndStore.audio.Play();
}
private void Move(AvailablePart aPart, int qty, KASModuleContainer destContainer)
{
KASModuleGrab moduleGrab = aPart.partPrefab.GetComponent<KASModuleGrab>();
if (!moduleGrab)
{
fxSndBipWrong.audio.Play();
KAS_Shared.DebugError("Cannot grab the part taken, no grab module found !");
return;
}
if (destContainer.MaxSizeReached(aPart, 1))
{
fxSndBipWrong.audio.Play();
ScreenMessages.PostScreenMessage("Max size of the destination container reached !", 5, ScreenMessageStyle.UPPER_CENTER);
return;
}
Remove(aPart, qty);
destContainer.Add(aPart, qty);
}
public void TakeContents(Vessel evaVessel)
{
if (showGUI != guiMode.None)
{
CloseGUI();
fxSndClose.audio.Play();
}
else
{
CloseAllGUI();
guiMainWindowPos = new Rect(Screen.width / 2, Screen.height / 2, 10, 10);
showGUI = guiMode.Take;
fxSndOpen.audio.Play();
}
}
public void ExchangeContents(KASModuleContainer moduleContainer)
{
if (showGUI != guiMode.None)
{
CloseGUI();
fxSndClose.audio.Play();
}
else
{
CloseAllGUI();
exchangeContainer = moduleContainer;
guiMainWindowPos = new Rect(Screen.width / 2, Screen.height / 2, 10, 10);
showGUI = guiMode.Exchange;
fxSndOpen.audio.Play();
}
}
public void EditContents()
{
if (showGUI != guiMode.None)
{
CloseGUI();
fxSndClose.audio.Play();
}
else
{
CloseAllGUI();
availableContents = GetAvailableContents();
guiMainWindowPos = new Rect(Screen.width / 3, 35, 10, 10);
showGUI = guiMode.Edit;
fxSndOpen.audio.Play();
}
}
public void ViewContents()
{
if (showGUI != guiMode.None)
{
CloseGUI();
}
else
{
CloseAllGUI();
guiMainWindowPos = new Rect(Screen.width / 2, Screen.height / 2, 10, 10);
showGUI = guiMode.View;
fxSndOpen.audio.Play();
}
}
private void CloseAllGUI()
{
List<KASModuleContainer> allInvModule = new List<KASModuleContainer>(GameObject.FindObjectsOfType(typeof(KASModuleContainer)) as KASModuleContainer[]);
foreach (KASModuleContainer invModule in allInvModule)
{
if (invModule.showGUI != guiMode.None)
{
invModule.CloseGUI();
}
}
}
private void CloseGUI()
{
showGUI = guiMode.None;
exchangeContainer = null;
availableContents = null;
if (HighLogic.LoadedSceneIsEditor) EditorLogic.fetch.Unlock();
}
void OnGUI()
{
GUI.skin = HighLogic.Skin;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
if (showGUI != guiMode.None)
{
guiMainWindowPos = GUILayout.Window(5501, guiMainWindowPos, GuiMainWindow, this.part.partInfo.title, GUILayout.MinWidth(1), GUILayout.MinHeight(1));
if (HighLogic.LoadedSceneIsEditor) KAS_Shared.DisableEditorClickthrough(guiMainWindowPos);
}
}
private void GuiStyles()
{
guiButtonStyle = new GUIStyle(GUI.skin.button);
guiButtonStyle.normal.textColor = guiButtonStyle.focused.textColor = Color.white;
guiButtonStyle.hover.textColor = guiButtonStyle.active.textColor = Color.yellow;
guiButtonStyle.onNormal.textColor = guiButtonStyle.onFocused.textColor = guiButtonStyle.onHover.textColor = guiButtonStyle.onActive.textColor = Color.green;
guiButtonStyle.padding = new RectOffset(4, 4, 4, 4);
guiButtonStyle.alignment = TextAnchor.MiddleCenter;
guiDataboxStyle = new GUIStyle(GUI.skin.box);
guiDataboxStyle.margin.top = guiDataboxStyle.margin.bottom = -5;
guiDataboxStyle.border.top = guiDataboxStyle.border.bottom = 0;
guiDataboxStyle.wordWrap = false;
guiDataboxStyle.alignment = TextAnchor.MiddleCenter;
guiCenterStyle = new GUIStyle(GUI.skin.label);
guiCenterStyle.wordWrap = false;
guiCenterStyle.alignment = TextAnchor.MiddleCenter;
guiBoldCyanCenterStyle = new GUIStyle(GUI.skin.label);
guiBoldCyanCenterStyle.wordWrap = false;
guiBoldCyanCenterStyle.fontStyle = FontStyle.Bold;
guiBoldCyanCenterStyle.normal.textColor = Color.cyan;
guiBoldCyanCenterStyle.alignment = TextAnchor.MiddleCenter;
guiTooltipStyle = new GUIStyle(GUI.skin.label);
guiTooltipStyle.normal.textColor = Color.white;
guiTooltipStyle.alignment = TextAnchor.MiddleCenter;
guiTooltipStyle.fontSize = 14;
}
private void GuiMainWindow(int windowID)
{
GuiStyles();
GUILayout.Space(15);
if (showGUI == guiMode.Edit)
{
GuiEditTabs();
GUILayout.Label("- Available contents -", guiBoldCyanCenterStyle);
scrollPos1 = GuiContentList(availableContents, ShowButton.Add, this, scrollPos1, false, scrollHeight:400f);
GUILayout.Label("- Container contents -", guiBoldCyanCenterStyle);
scrollPos2 = GuiContentList(GetContent(), ShowButton.Remove, this, scrollPos2, scrollHeight: 200f);
}
if (showGUI == guiMode.Take)
{
GUILayout.Label("- Container contents -", guiBoldCyanCenterStyle);
scrollPos1 = GuiContentList(GetContent(), ShowButton.Take, this, scrollPos1, scrollHeight: 200f);
}
if (showGUI == guiMode.View)
{
GUILayout.Label("- Container contents -", guiBoldCyanCenterStyle);
scrollPos1 = GuiContentList(GetContent(), ShowButton.None, this, scrollPos1, scrollHeight: 200f);
}
if (showGUI == guiMode.Exchange)
{
GUILayout.Label("- Source container contents -", guiBoldCyanCenterStyle);
scrollPos1 = GuiContentList(GetContent(), ShowButton.Move, this, scrollPos1, true, exchangeContainer, scrollHeight: 300f);
GUILayout.Label("- Destination container contents -", guiBoldCyanCenterStyle);
scrollPos2 = GuiContentList(GetContent(), ShowButton.Move, exchangeContainer, scrollPos2, true, this, scrollHeight: 300f);
}
GUILayout.BeginHorizontal();
if (GUILayout.Button(new GUIContent("Close", "Close container"), guiButtonStyle, GUILayout.Width(60f)))
{
CloseAllGUI();
fxSndClose.audio.Play();
}
if (showGUI == guiMode.Take)
{
if (GUILayout.Button(new GUIContent("Store", "Store current grabbed part"), guiButtonStyle, GUILayout.Width(60f)))
{
StoreGrabbedPart();
}
}
GUILayout.EndHorizontal();
GUI.Label(new Rect(0, 18, guiMainWindowPos.width, 30), GUI.tooltip, guiTooltipStyle);
GUI.DragWindow();
}
private void GuiEditTabs()
{
GUILayout.BeginHorizontal(guiCenterStyle);
guiButtonStyle.fontSize = 12;
float tabButtonWidth = 60f;
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.All) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("All", "Show all part(s)"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents();
activeEditTab = EditTab.All;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.Pods) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("Pods", "Show part(s) from pods category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.Pods);
activeEditTab = EditTab.Pods;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.Propulsion) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("Propulsion", "Show part(s) from propulsion category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.Propulsion);
activeEditTab = EditTab.Propulsion;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.Control) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("Control", "Show part(s) from control category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.Control);
activeEditTab = EditTab.Control;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.Structural) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("Structural", "Show part(s) from structural category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.Structural);
activeEditTab = EditTab.Structural;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.Aero) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("Aero", "Show part(s) from aero category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.Aero);
activeEditTab = EditTab.Aero;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.Utility) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("Utility", "Show part(s) from utility category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.Utility);
activeEditTab = EditTab.Utility;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.Science) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("Science", "Show part(s) from science category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.Science);
activeEditTab = EditTab.Science;
}
guiButtonStyle.normal.textColor = (activeEditTab == EditTab.None) ? Color.yellow : Color.white;
if (GUILayout.Button(new GUIContent("None", "Show part(s) without any category"), guiButtonStyle, GUILayout.Width(tabButtonWidth)))
{
availableContents = GetAvailableContents(PartCategories.none);
activeEditTab = EditTab.None;
}
guiButtonStyle.fontSize = 14;
guiButtonStyle.normal.textColor = Color.white;
GUILayout.EndHorizontal();
}
private Vector2 GuiContentList(Dictionary<AvailablePart, int> contentsList, ShowButton showButton, KASModuleContainer actionContainer, Vector2 scrollPos, bool showTotal = true, KASModuleContainer destContainer = null, float scrollHeight = 300f)
{
scrollPos = GUILayout.BeginScrollView(scrollPos, guiDataboxStyle, GUILayout.Width(600f), GUILayout.Height(scrollHeight));
foreach (KeyValuePair<AvailablePart, int> ct in contentsList)
{
GUILayout.BeginHorizontal();
GUILayout.Label(new GUIContent(" " + ct.Key.title, "Name"), guiCenterStyle, GUILayout.Width(300f));
GUILayout.Label(new GUIContent(" " + GetPartSize(ct.Key).ToString("0.0"), "Size"), guiCenterStyle, GUILayout.Width(50f));
GUILayout.Label(new GUIContent(" " + ct.Key.partPrefab.mass.ToString("0.000"), "Mass"), guiCenterStyle, GUILayout.Width(50f));
GUILayout.Label(new GUIContent(" " + ct.Value, "Quantity"), guiCenterStyle, GUILayout.Width(50f));
if (showButton == ShowButton.Add)
{
if (GUILayout.Button(new GUIContent("Add", "Add part to container"), guiButtonStyle, GUILayout.Width(50f)))
{
if (actionContainer.MaxSizeReached(ct.Key, 1))
{
fxSndBipWrong.audio.Play();
ScreenMessages.PostScreenMessage("Max size of the container reached !", 5, ScreenMessageStyle.UPPER_CENTER);
return scrollPos;
}
else
{
actionContainer.Add(ct.Key, 1);
}
}
}
if (showButton == ShowButton.Remove)
{
if (GUILayout.Button(new GUIContent("Remove", "Remove part from container"), guiButtonStyle, GUILayout.Width(60f)))
{
actionContainer.Remove(ct.Key, 1);
}
}
if (showButton == ShowButton.Take)
{
if (GUILayout.Button(new GUIContent("Take", "Take part from container"), guiButtonStyle, GUILayout.Width(60f)))
{
actionContainer.Take(ct.Key);
}
/*
if (GUILayout.Button(new GUIContent("Attach", "Attach part"), guiButtonStyle, GUILayout.Width(60f)))
{
KASModuleGrab moduleGrab = ct.Key.partPrefab.GetComponent<KASModuleGrab>();
KASAddonPointer.StartPointer(ct.Key.partPrefab, KASAddonPointer.PointerMode.CopyAndAttach, true, true, true, 2, this.part.transform, false);
}*/
}
if (showButton == ShowButton.Move)
{
if (GUILayout.Button(new GUIContent("Move", "Move part to the destination container"), guiButtonStyle, GUILayout.Width(60f)))
{
actionContainer.Move(ct.Key, 1, destContainer);
}
}
GUILayout.EndHorizontal();
}
if (showTotal)
{
GUILayout.Space(5);
GUILayout.BeginVertical(guiCenterStyle);
GUILayout.BeginHorizontal();
GUIStyle guiLeftWhiteStyle = new GUIStyle(GUI.skin.label);
guiLeftWhiteStyle.wordWrap = false;
guiLeftWhiteStyle.normal.textColor = Color.white;
guiLeftWhiteStyle.alignment = TextAnchor.MiddleLeft;
GUIStyle guiRightWhiteStyle = new GUIStyle(GUI.skin.label);
guiRightWhiteStyle.wordWrap = false;
guiRightWhiteStyle.normal.textColor = Color.white;
guiRightWhiteStyle.alignment = TextAnchor.MiddleRight;
GUILayout.Label(new GUIContent("Weight : " + actionContainer.part.mass.ToString("0.000"), "Total weight of the container with contents"), guiRightWhiteStyle);
GUILayout.Label(new GUIContent(" | Space : " + actionContainer.totalSize.ToString("0.0") + " / " + actionContainer.maxSize.ToString("0.0"), "Space used / Max space"), guiRightWhiteStyle);
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
GUILayout.EndScrollView();
return scrollPos;
}
[KSPEvent(name = "ContextMenuOpenContainer", active = true, guiActive = true, guiActiveUnfocused = true, guiName = "Open container")]
public void ContextMenuOpenContainer()
{
if (FlightGlobals.ActiveVessel.isEVA)
{
TakeContents(FlightGlobals.ActiveVessel);
}
else
{
ViewContents();
}
}
}
}