CraftSynth.ImageEditor – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Drawing;
3 using System.Globalization;
4 using System.Reflection;
5 using System.Runtime.Serialization;
6  
7 namespace CraftSynth.ImageEditor
8 {
9 //[Serializable]
10 /// <summary>
11 /// Each <see cref="Layer"/> contains a list of Graphic Objects <see cref="GraphicsList"/>.
12 /// Each <see cref="Layer"/> is contained in the <see cref="Layers"/> collection.
13 /// Properties:
14 /// LayerName User-defined name of the Layer
15 /// Graphics Collection of <see cref="DrawObject"/>s in the Layer
16 /// IsVisible True if the <see cref="Layer"/> is visible
17 /// IsActive True if the <see cref="Layer"/> is the active <see cref="Layer"/> (only one <see cref="Layer"/> may be active at a time)
18 /// Dirty True if any object in the <see cref="Layer"/> is dirty
19 /// </summary>
20 public class Layer: IDisposable
21 {
22 private string _name;
23 private bool _isDirty;
24 private bool _visible;
25 private bool _active;
26 private GraphicsList _graphicsList;
27  
28 /// <summary>
29 /// <see cref="Layer"/> Name (User-defined)
30 /// </summary>
31 public string LayerName
32 {
33 get { return _name; }
34 set { _name = value; }
35 }
36  
37 /// <summary>
38 /// List of Graphic objects (derived from <see cref="DrawObject"/>) contained by this <see cref="Layer"/>
39 /// </summary>
40 public GraphicsList Graphics
41 {
42 get { return _graphicsList; }
43 set { _graphicsList = value; }
44 }
45  
46 /// <summary>
47 /// Returns True if this <see cref="Layer"/> is visible, else False
48 /// </summary>
49 public bool IsVisible
50 {
51 get { return _visible; }
52 set { _visible = value; }
53 }
54  
55 /// <summary>
56 /// Returns True if this is the active <see cref="Layer"/>, else False
57 /// </summary>
58 public bool IsActive
59 {
60 get { return _active; }
61 set { _active = value; }
62 }
63  
64 /// <summary>
65 /// Dirty is True if any elements in the contained <see cref="GraphicsList"/> are dirty, else False
66 /// </summary>
67 public bool Dirty
68 {
69 get
70 {
71 if (_isDirty == false)
72 _isDirty = _graphicsList.Dirty;
73 return _isDirty;
74 }
75 set
76 {
77 _graphicsList.Dirty = false;
78 _isDirty = false;
79 }
80 }
81  
82 private const string entryLayerName = "LayerName";
83 private const string entryLayerVisible = "LayerVisible";
84 private const string entryLayerActive = "LayerActive";
85 private const string entryObjectType = "ObjectType";
86 private const string entryGraphicsCount = "GraphicsCount";
87  
88 public void SaveToStream(SerializationInfo info, int orderNumber)
89 {
90 info.AddValue(
91 String.Format(CultureInfo.InvariantCulture,
92 "{0}{1}",
93 entryLayerName, orderNumber),
94 _name);
95  
96 info.AddValue(
97 String.Format(CultureInfo.InvariantCulture,
98 "{0}{1}",
99 entryLayerVisible, orderNumber),
100 _visible);
101  
102 info.AddValue(
103 String.Format(CultureInfo.InvariantCulture,
104 "{0}{1}",
105 entryLayerActive, orderNumber),
106 _active);
107  
108 info.AddValue(
109 String.Format(CultureInfo.InvariantCulture,
110 "{0}{1}",
111 entryGraphicsCount, orderNumber),
112 _graphicsList.Count);
113  
114 for (int i = 0; i < _graphicsList.Count; i++)
115 {
116 object o = _graphicsList[i];
117 info.AddValue(
118 String.Format(CultureInfo.InvariantCulture,
119 "{0}{1}-{2}",
120 entryObjectType, orderNumber, i),
121 o.GetType().FullName);
122  
123 ((DrawObject)o).SaveToStream(info, orderNumber, i);
124 }
125 }
126  
127 public void LoadFromStream(SerializationInfo info, int orderNumber)
128 {
129 _graphicsList = new GraphicsList();
130  
131 _name = info.GetString(
132 String.Format(CultureInfo.InvariantCulture,
133 "{0}{1}",
134 entryLayerName, orderNumber));
135  
136 _visible = info.GetBoolean(
137 String.Format(CultureInfo.InvariantCulture,
138 "{0}{1}",
139 entryLayerVisible, orderNumber));
140  
141 _active = info.GetBoolean(
142 String.Format(CultureInfo.InvariantCulture,
143 "{0}{1}",
144 entryLayerActive, orderNumber));
145  
146 int n = info.GetInt32(
147 String.Format(CultureInfo.InvariantCulture,
148 "{0}{1}",
149 entryGraphicsCount, orderNumber));
150  
151 for (int i = 0; i < n; i++)
152 {
153 string typeName;
154 typeName = info.GetString(
155 String.Format(CultureInfo.InvariantCulture,
156 "{0}{1}-{2}",
157 entryObjectType, orderNumber, i));
158  
159 object drawObject;
160 drawObject = Assembly.GetExecutingAssembly().CreateInstance(typeName);
161  
162 ((DrawObject)drawObject).LoadFromStream(info, orderNumber, i);
163  
164 // Thanks to Member 3272353 for this fix to object ordering problem.
165 // _graphicsList.Add((DrawObject)drawObject);
166 _graphicsList.Append((DrawObject) drawObject);
167 }
168 }
169  
170 internal void Draw(Graphics g)
171 {
172 _graphicsList.Draw(g);
173 }
174  
175 // Public implementation of Dispose pattern callable by consumers.
176 public void Dispose()
177 {
178 this.Dispose(true);
179 GC.SuppressFinalize(this);
180 }
181  
182 // Flag: Has Dispose already been called?
183 bool _disposed = false;
184  
185 // Protected implementation of Dispose pattern.
186 protected virtual void Dispose(bool disposing)
187 {
188 if (!this._disposed)
189 {
190  
191 if (disposing)
192 {
193 // Free any managed objects here.
194 //
195 if (this._graphicsList != null)
196 {
197 for (int i = 0; i < this._graphicsList.Count; i++)
198 {
199 if (this._graphicsList[i] != null)
200 {
201 this._graphicsList[i].Dispose();
202 }
203 }
204 }
205 }
206  
207 // Free any unmanaged objects here.
208 //
209  
210 this._disposed = true;
211 }
212 }
213  
214 ~Layer()
215 {
216 this.Dispose(false);
217 }
218 }
219 }