This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathTLMController.cs
265 lines (232 loc) · 11.8 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
using ColossalFramework;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using Klyte.Commons.Interfaces;
using Klyte.Commons.Utils;
using Klyte.TransportLinesManager.Cache;
using Klyte.TransportLinesManager.Extensions;
using Klyte.TransportLinesManager.ModShared;
using Klyte.TransportLinesManager.Overrides;
using Klyte.TransportLinesManager.UI;
using Klyte.TransportLinesManager.Utils;
using Klyte.TransportLinesManager.Xml;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using UnityEngine;
namespace Klyte.TransportLinesManager
{
public class TLMController : BaseController<TransportLinesManagerMod, TLMController>
{
internal static TLMController Instance => TransportLinesManagerMod.Controller;
public bool initializedWIP = false;
public static readonly string FOLDER_NAME = "TransportLinesManager";
public static readonly string FOLDER_PATH = FileUtils.BASE_FOLDER_PATH + FOLDER_NAME;
public const string PALETTE_SUBFOLDER_NAME = "ColorPalettes";
public const string EXPORTED_MAPS_SUBFOLDER_NAME = "ExportedMaps";
public const ulong REALTIME_MOD_ID = 1420955187;
public const ulong IPT2_MOD_ID = 928128676;
public const ulong RETURN_VEHICLE_MOD_ID = 2101977903UL;
public BuildingTransportLinesCache BuildingLines { get; private set; }
private bool? m_isRealTimeEnabled = null;
protected static string GlobalBaseConfigFileName { get; } = "TLM_GlobalData.xml";
public static string GlobalBaseConfigPath { get; } = Path.Combine(FOLDER_PATH, GlobalBaseConfigFileName);
public static bool IsRealTimeEnabled
{
get
{
if (Instance?.m_isRealTimeEnabled == null)
{
VerifyIfIsRealTimeEnabled();
}
return Instance?.m_isRealTimeEnabled == true;
}
}
public static void VerifyIfIsRealTimeEnabled()
{
if (Instance != null)
{
Instance.m_isRealTimeEnabled = VerifyModEnabled(REALTIME_MOD_ID);
}
}
public static bool IsIPT2Enabled() => VerifyModEnabled(IPT2_MOD_ID);
private static bool VerifyModEnabled(ulong modId)
{
PluginManager.PluginInfo pluginInfo = Singleton<PluginManager>.instance.GetPluginsInfo().FirstOrDefault((PluginManager.PluginInfo pi) => pi.publishedFileID.AsUInt64 == modId);
return !(pluginInfo == null || !pluginInfo.isEnabled);
}
public static string PalettesFolder { get; } = FOLDER_PATH + Path.DirectorySeparatorChar + PALETTE_SUBFOLDER_NAME;
public static string ExportedMapsFolder { get; } = FOLDER_PATH + Path.DirectorySeparatorChar + EXPORTED_MAPS_SUBFOLDER_NAME;
public ushort CurrentSelectedId { get; private set; }
public void SetCurrentSelectedId(ushort line) => CurrentSelectedId = line;
internal TLMLineCreationToolbox LineCreationToolbox => PublicTransportInfoViewPanelOverrides.Toolbox;
public TLMFacade SharedInstance { get; internal set; }
internal IBridgeADR ConnectorADR { get; private set; }
internal IBridgeWTS ConnectorWTS { get; private set; }
private bool m_dirtyRegionalLines;
public static Color AutoColor(ushort i, bool ignoreRandomIfSet = true, bool ignoreAnyIfSet = false)
{
ref TransportLine t = ref TransportManager.instance.m_lines.m_buffer[i];
try
{
var tsd = TransportSystemDefinition.GetDefinitionForLine(i, false);
if (tsd == default || (((t.m_flags & TransportLine.Flags.CustomColor) > 0) && ignoreAnyIfSet))
{
return Color.clear;
}
Color c = TLMPrefixesUtils.CalculateAutoColor(t.m_lineNumber, tsd, ((t.m_flags & TransportLine.Flags.CustomColor) > 0) && ignoreRandomIfSet, true);
if (c.a == 1)
{
Instance.StartCoroutine(TLMLineUtils.RunColorChange(Instance, i, c));
}
else
{
c = Singleton<TransportManager>.instance.m_lines.m_buffer[i].m_color;
}
LogUtils.DoLog("Colocada a cor #{0} na linha {1} ({3} {2})", c.ToRGB(), i, t.m_lineNumber, t.Info.m_transportType);
return c;
}
catch (Exception e)
{
LogUtils.DoErrorLog("ERRO!!!!! " + e.Message);
TLMBaseConfigXML.Instance.UseAutoColor = false;
return Color.clear;
}
}
public static void AutoName(ushort m_LineID) => TLMLineUtils.SetLineName(m_LineID, TLMLineUtils.CalculateAutoName(m_LineID, false, out _));
//------------------------------------
protected override void StartActions()
{
BuildingLines = gameObject.AddComponent<BuildingTransportLinesCache>();
TLMTransportTypeDataContainer.Instance.RefreshCapacities();
StartCoroutine(VehicleUtils.UpdateCapacityUnits());
InitWipSidePanels();
m_dirtyRegionalLines = true;
}
internal static bool UpdateRegionalLinesFromBuilding(ushort buildingId)
{
if (BuildingManager.instance.m_buildings.m_buffer[buildingId].Info.m_buildingAI is TransportStationAI)
{
TransportLinesManagerMod.Controller.m_dirtyRegionalLines = true;
return true;
}
return false;
}
internal static bool UpdateRegionalLinesFromNode(ushort nodeId)
{
if (NetManager.instance.m_nodes.m_buffer[nodeId].Info.m_netAI is TransportLineAI && NetManager.instance.m_nodes.m_buffer[nodeId].m_transportLine == 0)
{
TransportLinesManagerMod.Controller.m_dirtyRegionalLines = true;
return true;
}
return false;
}
public void Update()
{
if (m_dirtyRegionalLines)
{
foreach (var item in TLMBuildingDataContainer.Instance.GetAvailableEntries())
{
if (item.TlmManagedRegionalLines)
{
TransportLinesManagerMod.Controller.BuildingLines.SafeGet((ushort)(item.Id ?? 0));
if (TLMFacade.Instance != null)
{
foreach (var plats in item.PlatformMappings.Values)
{
foreach (var regLine in plats.TargetOutsideConnections)
{
TLMFacade.Instance.OnRegionalLineParameterChanged(regLine.Value.m_nodeStation);
}
}
}
}
}
m_dirtyRegionalLines = false;
}
}
private static void InitWipSidePanels()
{
BuildingWorldInfoPanel[] panelList = UIView.GetAView().GetComponentsInChildren<BuildingWorldInfoPanel>();
LogUtils.DoLog("WIP LIST: [{0}]", string.Join(", ", panelList.Select(x => x.name).ToArray()));
TLMLineItemButtonControl.EnsureTemplate();
foreach (BuildingWorldInfoPanel wip in panelList)
{
LogUtils.DoLog("LOADING WIP HOOK FOR: {0}", wip.name);
UIComponent parent = wip.GetComponent<UIComponent>();
if (parent is null)
{
continue;
}
KlyteMonoUtils.CreateUIElement(out UIPanel parent2, parent.transform, "TLMSidePanels", new Vector4(parent.width + 15, 50, 300, 0));
parent2.autoLayout = true;
parent2.autoLayoutPadding.bottom = 5;
parent2.autoFitChildrenVertically = true;
parent2.autoLayoutDirection = LayoutDirection.Vertical;
var isGrow = wip is ZonedBuildingWorldInfoPanel;
var controller = TLMNearLinesController.InitPanelNearLinesOnWorldInfoPanel(parent2);
parent.eventVisibilityChanged += (x, y) => controller?.EventWIPChanged(isGrow);
parent.eventPositionChanged += (x, y) => controller?.EventWIPChanged(isGrow);
parent.eventSizeChanged += (x, y) => controller?.EventWIPChanged(isGrow);
if (wip is CityServiceWorldInfoPanel)
{
var controllerP = TLMRegionalPlatformSelection.Init(parent2);
parent.eventVisibilityChanged += (x, y) => controllerP?.EventWIPChanged();
parent.eventPositionChanged += (x, y) => controllerP?.EventWIPChanged();
parent.eventSizeChanged += (x, y) => controllerP?.EventWIPChanged();
NetManagerOverrides.EventNodeChanged += (x) =>
{
controllerP?.EventWIPChanged();
controller?.EventWIPChanged(isGrow);
};
}
}
}
public void OpenTLMPanel() => TransportLinesManagerMod.Instance.OpenPanelAtModTab();
public void CloseTLMPanel() => TransportLinesManagerMod.Instance.ClosePanel();
public IEnumerator RenameCoroutine(ushort id, string newName)
{
if (Singleton<SimulationManager>.exists)
{
AsyncTask<bool> task = Singleton<SimulationManager>.instance.AddAction(Singleton<TransportManager>.instance.SetLineName(id, newName));
yield return task.WaitTaskCompleted(this);
UVMPublicTransportWorldInfoPanel.GetLineID(out ushort lineId, out bool fromBuilding);
if (id > 0 && lineId == id && !fromBuilding)
{
UVMPublicTransportWorldInfoPanel.m_obj.m_nameField.text = Singleton<TransportManager>.instance.GetLineName(id);
}
}
yield break;
}
internal void Awake()
{
SharedInstance = gameObject.AddComponent<TLMFacade>();
ConnectorADR = PluginUtils.GetImplementationTypeForMod<BridgeADRFallback, IBridgeADR>(gameObject, "KlyteAddresses", "2.99.99.0", "Klyte.TransportLinesManager.ModShared.BridgeADR");
ConnectorWTS = PluginUtils.GetImplementationTypeForMod<BridgeWTSFallback, IBridgeWTS>(gameObject, "KlyteWriteTheSigns", "0.3.0.0", "Klyte.TransportLinesManager.ModShared.BridgeWTS");
}
internal static readonly Color[] COLOR_ORDER = new Color[]
{
Color.red,
Color.Lerp(Color.red, Color.yellow,0.5f),
Color.yellow,
Color.green,
Color.cyan,
Color.blue,
Color.Lerp(Color.blue, Color.magenta,0.5f),
Color.magenta,
Color.white,
Color.black,
Color.Lerp( Color.red, Color.black,0.5f),
Color.Lerp( Color.Lerp(Color.red, Color.yellow,0.5f), Color.black,0.5f),
Color.Lerp( Color.yellow, Color.black,0.5f),
Color.Lerp( Color.green, Color.black,0.5f),
Color.Lerp( Color.cyan, Color.black,0.5f),
Color.Lerp( Color.blue, Color.black,0.5f),
Color.Lerp( Color.Lerp(Color.blue, Color.magenta,0.5f), Color.black,0.5f),
Color.Lerp( Color.magenta, Color.black,0.5f),
Color.Lerp( Color.white, Color.black,0.25f),
Color.Lerp( Color.white, Color.black,0.75f)
};
}
}