forked from klyte45/VehicleMasterControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVMController.cs
129 lines (111 loc) · 4.83 KB
/
SVMController.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
using ColossalFramework;
using ColossalFramework.UI;
using Klyte.Commons;
using Klyte.Commons.Extensors;
using Klyte.Commons.UI;
using Klyte.Commons.Utils;
using Klyte.ServiceVehiclesManager.Extensors.VehicleExt;
using Klyte.ServiceVehiclesManager.UI;
using Klyte.ServiceVehiclesManager.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace Klyte.ServiceVehiclesManager
{
internal class SVMController : Singleton<SVMController>
{
internal static UITextureAtlas taSVM;
public void Start()
{
KlyteModsPanel.instance.AddTab(ModTab.ServiceVehiclesManager, typeof(SVMServiceBuildingDetailPanel), taSVM, "ServiceVehiclesManagerIcon", "Service Vehicles Manager (v" + ServiceVehiclesManagerMod.version + ")");
SVMUtils.createUIElement(out UIPanel buildingInfoParent, FindObjectOfType<UIView>().transform, "SVMBuildingInfoPanel", new Vector4(0, 0, 0, 1));
buildingInfoParent.gameObject.AddComponent<SVMBuildingInfoPanel>();
var typeTarg = typeof(Redirector<>);
List<Type> instances = KlyteUtils.GetSubtypesRecursive(typeTarg, typeof(SVMController));
foreach (Type t in instances)
{
gameObject.AddComponent(t);
}
}
public void OpenSVMPanel()
{
KlyteModsPanel.instance.OpenAt(ModTab.ServiceVehiclesManager);
}
public void CloseSVMPanel()
{
KCController.instance.CloseKCPanel();
}
public void Awake()
{
initNearLinesOnWorldInfoPanel();
}
private void initNearLinesOnWorldInfoPanel()
{
BuildingWorldInfoPanel[] panelList = GameObject.Find("UIView").GetComponentsInChildren<BuildingWorldInfoPanel>();
SVMUtils.doLog("WIP LIST: [{0}]", string.Join(", ", panelList.Select(x => x.name).ToArray()));
foreach (BuildingWorldInfoPanel wip in panelList)
{
SVMUtils.doLog("LOADING WIP HOOK FOR: {0}", wip.name);
UIPanel parent = wip.gameObject.GetComponent<UIPanel>();
if (parent == null)
return;
parent.eventVisibilityChanged += (component, value) =>
{
updateBuildingEditShortcutButton(parent);
};
parent.eventPositionChanged += (component, value) =>
{
updateBuildingEditShortcutButton(parent);
};
}
}
private void updateBuildingEditShortcutButton(UIPanel parent)
{
if (parent != null)
{
UIButton buildingEditShortcut = parent.Find<UIButton>("SVMBuildingShortcut");
if (!buildingEditShortcut)
{
buildingEditShortcut = initBuildingEditOnWorldInfoPanel(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;
var ssds = ServiceSystemDefinition.from(Singleton<BuildingManager>.instance.m_buildings.m_buffer[buildingId].Info);
byte count = 0;
foreach (var ssd in ssds)
{
var maxCount = SVMBuildingUtils.GetMaxVehiclesBuilding(buildingId, ssd.vehicleType);
if (maxCount > 0)
{
count++;
break;
}
}
buildingEditShortcut.isVisible = count > 0;
}
}
private UIButton initBuildingEditOnWorldInfoPanel(UIPanel parent)
{
UIButton saida = parent.AddUIComponent<UIButton>();
saida.relativePosition = new Vector3(-40, parent.height - 50);
saida.atlas = taSVM;
saida.width = 30;
saida.height = 30;
saida.name = "SVMBuildingShortcut";
saida.color = new Color32(170, 170, 170, 255);
saida.hoveredColor = Color.white;
saida.tooltipLocaleID = "SVM_GOTO_BUILDING_CONFIG_EDIT";
SVMUtils.initButtonSameSprite(saida, "ServiceVehiclesManagerIcon");
saida.eventClick += (x, y) =>
{
var prop = typeof(WorldInfoPanel).GetField("m_InstanceID", BindingFlags.NonPublic | BindingFlags.Instance);
ushort buildingId = ((InstanceID)(prop.GetValue(parent.gameObject.GetComponent<WorldInfoPanel>()))).Building;
SVMBuildingInfoPanel.instance.openInfo(buildingId);
};
return saida;
}
}
}