Skip to content

Commit

Permalink
For #41: Change Junction Tool activation to not use C:S internal lists
Browse files Browse the repository at this point in the history
Update version to 0.10.5
  • Loading branch information
Craxy committed Aug 5, 2017
1 parent 4f02936 commit 8f6d388
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 15 deletions.
32 changes: 21 additions & 11 deletions src/ToggleTrafficLights/Game/Behaviours/MainMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,7 @@ private void SetupTtlButton()

private void SetupTool()
{
var current = ToolsModifierControl.GetCurrentTool<ToolBase>(); //Assert ToolsModifierControl.CollectTools() is called
_tool = ToolsModifierControl.toolController.gameObject.GetComponent<JunctionTool>()
?? ToolsModifierControl.toolController.gameObject.AddComponent<JunctionTool>();
ToolsModifierControl.toolController.CurrentTool = current;
ToolHelper.AddToToolsModifierControl(_tool);

_tool = ToolHelper.AddTool<JunctionTool>();
_tool.Disabled += OnToolDisabled;
}

Expand All @@ -189,9 +184,8 @@ private void Destroy()

ToggleTrafficLightsButton.DestroyAll();

ToolHelper.RemoveFromToolsModifierControl<JunctionTool>();
_tool.Disabled += OnToolDisabled;
GameObject.Destroy(_tool);
_tool.Disabled -= OnToolDisabled;
ToolHelper.RemoveTool(_tool);
_tool = null;

ForgetPreviousTool();
Expand Down Expand Up @@ -311,7 +305,21 @@ private void OnJunctionSettingsVisibilityChanged(UIComponent _, bool visible)

private void ActivateIntersectionTool(bool keepInfoMode, JunctionTool.Elevation elevation)
{
var tool = ActivateAndReturnTool<JunctionTool>();
// var tool = ActivateAndReturnTool<JunctionTool>();
var tool = ToolHelper.SetTool<JunctionTool>();

if (tool == null)
{
var tools = ReflectionExtensions.GetNonPublicStaticField<ToolsModifierControl, Dictionary<System.Type, ToolBase>>("m_Tools")
.Values
.Select(t => t.GetType().FullName)
.ToArray();
var components = ToolsModifierControl.toolController.GetComponents<ToolBase>()
.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;
Expand Down Expand Up @@ -365,7 +373,9 @@ private T ActivateAndReturnTool<T>()
if (tool == null)
{
var tools = ReflectionExtensions.GetNonPublicStaticField<ToolsModifierControl, Dictionary<System.Type, ToolBase>>("m_Tools")
.Select(t => t.GetType().FullName).ToArray();
.Values
.Select(t => t.GetType().FullName)
.ToArray();
var components = ToolsModifierControl.toolController.GetComponents<ToolBase>()
.Select(t => t.GetType().FullName).ToArray();
var msg = $"SetTool returned null.\nCollected tools: {string.Join(", ", tools)}\nAdded Components: {string.Join(", ", components)}";
Expand Down
2 changes: 1 addition & 1 deletion src/ToggleTrafficLights/ToggleTrafficLights.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<RootNamespace>Craxy.CitiesSkylines.ToggleTrafficLights</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<Version>0.10.4</Version>
<Version>0.10.5</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Product>Toggle Traffic Lights</Product>
Expand Down
67 changes: 64 additions & 3 deletions src/ToggleTrafficLights/Utils/ToolHelper.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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<Type, ToolBase> GetToolsDictionary()
{
var tools = ReflectionExtensions.GetNonPublicStaticField<ToolsModifierControl, Dictionary<Type, ToolBase>>("m_Tools");
return tools;
}

public static void AddToToolsModifierControl<T>(T tool)
private static void AddToToolsModifierControl<T>(T tool)
where T : ToolBase
{
var tools = GetToolsDictionary();
Expand All @@ -22,7 +23,7 @@ public static void AddToToolsModifierControl<T>(T tool)
}
}

public static void RemoveFromToolsModifierControl<T>()
private static void RemoveFromToolsModifierControl<T>()
where T : ToolBase
{
var tools = GetToolsDictionary();
Expand All @@ -31,5 +32,65 @@ public static void RemoveFromToolsModifierControl<T>()
tools.Remove(typeof(T));
}
}

public static T AddTool<T>()
where T : ToolBase
{
var current = ToolsModifierControl.GetCurrentTool<ToolBase>(); //Assert ToolsModifierControl.CollectTools() is called
var tool = ToolsModifierControl.toolController.gameObject.GetComponent<T>()
?? ToolsModifierControl.toolController.gameObject.AddComponent<T>();
ToolsModifierControl.toolController.CurrentTool = current;

return tool;
}

public static void RemoveTool<T>(T tool)
where T : ToolBase
{
GameObject.Destroy(tool);
}

/// <summary>
/// ToolsModifierControl.GetTool
/// But instead of searching the tool in ToolsModifierControl.m_Tools,
/// this method uses ToolsModifierControl.toolController.gameObject.GetComponent<T>
/// </summary>
public static T GetTool<T>()
where T : ToolBase
{
return ToolsModifierControl.toolController.GetComponent<T>();
}

/// <summary>
/// ToolsModifierControl.SetTool
/// But instead of searching the tool in ToolsModifierControl.m_Tools,
/// this method uses ToolsModifierControl.toolController.gameObject.GetComponent<T>
/// </summary>
public static T SetTool<T>()
where T : ToolBase
{
var toolController = ToolsModifierControl.toolController;
if (toolController == null)
{
return null;
}
var tool = toolController.GetComponent<T>();
if (tool == null)
{
return null;
}

if (!ToolsModifierControl.keepThisWorldInfoPanel)
{
WorldInfoPanel.HideAllWorldInfoPanels();
}
GameAreaInfoPanel.Hide();
ToolsModifierControl.keepThisWorldInfoPanel = false;
if (toolController.CurrentTool != tool)
{
toolController.CurrentTool = tool;
}
return tool;
}
}
}

0 comments on commit 8f6d388

Please sign in to comment.