CraftSynth.ImageEditor – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System.Collections.Generic; |
2 | |||
3 | namespace CraftSynth.ImageEditor |
||
4 | { |
||
5 | /// <summary> |
||
6 | /// Changing state of existing objects: |
||
7 | /// move, resize, change properties. |
||
8 | /// </summary> |
||
9 | internal class CommandChangeState : Command |
||
10 | { |
||
11 | // Selected object(s) before operation |
||
12 | private List<DrawObject> listBefore; |
||
13 | |||
14 | // Selected object(s) after operation |
||
15 | private List<DrawObject> listAfter; |
||
16 | |||
17 | // Track the active Layer where the change took place |
||
18 | private int activeLayer; |
||
19 | |||
20 | // Create this command BEFORE operation. |
||
21 | public CommandChangeState(Layers layerList) |
||
22 | { |
||
23 | // Keep objects state before operation. |
||
24 | activeLayer = layerList.ActiveLayerIndex; |
||
25 | FillList(layerList[activeLayer].Graphics, ref listBefore); |
||
26 | } |
||
27 | |||
28 | // Call this function AFTER operation. |
||
29 | public void NewState(Layers layerList) |
||
30 | { |
||
31 | // Keep objects state after operation. |
||
32 | FillList(layerList[activeLayer].Graphics, ref listAfter); |
||
33 | } |
||
34 | |||
35 | public override void Undo(Layers list) |
||
36 | { |
||
37 | // Replace all objects in the list with objects from listBefore |
||
38 | ReplaceObjects(list[activeLayer].Graphics, listBefore); |
||
39 | } |
||
40 | |||
41 | public override void Redo(Layers list) |
||
42 | { |
||
43 | // Replace all objects in the list with objects from listAfter |
||
44 | ReplaceObjects(list[activeLayer].Graphics, listAfter); |
||
45 | } |
||
46 | |||
47 | // Replace objects in graphicsList with objects from list |
||
48 | private void ReplaceObjects(GraphicsList graphicsList, List<DrawObject> list) |
||
49 | { |
||
50 | for (int i = 0; i < graphicsList.Count; i++) |
||
51 | { |
||
52 | DrawObject replacement = null; |
||
53 | |||
54 | foreach (DrawObject o in list) |
||
55 | { |
||
56 | if (o.ID == |
||
57 | graphicsList[i].ID) |
||
58 | { |
||
59 | replacement = o; |
||
60 | break; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | if (replacement != null) |
||
65 | { |
||
66 | graphicsList.Replace(i, replacement); |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | // Fill list from selection |
||
72 | private void FillList(GraphicsList graphicsList, ref List<DrawObject> listToFill) |
||
73 | { |
||
74 | listToFill = new List<DrawObject>(); |
||
75 | |||
76 | foreach (DrawObject o in graphicsList.Selection) |
||
77 | { |
||
78 | listToFill.Add(o.Clone()); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | #region Destruction |
||
83 | |||
84 | // Flag: Has Dispose already been called? |
||
85 | bool _disposed = false; |
||
86 | |||
87 | // Protected implementation of Dispose pattern. |
||
88 | public override void Dispose(bool disposing) |
||
89 | { |
||
90 | if (!this._disposed) |
||
91 | { |
||
92 | |||
93 | if (disposing) |
||
94 | { |
||
95 | // Free any managed objects here. |
||
96 | // |
||
97 | if (this.listBefore != null) |
||
98 | { |
||
99 | foreach (DrawObject drawObject in listBefore) |
||
100 | { |
||
101 | if (drawObject != null) |
||
102 | { |
||
103 | drawObject.Dispose(); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if (this.listAfter != null) |
||
109 | { |
||
110 | foreach (DrawObject drawObject in listAfter) |
||
111 | { |
||
112 | if (drawObject != null) |
||
113 | { |
||
114 | drawObject.Dispose(); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Free any unmanaged objects here. |
||
121 | // |
||
122 | |||
123 | this._disposed = true; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | ~CommandChangeState() |
||
128 | { |
||
129 | this.Dispose(false); |
||
130 | } |
||
131 | #endregion |
||
132 | } |
||
133 | } |