Follow formatting NUI
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / ViewProperty / ShadowBase.cs
1 /*
2  * Copyright(c) 2020 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.ComponentModel;
19 using System.Diagnostics;
20
21 namespace Tizen.NUI
22 {
23     /// <summary>
24     /// The property map class that has transform property for one of its items.
25     /// This class can be used to convert visual properties to map.
26     /// </summary>
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public abstract class ShadowBase
29     {
30         private static readonly Vector2 noOffset = new Vector2(0, 0);
31
32         private static readonly Vector2 noExtents = new Vector2(0, 0);
33
34         /// <summary>
35         /// Constructor
36         /// </summary>
37         [EditorBrowsable(EditorBrowsableState.Never)]
38         protected ShadowBase() : this(noOffset, noExtents)
39         {
40         }
41
42         /// <summary>
43         /// Constructor
44         /// </summary>
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         protected ShadowBase(Vector2 offset, Vector2 extents)
47         {
48             Offset = offset == null ? null : new Vector2(offset);
49             Extents = extents == null ? null : new Vector2(extents);
50         }
51
52         /// <summary>
53         /// Create a Shadow from a property map.
54         /// </summary>
55         [EditorBrowsable(EditorBrowsableState.Never)]
56         protected ShadowBase(PropertyMap propertyMap)
57         {
58             Debug.Assert(propertyMap != null);
59
60             Offset = noOffset;
61             Extents = noExtents;
62
63             var transformProperty = propertyMap.Find(Visual.Property.Transform);
64
65             if (transformProperty == null)
66             {
67                 // No transform map
68                 return;
69             }
70
71             var transformMap = new PropertyMap();
72
73             if (transformProperty.Get(transformMap))
74             {
75                 SetTransformMap(transformMap);
76             }
77         }
78
79         /// <summary>
80         /// Copy Constructor
81         /// </summary>
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         protected ShadowBase(ShadowBase other) : this(other.Offset, other.Extents)
84         {
85         }
86
87         /// <summary>
88         /// The position offset value (x, y) from the top left corner.
89         /// </summary>
90         [EditorBrowsable(EditorBrowsableState.Never)]
91         public Vector2 Offset { get; set; }
92
93         /// <summary>
94         /// The shadow will extend its size by specified amount of length.<br />
95         /// If the value is negative then the shadow will shrink.
96         /// For example, when View's size is (100, 100) and the Shadow's Extents is (5, -5),
97         /// the output shadow will have size (105, 95).
98         /// </summary>
99         [EditorBrowsable(EditorBrowsableState.Never)]
100         public Vector2 Extents { get; set; }
101
102         /// <summary>
103         /// Equality operator.
104         /// </summary>
105         [EditorBrowsable(EditorBrowsableState.Never)]
106         public static bool operator ==(ShadowBase shadow1, ShadowBase shadow2)
107         {
108             return object.ReferenceEquals(shadow1, null) ? object.ReferenceEquals(shadow2, null) : shadow1.Equals(shadow2);
109         }
110
111         /// <summary>
112         /// Inequality operator.
113         /// </summary>
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public static bool operator !=(ShadowBase shadow1, ShadowBase shadow2)
116         {
117             return !(shadow1 == shadow2);
118         }
119
120         /// <inheritdoc/>
121         [EditorBrowsable(EditorBrowsableState.Never)]
122         public override bool Equals(object other)
123         {
124             if ((other == null) || !GetType().Equals(other.GetType()))
125             {
126                 return false;
127             }
128
129             var otherShadow = (ShadowBase)other;
130
131             if (!((Offset == null) ? otherShadow.Offset == null : Offset.Equals(otherShadow.Offset)))
132             {
133                 return false;
134             }
135
136             return (Extents == null) ? otherShadow.Extents == null : Extents.Equals(otherShadow.Extents);
137         }
138
139         /// <inheritdoc/>
140         [EditorBrowsable(EditorBrowsableState.Never)]
141         public override int GetHashCode()
142         {
143             unchecked
144             {
145                 int hash = Offset == null ? 0 : Offset.GetHashCode();
146                 hash = (hash * 7) + (Extents == null ? 0 : Extents.GetHashCode());
147                 return hash;
148             }
149         }
150
151         internal abstract bool IsEmpty();
152
153         internal PropertyValue ToPropertyValue(BaseComponents.View attachedView)
154         {
155             if (IsEmpty())
156             {
157                 return new PropertyValue();
158             }
159
160             var map = GetPropertyMap();
161
162             if (attachedView.CornerRadius > 0)
163             {
164                 map[Visual.Property.CornerRadius] = new PropertyValue(attachedView.CornerRadius);
165             }
166
167             map[Visual.Property.Transform] = GetTransformMap();
168
169             return new PropertyValue(map);
170         }
171
172         /// <summary>
173         /// Extract a property map.
174         /// </summary>
175         [EditorBrowsable(EditorBrowsableState.Never)]
176         protected virtual PropertyMap GetPropertyMap()
177         {
178             PropertyMap map = new PropertyMap();
179
180             return map;
181         }
182
183         private PropertyValue GetTransformMap()
184         {
185             var transformMap = new PropertyMap();
186
187             if (!noOffset.Equals(Offset))
188             {
189                 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute));
190                 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(Offset);
191             }
192
193             if (!noExtents.Equals(Extents))
194             {
195                 transformMap[(int)VisualTransformPropertyType.ExtraSize] = PropertyValue.CreateWithGuard(Extents);
196             }
197
198             transformMap[(int)VisualTransformPropertyType.Origin] = new PropertyValue((int)Visual.AlignType.Center);
199             transformMap[(int)VisualTransformPropertyType.AnchorPoint] = new PropertyValue((int)Visual.AlignType.Center);
200
201             return new PropertyValue(transformMap);
202         }
203
204         private void SetTransformMap(PropertyMap transformMap)
205         {
206             if (transformMap == null)
207             {
208                 return;
209             }
210
211             transformMap.Find((int)VisualTransformPropertyType.Offset)?.Get(Offset);
212             transformMap.Find((int)VisualTransformPropertyType.ExtraSize)?.Get(Extents);
213         }
214     }
215 }
216
217