1
1
import { Point } from './Point' ;
2
2
import { Wire } from './Wire' ;
3
3
import { isNull } from 'util' ;
4
+ import { BoundingBox } from './Geometry' ;
4
5
5
6
/**
6
7
* Abstract Class Circuit Elements
@@ -110,6 +111,14 @@ export abstract class CircuitElement {
110
111
} ) ;
111
112
}
112
113
}
114
+
115
+ /**
116
+ * Returns bounding box of the circuit element
117
+ */
118
+ getBoundingBox ( ) : BoundingBox {
119
+ return BoundingBox . loadFromRaphaelBbox ( this . elements . getBBox ( ) ) ;
120
+ }
121
+
113
122
/**
114
123
* Draws circuit nodes
115
124
* @param canvas Raphael Canvas
@@ -136,26 +145,23 @@ export abstract class CircuitElement {
136
145
* @param drawData Draw Data
137
146
*/
138
147
DrawElement ( canvas : any , drawData : any ) {
148
+ const elementsDrawn = [ ] ;
139
149
for ( const item of drawData ) {
150
+ let element ;
140
151
// Draw image
141
152
if ( item . type === 'image' ) {
142
- this . elements . push (
143
- canvas . image (
153
+ element = canvas . image (
144
154
item . url ,
145
155
this . x + item . x ,
146
156
this . y + item . y ,
147
157
item . width ,
148
158
item . height
149
- )
150
- ) ;
159
+ ) ;
151
160
} else if ( item . type === 'path' ) {
152
- this . elements . push (
153
- this . DrawPath ( canvas , item )
154
- ) ;
161
+ element = this . DrawPath ( canvas , item ) ;
155
162
} else if ( item . type === 'rectangle' ) {
156
163
// Draw rectangle
157
- this . elements . push (
158
- canvas . rect (
164
+ element = canvas . rect (
159
165
this . x + item . x ,
160
166
this . y + item . y ,
161
167
item . width ,
@@ -164,24 +170,24 @@ export abstract class CircuitElement {
164
170
) . attr ( {
165
171
fill : item . fill || 'none' ,
166
172
stroke : item . stroke || 'none'
167
- } )
168
- ) ;
173
+ } ) ;
169
174
} else if ( item . type === 'circle' ) {
170
175
// Draw a circle
171
- this . elements . push (
172
- canvas . circle (
176
+ element = canvas . circle (
173
177
this . x + item . x ,
174
178
this . y + item . y ,
175
179
item . radius ,
176
180
) . attr ( {
177
181
fill : item . fill || 'none' ,
178
182
stroke : item . stroke || 'none'
179
- } )
180
- ) ;
183
+ } ) ;
181
184
} else if ( item . type === 'polygon' ) {
182
- this . DrawPolygon ( canvas , item ) ;
185
+ element = this . DrawPolygon ( canvas , item ) ;
183
186
}
187
+ this . elements . push ( element ) ;
188
+ elementsDrawn . push ( element ) ;
184
189
}
190
+ return elementsDrawn ;
185
191
}
186
192
/**
187
193
* Draws an Polygon
@@ -198,13 +204,11 @@ export abstract class CircuitElement {
198
204
tmp += `${ this . x + point [ 0 ] } ,${ this . y + point [ 1 ] } L` ;
199
205
}
200
206
tmp = tmp . substr ( 0 , tmp . length - 1 ) + 'z' ;
201
- this . elements . push (
202
- canvas . path ( tmp )
203
- . attr ( {
204
- fill : item . fill || 'none' ,
205
- stroke : item . stroke || 'none'
206
- } )
207
- ) ;
207
+ return canvas . path ( tmp )
208
+ . attr ( {
209
+ fill : item . fill || 'none' ,
210
+ stroke : item . stroke || 'none'
211
+ } ) ;
208
212
}
209
213
/**
210
214
* Draw a Path
@@ -227,13 +231,11 @@ export abstract class CircuitElement {
227
231
str = this . calcRelative ( str , horizontal , canvas ) ;
228
232
str = this . calcRelative ( str , vertical , canvas ) ;
229
233
str = this . calcRelative ( str , sCurve , canvas ) ;
230
- this . elements . push (
231
- canvas . path ( str )
232
- . attr ( {
233
- fill : item . fill || 'none' ,
234
- stroke : item . stroke || 'none'
235
- } )
236
- ) ;
234
+ return canvas . path ( str )
235
+ . attr ( {
236
+ fill : item . fill || 'none' ,
237
+ stroke : item . stroke || 'none'
238
+ } ) ;
237
239
}
238
240
/**
239
241
* Draw path relative to the component
@@ -291,6 +293,7 @@ export abstract class CircuitElement {
291
293
for ( let i = 0 ; i < this . nodes . length ; ++ i ) {
292
294
this . nodes [ i ] . move ( tmpar [ i ] [ 0 ] + dx , tmpar [ i ] [ 1 ] + dy ) ;
293
295
}
296
+ window [ 'onDragEvent' ] ( this ) ;
294
297
} , ( ) => {
295
298
fdx = 0 ;
296
299
fdy = 0 ;
@@ -308,6 +311,7 @@ export abstract class CircuitElement {
308
311
// }
309
312
this . tx += fdx ;
310
313
this . ty += fdy ;
314
+ window [ 'onDragStopEvent' ] ( this ) ;
311
315
} ) ;
312
316
}
313
317
/**
0 commit comments