[NUI] Apply CornerRadius to View (#1463)
[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     abstract public class ShadowBase
29     {
30         internal delegate void PropertyChangedCallback(ShadowBase instance);
31
32         internal PropertyChangedCallback OnPropertyChanged = null;
33
34         private static readonly Vector2 noOffset = new Vector2(0, 0);
35
36         private static readonly Vector2 noExtents = new Vector2(0, 0);
37
38         private static readonly Vector2 absoluteTransformPolicy = new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute);
39
40
41         /// <summary>
42         /// The location offset value from the View
43         /// </summary>
44         protected internal Vector2 offset;
45
46         /// <summary>
47         /// The size value in extension length
48         /// </summary>
49         protected internal Vector2 extents;
50
51         /// <summary>
52         /// The output property map
53         /// </summary>
54         protected internal PropertyMap propertyMap;
55
56         /// <summary>
57         /// Constructor
58         /// </summary>
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public ShadowBase() : this(noOffset, noExtents)
61         {
62         }
63
64         /// <summary>
65         /// Copy Constructor
66         /// </summary>
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         public ShadowBase(ShadowBase other) : this(other.offset, other.extents)
69         {
70         }
71
72         /// <summary>
73         /// Constructor
74         /// </summary>
75         private ShadowBase(Vector2 offset, Vector2 extents)
76         {
77             propertyMap = new PropertyMap();
78
79             Offset = offset;
80             Extents = extents;
81         }
82
83         private void OnOffsetOrSizeChanged(float x, float y)
84         {
85             OnPropertyChanged?.Invoke(this);
86         }
87
88         /// <summary>
89         /// The position offset value (x, y) from the top left corner.
90         /// </summary>
91         [EditorBrowsable(EditorBrowsableState.Never)]
92         public Vector2 Offset
93         {
94             get
95             {
96                 return offset;
97             }
98             set
99             {
100                 offset = new Vector2(OnOffsetOrSizeChanged, value ?? noOffset);
101                 OnPropertyChanged?.Invoke(this);
102             }
103         }
104
105         /// <summary>
106         /// The value indicates percentage of the container size. <br />
107         /// e.g. (0.5f, 1.0f) means 50% of the container's width and 100% of container's height.
108         /// </summary>
109         /// <Remarks> It is not guaranteed that this would work well. It will be removed in the next version.</Remarks>
110         [EditorBrowsable(EditorBrowsableState.Never)]
111         public Vector2 Scale
112         {
113             get
114             {
115                 return new Vector2();
116             }
117             set
118             {
119             }
120         }
121
122         /// <summary>
123         /// The shadow will extend its size by specified amount of length.<br />
124         /// If the value is negative then the shadow will shrink.
125         /// For example, when View's size is (100, 100) and the Shadow's Extents is (5, -5),
126         /// the output shadow will have size (105, 95).
127         /// </summary>
128         [EditorBrowsable(EditorBrowsableState.Never)]
129         public Vector2 Extents
130         {
131             get
132             {
133                 return extents;
134             }
135             set
136             {
137                 extents = new Vector2(OnOffsetOrSizeChanged, value ?? noExtents);
138                 OnPropertyChanged?.Invoke(this);
139             }
140         }
141
142         private PropertyValue GetTransformMap(BaseComponents.View attachedView)
143         {
144             var transformMap = new PropertyMap();
145
146             if (!offset.Equals(noOffset))
147             {
148                 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(absoluteTransformPolicy);
149                 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
150             }
151
152             if (!extents.Equals(noExtents))
153             {
154                 var viewSize = new Vector2(attachedView.GetRelayoutSize(DimensionType.Width), attachedView.GetRelayoutSize(DimensionType.Height));
155                 var shadowSize = viewSize + extents;
156
157                 transformMap[(int)VisualTransformPropertyType.SizePolicy] = new PropertyValue(absoluteTransformPolicy);
158                 transformMap[(int)VisualTransformPropertyType.Size] = PropertyValue.CreateWithGuard(shadowSize);
159             }
160
161             return transformMap.Count() == 0 ? new PropertyValue() : new PropertyValue(transformMap);
162         }
163
164         abstract internal bool IsValid();
165
166         internal bool HasValidSizeExtents()
167         {
168             return IsValid() && !extents.Equals(noExtents);
169         }
170
171         static internal PropertyValue ToPropertyValue(ShadowBase instance, BaseComponents.View attachedView)
172         {
173             if (instance == null || !instance.IsValid())
174             {
175                 return new PropertyValue();
176             }
177
178             if (attachedView.CornerRadius > 0)
179             {
180                 instance.propertyMap[Visual.Property.CornerRadius] = new PropertyValue(attachedView.CornerRadius);
181             }
182
183             instance.propertyMap[Visual.Property.Transform] = instance.GetTransformMap(attachedView);
184
185             return new PropertyValue(instance.propertyMap);
186         }
187     }
188 }
189
190