forked from rcav8tr/CS1Mod-PopulationDemographics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulationDemographicsLoading.cs
147 lines (134 loc) · 6.18 KB
/
PopulationDemographicsLoading.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
using ColossalFramework.UI;
using ICities;
using UnityEngine;
using System;
namespace PopulationDemographics
{
/// <summary>
/// handle game loading and unloading
/// </summary>
/// <remarks>A new instance of PopulationDemographicsLoading is NOT created when loading a game from the Pause Menu.</remarks>
public class PopulationDemographicsLoading : LoadingExtensionBase
{
// a panel to display the population demographics
public static PopulationDemographicsPanel panel;
// a button to display the panel
private UIButton _demographics;
// a label that follows the cursor
public static UILabel cursorLabel;
public override void OnLevelLoaded(LoadMode mode)
{
// do base processing
base.OnLevelLoaded(mode);
try
{
// check for new or loaded game
if (mode == LoadMode.NewGame || mode == LoadMode.NewGameFromScenario || mode == LoadMode.LoadGame)
{
// get the PopulationInfoViewPanel panel (displayed when the user clicks on the Population info view button)
PopulationInfoViewPanel populationPanel = UIView.library.Get<PopulationInfoViewPanel>(typeof(PopulationInfoViewPanel).Name);
if (populationPanel == null)
{
LogUtil.LogError("Unable to find PopulationInfoViewPanel.");
return;
}
// create a new PopulationDemographicsPanel which will eventually trigger the panel's Start method
panel = populationPanel.component.AddUIComponent<PopulationDemographicsPanel>();
if (panel == null)
{
LogUtil.LogError("Unable to create Population Demographics panel on PopulationInfoViewPanel.");
return;
}
// create button to show the panel
_demographics = populationPanel.component.AddUIComponent<UIButton>();
if (_demographics == null)
{
LogUtil.LogError("Unable to create Demographics button on PopulationInfoViewPanel.");
return;
}
_demographics.name = "Demographics";
_demographics.text = "Demographics";
_demographics.textScale = 0.75f;
_demographics.horizontalAlignment = UIHorizontalAlignment.Center;
_demographics.textVerticalAlignment = UIVerticalAlignment.Middle;
_demographics.autoSize = false;
_demographics.size = new Vector2(120f, 20f);
_demographics.relativePosition = new Vector3(220f, 290f);
_demographics.normalBgSprite = "ButtonMenu";
_demographics.hoveredBgSprite = "ButtonMenuHovered";
_demographics.pressedBgSprite = "ButtonMenuPressed";
_demographics.isVisible = true;
_demographics.eventClicked += Demographics_eventClicked;
// create cursor label
cursorLabel = (UILabel)UIView.GetAView().AddUIComponent(typeof(UILabel));
if (cursorLabel == null)
{
LogUtil.LogError("Unable to create cursor label on main view.");
return;
}
cursorLabel.name = "PopulationDemographicsCursorLabel";
cursorLabel.text = "000.0";
cursorLabel.textColor = Color.cyan;
cursorLabel.outlineColor = Color.black;
cursorLabel.outlineSize = 1;
cursorLabel.useOutline = true;
cursorLabel.textAlignment = UIHorizontalAlignment.Left;
cursorLabel.textScale = 0.75f;
cursorLabel.opacity = 1f; // by not setting a background, the background is transparent and opacity is for only the text
cursorLabel.canFocus = false;
cursorLabel.autoSize = false;
cursorLabel.size = new Vector2(50f, 15f);
cursorLabel.relativePosition = new Vector3(0f, 0f);
cursorLabel.isVisible = false; // start hidden
// create the Harmony patches
if (!BuildingAIPatch.CreatePatches()) { return; }
}
}
catch (Exception ex)
{
LogUtil.LogException(ex);
}
}
/// <summary>
/// handle Demographics button clicked
/// </summary>
private void Demographics_eventClicked(UIComponent component, UIMouseEventParameter eventParam)
{
// toggle the panel visibility
panel.isVisible = !panel.isVisible;
Configuration.SavePanelVisible(panel.isVisible);
}
public override void OnLevelUnloading()
{
// do base processing
base.OnLevelUnloading();
try
{
// remove Harmony patches
BuildingAIPatch.RemovePatches();
// destroy the objects added directly to the PopulationInfoViewPanel
// must do this explicitly because loading a saved game from the Pause Menu
// does not destroy the objects implicitly like returning to the Main Menu to load a saved game
if (cursorLabel != null)
{
UnityEngine.Object.Destroy(cursorLabel);
cursorLabel = null;
}
if (_demographics != null)
{
UnityEngine.Object.Destroy(_demographics);
_demographics = null;
}
if (panel != null)
{
UnityEngine.Object.Destroy(panel);
panel = null;
}
}
catch (Exception ex)
{
LogUtil.LogException(ex);
}
}
}
}