Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1452 chris 1
#region License
2
 //
3
 // The Open Toolkit Library License
4
 //
5
 // Copyright (c) 2006 - 2009 the Open Toolkit library.
6
 //
7
 // Permission is hereby granted, free of charge, to any person obtaining a copy
8
 // of this software and associated documentation files (the "Software"), to deal
9
 // in the Software without restriction, including without limitation the rights to 
10
 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
 // the Software, and to permit persons to whom the Software is furnished to do
12
 // so, subject to the following conditions:
13
 //
14
 // The above copyright notice and this permission notice shall be included in all
15
 // copies or substantial portions of the Software.
16
 //
17
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
 // OTHER DEALINGS IN THE SOFTWARE.
25
 //
26
 #endregion
27
 
28
using System;
29
using System.Collections.Generic;
30
using System.Text;
31
 
32
namespace OpenTK
33
{
34
#if NO_SYSDRAWING
35
    /// <summary>
36
    /// Represents a rectangular region on a two-dimensional plane.
37
    /// </summary>
38
    public struct Rectangle : IEquatable<Rectangle>
39
    {
40
        #region Fields
41
 
42
        Point location;
43
        Size size;
44
 
45
        #endregion
46
 
47
        #region Constructors
48
 
49
        /// <summary>
50
        /// Constructs a new Rectangle instance.
51
        /// </summary>
52
        /// <param name="location">The top-left corner of the Rectangle.</param>
53
        /// <param name="size">The width and height of the Rectangle.</param>
54
        public Rectangle(Point location, Size size)
55
            : this()
56
        {
57
            Location = location;
58
            Size = size;
59
        }
60
 
61
        /// <summary>
62
        /// Constructs a new Rectangle instance.
63
        /// </summary>
64
        /// <param name="x">The x coordinate of the Rectangle.</param>
65
        /// <param name="y">The y coordinate of the Rectangle.</param>
66
        /// <param name="width">The width coordinate of the Rectangle.</param>
67
        /// <param name="height">The height coordinate of the Rectangle.</param>
68
        public Rectangle(int x, int y, int width, int height)
69
            : this(new Point(x, y), new Size(width, height))
70
        { }
71
 
72
        #endregion
73
 
74
        #region Public Members
75
 
76
        /// <summary>
77
        /// Gets or sets the x coordinate of the Rectangle.
78
        /// </summary>
79
        public int X
80
        {
81
            get { return Location.X; }
82
            set { Location = new Point (value, Y); }
83
        }
84
 
85
        /// <summary>
86
        /// Gets or sets the y coordinate of the Rectangle.
87
        /// </summary>
88
        public int Y
89
        {
90
            get { return Location.Y; }
91
            set { Location = new Point (X, value); }
92
        }
93
 
94
        /// <summary>
95
        /// Gets or sets the width of the Rectangle.
96
        /// </summary>
97
        public int Width
98
        {
99
            get { return Size.Width; }
100
            set { Size = new Size (value, Height); }
101
        }
102
 
103
        /// <summary>
104
        /// Gets or sets the height of the Rectangle.
105
        /// </summary>
106
        public int Height
107
        {
108
            get { return Size.Height; }
109
            set { Size = new Size(Width, value); }
110
        }
111
 
112
        /// <summary>
113
        /// Gets or sets a <see cref="Point"/> representing the x and y coordinates
114
        /// of the Rectangle.
115
        /// </summary>
116
        public Point Location
117
        {
118
            get { return location; }
119
            set { location = value; }
120
        }
121
 
122
        /// <summary>
123
        /// Gets or sets a <see cref="Size"/> representing the width and height
124
        /// of the Rectangle.
125
        /// </summary>
126
        public Size Size
127
        {
128
            get { return size; }
129
            set { size = value; }
130
        }
131
 
132
        /// <summary>
133
        /// Gets the y coordinate of the top edge of this Rectangle.
134
        /// </summary>
135
        public int Top { get { return Y; } }
136
 
137
        /// <summary>
138
        /// Gets the x coordinate of the right edge of this Rectangle.
139
        /// </summary>
140
        public int Right { get { return X + Width; } }
141
 
142
        /// <summary>
143
        /// Gets the y coordinate of the bottom edge of this Rectangle.
144
        /// </summary>
145
        public int Bottom { get { return Y + Height; } }
146
 
147
        /// <summary>
148
        /// Gets the x coordinate of the left edge of this Rectangle.
149
        /// </summary>
150
        public int Left { get { return X; } }
151
 
152
        /// <summary>
153
        /// Gets a <see cref="System.Boolean"/> that indicates whether this
154
        /// Rectangle is equal to the empty Rectangle.
155
        /// </summary>
156
        public bool IsEmpty
157
        {
158
            get { return Location.IsEmpty && Size.IsEmpty; }
159
        }
160
 
161
        /// <summary>
162
        /// Defines the empty Rectangle.
163
        /// </summary>
164
        public static readonly Rectangle Zero = new Rectangle();
165
 
166
        /// <summary>
167
        /// Defines the empty Rectangle.
168
        /// </summary>
169
        public static readonly Rectangle Empty = new Rectangle();
170
 
171
        /// <summary>
172
        /// Constructs a new instance with the specified edges.
173
        /// </summary>
174
        /// <param name="left">The left edge of the Rectangle.</param>
175
        /// <param name="top">The top edge of the Rectangle.</param>
176
        /// <param name="right">The right edge of the Rectangle.</param>
177
        /// <param name="bottom">The bottom edge of the Rectangle.</param>
178
        /// <returns>A new Rectangle instance with the specified edges.</returns>
179
        public static Rectangle FromLTRB(int left, int top, int right, int bottom)
180
        {
181
            return new Rectangle(new Point(left, top), new Size(right - left, bottom - top));
182
        }
183
 
184
        /// <summary>
185
        /// Tests whether this instance contains the specified Point.
186
        /// </summary>
187
        /// <param name="point">The <see cref="Point"/> to test.</param>
188
        /// <returns>True if this instance contains point; false otherwise.</returns>
189
        /// <remarks>The left and top edges are inclusive. The right and bottom edges
190
        /// are exclusive.</remarks>
191
        public bool Contains(Point point)
192
        {
193
            return point.X >= Left && point.X < Right &&
194
                point.Y >= Top && point.Y < Bottom;
195
        }
196
 
197
        /// <summary>
198
        /// Tests whether this instance contains the specified Rectangle.
199
        /// </summary>
200
        /// <param name="rect">The <see cref="Rectangle"/> to test.</param>
201
        /// <returns>True if this instance contains rect; false otherwise.</returns>
202
        /// <remarks>The left and top edges are inclusive. The right and bottom edges
203
        /// are exclusive.</remarks>
204
        public bool Contains(Rectangle rect)
205
        {
206
            return Contains(rect.Location) && Contains(rect.Location + rect.Size);
207
        }
208
 
209
        /// <summary>
210
        /// Compares two instances for equality.
211
        /// </summary>
212
        /// <param name="left">The first instance.</param>
213
        /// <param name="right">The second instance.</param>
214
        /// <returns>True, if left is equal to right; false otherwise.</returns>
215
        public static bool operator ==(Rectangle left, Rectangle right)
216
        {
217
            return left.Equals(right);
218
        }
219
 
220
        /// <summary>
221
        /// Compares two instances for inequality.
222
        /// </summary>
223
        /// <param name="left">The first instance.</param>
224
        /// <param name="right">The second instance.</param>
225
        /// <returns>True, if left is not equal to right; false otherwise.</returns>
226
        public static bool operator !=(Rectangle left, Rectangle right)
227
        {
228
            return !left.Equals(right);
229
        }
230
 
231
        /// <summary>
232
        /// Converts an OpenTK.Rectangle instance to a System.Drawing.Rectangle.
233
        /// </summary>
234
        /// <param name="rect">
235
        /// The <see cref="Rectangle"/> instance to convert.
236
        /// </param>
237
        /// <returns>
238
        /// A <see cref="System.Drawing.Rectangle"/> instance equivalent to rect.
239
        /// </returns>
240
        public static implicit operator System.Drawing.Rectangle(Rectangle rect)
241
        {
242
            return new System.Drawing.Rectangle(rect.Location, rect.Size);
243
        }
244
 
245
        /// <summary>
246
        /// Converts a System.Drawing.Rectangle instance to an OpenTK.Rectangle.
247
        /// </summary>
248
        /// <param name="rect">
249
        /// The <see cref="System.Drawing.Rectangle"/> instance to convert.
250
        /// </param>
251
        /// <returns>
252
        /// A <see cref="Rectangle"/> instance equivalent to point.
253
        /// </returns>
254
        public static implicit operator Rectangle(System.Drawing.Rectangle rect)
255
        {
256
            return new Rectangle(rect.Location, rect.Size);
257
        }
258
 
259
        /// <summary>
260
        /// Converts an OpenTK.Rectangle instance to a System.Drawing.RectangleF.
261
        /// </summary>
262
        /// <param name="rect">
263
        /// The <see cref="Rectangle"/> instance to convert.
264
        /// </param>
265
        /// <returns>
266
        /// A <see cref="System.Drawing.RectangleF"/> instance equivalent to rect.
267
        /// </returns>
268
        public static implicit operator System.Drawing.RectangleF(Rectangle rect)
269
        {
270
            return new System.Drawing.RectangleF(rect.Location, rect.Size);
271
        }
272
 
273
        /// <summary>
274
        /// Indicates whether this instance is equal to the specified object.
275
        /// </summary>
276
        /// <param name="obj">The object instance to compare to.</param>
277
        /// <returns>True, if both instances are equal; false otherwise.</returns>
278
        public override bool Equals(object obj)
279
        {
280
            if (obj is Rectangle)
281
                return Equals((Rectangle)obj);
282
 
283
            return false;
284
        }
285
 
286
        /// <summary>
287
        /// Returns the hash code for this instance.
288
        /// </summary>
289
        /// <returns>A <see cref="System.Int32"/> that represents the hash code for this instance./></returns>
290
        public override int GetHashCode()
291
        {
292
            return Location.GetHashCode() & Size.GetHashCode();
293
        }
294
 
295
        /// <summary>
296
        /// Returns a <see cref="System.String"/> that describes this instance.
297
        /// </summary>
298
        /// <returns>A <see cref="System.String"/> that describes this instance.</returns>
299
        public override string ToString()
300
        {
301
            return String.Format("{{{0}-{1}}}", Location, Location + Size);
302
        }
303
 
304
 
305
        #endregion
306
 
307
        #region IEquatable<Rectangle> Members
308
 
309
        /// <summary>
310
        /// Indicates whether this instance is equal to the specified Rectangle.
311
        /// </summary>
312
        /// <param name="other">The instance to compare to.</param>
313
        /// <returns>True, if both instances are equal; false otherwise.</returns>
314
        public bool Equals(Rectangle other)
315
        {
316
            return Location.Equals(other.Location) &&
317
                Size.Equals(other.Size);
318
        }
319
 
320
        #endregion
321
    }
322
#endif
323
}