a0951d57f86c4d9b15c27644ff237e00186e70e3
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / ViewProperty / Shadow.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     /// Represents a shadow with color and blur radius for a View.
25     /// </summary>
26     [EditorBrowsable(EditorBrowsableState.Never)]
27     public class Shadow : ShadowBase
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         /// <summary>
34         /// Create a Shadow with default values.
35         /// </summary>
36         [EditorBrowsable(EditorBrowsableState.Never)]
37         public Shadow() : base()
38         {
39             BlurRadius = 0;
40             Color = defaultColor;
41         }
42
43         /// <summary>
44         /// Create a Shadow with custom values.
45         /// </summary>
46         [EditorBrowsable(EditorBrowsableState.Never)]
47         public Shadow(float blurRadius, Vector2 offset, Color color, Vector2 extents) : base(offset, extents)
48         {
49             BlurRadius = blurRadius;
50             Color = color;
51         }
52
53         /// <summary>
54         /// Copy constructor.
55         /// </summary>
56         [EditorBrowsable(EditorBrowsableState.Never)]
57         public Shadow(Shadow other) : this(other.BlurRadius, other.Offset, other.Color, other.Extents)
58         {
59         }
60
61         /// <summary>
62         /// Create a Shadow from a propertyMap.
63         /// </summary>
64         [EditorBrowsable(EditorBrowsableState.Never)]
65         internal Shadow(PropertyMap propertyMap) : base(propertyMap)
66         {
67         }
68
69         /// <summary>
70         /// The color for the shadow.
71         /// </summary>
72         [EditorBrowsable(EditorBrowsableState.Never)]
73         public Color Color { get; set; }
74
75         /// <summary>
76         /// The blur radius value for the shadow. Bigger value, much blurry.
77         /// </summary>
78         /// <remark>
79         /// Negative value is ignored. (no blur)
80         /// </remark>
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         public float BlurRadius { get; set; }
83
84         /// <inheritdoc/>
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         public override bool Equals(object other)
87         {
88             if (!base.Equals(other))
89             {
90                 return false;
91             }
92
93             var otherShadow = (Shadow)other;
94
95             if (!((Color == null) ? otherShadow.Color == null : Color.Equals(otherShadow.Color)))
96             {
97                 return false;
98             }
99
100             return BlurRadius.Equals(otherShadow.BlurRadius);
101         }
102
103         /// <inheritdoc/>
104         [EditorBrowsable(EditorBrowsableState.Never)]
105         public override int GetHashCode()
106         {
107             unchecked
108             {
109                 int hash = base.GetHashCode();
110                 hash = (hash * 7) + (Color == null ? 0 : Color.GetHashCode());
111                 hash = (hash * 7) + (BlurRadius.GetHashCode());
112                 return hash;
113             }
114         }
115
116         internal override bool IsEmpty()
117         {
118             return (Color == null || Color.A == 0);
119         }
120
121         /// <inheritdoc/>
122         [EditorBrowsable(EditorBrowsableState.Never)]
123         protected override PropertyMap GetPropertyMap()
124         {
125             var map = base.GetPropertyMap();
126
127             map[Visual.Property.Type] = new PropertyValue((int)Visual.Type.Color);
128
129             map[ColorVisualProperty.MixColor] = new PropertyValue(Color ?? noColor);
130
131             map[ColorVisualProperty.BlurRadius] = new PropertyValue(BlurRadius < 0 ? 0 : BlurRadius);
132
133             return map;
134         }
135
136         /// <inheritdoc/>
137         [EditorBrowsable(EditorBrowsableState.Never)]
138         protected override bool SetPropertyMap(PropertyMap propertyMap)
139         {
140             if (!base.SetPropertyMap(propertyMap))
141             {
142                 return false;
143             }
144
145             Color = noColor;
146             propertyMap.Find(ColorVisualProperty.MixColor)?.Get(Color);
147
148             float blurRadius = 0;
149             propertyMap.Find(ColorVisualProperty.BlurRadius)?.Get(out blurRadius);
150             BlurRadius = blurRadius;
151
152             return true;
153         }
154     }
155 }
156
157