Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Rect.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// The Rect is a struct that represent rectangluar space.
24     /// </summary>
25     [StructLayout(LayoutKind.Sequential)]
26     public struct Rect : IEquatable<Rect>
27     {
28         /// <summary>
29         /// Creates and initializes a new instance of the Rect class.
30         /// </summary>
31         /// <param name="x">X axis value.</param>
32         /// <param name="y">Y axis value.</param>
33         /// <param name="w">Width value.</param>
34         /// <param name="h">Height value.</param>
35         public Rect(int x, int y, int w, int h)
36         {
37             X = x;
38             Y = y;
39             Width = w;
40             Height = h;
41         }
42         /// <summary>
43         /// Gets or sets the position of this Rectangle on the X axis.
44         /// </summary>
45         public int X { get; set; }
46
47         /// <summary>
48         /// Gets or sets the position of this Rectangle on the Y axis.
49         /// </summary>
50         public int Y { get; set; }
51
52         /// <summary>
53         /// Gets or sets the width of this Rectangle.
54         /// </summary>
55         public int Width { get; set; }
56
57         /// <summary>
58         /// Gets or sets the height of this Rectangle.
59         /// </summary>
60         public int Height { get; set; }
61
62         /// <summary>
63         /// Gets the position of this Rectangle on the X axis.
64         /// </summary>
65         public int Left { get { return X; } }
66
67         /// <summary>
68         /// Gets the extent along the X axis.
69         /// </summary>
70         public int Right { get { return X + Width; } }
71
72         /// <summary>
73         /// Gets the position of this Rectangle on the Y axis.
74         /// </summary>
75         public int Top { get { return Y; } }
76
77         /// <summary>
78         /// Gets the extent along the Y axis.
79         /// </summary>
80         public int Bottom { get { return Y + Height; } }
81
82         /// <summary>
83         /// Gets the Point defined by Rectangle.Left and Rectangle.Top.
84         /// </summary>
85         public Point Location { get { return new Point { X = X, Y = Y }; } }
86
87         /// <summary>
88         /// Gets the extent of the Rectangle along its X and Y axis.
89         /// </summary>
90         public Size Size { get { return new Size { Width = Width, Height = Height }; } }
91
92         public override string ToString()
93         {
94             return string.Format("{{X={0} Y={1} Width={2} Height={3}}}", X, Y, Width, Height);
95         }
96
97         public override int GetHashCode()
98         {
99             unchecked
100             {
101                 int hashCode = X.GetHashCode();
102                 hashCode = (hashCode * 397) ^ Y.GetHashCode();
103                 hashCode = (hashCode * 397) ^ Width.GetHashCode();
104                 hashCode = (hashCode * 397) ^ Height.GetHashCode();
105                 return hashCode;
106             }
107         }
108
109         public override bool Equals(object obj)
110         {
111             if (!(obj is Rect))
112                 return false;
113
114             return Equals((Rect)obj);
115         }
116
117         public bool Equals(Rect other)
118         {
119             return X.Equals(other.X) && Y.Equals(other.Y) && Width.Equals(other.Width) && Height.Equals(other.Height);
120         }
121
122         /// <summary>
123         /// Whether the two <see cref="T:Tizen.UI.Rectangle" />s are equal.
124         /// </summary>
125         /// <param name="r1">A <see cref="T:Tizen.UI.Rectangle" /> on the left hand side.</param>
126         /// <param name="r2">A <see cref="T:Tizen.UI.Rectangle" /> on the right hand side.</param>
127         /// <returns>True if the two <see cref="T:Tizen.UI.Rectangle" />s have equal values.</returns>
128         public static bool operator ==(Rect r1, Rect r2)
129         {
130             return r1.Equals(r2);
131         }
132
133         /// <summary>
134         /// Whether two <see cref="T:Tizen.UI.Rectangle" />s are not equal.
135         /// </summary>
136         /// <param name="r1">A <see cref="T:Tizen.UI.Rectangle" /> on the left hand side.</param>
137         /// <param name="r2">A <see cref="T:Tizen.UI.Rectangle" /> on the right hand side.</param>
138         /// <returns>True if the two <see cref="T:Tizen.UI.Rectangle" />s do not have equal values.</returns>
139         public static bool operator !=(Rect r1, Rect r2)
140         {
141             return !r1.Equals(r2);
142         }
143     }
144 }