Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AutoLineColor/AutoLineColor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="ColorMonitor.cs" />
<Compile Include="Configuration.cs" />
<Compile Include="Console.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Naming\DistrictNamingStrategy.cs" />
<Compile Include="Coloring\IColorStrategy.cs" />
<Compile Include="Naming\INamingStrategy.cs" />
Expand Down
31 changes: 25 additions & 6 deletions AutoLineColor/AutoLineColorMod.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
using ICities;
using System;
using System.Collections.Generic;

namespace AutoLineColor
{
public class AutoLineColorMod : IUserMod
{
private Configuration Config;
private static Console logger = Console.Instance;
public string Name
{
get { return "Auto Line Color"; }
get { return Constants.ModName; }
}

public string Description
{
get
{
return
"Monitors all transport line looking for lines set to the default color. When found it sets a new color and a line name";
}
get { return Constants.Description; }
}

public void OnSettingsUI(UIHelperBase helper)
{
Config = Configuration.Instance;
Config.FlushStagedChanges(); //make sure no prior changes are still around
//Generate arrays of colors and naming strategies
String[] ColorStrategies = Enum.GetNames(typeof(ColorStrategy));
String[] NamingStrategies = Enum.GetNames(typeof(NamingStrategy));
UIHelperBase group = helper.AddGroup(Constants.ModName);
group.AddDropdown("Color Strategy", ColorStrategies, (int)Config.ColorStrategy, Config.ColorStrategyChange);
group.AddDropdown("Naming Strategy", NamingStrategies, (int)Config.NamingStrategy, Config.NamingStrategyChange);
group.AddSpace(5);

group.AddGroup("Advanced Settings");
group.AddSlider("Max Different Color Picks", 1f, 20f, 1f, (float)Config.MaxDiffColorPickAttempt, Config.MaxDiffColorPickChange);
group.AddSlider("MinColorDifference", 1f, 100f, 5f, (float)Config.MinColorDiffPercentage, Config.MinColorDiffChange);
group.AddCheckbox("Debug", logger.debug, logger.SetDebug);
group.AddButton("Save", Config.Save);
}
}
}
117 changes: 62 additions & 55 deletions AutoLineColor/ColorMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,31 @@ namespace AutoLineColor
{
public class ColorMonitor : ThreadingExtensionBase
{
private static DateTimeOffset _lastOutputTime = DateTimeOffset.Now.AddSeconds(-100);
private static DateTimeOffset _nextUpdateTime = DateTimeOffset.Now;
private bool _initialized;
private IColorStrategy _colorStrategy;
private INamingStrategy _namingStrategy;
private List<Color32> _usedColors;
private Configuration _config;
private static Console logger = Console.Instance;

public override void OnCreated(IThreading threading)
{

Console.Message("loading auto color monitor");
Console.Message("initializing colors");
logger.Message("===============================");
logger.Message("Initializing auto color monitor");
logger.Message("Initializing colors");
RandomColor.Initialize();
CategorisedColor.Initialize();
GenericNames.Initialize();

Console.Message("loading current config");
var config = Configuration.LoadConfig();
_colorStrategy = SetColorStrategy(config.ColorStrategy);
_namingStrategy = SetNamingStrategy(config.NamingStrategy);
logger.Message("Loading current config");
_config = Configuration.Instance;
_colorStrategy = SetColorStrategy(_config.ColorStrategy);
_namingStrategy = SetNamingStrategy(_config.NamingStrategy);
_usedColors = new List<Color32>();

Console.Message("Found color strategy of " + config.ColorStrategy);
Console.Message("Found naming strategy of " + config.NamingStrategy);
logger.Message("Found color strategy of " + _config.ColorStrategy);
logger.Message("Found naming strategy of " + _config.NamingStrategy);

_initialized = true;

Expand All @@ -45,6 +47,7 @@ public override void OnCreated(IThreading threading)

private static INamingStrategy SetNamingStrategy(NamingStrategy namingStrategy)
{
logger.Message("Naming Strategy: " + namingStrategy.ToString());
switch (namingStrategy)
{
case NamingStrategy.None:
Expand All @@ -54,13 +57,14 @@ private static INamingStrategy SetNamingStrategy(NamingStrategy namingStrategy)
case NamingStrategy.London:
return new LondonNamingStrategy();
default:
Console.Error("unknown naming strategy");
logger.Error("unknown naming strategy");
return new NoNamingStrategy();
}
}

private static IColorStrategy SetColorStrategy(ColorStrategy colorStrategy)
private IColorStrategy SetColorStrategy(ColorStrategy colorStrategy)
{
logger.Message("Color Strategy: " + colorStrategy.ToString());
switch (colorStrategy)
{
case ColorStrategy.RandomHue:
Expand All @@ -70,85 +74,88 @@ private static IColorStrategy SetColorStrategy(ColorStrategy colorStrategy)
case ColorStrategy.CategorisedColor:
return new CategorisedColorStrategy();
default:
Console.Error("unknown color strategy");
logger.Error("unknown color strategy");
return new RandomHueStrategy();
}
}

public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
{
{ //Digest changes
if (_config.UndigestedChanges == true) {
logger.Message("Applying undigested changes");
_colorStrategy = SetColorStrategy(_config.ColorStrategy);
_namingStrategy = SetNamingStrategy(_config.NamingStrategy);
_config.UndigestedChanges = false;
}

if (_initialized == false)
return;

// try and limit how often we are scanning for lines. this ain't that important
if (_nextUpdateTime >= DateTimeOffset.Now)
return;

var theTransportManager = Singleton<TransportManager>.instance;
var lines = theTransportManager.m_lines.m_buffer;

try
{
if (_initialized == false)
return;


// try and limit how often we are scanning for lines. this ain't that important
if (_lastOutputTime.AddMilliseconds(1000) >= DateTimeOffset.Now)
return;



_lastOutputTime = DateTimeOffset.Now;

while (!Monitor.TryEnter(lines, SimulationManager.SYNCHRONIZE_TIMEOUT))
{ }


_nextUpdateTime = DateTimeOffset.Now.AddSeconds(10);
_usedColors = lines.Where(l => l.IsActive()).Select(l => l.m_color).ToList();

for (ushort counter = 0; counter < lines.Length - 1; counter++)
for (ushort i = 0; i < theTransportManager.m_lines.m_buffer.Length - 1; i++)
{
var transportLine = lines[counter];
<<<<<<< Updated upstream
=======

var transportLine = lines[i];
//logger.Message(string.Format("Starting on line {0}", i));

>>>>>>> Stashed changes
if (transportLine.m_flags == TransportLine.Flags.None)
continue;

if (!transportLine.Complete)
continue;

// only worry about fully created lines
if (!transportLine.IsActive() || transportLine.HasCustomName() || !transportLine.m_color.IsDefaultColor())
continue;

logger.Message("work to be done!");

var lineName = _namingStrategy.GetName(transportLine);
var color = _colorStrategy.GetColor(transportLine, _usedColors);
logger.Message(string.Format("Working on line {0}", i));

Console.Message(string.Format("New line found. {0} {1}", lineName, color));
var instanceID = new InstanceID();
var lineName = theTransportManager.GetLineName(i);
var newName = _namingStrategy.GetName(transportLine);

if (!transportLine.HasCustomColor() || transportLine.m_color.IsDefaultColor())
{
// set the color
var color = _colorStrategy.GetColor(transportLine, _usedColors);

logger.Message(string.Format("About to change line color to {0}", color));
transportLine.m_color = color;
transportLine.m_flags |= TransportLine.Flags.CustomColor;
}
else
{
Console.Message(transportLine.m_color.ToString());
theTransportManager.SetLineColor(i, color);

logger.Message(string.Format("Changed line color. '{0}' {1} -> {2}", lineName, theTransportManager.GetLineColor(i), color));
}

if (string.IsNullOrEmpty(lineName) == false && transportLine.HasCustomName() == false)
{
logger.Message("New line name time!");
// set the name
Singleton<InstanceManager>.instance.SetName(new InstanceID { TransportLine = counter },
lineName);
if (string.IsNullOrEmpty(newName) == false &&
transportLine.HasCustomName() == false) {
logger.Message(string.Format("About to rename line to {0}", newName));

transportLine.m_flags |= TransportLine.Flags.CustomName;
theTransportManager.SetLineName(i, newName);

logger.Message(string.Format("Renamed Line '{0}' -> '{1}'", lineName, newName));
}

lines[counter] = transportLine;
logger.Message(string.Format("Line is now {0} and {1}",
theTransportManager.GetLineName(i),
theTransportManager.GetLineColor(i)));

lines[i] = transportLine;
}
}
catch (Exception ex)
{
Console.Message(ex.ToString(), PluginManager.MessageType.Message);
logger.Error(ex.ToString());
}
finally
{
Expand Down Expand Up @@ -180,7 +187,7 @@ public static bool IsColorEqual(this Color32 color1, Color32 color2)

public static bool IsActive(this TransportLine transportLine)
{
if ((transportLine.m_flags & (TransportLine.Flags.Complete | TransportLine.Flags.Created | TransportLine.Flags.Hidden)) == 0)
if ((transportLine.m_flags & (TransportLine.Flags.Created | TransportLine.Flags.Hidden)) == 0)
return false;

// stations are marked with this flag
Expand Down
6 changes: 3 additions & 3 deletions AutoLineColor/Coloring/CategorisedColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CategorisedColor
private const string DefaultPaleColors = "#bccaff, #ffbcbc, #bcffd2, #d6bcff, #fffbbc, #bcddff, #ffbcc6, #bcf7bd, #c2bcff, #ffe4bc, #bcf3ff, #ffbce0, #c7ffbc, #bcbcff, #ffc1bc, #bcffe8, #f5bcff, #daffbc, #a6baff, #ffa6a6, #a6ffc4, #cba6ff, #fffaa6, #a6d3ff, #ffa6b5, #a6f5a8, #afa6ff, #ffdda6, #a6efff, #ffa6d7, #b6ffa6, #a6a7ff, #ffaea6, #a6ffe1, #f3a6ff, #d0ffa6, #7f9eff, #ff7f7f, #7fffae, #b77fff, #fff97f, #7fc3ff, #ff7f97, #7ff282, #8d7fff, #ffd17f, #7feaff, #ff7fc9, #97ff7f, #7f80ff, #ff8c7f, #7fffd7, #ef7fff, #bfff7f";
private const string DefaultDarkColors = "#7f2200, #007f64, #75007f, #527f00, #00377f, #7f0100, #007f45, #4c007f, #7f7b00, #00567f, #7f002f, #007710, #24007f, #7f6000, #00727f, #7f005a, #307f00, #000a7f";


private static Console logger = Console.Instance;
private static List<Color32> _bright_colors;
private static List<Color32> _pale_colors;
private static List<Color32> _dark_colors;
Expand All @@ -80,13 +80,13 @@ private static List<Color32> BuildColorList(string defaultColorList, string file
}
else
{
Console.Message("No colors found, writing default values to " + fullPath);
logger.Message("No colors found, writing default values to " + fullPath);
File.WriteAllText(fullPath, unparsedColors);
}
}
catch (Exception ex)
{
Console.Error("error reading colors from disk " + ex);
logger.Error("error reading colors from disk " + ex);
}

// split on new lines, commas and semi-colons
Expand Down
1 change: 0 additions & 1 deletion AutoLineColor/Coloring/CategorisedColorStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public Color32 GetColor(TransportLine transportLine)
}
}


public Color32 GetColor(TransportLine transportLine, System.Collections.Generic.List<Color32> usedColors)
{
switch (transportLine.Info.m_transportType)
Expand Down
13 changes: 7 additions & 6 deletions AutoLineColor/Coloring/RandomColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RandomColor
private const string DefaultGreenColors = "#6af500, #54b807, #7ad633, #8df53d, #467a1f, #66a832, #629939, #527a33, #87c756, #b6f587, #75995a, #a9d687, #97b87f, #738a62, #349912, #54d629, #3d8a24, #56b835, #83f55d, #87e667, #70b858, #629950, #8ed676, #7fb86c, #567a49, #b8f5a4, #087a00, #13a808, #2f7a2a, #45a83e, #427a3e, #a0f59a, #9cd698, #0be625, #3ed650, #4ef562, #6bd678, #7ff58d, #65b86f, #57995f, #79b880, #547a59, #06b83b, #048a2c, #2ab855, #28994a, #49e678, #2f8a4a, #367a4a, #74d691, #89f5a9, #5d996f, #93e6ac, #7fb890, #98d6ab";
private const string DefaultOrangeColors = "#a80000, #c70202, #e51515, #991212, #c71c1c, #e52c2c, #b82727, #992525, #f52f02, #b82504, #991f03, #e5411c, #99311a, #b84025, #f55d3b, #d65133, #b84402, #f55e07, #d65911, #99400c, #c7632a, #a85525, #f57d38, #e58005, #c76f04, #a86718, #f5992a, #c7812c, #996423, #997000, #b88806, #f5bc20, #d6a51e, #a88628, #d6c400, #a89c16, #f5e322, #c7ba2c";

private static Console logger = Console.Instance;
private static Dictionary<ColorFamily, Color32[]> _colors;

public static void Initialize()
Expand Down Expand Up @@ -49,13 +50,13 @@ private static Color32[] BuildColorList(string defaultColorList, string fileName
}
else
{
Console.Message("No colors found, writing default values to " + fullPath);
logger.Message("No colors found, writing default values to " + fullPath);
File.WriteAllText(fullPath, unparsedColors);
}
}
catch (Exception ex)
{
Console.Error("error reading colors from disk " + ex);
logger.Error("error reading colors from disk " + ex);
}

// split on new lines, commas and semi-colons
Expand Down Expand Up @@ -117,7 +118,7 @@ public static Color32 GetColor(ColorFamily colorFamily, List<Color32> usedColors
color = GetColor(colorFamily);
difference = CompareColorWithUsedColors(usedColors, color);

} while (difference < Configuration.Instance.MinimumColorDifferencePercentage && (atempts < Configuration.Instance.MaximunDifferentCollorPickAtempt));
} while (difference < Configuration.Instance.MinColorDiffPercentage && (atempts < Configuration.Instance.MaxDiffColorPickAttempt));

if (difference <= 0)
{
Expand All @@ -130,7 +131,7 @@ public static Color32 GetColor(ColorFamily colorFamily, List<Color32> usedColors
{
color = colorItem;
differentColorFound = true;
Console.Message(string.Format("Color not repeated: {0} Color2: {2} Diference: {1}", color, CompareColorWithUsedColors(usedColors, color), usedColor));
logger.Message(string.Format("Color not repeated: {0} Color2: {2} Diference: {1}", color, CompareColorWithUsedColors(usedColors, color), usedColor));
break;
}
}
Expand All @@ -140,7 +141,7 @@ public static Color32 GetColor(ColorFamily colorFamily, List<Color32> usedColors

}

Console.Message(string.Format("Diference: {0} Atempts: {1}", difference, atempts));
logger.Message(string.Format("Diference: {0} Atempts: {1}", difference, atempts));

return color;
}
Expand Down Expand Up @@ -176,7 +177,7 @@ public static double CompareColors(Color32 color1, Color32 color2)
var p = d / Math.Sqrt((255) ^ 2 + (255) ^ 2 + (255) ^ 2 );

if (Math.Abs(p) <= 0)
Console.Message(string.Format("Color1: {1} Color2: {2} D: {0}", d, color1, color2));
logger.Message(string.Format("Color1: {1} Color2: {2} D: {0}", d, color1, color2));
return p * 100;
}
}
Expand Down
2 changes: 1 addition & 1 deletion AutoLineColor/Coloring/RandomColorStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public Color32 GetColor(TransportLine transportLine)
{
return RandomColor.GetColor(ColorFamily.Any);
}

public Color32 GetColor(TransportLine transportLine, System.Collections.Generic.List<Color32> usedColors)
{
return RandomColor.GetColor(ColorFamily.Any, usedColors);
Expand Down
Loading