06ac918b7704a5e47fcf97c932e5670bb6ccfb1f
[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 noScale = new Vector2(1, 1);
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 scale
48         /// </summary>
49         protected internal Vector2 scale;
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, noScale)
61         {
62         }
63
64         /// <summary>
65         /// Copy Constructor
66         /// </summary>
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         public ShadowBase(ShadowBase other) : this(other.offset, other.scale)
69         {
70         }
71
72         /// <summary>
73         /// Constructor
74         /// </summary>
75         protected internal ShadowBase(Vector2 offset, Vector2 scale)
76         {
77             propertyMap = new PropertyMap();
78
79             Offset = offset;
80             Scale = scale;
81         }
82
83         private void OnOffsetChanged(float x, float y)
84         {
85             OnPropertyChanged?.Invoke(this);
86         }
87
88         private void OnScaleChanged(float widht, float height)
89         {
90             OnPropertyChanged?.Invoke(this);
91         }
92
93         /// <summary>
94         /// The position offset value (x, y) from the top left corner.
95         /// </summary>
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public Vector2 Offset
98         {
99             get
100             {
101                 return offset;
102             }
103             set
104             {
105                 offset = new Vector2(OnOffsetChanged, value ?? noOffset);
106                 OnPropertyChanged?.Invoke(this);
107             }
108         }
109
110         /// <summary>
111         /// The value indicates percentage of the container size. <br />
112         /// e.g. (0.5f, 1.0f) means 50% of the container's width and 100% of container's height.
113         /// </summary>
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public Vector2 Scale
116         {
117             get
118             {
119                 return scale;
120             }
121             set
122             {
123                 scale = new Vector2(OnScaleChanged, value ?? noScale);
124                 OnPropertyChanged?.Invoke(this);
125             }
126         }
127
128         private PropertyValue GetTransformMap()
129         {
130             var transformMap = new PropertyMap();
131
132             if (!offset.Equals(noOffset))
133             {
134                 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(absoluteTransformPolicy);
135                 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
136             }
137
138             if (!scale.Equals(noScale))
139             {
140                 transformMap[(int)VisualTransformPropertyType.Size] = PropertyValue.CreateWithGuard(scale);
141             }
142
143             return transformMap.Count() == 0 ? new PropertyValue() : new PropertyValue(transformMap);
144         }
145
146         abstract internal bool IsValid();
147
148         static internal PropertyValue ToPropertyValue(ShadowBase instance)
149         {
150             if (instance == null || !instance.IsValid())
151             {
152                 return new PropertyValue();
153             }
154
155             instance.propertyMap[Visual.Property.Transform] = instance.GetTransformMap();
156
157             return new PropertyValue(instance.propertyMap);
158         }
159     }
160 }
161
162