2 * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
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.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 abstract public class TransformablePropertyMap
30 internal delegate void PropertyChangedCallback(TransformablePropertyMap instance);
32 internal PropertyChangedCallback OnPropertyChanged = null;
34 private static readonly Vector2 noOffset = new Vector2(0, 0);
36 private static readonly Vector2 noScale = new Vector2(1, 1);
38 private static readonly Vector2 defaultOffset = new Vector2(0, 0);
40 private static readonly Vector2 defaultOffsetPolicy = new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute);
44 /// The offset value that tansform should have in common
46 protected internal Vector2 offset = defaultOffset;
49 /// The size value in scale that tansform should have in common
51 protected internal Vector2 scale = noScale;
54 /// The output property map
56 protected internal PropertyMap propertyMap;
59 /// The transform property map
61 protected internal PropertyMap transformMap;
66 [EditorBrowsable(EditorBrowsableState.Never)]
67 public TransformablePropertyMap()
70 propertyMap = new PropertyMap();
71 transformMap = new PropertyMap();
73 // Offet has default value, so need to update map
74 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(defaultOffsetPolicy);
75 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
76 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
79 private void OnOffsetChanged(float x, float y)
84 private void OnScaleChanged(float widht, float height)
89 private void UpdateOffset()
91 if (!ClearTransformMapIfNeeds())
93 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
94 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
96 OnPropertyChanged?.Invoke(this);
99 private void UpdateScale()
101 if (!ClearTransformMapIfNeeds())
103 transformMap[(int)VisualTransformPropertyType.Size] = PropertyValue.CreateWithGuard(scale);
104 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
106 OnPropertyChanged?.Invoke(this);
110 /// Indicates whether the transform map is needed or not.
111 /// This checks offset and scale values are valid.
112 /// It can be overwritten in the derived class.
114 virtual protected internal bool NeedTransformMap()
116 return (offset != null && !offset.Equals(noOffset)) || (scale != null && !scale.Equals(noScale));
120 /// If this map does not need to have transform property(= no offset and no size),
121 /// clear existing transform map and return true.
122 /// Return false when it needs to transform.
124 protected internal bool ClearTransformMapIfNeeds()
126 if (!NeedTransformMap())
128 transformMap.Clear();
129 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(defaultOffsetPolicy);
130 propertyMap[Visual.Property.Transform] = new PropertyValue();
137 /// The position offset value (x, y) from the top left corner.
139 [EditorBrowsable(EditorBrowsableState.Never)]
140 public Vector2 Offset
148 offset = value == null? null : new Vector2(OnOffsetChanged, value);
154 /// The value indicates percentage of the container size. <br />
155 /// e.g. (0.5f, 1.0f) means 50% of the container's width and 100% of container's height.
165 scale = value == null? null : new Vector2(OnScaleChanged, value);
170 abstract internal string ToDebugString();
172 abstract internal bool IsValid();
174 static internal PropertyValue ToPropertyValue(TransformablePropertyMap instance)
176 return (instance != null && instance.IsValid()) ? new PropertyValue(instance.propertyMap) : new PropertyValue();