PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Style.cs

https://bitbucket.org/rstarkov/tankiconmaker
C# | 181 lines | 139 code | 24 blank | 18 comment | 14 complexity | 7951b3ff08cfd00bd85be41206544b98 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.Collections.ObjectModel;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Xml.Linq;
  7. using RT.Util.Serialization;
  8. namespace TankIconMaker
  9. {
  10. enum StyleKind { Original, Current, BuiltIn, User }
  11. sealed class Style : INotifyPropertyChanged, IComparable<Style>, IClassifyXmlObjectProcessor
  12. {
  13. /// <summary>The name of the style (chosen by the artist).</summary>
  14. public string Name { get { return _Name; } set { _Name = value; NotifyPropertyChanged("Name"); NotifyPropertyChanged("Display"); } }
  15. private string _Name;
  16. /// <summary>The name of the author of this style.</summary>
  17. public string Author { get { return _Author; } set { _Author = value; NotifyPropertyChanged("Author"); NotifyPropertyChanged("Display"); } }
  18. private string _Author;
  19. /// <summary>A template for the path where the icons are to be saved.</summary>
  20. public string PathTemplate { get { return _PathTemplate; } set { _PathTemplate = value; NotifyPropertyChanged("PathTemplate"); } }
  21. private string _PathTemplate;
  22. /// <summary>A template for the path where the battleAtlas are to be saved.</summary>
  23. public string BattleAtlasPathTemplate { get { return _BattleAtlasPathTemplate; } set { _BattleAtlasPathTemplate = value; NotifyPropertyChanged("BattleAtlasPathTemplate"); } }
  24. private string _BattleAtlasPathTemplate;
  25. /// <summary>A template for the path where the vehicleMarkersAtlas are to be saved.</summary>
  26. public string VehicleMarkersAtlasPathTemplate { get { return _VehicleMarkersAtlasPathTemplate; } set { _VehicleMarkersAtlasPathTemplate = value; NotifyPropertyChanged("VehicleMarkersAtlasPathTemplate"); } }
  27. private string _VehicleMarkersAtlasPathTemplate;
  28. /// <summary>Enable/disable saving the icons.</summary>
  29. public bool IconsBulkSaveEnabled
  30. {
  31. get { return _IconsBulkSaveEnabled; }
  32. set
  33. {
  34. _IconsBulkSaveEnabled = value;
  35. NotifyPropertyChanged("IconsBulkSaveEnabled");
  36. }
  37. }
  38. private bool _IconsBulkSaveEnabled = true;
  39. /// <summary>Enable/disable saving the battleAtlas.</summary>
  40. public bool BattleAtlasBulkSaveEnabled
  41. {
  42. get { return _BattleAtlasBulkSaveEnabled; }
  43. set
  44. {
  45. _BattleAtlasBulkSaveEnabled = value;
  46. NotifyPropertyChanged("BattleAtlasBulkSaveEnabled");
  47. }
  48. }
  49. private bool _BattleAtlasBulkSaveEnabled = false;
  50. /// <summary>Enable/disable saving the vehicleMarkersAtlas.</summary>
  51. public bool VehicleMarkersAtlasBulkSaveEnabled
  52. {
  53. get { return _VehicleMarkersAtlasBulkSaveEnabled; }
  54. set
  55. {
  56. _VehicleMarkersAtlasBulkSaveEnabled = value;
  57. NotifyPropertyChanged("VehicleMarkersAtlasBulkSaveEnabled");
  58. }
  59. }
  60. private bool _VehicleMarkersAtlasBulkSaveEnabled = false;
  61. /// <summary>Icon width; defaults to the value used in old clients so that old styles can be loaded correctly.</summary>
  62. public int IconWidth = 80;
  63. /// <summary>Icon height; defaults to the value used in old clients so that old styles can be loaded correctly.</summary>
  64. public int IconHeight = 24;
  65. /// <summary>
  66. /// If true, the number of transparent pixels on the right will be made the same as on the left (up to <see
  67. /// cref="IconWidth"/>), so as to make it possible to vertically center the icon. Defaults to the value used in old
  68. /// clients so that old styles can be loaded correctly.</summary>
  69. public bool Centerable = false;
  70. public string Display
  71. {
  72. get
  73. {
  74. switch (Kind)
  75. {
  76. case StyleKind.Original: return App.Translation.Misc.StyleDisplay_Original;
  77. case StyleKind.Current: return App.Translation.Misc.StyleDisplay_Current;
  78. case StyleKind.BuiltIn: return App.Translation.Misc.StyleDisplay_BuiltIn.Fmt(Name, Author);
  79. case StyleKind.User: return App.Translation.Misc.StyleDisplay_Normal.Fmt(Name, Author);
  80. default: throw new Exception("9742978");
  81. }
  82. }
  83. }
  84. /// <summary>A list of layers that this style is made up of.</summary>
  85. public ObservableCollection<LayerBase> Layers = new ObservableCollection<LayerBase>();
  86. /// <summary>A link to the forum post by the original author describing this style. Only used for built-in styles.</summary>
  87. public string ForumLink { get; set; }
  88. /// <summary>Determines whether this style is a built-in one. Not saved as a setting.</summary>
  89. [ClassifyIgnore]
  90. public StyleKind Kind { get; set; }
  91. public Style()
  92. {
  93. Kind = StyleKind.User;
  94. Layers.CollectionChanged += updateLayerStyle;
  95. }
  96. void updateLayerStyle(object sender, NotifyCollectionChangedEventArgs e)
  97. {
  98. if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace)
  99. foreach (var item in e.NewItems.OfType<LayerBase>())
  100. item.ParentStyle = this;
  101. else if (e.Action == NotifyCollectionChangedAction.Reset)
  102. foreach (var item in Layers)
  103. item.ParentStyle = this;
  104. }
  105. private void NotifyPropertyChanged(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); }
  106. public event PropertyChangedEventHandler PropertyChanged = (_, __) => { };
  107. public void TranslationChanged()
  108. {
  109. NotifyPropertyChanged("Name");
  110. NotifyPropertyChanged("Author");
  111. NotifyPropertyChanged("Display");
  112. }
  113. public int CompareTo(Style other)
  114. {
  115. if (other == null)
  116. return -1;
  117. int result = Kind.CompareTo(other.Kind);
  118. if (result != 0)
  119. return result;
  120. result = StringComparer.OrdinalIgnoreCase.Compare(_Name, other._Name);
  121. if (result != 0)
  122. return result;
  123. return StringComparer.OrdinalIgnoreCase.Compare(_Author, other._Author);
  124. }
  125. public override string ToString()
  126. {
  127. return Display;
  128. }
  129. public Style Clone()
  130. {
  131. var result = MemberwiseClone() as Style;
  132. result.PropertyChanged = (_, __) => { };
  133. result.Layers = new ObservableCollection<LayerBase>();
  134. result.Layers.CollectionChanged += result.updateLayerStyle;
  135. foreach (var l in Layers)
  136. result.Layers.Add(l.Clone());
  137. return result;
  138. }
  139. public void AfterDeserialize(XElement xml)
  140. {
  141. foreach (var layer in Layers)
  142. layer.ParentStyle = this;
  143. Layers.CollectionChanged -= updateLayerStyle;
  144. Layers.CollectionChanged += updateLayerStyle;
  145. }
  146. public void AfterSerialize(XElement xml) { }
  147. public void BeforeSerialize() { }
  148. public void BeforeDeserialize(XElement xml) { }
  149. }
  150. /// <summary>Thrown from a layer or effect implementation to report an error that the user can fix or needs to know about.</summary>
  151. class StyleUserError : Exception
  152. {
  153. public bool Formatted { get; private set; }
  154. public StyleUserError(string message) : this(message, false) { }
  155. public StyleUserError(string message, bool formatted) : base(message) { Formatted = formatted; }
  156. }
  157. }