@@ -28,36 +28,44 @@ public class CanvasRect {
2828 public double width { get ; set ; default = 0.0 ; }
2929 public double height { get ; set ; default = 0.0 ; }
3030
31- /* Constructor */
31+ // -------------------------------------------------------------
32+ // Constructor
3233 public CanvasRect () {}
3334
34- /* Constructor */
35+ // -------------------------------------------------------------
36+ // Constructor
3537 public CanvasRect.from_coords ( double x , double y , double width , double height ) {
3638 copy_coords( x, y, width, height );
3739 }
3840
39- /* Copy constructor */
41+ // -------------------------------------------------------------
42+ // Copy constructor
4043 public CanvasRect.from_rect ( CanvasRect rect ) {
4144 copy( rect );
4245 }
4346
44- /* Makes a copy of the given rectangle */
47+ // -------------------------------------------------------------
48+ // Makes a copy of the given rectangle
4549 public void copy ( CanvasRect rect ) {
4650 x = rect. x;
4751 y = rect. y;
4852 width = rect. width;
4953 height = rect. height;
5054 }
5155
52- /* Makes a copy of the given rectangle coordinates to this rectangle */
56+ // -------------------------------------------------------------
57+ // Makes a copy of the given rectangle coordinates to this
58+ // rectangle.
5359 public void copy_coords ( double x , double y , double width , double height ) {
5460 this . x = x;
5561 this . y = y;
5662 this . width = width;
5763 this . height = height;
5864 }
5965
60- /* Converts this rectangle into a Gdk.Rectangle and returns the result */
66+ // -------------------------------------------------------------
67+ // Converts this rectangle into a Gdk.Rectangle and returns the
68+ // result.
6169 public Gdk .Rectangle to_rectangle ( double zoom_factor = 1 .0 ) {
6270 Gdk . Rectangle rect = {0 , 0 , 0 , 0 };
6371 rect. x = (int )(x * zoom_factor);
@@ -67,60 +75,72 @@ public class CanvasRect {
6775 return ( rect );
6876 }
6977
70- /* Returns the x1 value */
78+ // -------------------------------------------------------------
79+ // Returns the x1 value.
7180 public double x1 () {
7281 return ( x );
7382 }
7483
75- /* Returns the y1 value */
84+ // -------------------------------------------------------------
85+ // Returns the y1 value.
7686 public double y1 () {
7787 return ( y );
7888 }
7989
80- /* Returns the x2 value */
90+ // -------------------------------------------------------------
91+ // Returns the x2 value.
8192 public double x2 () {
8293 return ( (x + width) );
8394 }
8495
85- /* Returns the y2 value */
96+ // -------------------------------------------------------------
97+ // Returns the y2 value.
8698 public double y2 () {
8799 return ( (y + height) );
88100 }
89101
90- /* Returns the mid-point between x1 and x2 */
102+ // -------------------------------------------------------------
103+ // Returns the mid-point between x1 and x2.
91104 public double mid_x () {
92105 return ( x + (width / 2 ) );
93106 }
94107
95- /* Returns the mid-point between y1 and y2 */
108+ // -------------------------------------------------------------
109+ // Returns the mid-point between y1 and y2.
96110 public double mid_y () {
97111 return ( y + (height / 2 ) );
98112 }
99113
100- /* Returns the absolute width of the rectangle */
114+ // -------------------------------------------------------------
115+ // Returns the absolute width of the rectangle.
101116 public double abs_width () {
102117 return ( width. abs() );
103118 }
104119
105- /* Returns the absolute height of the rectangle */
120+ // -------------------------------------------------------------
121+ // Returns the absolute height of the rectangle.
106122 public double abs_height () {
107123 return ( height. abs() );
108124 }
109125
110- /* Resizes the size of the rectangle by the numb of pixels */
126+ // -------------------------------------------------------------
127+ // Resizes the size of the rectangle by the numb of pixels.
111128 public void resize ( double amount ) {
112129 x - = amount;
113130 y - = amount;
114131 width + = (amount * 2 );
115132 height + = (amount * 2 );
116133 }
117134
118- /* Returns true if the given rectangle matches this rectangle */
135+ // -------------------------------------------------------------
136+ // Returns true if the given rectangle matches this rectangle.
119137 public bool equals ( CanvasRect rect ) {
120138 return ( (x == rect. x) && (y == rect. y) && (width == rect. width) && (height == rect. height) );
121139 }
122140
123- /* Returns true if the given rectangle intersects with this rectangle */
141+ // -------------------------------------------------------------
142+ // Returns true if the given rectangle intersects with this
143+ // rectangle.
124144 public bool intersects ( CanvasRect rect ) {
125145 var x5 = (x1() < rect. x1()) ? rect. x1() : x1();
126146 var y5 = (y2() < rect. y2()) ? rect. y2() : y2();
@@ -129,9 +149,9 @@ public class CanvasRect {
129149 return ( (x5 <= x6) && (y6 <= y5) );
130150 }
131151
132- /*
133- Stores the intersected rectangle of the two specified rectangles in ourself.
134- */
152+ // -------------------------------------------------------------
153+ // Stores the intersected rectangle of the two specified
154+ // rectangles in ourself.
135155 public void intersection ( CanvasRect a , CanvasRect b ) {
136156 var x1 = (a. x1() < b. x1()) ? b. x1() : a. x1();
137157 var y1 = (a. y1() < b. y1()) ? b. y1() : a. y1();
@@ -143,12 +163,14 @@ public class CanvasRect {
143163 height = (y2 - y1);
144164 }
145165
146- /* Returns true if this rectangle contains the given point */
166+ // -------------------------------------------------------------
167+ // Returns true if this rectangle contains the given point.
147168 public bool contains ( double x , double y ) {
148169 return ( Utils . is_within_bounds( x, y, this . x, this . y, this . width, this . height ) );
149170 }
150171
151- /* Creates a rectangle that is a drawable version of this rectangle */
172+ // -------------------------------------------------------------
173+ // Creates a rectangle that is a drawable version of this rectangle.
152174 public void normalize ( out CanvasRect rect ) {
153175 rect = new CanvasRect .from_coords(
154176 ((width < 0 ) ? (x + width) : x),
@@ -158,11 +180,53 @@ public class CanvasRect {
158180 );
159181 }
160182
161- /* Returns a string version of this rectangle */
183+ // -------------------------------------------------------------
184+ // Returns a string version of this rectangle.
162185 public string to_string () {
163186 return ( " x: %g , y: %g , w: %g , h: %g " . printf( x, y, width, height ) );
164187 }
165188
189+ // -------------------------------------------------------------
190+ // Saves this rectangle to XML format.
191+ public Xml .Node * save () {
192+
193+ Xml . Node * node = new Xml .Node ( null , " rect" );
194+
195+ node- > set_prop( " x" , x. to_string() );
196+ node- > set_prop( " y" , y. to_string() );
197+ node- > set_prop( " width" , width. to_string() );
198+ node- > set_prop( " height" , height. to_string() );
199+
200+ return ( node );
201+
202+ }
203+
204+ // -------------------------------------------------------------
205+ // Loads the rectangle information from XML format.
206+ public void load ( Xml .Node * node ) {
207+
208+ var xs = node- > get_prop( " x" );
209+ if ( xs != null ) {
210+ x = double . parse( xs );
211+ }
212+
213+ var ys = node- > get_prop( " y" );
214+ if ( ys != null ) {
215+ y = double . parse( ys );
216+ }
217+
218+ var w = node- > get_prop( " width" );
219+ if ( w != null ) {
220+ width = double . parse( w );
221+ }
222+
223+ var h = node- > get_prop( " height" );
224+ if ( h != null ) {
225+ height = double . parse( h );
226+ }
227+
228+ }
229+
166230}
167231
168232
0 commit comments