From 8f6d388d2c2a89689b68c34f7b1753c88a840af3 Mon Sep 17 00:00:00 2001 From: Craxy Date: Sat, 5 Aug 2017 21:42:00 +0200 Subject: [PATCH] For #41: Change Junction Tool activation to not use C:S internal lists Update version to 0.10.5 --- .../Game/Behaviours/MainMachine.cs | 32 ++++++--- .../ToggleTrafficLights.csproj | 2 +- src/ToggleTrafficLights/Utils/ToolHelper.cs | 67 ++++++++++++++++++- 3 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/ToggleTrafficLights/Game/Behaviours/MainMachine.cs b/src/ToggleTrafficLights/Game/Behaviours/MainMachine.cs index cc48bc5..e22c12e 100644 --- a/src/ToggleTrafficLights/Game/Behaviours/MainMachine.cs +++ b/src/ToggleTrafficLights/Game/Behaviours/MainMachine.cs @@ -158,12 +158,7 @@ private void SetupTtlButton() private void SetupTool() { - var current = ToolsModifierControl.GetCurrentTool(); //Assert ToolsModifierControl.CollectTools() is called - _tool = ToolsModifierControl.toolController.gameObject.GetComponent() - ?? ToolsModifierControl.toolController.gameObject.AddComponent(); - ToolsModifierControl.toolController.CurrentTool = current; - ToolHelper.AddToToolsModifierControl(_tool); - + _tool = ToolHelper.AddTool(); _tool.Disabled += OnToolDisabled; } @@ -189,9 +184,8 @@ private void Destroy() ToggleTrafficLightsButton.DestroyAll(); - ToolHelper.RemoveFromToolsModifierControl(); - _tool.Disabled += OnToolDisabled; - GameObject.Destroy(_tool); + _tool.Disabled -= OnToolDisabled; + ToolHelper.RemoveTool(_tool); _tool = null; ForgetPreviousTool(); @@ -311,7 +305,21 @@ private void OnJunctionSettingsVisibilityChanged(UIComponent _, bool visible) private void ActivateIntersectionTool(bool keepInfoMode, JunctionTool.Elevation elevation) { - var tool = ActivateAndReturnTool(); +// var tool = ActivateAndReturnTool(); + var tool = ToolHelper.SetTool(); + + if (tool == null) + { + var tools = ReflectionExtensions.GetNonPublicStaticField>("m_Tools") + .Values + .Select(t => t.GetType().FullName) + .ToArray(); + var components = ToolsModifierControl.toolController.GetComponents() + .Select(t => t.GetType().FullName).ToArray(); + var msg = $"ToolHelper.SetTool returned null.\nCollected tools: {string.Join(", ", tools)}\nAdded Components: {string.Join(", ", components)}"; + Utils.Log.Error(msg); + } + if (keepInfoMode) { tool.CurrentElevation = elevation; @@ -365,7 +373,9 @@ private T ActivateAndReturnTool() if (tool == null) { var tools = ReflectionExtensions.GetNonPublicStaticField>("m_Tools") - .Select(t => t.GetType().FullName).ToArray(); + .Values + .Select(t => t.GetType().FullName) + .ToArray(); var components = ToolsModifierControl.toolController.GetComponents() .Select(t => t.GetType().FullName).ToArray(); var msg = $"SetTool returned null.\nCollected tools: {string.Join(", ", tools)}\nAdded Components: {string.Join(", ", components)}"; diff --git a/src/ToggleTrafficLights/ToggleTrafficLights.csproj b/src/ToggleTrafficLights/ToggleTrafficLights.csproj index 449a4d1..9c5a6b3 100644 --- a/src/ToggleTrafficLights/ToggleTrafficLights.csproj +++ b/src/ToggleTrafficLights/ToggleTrafficLights.csproj @@ -9,7 +9,7 @@ Craxy.CitiesSkylines.ToggleTrafficLights - 0.10.4 + 0.10.5 $(Version) $(Version) Toggle Traffic Lights diff --git a/src/ToggleTrafficLights/Utils/ToolHelper.cs b/src/ToggleTrafficLights/Utils/ToolHelper.cs index 7c72ea9..07f9aa2 100644 --- a/src/ToggleTrafficLights/Utils/ToolHelper.cs +++ b/src/ToggleTrafficLights/Utils/ToolHelper.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; using Craxy.CitiesSkylines.ToggleTrafficLights.Utils.Extensions; +using UnityEngine; namespace Craxy.CitiesSkylines.ToggleTrafficLights.Utils { - public class ToolHelper + public static class ToolHelper { private static Dictionary GetToolsDictionary() { @@ -12,7 +13,7 @@ private static Dictionary GetToolsDictionary() return tools; } - public static void AddToToolsModifierControl(T tool) + private static void AddToToolsModifierControl(T tool) where T : ToolBase { var tools = GetToolsDictionary(); @@ -22,7 +23,7 @@ public static void AddToToolsModifierControl(T tool) } } - public static void RemoveFromToolsModifierControl() + private static void RemoveFromToolsModifierControl() where T : ToolBase { var tools = GetToolsDictionary(); @@ -31,5 +32,65 @@ public static void RemoveFromToolsModifierControl() tools.Remove(typeof(T)); } } + + public static T AddTool() + where T : ToolBase + { + var current = ToolsModifierControl.GetCurrentTool(); //Assert ToolsModifierControl.CollectTools() is called + var tool = ToolsModifierControl.toolController.gameObject.GetComponent() + ?? ToolsModifierControl.toolController.gameObject.AddComponent(); + ToolsModifierControl.toolController.CurrentTool = current; + + return tool; + } + + public static void RemoveTool(T tool) + where T : ToolBase + { + GameObject.Destroy(tool); + } + + /// + /// ToolsModifierControl.GetTool + /// But instead of searching the tool in ToolsModifierControl.m_Tools, + /// this method uses ToolsModifierControl.toolController.gameObject.GetComponent + /// + public static T GetTool() + where T : ToolBase + { + return ToolsModifierControl.toolController.GetComponent(); + } + + /// + /// ToolsModifierControl.SetTool + /// But instead of searching the tool in ToolsModifierControl.m_Tools, + /// this method uses ToolsModifierControl.toolController.gameObject.GetComponent + /// + public static T SetTool() + where T : ToolBase + { + var toolController = ToolsModifierControl.toolController; + if (toolController == null) + { + return null; + } + var tool = toolController.GetComponent(); + if (tool == null) + { + return null; + } + + if (!ToolsModifierControl.keepThisWorldInfoPanel) + { + WorldInfoPanel.HideAllWorldInfoPanels(); + } + GameAreaInfoPanel.Hide(); + ToolsModifierControl.keepThisWorldInfoPanel = false; + if (toolController.CurrentTool != tool) + { + toolController.CurrentTool = tool; + } + return tool; + } } }