-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
218 lines (183 loc) · 4.74 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
*@author Xizheng Ding
*/
/**
*define a interface with two methods 'execute', 'undo'
*/
var command = new Interface('command',['execute','undo']);
var addCmd = Class.extend({
_canvasObject: null,
_tempObject: null,
init: function(canvasobject){
/*check interface implementation*/
Interface.ensureImplements(this, command);
this._canvasObject = canvasobject;
},
getcanvasObject: function(){
return this._canvasObject;
},
mousedown: function(){
},
drawing: function(){
},
execute: function(){
var state = State.getInstance();
state.addObject(this.getcanvasObject());
state.layerUpdate();
},
undo: function(){
var state = State.getInstance();
state.removeObject(this.getcanvasObject());
state.layerUpdate();
},
});
//add_rectangle command
var addRectCmd = addCmd.extend({
init: function(canvasobject){
this._super(canvasobject);
this._tempObject = new canvasRect();
},
});
//var _addR = new addRectCmd();
//add_line command
var addLineCmd = addCmd.extend({ // implements command
init: function(canvasobject){
this._super(canvasobject);
},
});
//add_circle command
var addCircleCmd = addCmd.extend({ // implements command
init: function(canvasobject){
this._super(canvasobject);
},
});
//delete obejct command
var deleteObejctCmd = addCmd.extend({ // implements command
_canvasObject: null,
init: function(canvasobject){
this._canvasObject = canvasobject;
},
execute: function(){
var state = State.getInstance();
state.hideObject(this.getcanvasObject());
state.layerUpdate();
},
undo: function(){
var state = State.getInstance();
state.showObject(this.getcanvasObject());
state.layerUpdate();
},
});
//copy_shape command
var copyShapeCmd = Class.extend({ // implements command
_canvasObject: null,
_lastUnpasted: null,
init: function(canvasobject){
//check interface implementation
Interface.ensureImplements(this, command);
this.copyAttributes(canvasobject);
this._canvasObject.copy(canvasobject);
this._canvasObject.render();
},
copyAttributes: function(canvasobject){
if(canvasobject instanceof canvasCircle)
this._canvasObject = new canvasCircle();
else if(canvasobject instanceof canvasRect)
this._canvasObject = new canvasRect();
else
this._canvasObject = new canvasLine();
},
getcanvasObject: function(){
return this._canvasObject;
},
execute: function(){
var state = State.getInstance();
this._lastUnpasted = state._unpastedObject;
state.copyObject(this.getcanvasObject());
state.layerUpdate();
},
undo: function(){
var state = State.getInstance();
state.copyObject(this._lastUnpasted);
state.layerUpdate();
},
});
//cut_shape command
var cutShapeCmd = Class.extend({ // implements command
_targetObject:null,
_canvasObject: null,
_isCutEntered: null,
init: function(canvasobject){
//check interface implementation
Interface.ensureImplements(this, command);
this._targetObject = canvasobject;
},
execute: function(){
var state = State.getInstance();
var copy = new copyShapeCmd(this._targetObject);
var deletec = new deleteObejctCmd(this._targetObject);
state.doCmd(copy);
state.doCmd(deletec);
},
undo: function(){
var state = State.getInstance();
state.undoCmd();
state.undoCmd();
},
});
//paste_shape command
var pasteShapeCmd = Class.extend({ // implements command
_canvasObject: null,
_iscut: false,
init: function(){
//check interface implementation
Interface.ensureImplements(this, command);
this._canvasObject = State.getInstance()._unpastedObject;
},
getcanvasObject: function(){
return this._canvasObject;
},
execute: function(){
var state = State.getInstance();
state.pasteObject();
},
undo: function(){
var state = State.getInstance();
state.undoCmd();
state.removeObject(this.getcanvasObject());
state.layerUpdate();
},
});
//edit Cmd
var editShapeCmd = Class.extend({ // implements command
_canvasObject: null,
init: function(canvasobject){
//check interface implementation
Interface.ensureImplements(this, command);
this._canvasObject = canvasobject;
for(attribute in canvasobject.getparams())
this[attribute] = canvasobject.getparams()[attribute];
},
getcanvasObject: function(){
return this._canvasObject;
},
execute: function(){
},
undo: function(){
for(attribute in this.getcanvasObject().getparams())
this.getcanvasObject().getparams()[attribute] = this[attribute];
this.getcanvasObject().render();
//console.log("nihao");
},
});
//reize_shape command
var resizeShapeCmd = Class.extend({ // implements command
init: function(){
//check interface implementation
Interface.ensureImplements(this, command);
},
execute: function(){
},
undo: function(){},
});
var _addCircleCmd = new addCircleCmd();