forked from klyte45/TransportLinesManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLMController.cs
440 lines (380 loc) · 18.4 KB
/
TLMController.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
using ColossalFramework;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TLMCW = Klyte.TransportLinesManager.TLMConfigWarehouse;
using Klyte.TransportLinesManager.UI;
using Klyte.TransportLinesManager.LineList;
using ColossalFramework.Globalization;
using Klyte.TransportLinesManager.Utils;
using Klyte.TransportLinesManager.Extensors;
namespace Klyte.TransportLinesManager
{
public class TLMController
{
private static TLMController _instance;
public static TLMController instance
{
get {
if (_instance == null) {
_instance = new TLMController();
}
return _instance;
}
}
public static UITextureAtlas taTLM = null;
public static UITextureAtlas taLineNumber = null;
public UIView uiView;
public UIComponent mainRef;
public TransportManager tm
{
get {
return Singleton<TransportManager>.instance;
}
}
public InfoManager im
{
get {
return Singleton<InfoManager>.instance;
}
}
public UIButton abrePainelButton;
public bool initialized = false;
public bool initializedWIP = false;
private TLMLineInfoPanel m_lineInfoPanel;
private TLMDepotInfoPanel m_depotInfoPanel;
private int lastLineCount = 0;
private UIPanel _cachedDefaultListingLinesPanel;
public UIPanel defaultListingLinesPanel
{
get {
if (_cachedDefaultListingLinesPanel == null) {
_cachedDefaultListingLinesPanel = GameObject.Find("UIView").GetComponentInChildren<TLMPublicTransportDetailPanel>().GetComponent<UIPanel>();
}
return _cachedDefaultListingLinesPanel;
}
}
public TLMLineInfoPanel lineInfoPanel
{
get {
return m_lineInfoPanel;
}
}
public TLMDepotInfoPanel depotInfoPanel
{
get {
return m_depotInfoPanel;
}
}
public Transform transform
{
get {
return mainRef.transform;
}
}
private TLMController() { }
public void destroy()
{
if (abrePainelButton != null && abrePainelButton.gameObject != null) {
UnityEngine.Object.Destroy(abrePainelButton.gameObject);
}
if (m_lineInfoPanel != null && m_lineInfoPanel.linearMap != null && m_lineInfoPanel.linearMap.gameObject != null) {
UnityEngine.Object.Destroy(m_lineInfoPanel.linearMap.gameObject);
}
if (m_lineInfoPanel != null && m_lineInfoPanel.gameObject != null) {
UnityEngine.Object.Destroy(m_lineInfoPanel.gameObject);
}
if (m_depotInfoPanel != null && m_depotInfoPanel.gameObject != null) {
UnityEngine.Object.Destroy(m_depotInfoPanel.gameObject);
}
initialized = false;
initializedWIP = false;
_instance = null;
}
public void update()
{
if (!GameObject.FindGameObjectWithTag("GameController") || ((GameObject.FindGameObjectWithTag("GameController").GetComponent<ToolController>()).m_mode & ItemClass.Availability.Game) == ItemClass.Availability.None) {
TLMUtils.doErrorLog("GameController NOT FOUND!");
return;
}
if (!initialized) {
TransportLinesManagerMod.instance.loadTLMLocale(false);
uiView = GameObject.FindObjectOfType<UIView>();
if (!uiView)
return;
mainRef = uiView.FindUIComponent<UIPanel>("InfoPanel").Find<UITabContainer>("InfoViewsContainer").Find<UIPanel>("InfoViewsPanel");
if (!mainRef)
return;
mainRef.eventVisibilityChanged += delegate (UIComponent component, bool b) {
if (b) {
TransportLinesManagerMod.instance.showVersionInfoPopup();
}
};
createViews();
mainRef.clipChildren = false;
initialized = true;
}
initNearLinesOnWorldInfoPanel();
if (m_lineInfoPanel.isVisible) {
m_lineInfoPanel.updateBidings();
}
if (m_depotInfoPanel.isVisible) {
m_depotInfoPanel.updateBidings();
}
lastLineCount = tm.m_lineCount;
TLMPublicTransportDetailPanelHooks.instance.update();
return;
}
const int maxTryLoads = 100;
public Color AutoColor(ushort i)
{
TransportLine t = tm.m_lines.m_buffer[(int) i];
try {
var tsd = TLMCW.getDefinitionForLine(i);
if (tsd == default(TransportSystemDefinition)) {
return Color.clear;
}
TLMCW.ConfigIndex transportType = tsd.toConfigIndex();
bool prefixBased = TLMCW.getCurrentConfigBool(transportType | TLMCW.ConfigIndex.PALETTE_PREFIX_BASED);
bool randomOnOverflow = TLMCW.getCurrentConfigBool(transportType | TLMCW.ConfigIndex.PALETTE_RANDOM_ON_OVERFLOW);
string pal = TLMCW.getCurrentConfigString(transportType | TLMCW.ConfigIndex.PALETTE_SUBLINE);
ushort num = t.m_lineNumber;
if (num >= 1000 && TLMCW.getCurrentConfigInt(transportType | TLMCW.ConfigIndex.PREFIX) != (int) ModoNomenclatura.Nenhum) {
pal = TLMCW.getCurrentConfigString(transportType | TLMCW.ConfigIndex.PALETTE_MAIN);
if (prefixBased) {
num /= 1000;
} else {
num %= 1000;
}
}
Color c = TLMAutoColorPalettes.getColor(num, pal, randomOnOverflow);
TLMUtils.setLineColor(i, c);
//TLMUtils.doLog("Colocada a cor {0} na linha {1} ({3} {2})", c, i, t.m_lineNumber, t.Info.m_transportType);
return c;
} catch (Exception e) {
TLMUtils.doErrorLog("ERRO!!!!! " + e.Message);
TLMCW.setCurrentConfigBool(TLMCW.ConfigIndex.AUTO_COLOR_ENABLED, false);
return Color.clear;
}
}
public void AutoName(ushort m_LineID)
{
string format = (TLMCW.getCurrentConfigBool(TLMConfigWarehouse.ConfigIndex.ADD_LINE_NUMBER_IN_AUTONAME)) ? "[{0}] {1}" : "{1}";
TLMUtils.setLineName(m_LineID, string.Format(format, TLMLineUtils.getLineStringId(m_LineID).Replace('\n', ' '), TLMUtils.calculateAutoName(m_LineID)));
}
//NAVEGACAO
private void swapWindow(UIComponent component, UIMouseEventParameter eventParam)
{
if (m_lineInfoPanel.isVisible || defaultListingLinesPanel.isVisible || m_depotInfoPanel.isVisible) {
fecharTelaTransportes(component, eventParam);
} else {
abrirTelaTransportes(component, eventParam);
}
}
private void abrirTelaTransportes(UIComponent component, UIMouseEventParameter eventParam)
{
// DebugOutputPanel.AddMessage (ColossalFramework.Plugins.PluginManager.MessageType.Warning, "ABRE1!");
abrePainelButton.normalFgSprite = abrePainelButton.focusedFgSprite;
m_lineInfoPanel.Hide();
m_depotInfoPanel.Hide();
defaultListingLinesPanel.Show();
tm.LinesVisible = 0x7FFFFFFF;
// MainMenu ();
// DebugOutputPanel.AddMessage (ColossalFramework.Plugins.PluginManager.MessageType.Warning, "ABRE2!");
}
private void fecharTelaTransportes(UIComponent component, UIFocusEventParameter eventParam)
{
fecharTelaTransportes(component, (UIMouseEventParameter) null);
}
private void fecharTelaTransportes(UIComponent component, UIMouseEventParameter eventParam)
{
abrePainelButton.normalFgSprite = abrePainelButton.disabledFgSprite;
defaultListingLinesPanel.Hide();
m_lineInfoPanel.Hide();
m_depotInfoPanel.Hide();
tm.LinesVisible = 0;
InfoManager im = Singleton<InfoManager>.instance;
// DebugOutputPanel.AddMessage (ColossalFramework.Plugins.PluginManager.MessageType.Warning, "FECHA!");
}
private void createViews()
{
m_lineInfoPanel = new TLMLineInfoPanel(this);
m_depotInfoPanel = new TLMDepotInfoPanel(this);
}
private void initNearLinesOnWorldInfoPanel()
{
if (!initializedWIP) {
UIPanel parent = GameObject.Find("UIView").transform.GetComponentInChildren<CityServiceWorldInfoPanel>().gameObject.GetComponent<UIPanel>();
if (parent == null)
return;
parent.eventVisibilityChanged += (component, value) => {
updateNearLines(TransportLinesManagerMod.savedShowNearLinesInCityServicesWorldInfoPanel.value ? parent : null, true);
updateDepotEditShortcutButton(parent);
};
parent.eventPositionChanged += (component, value) => {
updateNearLines(TransportLinesManagerMod.savedShowNearLinesInCityServicesWorldInfoPanel.value ? parent : null, true);
updateDepotEditShortcutButton(parent);
};
UIPanel parent2 = GameObject.Find("UIView").transform.GetComponentInChildren<ZonedBuildingWorldInfoPanel>().gameObject.GetComponent<UIPanel>();
if (parent2 == null)
return;
parent2.eventVisibilityChanged += (component, value) => {
updateNearLines(TransportLinesManagerMod.savedShowNearLinesInZonedBuildingWorldInfoPanel.value ? parent2 : null, true);
updateDepotEditShortcutButton(parent2);
};
parent2.eventPositionChanged += (component, value) => {
updateNearLines(TransportLinesManagerMod.savedShowNearLinesInZonedBuildingWorldInfoPanel.value ? parent2 : null, true);
updateDepotEditShortcutButton(parent2);
};
UIPanel parent3 = GameObject.Find("UIView").transform.GetComponentInChildren<PublicTransportWorldInfoPanel>().gameObject.GetComponent<UIPanel>();
if (parent3 == null)
return;
parent3.eventVisibilityChanged += (component, value) => {
if (TransportLinesManagerMod.overrideWorldInfoPanelLine && value) {
PublicTransportWorldInfoPanel ptwip = parent3.gameObject.GetComponent<PublicTransportWorldInfoPanel>();
ptwip.StartCoroutine(OpenLineInfo(ptwip));
ptwip.Hide();
}
};
initializedWIP = true;
}
}
private IEnumerator OpenLineInfo(PublicTransportWorldInfoPanel ptwip)
{
yield return 0;
ushort lineId = 0;
while (lineId == 0) {
lineId = (ushort) (typeof(PublicTransportWorldInfoPanel).GetMethod("GetLineID", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance).Invoke(ptwip, new object[0]));
}
TLMController.instance.lineInfoPanel.openLineInfo(lineId);
}
private ushort lastBuildingSelected = 0;
private void updateNearLines(UIPanel parent, bool force = false)
{
if (parent != null) {
Transform linesPanelObj = parent.transform.Find("TLMLinesNear");
if (!linesPanelObj) {
linesPanelObj = initPanelNearLinesOnWorldInfoPanel(parent);
}
var prop = typeof(WorldInfoPanel).GetField("m_InstanceID", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
ushort buildingId = ((InstanceID) (prop.GetValue(parent.gameObject.GetComponent<WorldInfoPanel>()))).Building;
if (lastBuildingSelected == buildingId && !force) {
return;
} else {
lastBuildingSelected = buildingId;
}
Building b = Singleton<BuildingManager>.instance.m_buildings.m_buffer[buildingId];
List<ushort> nearLines = new List<ushort>();
TLMLineUtils.GetNearLines(b.CalculateSidewalkPosition(), 120f, ref nearLines);
bool showPanel = nearLines.Count > 0;
// DebugOutputPanel.AddMessage (PluginManager.MessageType.Warning, "nearLines.Count = " + nearLines.Count);
if (showPanel) {
foreach (Transform t in linesPanelObj) {
if (t.GetComponent<UILabel>() == null) {
GameObject.Destroy(t.gameObject);
}
}
Dictionary<string, ushort> lines = TLMLineUtils.SortLines(nearLines);
TLMLineUtils.PrintIntersections("", "", "", "", "", linesPanelObj.GetComponent<UIPanel>(), lines, scale, perLine);
}
linesPanelObj.GetComponent<UIPanel>().isVisible = showPanel;
} else {
var go = GameObject.Find("TLMLinesNear");
if (!go) {
return;
}
Transform linesPanelObj = go.transform;
linesPanelObj.GetComponent<UIPanel>().isVisible = false;
}
}
private float scale = 1f;
private int perLine = 9;
private Transform initPanelNearLinesOnWorldInfoPanel(UIPanel parent)
{
UIPanel saida = parent.AddUIComponent<UIPanel>();
saida.relativePosition = new Vector3(0, parent.height);
saida.width = parent.width;
saida.autoFitChildrenVertically = true;
saida.autoLayout = true;
saida.autoLayoutDirection = LayoutDirection.Horizontal;
saida.autoLayoutPadding = new RectOffset(2, 2, 2, 2);
saida.padding = new RectOffset(2, 2, 2, 2);
saida.autoLayoutStart = LayoutStart.TopLeft;
saida.wrapLayout = true;
saida.name = "TLMLinesNear";
saida.backgroundSprite = "GenericPanel";
UILabel title = saida.AddUIComponent<UILabel>();
title.autoSize = false;
title.width = saida.width;
title.textAlignment = UIHorizontalAlignment.Left;
title.localeID = "TLM_NEAR_LINES";
title.useOutline = true;
title.height = 18;
return saida.transform;
}
private UIButton initDepotShortcutOnWorldInfoPanel(UIPanel parent)
{
UIButton saida = parent.AddUIComponent<UIButton>();
saida.relativePosition = new Vector3(10, parent.height - 50);
saida.atlas = taTLM;
saida.width = 30;
saida.height = 30;
saida.name = "TLMDepotShortcut";
saida.tooltipLocaleID = "TLM_GOTO_DEPOT_PREFIX_EDIT";
TLMUtils.initButton(saida, false, "TransportLinesManagerIcon");
saida.eventClick += (x, y) => {
var prop = typeof(WorldInfoPanel).GetField("m_InstanceID", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
ushort buildingId = ((InstanceID) (prop.GetValue(parent.gameObject.GetComponent<WorldInfoPanel>()))).Building;
depotInfoPanel.openDepotInfo(buildingId, false);
};
UILabel prefixes = saida.AddUIComponent<UILabel>();
prefixes.autoSize = false;
prefixes.width = 200;
prefixes.wordWrap = true;
prefixes.textAlignment = UIHorizontalAlignment.Left;
prefixes.prefix = Locale.Get("TLM_PREFIXES_SERVED") + ":\n";
prefixes.useOutline = true;
prefixes.height = 60;
prefixes.textScale = 0.6f;
prefixes.relativePosition = new Vector3(35, 1);
prefixes.name = "Prefixes";
return saida;
}
private void updateDepotEditShortcutButton(UIPanel parent)
{
if (parent != null) {
UIButton depotShortcut = parent.Find<UIButton>("TLMDepotShortcut");
if (!depotShortcut) {
depotShortcut = initDepotShortcutOnWorldInfoPanel(parent);
}
var prop = typeof(WorldInfoPanel).GetField("m_InstanceID", System.Reflection.BindingFlags.NonPublic
| System.Reflection.BindingFlags.Instance);
ushort buildingId = ((InstanceID) (prop.GetValue(parent.gameObject.GetComponent<WorldInfoPanel>()))).Building;
DepotAI ai = Singleton<BuildingManager>.instance.m_buildings.m_buffer[buildingId].Info.GetAI() as DepotAI;
if (ai != null) {
byte count = 0;
List<string> lines = new List<string>();
if (ai.m_transportInfo != null && ai.m_maxVehicleCount > 0) {
lines.Add(string.Format("{0}: {1}", TLMConfigWarehouse.getNameForTransportType(TransportSystemDefinition.from(ai.m_transportInfo).toConfigIndex()), TLMUtils.getPrefixesServedAbstract(buildingId, false)));
count++;
}
if (ai.m_secondaryTransportInfo != null && ai.m_maxVehicleCount2 > 0) {
lines.Add(string.Format("{0}: {1}", TLMConfigWarehouse.getNameForTransportType(TransportSystemDefinition.from(ai.m_secondaryTransportInfo).toConfigIndex()), TLMUtils.getPrefixesServedAbstract(buildingId, true)));
count++;
}
UILabel label = depotShortcut.GetComponentInChildren<UILabel>();
label.text = string.Join("\n", lines.ToArray());
depotShortcut.isVisible = count > 0;
} else {
depotShortcut.isVisible = false;
}
}
}
}
}