PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/Settings.cs

https://bitbucket.org/rstarkov/tankiconmaker
C# | 158 lines | 96 code | 18 blank | 44 comment | 19 complexity | 75237c1176168fb89334053e5b179189 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, CC-BY-SA-3.0
  1. using System;
  2. using System.Linq;
  3. using System.Windows.Media;
  4. using System.Xml.Linq;
  5. using RT.Util;
  6. using RT.Util.Forms;
  7. using RT.Util.Lingo;
  8. using RT.Util.Serialization;
  9. using WotDataLib;
  10. using WpfCrutches;
  11. namespace TankIconMaker
  12. {
  13. /// <summary>
  14. /// Stores all the settings for TankIconMaker. All the defaults set in the field initializer or the constructor are preserved
  15. /// whenever they are missing from the settings XML file. Background saves are supported efficiently via
  16. /// <see cref="SaveThreaded"/>, which is designed to be callable efficiently multiple times in a row.
  17. /// </summary>
  18. [Settings("TankIconMaker2", SettingsKind.UserSpecific)]
  19. sealed class Settings : SettingsThreadedBase, IClassifyXmlObjectProcessor
  20. {
  21. /// <summary>Stores the program version that was used to save this settings file.</summary>
  22. public int SavedByVersion = 17; // this feature was introduced in v018, so default to 17 for "everything before that".
  23. /// <summary>
  24. /// MainWindow settings, such as position, size and maximized state. Given that TankIconMaker is essentially a single-window
  25. /// application, all the other MainWindow-specific settings are stored directly in <see cref="Settings"/> for simplicity.
  26. /// </summary>
  27. public ManagedWindow.Settings MainWindow = new ManagedWindow.Settings();
  28. /// <summary>AddWindow-related settings.</summary>
  29. public ManagedWindow.Settings AddWindow = new ManagedWindow.Settings();
  30. /// <summary>Settings for all the short prompts shown using the PromptWindow.</summary>
  31. public ManagedWindow.Settings RenameWindow = new ManagedWindow.Settings();
  32. /// <summary>Settings for the path template edit prompts.</summary>
  33. public ManagedWindow.Settings PathTemplateWindow = new ManagedWindow.Settings();
  34. /// <summary>CheckListWindow-related settings.</summary>
  35. public ManagedWindow.Settings CheckListWindow = new ManagedWindow.Settings();
  36. /// <summary>CheckListWindow-related settings.</summary>
  37. public ManagedWindow.Settings BulkSaveSettingsWindow = new ManagedWindow.Settings();
  38. /// <summary>Translation window related settings.</summary>
  39. public TranslationForm<Translation>.Settings TranslationFormSettings = new TranslationForm<Translation>.Settings();
  40. /// <summary>The width of the sidebar in the main window; null to use the width set at design time.</summary>
  41. public double? LeftColumnWidth = null;
  42. /// <summary>The width of the name column in the maker property editor; null to use the width set at design time.</summary>
  43. public double? NameColumnWidth = null;
  44. /// <summary>The last used index of the display mode dropdown.</summary>
  45. public DisplayFilter DisplayFilter = DisplayFilter.All;
  46. /// <summary>The path last used with the "Save to folder" command.</summary>
  47. public string SaveToFolderPath = null;
  48. public string SaveToAtlas = null;
  49. /// <summary>The path last used with the "Bulk save to folder" command.</summary>
  50. public string BulkSaveToFolderPath = null;
  51. /// <summary>Specifies the name of the selected background file, or ":checkered" / ":solid" for these special backgrounds.</summary>
  52. public string Background = "Ruinberg (Руинберг).jpg";
  53. public Color BackgroundCheckeredColor1 = Color.FromRgb(0xC0, 0xC0, 0xC0);
  54. public Color BackgroundCheckeredColor2 = Color.FromRgb(0xA0, 0xA0, 0xA0);
  55. public Color BackgroundSolidColor = Color.FromRgb(0x80, 0xC0, 0xFF);
  56. public int[] CustomColors = new int[] { 0xC0C0C0, 0xFFC080, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xA0A0A0, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0xFFFFFF };
  57. /// <summary>Specifies the scale at which icons are displayed with the "Zoom" checkbox checked or unchecked. The scale is relative to screen pixels, so a scale of 1 shows 1:1 pixels.</summary>
  58. public double IconScaleNormal = 1;
  59. public double IconScaleZoomed = 5;
  60. /// <summary>Specifies a zoom factor for the entire UI, except for the icons.</summary>
  61. public double UiZoom = 1;
  62. /// <summary>Program language, and also the default language for property values when not specified.</summary>
  63. public Language Lingo = Language.EnglishUK;
  64. /// <summary>The language of the operating system selected last time the program started. When this changes, <see cref="Lingo"/> is autoselected.</summary>
  65. public Language? OsLingo = null;
  66. /// <summary>When a property name is specified without the author, and multiple authors are available, this author is given preference.</summary>
  67. public string DefaultPropertyAuthor = "Wargaming";
  68. /// <summary>Holds every game installation the user added to the program, as well as any installation-specific settings (currently none).</summary>
  69. public ObservableSortedList<TimGameInstallation> GameInstallations = new ObservableSortedList<TimGameInstallation>();
  70. /// <summary>Remembers which game installation the user selected.</summary>
  71. [Obsolete("Use this only for settings!")] // this field should only be used on startup to load the value and on change to save it. At all other times this should be retrieved either directly from the GUI or from the relevant WotContext.
  72. public TimGameInstallation ActiveInstallation;
  73. /// <summary>A list of all the available user styles. Built-in styles are not stored in the settings file.</summary>
  74. public ObservableSortedList<Style> Styles = new ObservableSortedList<Style>();
  75. /// <summary>
  76. /// The authoritative source on which of the game styles the user has activated in the UI. This is only null on first run, until the initialization
  77. /// has finished. Note that this may also be set to a built-in style, even though those aren't stored in <see cref="Styles"/>; in this case,
  78. /// the correct built-in style instance is substituted on startup by matching the style name.
  79. /// </summary>
  80. public Style ActiveStyle;
  81. protected override SettingsThreadedBase CloneForSaveThreaded()
  82. {
  83. // Note: this currently makes only a partial clone. This means that the user could potentially change some important setting
  84. // while a background save is happening, resulting in a save that's inconsistent. The risk is not non-existent, but fairly low.
  85. // Still, doing a deep clone here would be nice.
  86. var result = (Settings) MemberwiseClone();
  87. return result;
  88. }
  89. #region Upgrade-related
  90. void IClassifyObjectProcessor<XElement>.AfterSerialize(XElement element) { }
  91. void IClassifyObjectProcessor<XElement>.BeforeDeserialize(XElement element) { }
  92. void IClassifyObjectProcessor<XElement>.BeforeSerialize() { }
  93. void IClassifyObjectProcessor<XElement>.AfterDeserialize(XElement element)
  94. {
  95. // Some people have strange broken styles in their settings, probably added due to some bug in an earlier version of the program.
  96. Styles.RemoveWhere(style => style.Layers == null || style.Layers.Count == 0);
  97. foreach (var style in Styles)
  98. {
  99. if (style.Name == null)
  100. style.Name = "<unknown>";
  101. if (style.Author == null)
  102. style.Author = "<unknown>";
  103. if (style.PathTemplate == null)
  104. style.PathTemplate = "";
  105. }
  106. // Added in v019
  107. if (SavedByVersion < 19 && GameInstalls != null)
  108. GameInstallations = GameInstalls;
  109. #pragma warning disable 0618 // ActiveInstallation should only be used for loading/saving the setting, which is what the code below does.
  110. if (SavedByVersion < 19 && SelectedGamePath != null)
  111. ActiveInstallation = GameInstallations.Where(gi => gi.Path.EqualsNoCase(SelectedGamePath)).FirstOrDefault() ?? GameInstallations.FirstOrDefault();
  112. #pragma warning restore 0618
  113. if (SavedByVersion < 19 && SelectedStyleNameAndAuthor != null)
  114. // This is a fairly approximate match but this way at least some users will see the right style still selected. The old property was too lossy to allow for reliable matching.
  115. ActiveStyle = Styles.FirstOrDefault(s => SelectedStyleNameAndAuthor.Contains(s.Name) && SelectedStyleNameAndAuthor.Contains(s.Author));
  116. GameInstalls = null;
  117. SelectedGamePath = null;
  118. SelectedStyleNameAndAuthor = null;
  119. }
  120. // Fields obsoleted in v019
  121. [ClassifyIgnoreIfDefault]
  122. private ObservableSortedList<TimGameInstallation> GameInstalls;
  123. [ClassifyIgnoreIfDefault]
  124. private string SelectedGamePath;
  125. [ClassifyIgnoreIfDefault]
  126. private string SelectedStyleNameAndAuthor;
  127. #endregion
  128. }
  129. /// <summary>Specifies which tank icons are to be displayed in the preview area.</summary>
  130. /// <remarks>The numeric value corresponds to the index of the relevant item in the filter drop-down.</remarks>
  131. enum DisplayFilter
  132. {
  133. All = 0,
  134. OneOfEach = 1,
  135. USSR = 3, Germany, USA, France, UK, China, Japan, Czech, Sweden,
  136. Light = 13, Medium, Heavy, Artillery, Destroyer,
  137. Normal = 19, Premium, Special,
  138. TierLow = 23, TierMedHigh, TierHigh,
  139. }
  140. }