@@ -49,6 +49,14 @@ var Path = this.Path = PathItem.extend({
4949 this . _add ( Segment . read ( segments , i , 1 ) ) ;
5050 } ,
5151
52+ getFirstSegment : function ( ) {
53+ return this . _segments [ 0 ] ;
54+ } ,
55+
56+ getLastSegment : function ( ) {
57+ return this . _segments [ this . _segments . length - 1 ] ;
58+ } ,
59+
5260 /**
5361 * The curves contained within the path.
5462 */
@@ -65,6 +73,15 @@ var Path = this.Path = PathItem.extend({
6573 return this . _curves ;
6674 } ,
6775
76+ getFirstCurve : function ( ) {
77+ return this . getCurves ( ) [ 0 ] ;
78+ } ,
79+
80+ getLastCurve : function ( ) {
81+ var curves = this . getCurves ( ) ;
82+ return curves [ curves . length - 1 ] ;
83+ } ,
84+
6885 getClosed : function ( ) {
6986 return this . _closed ;
7087 } ,
@@ -88,23 +105,6 @@ var Path = this.Path = PathItem.extend({
88105 }
89106 } ,
90107
91- getFirstSegment : function ( ) {
92- return this . _segments [ 0 ] ;
93- } ,
94-
95- getLastSegment : function ( ) {
96- return this . _segments [ this . _segments . length - 1 ] ;
97- } ,
98-
99- getFirstCurve : function ( ) {
100- return this . getCurves ( ) [ 0 ] ;
101- } ,
102-
103- getLastCurve : function ( ) {
104- var curves = this . getCurves ( ) ;
105- return curves [ curves . length - 1 ] ;
106- } ,
107-
108108 // TODO: Consider adding getSubPath(a, b), returning a part of the current
109109 // path, with the added benefit that b can be < a, and closed looping is
110110 // taken into account.
@@ -121,45 +121,87 @@ var Path = this.Path = PathItem.extend({
121121 /**
122122 * Private method that adds a segment to the segment list. It assumes that
123123 * the passed object is a segment already and does not perform any checks.
124+ * If a curves list was requested, it will kept in sync with the segments
125+ * list automatically.
124126 */
127+ // TODO: Add support for adding multiple segments at once
125128 _add : function ( segment , index ) {
126129 // If this segment belongs to another path already, clone it before
127130 // adding.
128131 if ( segment . _path )
129132 segment = new Segment ( segment ) ;
130- segment . _path = this ;
131133 if ( index === undefined ) {
132- this . _segments . push ( segment ) ;
134+ // Insert at the end
135+ index = this . _segments . push ( segment ) - 1 ;
133136 } else {
137+ // Insert somewhere else
134138 this . _segments . splice ( index , 0 , segment ) ;
139+ // Adjust the indices of the segments above.
140+ for ( var i = index + 1 , l = this . _segments . length ; i < l ; i ++ )
141+ this . _segments [ i ] . _index = i ;
142+ }
143+ segment . _path = this ;
144+ segment . _index = index ;
145+ // Keep the curves list in sync all the time in case it as requested
146+ // already. We need to step one index down from the inserted segment to
147+ // get its curve:
148+ if ( this . _curves && -- index >= 0 ) {
149+ // Insert a new curve as well and update the curves above
150+ this . _curves . splice ( index , 0 , Curve . create ( this , index ) ) ;
151+ // Adjust indices now for the curves above this one.
152+ for ( var i = index + 1 , l = this . _curves . length ; i < l ; i ++ ) {
153+ var curve = this . _curves [ i ] ;
154+ curve . _index1 = i ;
155+ // This is wrong for the last closing curve but it will be
156+ // corrected further down.
157+ curve . _index2 = i + 1 ;
158+ }
159+ // The curve that comes right after will has changed beyond a simple
160+ // shift in indices, so it needs an update:
161+ this . _curves [ index + 1 ] . _updateSegments ( ) ;
162+ // If this is a closed path, also update the closing curve
163+ if ( this . _closed )
164+ this . _curves [ l - 1 ] . _updateSegments ( ) ;
135165 }
136166 return segment ;
137167 } ,
138168
139- // TODO: Support multiple segments?
169+ // TODO: Add support for adding multiple segments at once
140170 add : function ( segment ) {
141171 segment = Segment . read ( arguments ) ;
142172 return segment ? this . _add ( segment ) : null ;
143173 } ,
144174
145- // TODO: Support multiple segments?
175+ // TODO: Add support for adding multiple segments at once
146176 insert : function ( index , segment ) {
147177 segment = Segment . read ( arguments , 1 ) ;
148178 return segment ? this . _add ( segment , index ) : null ;
149179 } ,
150180
151181 // TODO: Port back to Sg
152182 removeSegment : function ( index ) {
153- var segment = this . _segments [ index ]
154- return segment && segment . remove ( ) ? segment : null ;
183+ var segments = this . removeSegments ( index , index + 1 ) ;
184+ return segments ? segments [ 0 ] : null ;
155185 } ,
156186
157187 // TODO: Port back to Sg
158188 removeSegments : function ( from , to ) {
159- var i = Base . pick ( to , this . _segments . length - 1 ) ,
160- from = from || 0 ;
161- while ( i >= from )
162- this . removeSegment ( i -- ) ;
189+ from = from || 0 ;
190+ to = Base . pick ( to , this . _segments . length - 1 ) ;
191+ var amount = to - from ,
192+ segments = this . _segments . splice ( from , amount ) ;
193+ if ( segments . length == amount ) {
194+ // TODO: Keep _curves in sync
195+ for ( var i = 0 ; i < amount ; i ++ ) {
196+ var segment = segments [ 0 ] ;
197+ if ( segment . _selectionState ) {
198+ this . _selectedSegmentCount -- ;
199+ segment . _selectionState = 0 ;
200+ }
201+ }
202+ return segments ;
203+ }
204+ return null ;
163205 } ,
164206
165207 isSelected : function ( ) {
0 commit comments