forked from klyte45/TransportLinesManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLMController.cs
472 lines (411 loc) · 19 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
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
using ColossalFramework;
using ColossalFramework.Globalization;
using ColossalFramework.UI;
using Klyte.Commons;
using Klyte.Commons.Extensors;
using Klyte.Commons.UI;
using Klyte.TransportLinesManager.Extensors.TransportTypeExt;
using Klyte.TransportLinesManager.Interfaces;
using Klyte.TransportLinesManager.LineList;
using Klyte.TransportLinesManager.UI;
using Klyte.TransportLinesManager.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using TLMCW = Klyte.TransportLinesManager.TLMConfigWarehouse;
namespace Klyte.TransportLinesManager
{
internal class TLMController : LinearMapParentInterface<TLMController>
{
public static UITextureAtlas taTLM
{
get {
if (_taTLM == null)
{
TLMResourceLoader.Ensure();
_taTLM = TLMResourceLoader.instance.CreateTextureAtlas("UI.Images.sprites.png", "TransportLinesManagerSprites", GameObject.FindObjectOfType<UIPanel>().atlas.material, 64, 64, new string[] {
"TransportLinesManagerIcon","TransportLinesManagerIconHovered","AutoNameIcon","AutoColorIcon","RemoveUnwantedIcon","ConfigIcon","24hLineIcon", "PerHourIcon","AbsoluteMode","RelativeMode"
});
}
return _taTLM;
}
}
public static UITextureAtlas taLineNumber
{
get {
if (_taLineNumber == null)
{
TLMResourceLoader.Ensure();
_taLineNumber = TLMResourceLoader.instance.CreateTextureAtlas("UI.Images.lineFormat.png", "TransportLinesManagerLinearLineSprites", GameObject.FindObjectOfType<UIPanel>().atlas.material, 64, 64, new string[] {
"TourBusIcon","TourPedIcon", "CableCarTabIcon","TaxiTabIcon", "EvacBusIcon","DepotIcon", "LinearHalfStation","LinearStation","LinearBg","PlaneLineIcon","TramIcon","ShipLineIcon","FerryIcon","CableCarIcon", "BlimpIcon","BusIcon","SubwayIcon","TrainIcon","MonorailIcon","ShipIcon","AirplaneIcon","TaxiIcon","DayIcon",
"NightIcon","DisabledIcon","NoBudgetIcon","BulletTrainImage","LowBusImage","HighBusImage","VehicleLinearMap","RegionalTrainIcon"
});
}
return _taLineNumber;
}
}
private static UITextureAtlas _taTLM = null;
private static UITextureAtlas _taLineNumber = null;
public UIView uiView;
public UIComponent mainRef;
public TransportManager tm => Singleton<TransportManager>.instance;
public InfoManager im => Singleton<InfoManager>.instance;
public bool initialized = false;
public bool initializedWIP = false;
private TLMLineInfoPanel m_lineInfoPanel;
private TLMDepotInfoPanel m_depotInfoPanel;
private TLMLinearMap m_linearMapCreatingLine;
private TLMLineCreationToolbox m_lineCreationToolbox;
private int lastLineCount = 0;
//private UIPanel _cachedDefaultListingLinesPanel;
public TLMLineInfoPanel lineInfoPanel => m_lineInfoPanel;
public TLMDepotInfoPanel depotInfoPanel => m_depotInfoPanel;
public Transform TargetTransform => mainRef?.transform;
public override Transform TransformLinearMap => uiView?.transform;
private ushort m_currentSelectedId;
public override ushort CurrentSelectedId => m_currentSelectedId;
public void setCurrentSelectedId(ushort line) => m_currentSelectedId = line;
public override bool CanSwitchView => false;
public TLMLinearMap LinearMapCreatingLine
{
get {
if (m_linearMapCreatingLine != null)
{
return m_linearMapCreatingLine;
}
else
{
TLMUtils.doErrorLog("LinearMapCreatingLine is NULL!!!!");
return null;
}
}
}
public TLMLineCreationToolbox LineCreationToolbox
{
get {
if (m_lineCreationToolbox != null)
{
return m_lineCreationToolbox;
}
else
{
TLMUtils.doErrorLog("LineCreationToolbox is NULL!!!!");
return null;
}
}
}
public override bool ForceShowStopsDistances
{
get {
return true;
}
}
public override TransportInfo CurrentTransportInfo
{
get {
return Singleton<TransportTool>.instance.m_prefab;
}
}
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)
{
Awake();
}
if (m_lineInfoPanel?.isVisible ?? false)
{
m_lineInfoPanel?.updateBidings();
}
if (m_depotInfoPanel?.isVisible ?? false)
{
m_depotInfoPanel?.updateBidings();
}
lastLineCount = tm.m_lineCount;
m_lineInfoPanel?.assetSelectorWindow?.RotateCamera();
}
public void Awake()
{
if (!initialized && gameObject != null)
{
TLMSingleton.instance.loadTLMLocale(false);
uiView = GameObject.FindObjectOfType<UIView>();
if (!uiView)
return;
mainRef = uiView.FindUIComponent<UIPanel>("InfoPanel").Find<UITabContainer>("InfoViewsContainer").Find<UIPanel>("InfoViewsPanel");
if (!mainRef)
return;
createViews();
mainRef.clipChildren = false;
var typeTarg = typeof(Redirector<>);
var instances = from t in Assembly.GetAssembly(typeof(TLMController)).GetTypes()
let y = t.BaseType
where t.IsClass && !t.IsAbstract && y != null && y.IsGenericType && y.GetGenericTypeDefinition() == typeTarg
select t;
foreach (Type t in instances)
{
gameObject.AddComponent(t);
}
initialized = true;
}
}
public Color AutoColor(ushort i, bool ignoreRandomIfSet = true, bool ignoreAnyIfSet = false)
{
TransportLine t = tm.m_lines.m_buffer[(int)i];
try
{
var tsd = TransportSystemDefinition.getDefinitionForLine(i);
if (tsd == default(TransportSystemDefinition) || (((t.m_flags & TransportLine.Flags.CustomColor) > 0) && ignoreAnyIfSet))
{
return Color.clear;
}
TLMCW.ConfigIndex transportType = tsd.toConfigIndex();
Color c = TLMUtils.CalculateAutoColor(t.m_lineNumber, transportType, ((t.m_flags & TransportLine.Flags.CustomColor) > 0) && ignoreRandomIfSet, true);
if (c.a == 1)
{
TLMLineUtils.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)
{
TLMLineUtils.setLineName(m_LineID, TLMLineUtils.calculateAutoName(m_LineID));
}
//NAVEGACAO
private void createViews()
{
TLMUtils.createElement(out m_lineInfoPanel, transform);
TLMUtils.createElement(out m_depotInfoPanel, transform);
TLMUtils.createElement(out m_linearMapCreatingLine, transform);
TLMUtils.createElement(out m_lineCreationToolbox, transform);
m_linearMapCreatingLine.parent = this;
m_linearMapCreatingLine.setVisible(false);
}
private void initNearLinesOnWorldInfoPanel()
{
if (!initializedWIP)
{
BuildingWorldInfoPanel[] panelList = GameObject.Find("UIView").GetComponentsInChildren<BuildingWorldInfoPanel>();
TLMUtils.doLog("WIP LIST: [{0}]", string.Join(", ", panelList.Select(x => x.name).ToArray()));
foreach (BuildingWorldInfoPanel wip in panelList)
{
TLMUtils.doLog("LOADING WIP HOOK FOR: {0}", wip.name);
UIComponent parent2 = wip.GetComponent<UIComponent>();
if (parent2 == null)
continue;
parent2.eventVisibilityChanged += (component, value) =>
{
updateNearLines(TLMSingleton.savedShowNearLinesInZonedBuildingWorldInfoPanel.value ? parent2 : null, true);
updateDepotEditShortcutButton(parent2);
};
parent2.eventPositionChanged += (component, value) =>
{
updateNearLines(TLMSingleton.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 (TLMSingleton.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(UIComponent 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(UIComponent 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(UIComponent 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(UIComponent 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;
if (Singleton<BuildingManager>.instance.m_buildings.m_buffer[buildingId].Info.GetAI() is DepotAI ai)
{
byte count = 0;
List<string> lines = new List<string>();
if (ai.m_transportInfo != null && ai.m_maxVehicleCount > 0 && TransportSystemDefinition.from(ai.m_transportInfo).isPrefixable())
{
lines.Add(string.Format("{0}: {1}", TLMConfigWarehouse.getNameForTransportType(TransportSystemDefinition.from(ai.m_transportInfo).toConfigIndex()), TLMLineUtils.getPrefixesServedString(buildingId, false)));
count++;
}
if (ai.m_secondaryTransportInfo != null && ai.m_maxVehicleCount2 > 0 && TransportSystemDefinition.from(ai.m_secondaryTransportInfo).isPrefixable())
{
lines.Add(string.Format("{0}: {1}", TLMConfigWarehouse.getNameForTransportType(TransportSystemDefinition.from(ai.m_secondaryTransportInfo).toConfigIndex()), TLMLineUtils.getPrefixesServedString(buildingId, true)));
count++;
}
depotShortcut.isVisible = count > 0;
if (depotShortcut.isVisible)
{
UILabel label = depotShortcut.GetComponentInChildren<UILabel>();
label.text = string.Join("\n", lines.ToArray());
}
}
else
{
depotShortcut.isVisible = false;
}
}
}
public override void OnRenameStationAction(string autoName)
{
}
//------------------------------------
public void Start()
{
KlyteModsPanel.instance.AddTab(ModTab.TransportLinesManager, typeof(TLMPublicTransportManagementPanel), taTLM, "TransportLinesManagerIconHovered", "Transport Lines Manager (v" + TLMSingleton.version + ")");
initNearLinesOnWorldInfoPanel();
}
public void OpenTLMPanel()
{
KlyteModsPanel.instance.OpenAt(ModTab.TransportLinesManager);
}
public void CloseTLMPanel()
{
KCController.instance.CloseKCPanel();
}
}
}