Options.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Drawing;
  8. using System.IO;
  9. using Newtonsoft.Json;
  10. namespace Optimizer
  11. {
  12. public class SettingsJson
  13. {
  14. public Theme Color { get; set; }
  15. }
  16. internal static class Options
  17. {
  18. readonly static string _themeFlag = "themeable";
  19. internal readonly static string SettingsFile = Required.CoreFolder + "\\Optimizer.json";
  20. internal static SettingsJson CurrentOptions = new SettingsJson();
  21. // use this to determine if changes have been made
  22. static SettingsJson _flag = new SettingsJson();
  23. internal static void ApplyTheme(Form f)
  24. {
  25. switch (CurrentOptions.Color)
  26. {
  27. case Theme.Caramel:
  28. SetTheme(f, Color.DarkOrange, Color.Chocolate);
  29. break;
  30. case Theme.Lime:
  31. SetTheme(f, Color.LimeGreen, Color.ForestGreen);
  32. break;
  33. case Theme.Magma:
  34. SetTheme(f, Color.Tomato, Color.Red);
  35. break;
  36. case Theme.Minimal:
  37. SetTheme(f, Color.Gray, Color.DimGray);
  38. break;
  39. case Theme.Ocean:
  40. SetTheme(f, Color.DodgerBlue, Color.RoyalBlue);
  41. break;
  42. case Theme.Zerg:
  43. SetTheme(f, Color.MediumOrchid, Color.DarkOrchid);
  44. break;
  45. }
  46. }
  47. private static void SetTheme(Form f, Color c1, Color c2)
  48. {
  49. Utilities.GetSelfAndChildrenRecursive(f).OfType<Button>().ToList().ForEach(b => b.BackColor = c1);
  50. Utilities.GetSelfAndChildrenRecursive(f).OfType<Button>().ToList().ForEach(b => b.FlatAppearance.BorderColor = c1);
  51. Utilities.GetSelfAndChildrenRecursive(f).OfType<Button>().ToList().ForEach(b => b.FlatAppearance.MouseDownBackColor = c2);
  52. Utilities.GetSelfAndChildrenRecursive(f).OfType<Button>().ToList().ForEach(b => b.FlatAppearance.MouseOverBackColor = c2);
  53. foreach (Label tmp in Utilities.GetSelfAndChildrenRecursive(f).OfType<Label>().ToList())
  54. {
  55. if ((string)tmp.Tag == _themeFlag)
  56. {
  57. tmp.ForeColor = c1;
  58. }
  59. }
  60. foreach (LinkLabel tmp in Utilities.GetSelfAndChildrenRecursive(f).OfType<LinkLabel>().ToList())
  61. {
  62. if ((string)tmp.Tag == _themeFlag)
  63. {
  64. tmp.LinkColor = c1;
  65. tmp.VisitedLinkColor = c1;
  66. tmp.ActiveLinkColor = c2;
  67. }
  68. }
  69. foreach (CheckBox tmp in Utilities.GetSelfAndChildrenRecursive(f).OfType<CheckBox>().ToList())
  70. {
  71. if ((string)tmp.Tag == _themeFlag)
  72. {
  73. tmp.ForeColor = c1;
  74. }
  75. }
  76. }
  77. internal static void SaveSettings()
  78. {
  79. if (File.Exists(SettingsFile))
  80. {
  81. // compare with flag to determine if changes have been made
  82. if (_flag.Color != CurrentOptions.Color)
  83. {
  84. using (FileStream fs = File.Open(SettingsFile, FileMode.OpenOrCreate))
  85. using (StreamWriter sw = new StreamWriter(fs))
  86. using (JsonWriter jw = new JsonTextWriter(sw))
  87. {
  88. jw.Formatting = Formatting.Indented;
  89. JsonSerializer serializer = new JsonSerializer();
  90. serializer.Serialize(jw, CurrentOptions);
  91. }
  92. }
  93. }
  94. }
  95. internal static void LoadSettings()
  96. {
  97. if (!File.Exists(SettingsFile))
  98. {
  99. CurrentOptions.Color = Theme.Zerg;
  100. using (FileStream fs = File.Open(SettingsFile, FileMode.CreateNew))
  101. using (StreamWriter sw = new StreamWriter(fs))
  102. using (JsonWriter jw = new JsonTextWriter(sw))
  103. {
  104. jw.Formatting = Formatting.Indented;
  105. JsonSerializer serializer = new JsonSerializer();
  106. serializer.Serialize(jw, CurrentOptions);
  107. }
  108. }
  109. else
  110. {
  111. CurrentOptions = JsonConvert.DeserializeObject<SettingsJson>(File.ReadAllText(SettingsFile));
  112. // initialize flag
  113. _flag.Color = CurrentOptions.Color;
  114. }
  115. }
  116. }
  117. }