[NUI] Implement shadow in View (#1223)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / ViewProperty / Shadow.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.ComponentModel;
19
20 namespace Tizen.NUI
21 {
22
23     /// <summary>
24     /// The platform provided shadow drawing for View
25     /// </summary>
26     [EditorBrowsable(EditorBrowsableState.Never)]
27     public class Shadow : TransformablePropertyMap
28     {
29         private static readonly Color noColor = new Color(0, 0, 0, 0);
30
31         private static readonly Color defaultColor = new Color(0, 0, 0, 0.5f);
32
33         private Color color;
34
35         private uint blurRadius;
36
37         /// <summary>
38         /// Constructor
39         /// </summary>
40         [EditorBrowsable(EditorBrowsableState.Never)]
41         public Shadow() : base()
42         {
43             Color = defaultColor;
44         }
45
46         /// <summary>
47         /// The boolean conversion
48         /// </summary>
49         [EditorBrowsable(EditorBrowsableState.Never)]
50         public static implicit operator Shadow(bool value)
51         {
52             Shadow shadow = new Shadow()
53             {
54                 Color = value ? defaultColor : noColor,
55             };
56             return shadow;
57         }
58
59         private void OnColorChanged(float r, float g, float b, float a)
60         {
61             UpdateColor();
62         }
63
64         private void UpdateColor()
65         {
66             propertyMap[ColorVisualProperty.MixColor] = PropertyValue.CreateWithGuard(color);
67             OnPropertyChanged?.Invoke(this);
68         }
69
70         private void UpdateBlurRadius()
71         {
72             // TODO update blur radius value in the property map
73             OnPropertyChanged?.Invoke(this);
74         }
75
76         /// <summary>
77         /// The color for the shadow.
78         /// </summary>
79         [EditorBrowsable(EditorBrowsableState.Never)]
80         public Color Color
81         {
82             get
83             {
84                 return color;
85             }
86             set
87             {
88                 color = value;
89                 UpdateColor();
90             }
91         }
92
93         /// <summary>
94         /// The blur radius value for the shadow. Bigger value, much blurry.
95         /// </summary>
96         private uint BlurRadius
97         {
98             get
99             {
100                 return blurRadius;
101             }
102             set
103             {
104                 blurRadius = value;
105                 UpdateBlurRadius();
106             }
107         }
108
109         override internal string ToDebugString()
110         {
111             string result = "";
112             // TODO
113             return result;
114         }
115
116         override internal bool IsValid()
117         {
118             return color != null && color.A != 0;
119         }
120
121         static internal new PropertyValue ToPropertyValue(TransformablePropertyMap instance)
122         {
123             if (instance == null || !instance.IsValid())
124             {
125                 return new PropertyValue();
126             }
127
128             // TODO to be other blurable visual in the future
129             instance.propertyMap.Insert(Visual.Property.Type, new PropertyValue((int)Visual.Type.Color));
130
131             return new PropertyValue(instance.propertyMap);
132         }
133     }
134 }
135
136