[NUI.Components] Remove InputField (#1283)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / ViewProperty / TransformablePropertyMap.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 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 TransformablePropertyMap
29     {
30         internal delegate void PropertyChangedCallback(TransformablePropertyMap 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 defaultOffset = new Vector2(0, 0);
39
40         private static readonly Vector2 defaultOffsetPolicy = new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute);
41
42
43         /// <summary>
44         /// The offset value that tansform should have in common
45         /// </summary>
46         protected internal Vector2 offset;
47
48         /// <summary>
49         /// The size value in scale that tansform should have in common
50         /// </summary>
51         protected internal Vector2 scale;
52
53         /// <summary>
54         /// The output property map
55         /// </summary>
56         protected internal PropertyMap propertyMap;
57
58         /// <summary>
59         /// The transform property map
60         /// </summary>
61         protected internal PropertyMap transformMap;
62
63         /// <summary>
64         /// Constructor
65         /// </summary>
66         [EditorBrowsable(EditorBrowsableState.Never)]
67         public TransformablePropertyMap() : this(defaultOffset, noScale)
68         {
69         }
70
71         /// <summary>
72         /// Copy Constructor
73         /// </summary>
74         [EditorBrowsable(EditorBrowsableState.Never)]
75         public TransformablePropertyMap(TransformablePropertyMap other) : this(other.offset, other.scale)
76         {
77         }
78
79         /// <summary>
80         /// Constructor
81         /// </summary>
82         protected internal TransformablePropertyMap(Vector2 offset, Vector2 scale)
83         {
84             propertyMap = new PropertyMap();
85             transformMap = new PropertyMap();
86             transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(defaultOffsetPolicy);
87
88             Offset = offset;
89             Scale = scale;
90             propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
91         }
92
93         private void OnOffsetChanged(float x, float y)
94         {
95             UpdateOffset();
96         }
97
98         private void OnScaleChanged(float widht, float height)
99         {
100             UpdateScale();
101         }
102
103         private void UpdateOffset()
104         {
105             if (!ClearTransformMapIfNeeds())
106             {
107                 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
108                 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
109             }
110             OnPropertyChanged?.Invoke(this);
111         }
112
113         private void UpdateScale()
114         {
115             if (!ClearTransformMapIfNeeds())
116             {
117                 transformMap[(int)VisualTransformPropertyType.Size] = PropertyValue.CreateWithGuard(scale);
118                 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
119             }
120             OnPropertyChanged?.Invoke(this);
121         }
122
123         /// <summary>
124         /// Indicates whether the transform map is needed or not.
125         /// This checks offset and scale values are valid.
126         /// It can be overwritten in the derived class.
127         /// </summary>
128         virtual protected internal bool NeedTransformMap()
129         {
130             return (offset != null && !offset.Equals(noOffset)) || (scale != null && !scale.Equals(noScale));
131         }
132
133         /// <summary>
134         /// If this map does not need to have transform property(= no offset and no size),
135         /// clear existing transform map and return true.
136         /// Return false when it needs to transform.
137         /// </summary>
138         protected internal bool ClearTransformMapIfNeeds()
139         {
140             if (!NeedTransformMap())
141             {
142                 transformMap.Clear();
143                 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(defaultOffsetPolicy);
144                 propertyMap[Visual.Property.Transform] = new PropertyValue();
145                 return true;
146             }
147             return false;
148         }
149
150         /// <summary>
151         /// The position offset value (x, y) from the top left corner.
152         /// </summary>
153         [EditorBrowsable(EditorBrowsableState.Never)]
154         public Vector2 Offset
155         {
156             get
157             {
158                 return offset;
159             }
160             set
161             {
162                 offset = value == null? null : new Vector2(OnOffsetChanged, value);
163                 UpdateOffset();
164             }
165         }
166
167         /// <summary>
168         /// The value indicates percentage of the container size. <br />
169         /// e.g. (0.5f, 1.0f) means 50% of the container's width and 100% of container's height.
170         /// </summary>
171         [EditorBrowsable(EditorBrowsableState.Never)]
172         public Vector2 Scale
173         {
174             get
175             {
176                 return scale;
177             }
178             set
179             {
180                 scale = value == null? null : new Vector2(OnScaleChanged, value);
181                 UpdateScale();
182             }
183         }
184
185         abstract internal string ToDebugString();
186
187         abstract internal bool IsValid();
188
189         static internal PropertyValue ToPropertyValue(TransformablePropertyMap instance)
190         {
191             return (instance != null && instance.IsValid()) ? new PropertyValue(instance.propertyMap) : new PropertyValue();
192         }
193     }
194 }
195
196