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

/Effect.cs

https://bitbucket.org/rstarkov/tankiconmaker
C# | 90 lines | 61 code | 16 blank | 13 comment | 0 complexity | 42df90632ecf4e52a641db70b52f272d MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, GPL-3.0, CC-BY-SA-3.0
  1. using System.ComponentModel;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Xml.Linq;
  5. using RT.Util.Lingo;
  6. using RT.Util.Serialization;
  7. using WpfCrutches;
  8. namespace TankIconMaker
  9. {
  10. abstract class EffectBase : IHasTreeViewItem, IHasTypeNameDescription, IClassifyXmlObjectProcessor, INotifyPropertyChanged
  11. {
  12. /// <summary>Describes what this effect type does as concisely as possible.</summary>
  13. [Browsable(false)]
  14. public abstract string TypeName { get; }
  15. /// <summary>Describes what this effect type does in more detail.</summary>
  16. [Browsable(false)]
  17. public abstract string TypeDescription { get; }
  18. /// <summary>Specifies the version of this layer’s settings - incremented on changing layer settings backwards-incompatibly.</summary>
  19. [Browsable(false)]
  20. public abstract int Version { get; }
  21. [Browsable(false)]
  22. public string Name { get { return _Name; } set { _Name = value; NotifyPropertyChanged("Name"); NotifyPropertyChanged("NameVisibility"); } }
  23. private string _Name;
  24. [Browsable(false)]
  25. public Visibility NameVisibility { get { return string.IsNullOrEmpty(Name) ? Visibility.Collapsed : Visibility.Visible; } }
  26. /// <summary>Keeps track of the layer that this effect belongs to. This value is kept up-to-date automatically.</summary>
  27. [Browsable(false), ClassifyIgnore]
  28. public LayerBase Layer;
  29. public bool Visible { get { return _Visible; } set { _Visible = value; NotifyPropertyChanged("Visible"); } }
  30. private bool _Visible;
  31. public static MemberTr VisibleTr(Translation tr) { return new MemberTr(tr.Category.General, tr.LayerAndEffect.EffectVisible); }
  32. public ValueSelector<BoolWithPassthrough> VisibleFor { get; set; }
  33. public static MemberTr VisibleForTr(Translation tr) { return new MemberTr(tr.Category.General, tr.LayerAndEffect.EffectVisibleFor); }
  34. public EffectBase()
  35. {
  36. Visible = true;
  37. VisibleFor = new ValueSelector<BoolWithPassthrough>(BoolWithPassthrough.Yes);
  38. }
  39. /// <summary>
  40. /// Applies the effect to the specified layer. Returns the resulting image. The caller must make sure that the layer
  41. /// is writable, because all Apply methods expect to be able to modify the layer if necessary and return it, instead of
  42. /// creating a new instance.
  43. /// </summary>
  44. public abstract BitmapBase Apply(RenderTask renderTask, BitmapBase layer);
  45. /// <summary>
  46. /// Stores the <see cref="Version"/> of the maker as it was at the time of saving settings to XML. This may
  47. /// then be used to apply transformations to the XML produced by old versions of a layer.
  48. /// </summary>
  49. public int SavedByVersion;
  50. void IClassifyObjectProcessor<XElement>.BeforeSerialize() { BeforeSerialize(); }
  51. protected virtual void BeforeSerialize()
  52. {
  53. SavedByVersion = Version;
  54. }
  55. void IClassifyObjectProcessor<XElement>.AfterSerialize(XElement xml) { AfterSerialize(xml); }
  56. protected virtual void AfterSerialize(XElement xml) { }
  57. void IClassifyObjectProcessor<XElement>.BeforeDeserialize(XElement xml) { BeforeDeserialize(xml); }
  58. protected virtual void BeforeDeserialize(XElement xml) { }
  59. void IClassifyObjectProcessor<XElement>.AfterDeserialize(XElement xml) { AfterDeserialize(xml); }
  60. protected virtual void AfterDeserialize(XElement xml) { }
  61. [ClassifyIgnore, Browsable(false)]
  62. public TreeViewItem TreeViewItem { get; set; }
  63. protected void NotifyPropertyChanged(string name) { PropertyChanged(this, new PropertyChangedEventArgs(name)); }
  64. public event PropertyChangedEventHandler PropertyChanged = (_, __) => { };
  65. public virtual EffectBase Clone()
  66. {
  67. var result = MemberwiseClone() as EffectBase;
  68. result.Layer = null;
  69. result.PropertyChanged = (_, __) => { };
  70. result.VisibleFor = VisibleFor.Clone();
  71. return result;
  72. }
  73. }
  74. }