[NUI] Integreation from dalihub (#988)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Layouting / LayoutLength.cs
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
18 using System;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// [Draft] A type that represents a layout length. Currently, this implies pixels, but could be extended to handle device dependant sizes, etc.
26     /// </summary>
27     internal struct LayoutLength
28     {
29         private float _value;
30
31         /// <summary>
32         /// [Draft] Constructor from an int
33         /// </summary>
34         /// <param name="value">Int to initialize with.</param>
35         public LayoutLength(int value)
36         {
37             _value = value;
38         }
39
40         /// <summary>
41         /// [Draft] Constructor from a float
42         /// </summary>
43         /// <param name="value">Float to initialize with.</param>
44         public LayoutLength(float value)
45         {
46             _value = value;
47         }
48
49         /// <summary>
50         /// [Draft] Constructor from a LayoutLength
51         /// </summary>
52         /// <param name="layoutLength">LayoutLength object to initialize with.</param>
53         public LayoutLength(LayoutLength layoutLength)
54         {
55             _value = layoutLength._value;
56         }
57
58         /// <summary>
59         /// [Draft] Return value as rounded value (whole number), best used as final output
60         /// </summary>
61         /// <returns>The layout length value as a rounded whole number.</returns>
62         public float AsRoundedValue()
63         {
64             return (float)Math.Round((decimal)_value, MidpointRounding.AwayFromZero);
65         }
66
67         /// <summary>
68         /// [Draft] Return value as the raw decimal value, best used for calculations
69         /// </summary>
70         /// <returns>The layout length value as the raw decimal value.</returns>
71         public float AsDecimal()
72         {
73             return _value;
74         }
75
76         /// <summary>
77         /// [Draft] The == operator.
78         /// </summary>
79         /// <param name="arg1">The first value.</param>
80         /// <param name="arg2">The second value</param>
81         /// <returns>true if LayoutLengths are equal</returns>
82         public static bool operator ==(LayoutLength arg1, LayoutLength arg2)
83         {
84             return arg1.Equals(arg2);
85         }
86
87         /// <summary>
88         /// [Draft] The != operator.
89         /// </summary>
90         /// <param name="arg1">The first value.</param>
91         /// <param name="arg2">The second value</param>
92         /// <returns>true if LayoutLengths are not equal</returns>
93         public static bool operator !=(LayoutLength arg1, LayoutLength arg2)
94         {
95             return !arg1.Equals(arg2);
96         }
97
98         /// <summary>
99         /// Determines whether the specified object is equal to the current object.
100         /// </summary>
101         /// <param name="obj">The object to compare with the current object.</param>
102         /// <returns>true if equal LayoutLength, else false.</returns>
103         public override bool Equals(object obj)
104         {
105             if (obj is LayoutLength)
106             {
107                 return this.Equals((LayoutLength)obj);
108             }
109             return false;
110         }
111
112         /// <summary>
113         /// Determines whether the specified object is equal to the current object.
114         /// </summary>
115         /// <param name="layoutLength">The LayoutLength to compare with the current LayoutLength.</param>
116         /// <returns>true if equal LayoutLengths, else false.</returns>
117         public bool Equals(LayoutLength layoutLength)
118         {
119             return (Math.Abs(_value - layoutLength._value ) <= float.Epsilon);
120         }
121
122         /// <summary>
123         /// A hash code for the current object.
124         /// </summary>
125         public override int GetHashCode()
126         {
127             return (int)Math.Ceiling(_value);
128         }
129
130         /// <summary>
131         /// The addition operator.
132         /// </summary>
133         /// <param name="arg1">The first value.</param>
134         /// <param name="arg2">The second value.</param>
135         /// <returns>The LayoutLength containing the result of the addition.</returns>
136         public static LayoutLength operator +(LayoutLength arg1, LayoutLength arg2)
137         {
138             return new LayoutLength( arg1._value + arg2._value );
139         }
140
141         /// <summary>
142         /// The addition operator.
143         /// </summary>
144         /// <param name="arg1">The first value.</param>
145         /// <param name="arg2">The second value.</param>
146         /// <returns>The LayoutLength containing the result of the addition.</returns>
147         public static LayoutLength operator +(LayoutLength arg1, int arg2)
148         {
149             return new LayoutLength(arg1._value + (float)arg2);
150         }
151
152         /// <summary>
153         /// The subtraction operator.
154         /// </summary>
155         /// <param name="arg1">The first value.</param>
156         /// <param name="arg2">The second value.</param>
157         /// <returns>The LayoutLength containing the result of the subtraction.</returns>
158         public static LayoutLength operator -(LayoutLength arg1, LayoutLength arg2)
159         {
160             return new LayoutLength(arg1._value - arg2._value);
161         }
162
163         /// <summary>
164         /// The subtraction operator.
165         /// </summary>
166         /// <param name="arg1">The first value.</param>
167         /// <param name="arg2">The second value.</param>
168         /// <returns>The LayoutLength containing the result of the subtraction.</returns>
169         public static LayoutLength operator -(LayoutLength arg1, int arg2)
170         {
171             return new LayoutLength(arg1._value - (float)arg2);
172         }
173
174         /// <summary>
175         /// The multiplication operator.
176         /// </summary>
177         /// <param name="arg1">The first value.</param>
178         /// <param name="arg2">The second value.</param>
179         /// <returns>The LayoutLength containing the result of the multiplication.</returns>
180         public static LayoutLength operator *(LayoutLength arg1, LayoutLength arg2)
181         {
182             return new LayoutLength(arg1._value * arg2._value);
183         }
184
185         /// <summary>
186         /// Th multiplication operator.
187         /// </summary>
188         /// <param name="arg1">The first value.</param>
189         /// <param name="arg2">The int value to scale the LayoutLength.</param>
190         /// <returns>The LayoutLength containing the result of the scaling.</returns>
191         public static LayoutLength operator *(LayoutLength arg1, int arg2)
192         {
193             return new LayoutLength(arg1._value * arg2);
194         }
195
196         /// <summary>
197         /// The division operator.
198         /// </summary>
199         /// <param name="arg1">The first value.</param>
200         /// <param name="arg2">The second value.</param>
201         /// <returns>The LayoutLength containing the result of the division.</returns>
202         public static LayoutLength operator /(LayoutLength arg1, LayoutLength arg2)
203         {
204             return new LayoutLength(arg1._value /  arg2._value);
205         }
206
207         /// <summary>
208         /// Th division operator.
209         /// </summary>
210         /// <param name="arg1">The first value.</param>
211         /// <param name="arg2">The int value to scale the vector by.</param>
212         /// <returns>The LayoutLength containing the result of the scaling.</returns>
213         public static LayoutLength operator /(LayoutLength arg1, int arg2)
214         {
215             return new LayoutLength(arg1._value / (float)arg2);
216         }
217     }
218 }