Last active
September 13, 2020 21:41
-
-
Save ronyx69/e556609f78efd918aa5895261d38d78e to your computer and use it in GitHub Desktop.
Source code of the transparent selectors mod.
This file contains hidden or 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.IO; | |
using ColossalFramework.UI; | |
using ICities; | |
using System; | |
using System.IO; | |
using System.Reflection; | |
using System.Xml.Serialization; | |
using UnityEngine; | |
namespace TransparentSelectors | |
{ | |
public class TransparentSelectorsMod : IUserMod | |
{ | |
public static readonly string m_name = "Transparent Selectors"; | |
public string Name => m_name; | |
public string Description => "Makes hover selectors transparent."; | |
internal static readonly string settingsFilePath = Path.Combine(DataLocation.localApplicationData, "TransparentSelectors.xml"); | |
private static TransparentSelectorsSettings m_settings; | |
public static TransparentSelectorsSettings Settings | |
{ | |
get | |
{ | |
if (m_settings == null) | |
{ | |
m_settings = TransparentSelectorsSettings.LoadConfiguration(); | |
if (m_settings == null) | |
{ | |
m_settings = new TransparentSelectorsSettings(); | |
m_settings.SaveConfiguration(); | |
} | |
} | |
return m_settings; | |
} | |
} | |
private string[] modes = new string[] { ((TooltipMode)0).ToString(), ((TooltipMode)1).ToString(), ((TooltipMode)2).ToString() }; | |
private UISlider slider; | |
private UIDropDown dropDown; | |
public void OnSettingsUI(UIHelperBase helper) | |
{ | |
helper.AddSpace(20); | |
slider = (UISlider)helper.AddSlider("Overlay Alpha", 0f, 255f, 1f, Settings.OverlayAlpha, (f) => | |
{ | |
Settings.OverlayAlpha = f; | |
Settings.SaveConfiguration(); | |
if (TransparentSelectors.Loaded) TransparentSelectors.Transparent(); | |
slider.tooltip = $"{Settings.OverlayAlpha}\n\nSet opacity level for tool overlays."; | |
slider.RefreshTooltip(); | |
} | |
); | |
slider.width = 510f; | |
slider.height = 10f; | |
slider.color = Color.cyan; | |
slider.scrollWheelAmount = 1f; | |
slider.tooltip = $"{Settings.OverlayAlpha}\n\nSet opacity level for tool overlays."; | |
helper.AddSpace(20); | |
dropDown = (UIDropDown)helper.AddDropdown("Construction Cost Tooltip", modes, Settings.TooltipMode, (i) => | |
{ | |
Settings.TooltipMode = i; | |
Settings.SaveConfiguration(); | |
if (TransparentSelectors.Loaded) TransparentSelectors.Transparent(); | |
}); | |
} | |
} | |
public class TransparentSelectorsSettings | |
{ | |
public float OverlayAlpha = 30f; | |
public int TooltipMode = 0; | |
public TransparentSelectorsSettings() { } | |
public void OnPreSerialize() { } | |
public void OnPostDeserialize() { } | |
public void SaveConfiguration() | |
{ | |
var fileName = TransparentSelectorsMod.settingsFilePath; | |
var config = TransparentSelectorsMod.Settings; | |
var serializer = new XmlSerializer(typeof(TransparentSelectorsSettings)); | |
using (var writer = new StreamWriter(fileName)) | |
{ | |
config.OnPreSerialize(); | |
serializer.Serialize(writer, config); | |
} | |
} | |
public static TransparentSelectorsSettings LoadConfiguration() | |
{ | |
var fileName = TransparentSelectorsMod.settingsFilePath; | |
var serializer = new XmlSerializer(typeof(TransparentSelectorsSettings)); | |
try | |
{ | |
using (var reader = new StreamReader(fileName)) | |
{ | |
var config = serializer.Deserialize(reader) as TransparentSelectorsSettings; | |
return config; | |
} | |
} | |
catch (Exception ex) | |
{ | |
Debug.Log($"[{TransparentSelectorsMod.m_name}]: Error Parsing {fileName}: {ex}"); | |
return null; | |
} | |
} | |
} | |
public class TransparentSelectorsLoading : LoadingExtensionBase | |
{ | |
public override void OnLevelUnloading() | |
{ | |
base.OnLevelUnloading(); | |
TransparentSelectors.Loaded = false; | |
} | |
} | |
public class TransparentSelectorsThreading : ThreadingExtensionBase | |
{ | |
public override void OnUpdate(float realTimeDelta, float simulationTimeDelta) | |
{ | |
base.OnUpdate(realTimeDelta, simulationTimeDelta); | |
if (!TransparentSelectors.Loaded && LoadingManager.instance.m_loadingComplete) | |
{ | |
TransparentSelectors.Transparent(); | |
TransparentSelectors.Loaded = true; | |
} | |
} | |
} | |
public enum TooltipMode | |
{ | |
None = -1, | |
Transparent, | |
Disabled, | |
Vanilla, | |
Count | |
} | |
public class TransparentSelectors | |
{ | |
public static bool Loaded; | |
internal static void Transparent() | |
{ | |
float alpha = TransparentSelectorsMod.Settings.OverlayAlpha / 255f; | |
var settings = TransparentSelectorsMod.Settings; | |
ToolController toolController = ToolsModifierControl.toolController; | |
toolController.m_errorColor.a = toolController.m_errorColorInfo.a = | |
toolController.m_warningColor.a = toolController.m_warningColorInfo.a = | |
toolController.m_validColor.a = toolController.m_validColorInfo.a = alpha; | |
GameObject.Find("CursorInfo").GetComponent<UILabel>().opacity = settings.TooltipMode == (int)TooltipMode.Disabled ? 0f : settings.TooltipMode == (int)TooltipMode.Vanilla ? 1f : alpha; | |
try | |
{ | |
Color oldColor; Color newColor; FieldInfo field; | |
field = Type.GetType("MoveIt.MoveItTool, MoveIt").GetField("m_hoverColor", BindingFlags.NonPublic | BindingFlags.Static); | |
if (field == null) goto Prop_Line_Tool; | |
oldColor = (Color)field.GetValue(null); | |
newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alpha); | |
field.SetValue(null, newColor); | |
field = Type.GetType("MoveIt.MoveItTool, MoveIt").GetField("m_moveColor", BindingFlags.NonPublic | BindingFlags.Static); | |
if (field == null) goto Prop_Line_Tool; | |
oldColor = (Color)field.GetValue(null); | |
newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alpha); | |
field.SetValue(null, newColor); | |
field = Type.GetType("MoveIt.MoveItTool, MoveIt").GetField("m_selectedColor", BindingFlags.NonPublic | BindingFlags.Static); | |
if (field == null) goto Prop_Line_Tool; | |
oldColor = (Color)field.GetValue(null); | |
newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alpha); | |
field.SetValue(null, newColor); | |
} | |
catch (Exception exception) | |
{ | |
Debug.Log($"[{TransparentSelectorsMod.m_name}]: {exception}"); | |
} | |
Prop_Line_Tool: | |
try | |
{ | |
Type propLineTool = Type.GetType("PropLineTool.PropLineTool, PropLineTool_v1"); | |
if (propLineTool == null) return; | |
FieldInfo field = propLineTool.GetField("m_PLTColor_default", BindingFlags.Public | BindingFlags.Instance); | |
object component = toolController.GetComponent(propLineTool);//this time it's an instance field, so we need to get a reference to the instance of PropLineTool. | |
Color oldColor = (Color)field.GetValue(component);//then we get the value from the instance | |
Color newColor = new Color(oldColor.r, oldColor.g, oldColor.b, alpha); | |
field.SetValue(component, newColor); | |
} | |
catch (Exception exception) | |
{ | |
Debug.Log($"[{TransparentSelectorsMod.m_name}]: {exception}"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment