Created
February 17, 2019 16:18
-
-
Save boformer/3e01770a0c86cf9273ccdcc33d9bbc42 to your computer and use it in GitHub Desktop.
HistoricalDistrictsMod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ColossalFramework; | |
using ColossalFramework.UI; | |
using ICities; | |
using UnityEngine; | |
namespace HistoricalDistricts | |
{ | |
public class HistoricalDistrictsMod : IUserMod | |
{ | |
public string Name => "Historical Districts"; | |
public string Description => "Set or unset the historical status of all buildings in a district"; | |
} | |
public class Loading : LoadingExtensionBase | |
{ | |
public override void OnLevelLoaded(LoadMode mode) | |
{ | |
InitializeUi(); | |
} | |
private void InitializeUi() | |
{ | |
var panel = UIView.library.Get<DistrictWorldInfoPanel>(typeof(DistrictWorldInfoPanel).Name); | |
if (panel == null) return; | |
var policiesPanel = panel.Find<UIPanel>("PoliciesPanel"); | |
if (policiesPanel == null) return; | |
var policiesButton = policiesPanel.Find<UIButton>("PoliciesButton"); | |
if (policiesButton == null) return; | |
// check if UI was already initialized | |
if (panel.Find<UIPanel>("HistoryPanel")) | |
{ | |
return; | |
} | |
var historyPanel = panel.component.AddUIComponent<UIPanel>(); | |
//var historyPanel = panel.Find<UIPanel>("HistoryPanel"); | |
historyPanel.name = "HistoryPanel"; | |
historyPanel.size = new Vector2(policiesPanel.width, 0f);//37f | |
historyPanel.relativePosition = policiesPanel.relativePosition + new Vector3(0f, policiesButton.height + 16f, 0f); | |
var makeHistorical = AddButton(historyPanel); | |
//var makeHistorical = historyPanel.Find<UIButton>("MakeHistorical"); | |
makeHistorical.name = "MakeHistorical"; | |
makeHistorical.text = "Set Historical"; | |
makeHistorical.tooltip = "Make all currently existing buildings in the district historical"; | |
makeHistorical.textPadding = new RectOffset(14, 14, 7, 7); | |
makeHistorical.font = policiesButton.font; | |
makeHistorical.autoSize = true; | |
makeHistorical.eventClick += OnMakeHistoricalClicked; | |
var unmakeHistorical = AddButton(historyPanel); | |
//var unmakeHistorical = historyPanel.Find<UIButton>("UnmakeHistorical"); | |
unmakeHistorical.name = "UnmakeHistorical"; | |
unmakeHistorical.text = "Unset Historical"; | |
unmakeHistorical.tooltip = "Unset the historical status of all buildings in the district"; | |
unmakeHistorical.textPadding = new RectOffset(14, 14, 7, 7); | |
unmakeHistorical.font = policiesButton.font; | |
unmakeHistorical.autoSize = true; | |
unmakeHistorical.eventClick += OnUnmakeHistoricalClicked; | |
historyPanel.autoLayoutDirection = LayoutDirection.Horizontal; | |
historyPanel.autoLayoutStart = LayoutStart.TopLeft; | |
historyPanel.autoLayoutPadding = new RectOffset(3, 3, 4, 4); | |
historyPanel.autoLayout = true; | |
historyPanel.FitChildrenVertically(); | |
} | |
protected void OnMakeHistoricalClicked(UIComponent component, UIMouseEventParameter p) | |
{ | |
SetCurrentDistrictHistorical(true); | |
} | |
protected void OnUnmakeHistoricalClicked(UIComponent component, UIMouseEventParameter p) | |
{ | |
SetCurrentDistrictHistorical(false); | |
} | |
private static void SetCurrentDistrictHistorical(bool historical) | |
{ | |
var districtId = WorldInfoPanel.GetCurrentInstanceID().District; | |
if (districtId == 0) return; | |
for (ushort buildingId = 1; buildingId < BuildingManager.instance.m_buildings.m_buffer.Length; buildingId++) | |
{ | |
var building = BuildingManager.instance.m_buildings.m_buffer[buildingId]; | |
if (!building.m_flags.IsFlagSet(Building.Flags.Created)) continue; | |
var buildingDistrictId = DistrictManager.instance.GetDistrict(building.m_position); | |
if (buildingDistrictId != districtId) continue; | |
var buildingAi = building.Info.m_buildingAI; | |
if (buildingAi == null) continue; | |
buildingAi.SetHistorical(buildingId, ref BuildingManager.instance.m_buildings.m_buffer[buildingId], historical); | |
} | |
} | |
private static UIButton AddButton(UIComponent parent) | |
{ | |
var button = parent.AddUIComponent<UIButton>(); | |
button.size = new Vector2(90f, 30f); | |
button.textScale = 0.8f; | |
button.textPadding = new RectOffset(14, 14, 7, 7); | |
button.autoSize = true; | |
button.normalBgSprite = "ButtonMenu"; | |
button.hoveredBgSprite = "ButtonMenuHovered"; | |
button.pressedBgSprite = "ButtonMenuPressed"; | |
button.canFocus = false; | |
return button; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment