-
Notifications
You must be signed in to change notification settings - Fork 9
/
GCEditorGUI.cs
192 lines (176 loc) · 6.45 KB
/
GCEditorGUI.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
// GCEditorGUI.cs
//
// Author:
// Allis Tauri <allista@gmail.com>
//
// Copyright (c) 2019 Allis Tauri
using System.Collections.Generic;
using AT_Utils;
using AT_Utils.UI;
using KSP.UI.Screens;
using UnityEngine;
namespace GroundConstruction
{
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class GCEditorGUI : AddonWindowBase<GCEditorGUI>
{
private VesselKit AssembleKit;
private VesselKit ConstructionKit;
private DockingNodeList DockingNodes => AssembleKit?.DockingNodes;
private bool update;
private bool highlight_all;
private ConstructDockingNode highlight_node;
private Dictionary<uint, Part> highlighted_parts = new Dictionary<uint, Part>();
private bool all_highlighted =>
highlighted_parts.Count > 0
&& AssembleKit != null
&& highlighted_parts.Count == AssembleKit.DockingNodes.Count;
public override void Awake()
{
base.Awake();
Title = "Global Construction Info";
width = 400;
height = 200;
WindowPos = new Rect(Screen.width / 2 - width / 2, Screen.height / 2 - height / 2, width, height);
GameEvents.onEditorShipModified.Add(OnShipModified);
GameEvents.onEditorLoad.Add(OnShipLoad);
GameEvents.onEditorRestart.Add(Restart);
GameEvents.onEditorStarted.Add(Started);
GameEvents.onEditorVesselNamingChanged.Add(OnShipNamingChanged);
Show(false);
}
public override void OnDestroy()
{
disable_highlights();
GameEvents.onEditorShipModified.Remove(OnShipModified);
GameEvents.onEditorLoad.Remove(OnShipLoad);
GameEvents.onEditorRestart.Remove(Restart);
GameEvents.onEditorStarted.Remove(Started);
GameEvents.onEditorVesselNamingChanged.Remove(OnShipNamingChanged);
base.OnDestroy();
}
private void Started()
{
update = true;
}
private void Restart()
{
update = true;
}
private void OnShipLoad(ShipConstruct ship, CraftBrowserDialog.LoadType load_type)
{
update = true;
}
private void OnShipModified(ShipConstruct ship)
{
update = true;
}
private void OnShipNamingChanged(GameEvents.HostedFromToAction<ShipConstruct, string> action)
{
update = true;
}
private void Update()
{
var ship = EditorLogic.fetch != null ? EditorLogic.fetch.ship : null;
if(update)
{
highlight_all = all_highlighted;
disable_highlights();
if(ship != null)
{
AssembleKit = new VesselKit(null, ship, false, true);
ConstructionKit = new VesselKit(null, ship, true, true);
}
update = false;
}
if(highlight_node != null)
{
Part part;
if(highlighted_parts.TryGetValue(highlight_node.PartId, out part))
{
part.SetHighlightDefault();
highlighted_parts.Remove(highlight_node.PartId);
}
else if(ship != null)
{
part = ship.Parts.GetPartByCraftID(highlight_node.PartId);
if(part != null)
highlighted_parts.Add(part.craftID, part);
}
highlight_node = null;
}
if(highlight_all)
{
if(all_highlighted)
disable_highlights();
else if(ship != null)
{
foreach(var n in DockingNodes)
{
var part = ship.Parts.GetPartByCraftID(n.PartId);
if(part != null && !highlighted_parts.ContainsKey(part.craftID))
highlighted_parts.Add(part.craftID, part);
}
}
highlight_all = false;
}
if(highlighted_parts.Count > 0)
{
foreach(var p in highlighted_parts.Values)
p.HighlightAlways(Colors.Active.color);
}
}
private void disable_highlights()
{
foreach(var p in highlighted_parts.Values)
p.SetHighlightDefault();
highlighted_parts.Clear();
}
private Vector2 scroll;
private void draw(int windowId)
{
GUILayout.BeginVertical(Styles.white);
if(AssembleKit != null)
{
GUILayout.Label("Assembly requirements", Styles.label, GUILayout.ExpandWidth(true));
AssembleKit.Draw();
}
if(ConstructionKit != null)
{
GUILayout.Label("Construction requirements", Styles.label, GUILayout.ExpandWidth(true));
ConstructionKit.Draw();
}
if(AssembleKit != null && AssembleKit.DockingPossible)
{
GUILayout.Label("Attach nodes for docked construction", Styles.label, GUILayout.ExpandWidth(true));
if(GUILayout.Button("Highight all nodes",
all_highlighted ? Styles.enabled_button : Styles.active_button,
GUILayout.ExpandWidth(true)))
highlight_all = true;
scroll = GUILayout.BeginScrollView(scroll, GUILayout.Height(100));
foreach(var n in DockingNodes)
{
if(GUILayout.Button(n.ToString(),
highlighted_parts.ContainsKey(n.PartId) ? Styles.active : Styles.white,
GUILayout.ExpandWidth(true)))
highlight_node = n;
}
GUILayout.EndScrollView();
}
GUILayout.EndVertical();
TooltipsAndDragWindow();
}
protected override void draw_gui()
{
LockControls();
WindowPos =
GUILayout.Window(GetInstanceID(),
WindowPos,
draw,
Title,
GUILayout.Width(width),
GUILayout.Height(height))
.clampToScreen();
}
}
}