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;
49 /// The size value in scale that tansform should have in common
51 protected internal Vector2 scale;
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() : this(defaultOffset, noScale)
74 [EditorBrowsable(EditorBrowsableState.Never)]
75 public TransformablePropertyMap(TransformablePropertyMap other) : this(other.offset, other.scale)
82 protected internal TransformablePropertyMap(Vector2 offset, Vector2 scale)
84 propertyMap = new PropertyMap();
85 transformMap = new PropertyMap();
86 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(defaultOffsetPolicy);
90 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
93 private void OnOffsetChanged(float x, float y)
98 private void OnScaleChanged(float widht, float height)
103 private void UpdateOffset()
105 if (!ClearTransformMapIfNeeds())
107 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
108 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
110 OnPropertyChanged?.Invoke(this);
113 private void UpdateScale()
115 if (!ClearTransformMapIfNeeds())
117 transformMap[(int)VisualTransformPropertyType.Size] = PropertyValue.CreateWithGuard(scale);
118 propertyMap[Visual.Property.Transform] = new PropertyValue(transformMap);
120 OnPropertyChanged?.Invoke(this);
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.
128 virtual protected internal bool NeedTransformMap()
130 return (offset != null && !offset.Equals(noOffset)) || (scale != null && !scale.Equals(noScale));
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.
138 protected internal bool ClearTransformMapIfNeeds()
140 if (!NeedTransformMap())
142 transformMap.Clear();
143 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(defaultOffsetPolicy);
144 propertyMap[Visual.Property.Transform] = new PropertyValue();
151 /// The position offset value (x, y) from the top left corner.
153 [EditorBrowsable(EditorBrowsableState.Never)]
154 public Vector2 Offset
162 offset = value == null? null : new Vector2(OnOffsetChanged, value);
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.
171 [EditorBrowsable(EditorBrowsableState.Never)]
180 scale = value == null? null : new Vector2(OnScaleChanged, value);
185 abstract internal string ToDebugString();
187 abstract internal bool IsValid();
189 static internal PropertyValue ToPropertyValue(TransformablePropertyMap instance)
191 return (instance != null && instance.IsValid()) ? new PropertyValue(instance.propertyMap) : new PropertyValue();