[NUI] Revert patch about StyleManager (#1970)
[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
20 namespace Tizen.NUI
21 {
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 = new Vector2(offset);
49             Extents = new Vector2(extents);
50         }
51
52         /// <summary></summary>
53         [EditorBrowsable(EditorBrowsableState.Never)]
54         internal protected ShadowBase(PropertyMap propertyMap)
55         {
56             Offset = noOffset;
57             Extents = noExtents;
58
59             if (propertyMap == null)
60             {
61                 return;
62             }
63
64             SetPropertyMap(propertyMap);
65         }
66
67         /// <summary>
68         /// Copy Constructor
69         /// </summary>
70         [EditorBrowsable(EditorBrowsableState.Never)]
71         protected ShadowBase(ShadowBase other) : this(other.Offset, other.Extents)
72         {
73         }
74
75         /// <summary>
76         /// The position offset value (x, y) from the top left corner.
77         /// </summary>
78         [EditorBrowsable(EditorBrowsableState.Never)]
79         public Vector2 Offset { get; set; }
80
81         /// <summary>
82         /// The shadow will extend its size by specified amount of length.<br />
83         /// If the value is negative then the shadow will shrink.
84         /// For example, when View's size is (100, 100) and the Shadow's Extents is (5, -5),
85         /// the output shadow will have size (105, 95).
86         /// </summary>
87         [EditorBrowsable(EditorBrowsableState.Never)]
88         public Vector2 Extents { get; set; }
89
90         /// <summary></summary>
91         [EditorBrowsable(EditorBrowsableState.Never)]
92         public static bool operator ==(ShadowBase shadow1, ShadowBase shadow2)
93         {
94             return object.ReferenceEquals(shadow1, null) ? object.ReferenceEquals(shadow2, null) : shadow1.Equals(shadow2);
95         }
96
97         /// <summary></summary>
98         [EditorBrowsable(EditorBrowsableState.Never)]
99         public static bool operator !=(ShadowBase shadow1, ShadowBase shadow2)
100         {
101             return !(shadow1 == shadow2);
102         }
103
104         /// <inheritdoc/>
105         [EditorBrowsable(EditorBrowsableState.Never)]
106         public override bool Equals(object other)
107         {
108             if ((other == null) || ! this.GetType().Equals(other.GetType())) 
109             {
110                 return false;
111             }
112             
113             var otherShadow = (ShadowBase)other;
114
115             if (!((Offset == null) ? otherShadow.Offset == null : Offset.Equals(otherShadow.Offset)))
116             {
117                 return false;
118             }
119
120             return ((Extents == null) ? otherShadow.Extents == null : Extents.Equals(otherShadow.Extents));
121         }
122
123         /// <inheritdoc/>
124         [EditorBrowsable(EditorBrowsableState.Never)]
125         public override int GetHashCode()
126         {
127             unchecked
128             {
129                 int hash = Offset == null ? 0 : Offset.GetHashCode();
130                 hash = (hash * 7) + (Extents == null ? 0 : Extents.GetHashCode());
131                 return hash;
132             }
133         }
134
135         internal abstract bool IsEmpty();
136
137         internal PropertyValue ToPropertyValue(BaseComponents.View attachedView)
138         {
139             if (IsEmpty())
140             {
141                 return new PropertyValue();
142             }
143
144             var map = GetPropertyMap();
145
146             if (attachedView.CornerRadius > 0)
147             {
148                 map[Visual.Property.CornerRadius] = new PropertyValue(attachedView.CornerRadius);
149             }
150
151             map[Visual.Property.Transform] = GetTransformMap();
152
153             return new PropertyValue(map);
154         }
155
156         /// <summary>
157         /// </summary>
158         [EditorBrowsable(EditorBrowsableState.Never)]
159         protected virtual PropertyMap GetPropertyMap()
160         {
161             PropertyMap map = new PropertyMap();
162
163             return map;
164         }
165
166         /// <summary>
167         /// </summary>
168         [EditorBrowsable(EditorBrowsableState.Never)]
169         protected virtual bool SetPropertyMap(PropertyMap propertyMap)
170         {
171             if (propertyMap == null)
172             {
173                 return false;
174             }
175
176             var transformProperty = propertyMap.Find(Visual.Property.Transform);
177
178             if (transformProperty == null)
179             {
180                 // No transform map
181                 return true;
182             }
183
184             var transformMap = new PropertyMap();
185
186             if (transformProperty.Get(transformMap))
187             {
188                 SetTransformMap(transformMap);
189             }
190
191             return true;
192         }
193
194         private PropertyValue GetTransformMap()
195         {
196             var transformMap = new PropertyMap();
197
198             if (!Offset.Equals(noOffset))
199             {
200                 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute));
201                 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(Offset);
202             }
203
204             if (!Extents.Equals(noExtents))
205             {
206                 transformMap[(int)VisualTransformPropertyType.ExtraSize] = PropertyValue.CreateWithGuard(Extents);
207             }
208
209             transformMap[(int)VisualTransformPropertyType.Origin] = new PropertyValue((int)Visual.AlignType.Center);
210             transformMap[(int)VisualTransformPropertyType.AnchorPoint] = new PropertyValue((int)Visual.AlignType.Center);
211
212             return new PropertyValue(transformMap);
213         }
214
215         /// <summary>
216         /// </summary>
217         [EditorBrowsable(EditorBrowsableState.Never)]
218         private void SetTransformMap(PropertyMap transformMap)
219         {
220             if (transformMap == null)
221             {
222                 return;
223             }
224
225             transformMap.Find((int)VisualTransformPropertyType.Offset)?.Get(Offset);
226             transformMap.Find((int)VisualTransformPropertyType.ExtraSize)?.Get(Extents);
227         }
228     }
229 }
230
231