2 * Copyright(c) 2020 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 ShadowBase
30 internal delegate void PropertyChangedCallback(ShadowBase 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 absoluteTransformPolicy = new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute);
42 /// The location offset value from the View
44 protected internal Vector2 offset;
47 /// The size value in scale
49 protected internal Vector2 scale;
52 /// The output property map
54 protected internal PropertyMap propertyMap;
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public ShadowBase() : this(noOffset, noScale)
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public ShadowBase(ShadowBase other) : this(other.offset, other.scale)
75 protected internal ShadowBase(Vector2 offset, Vector2 scale)
77 propertyMap = new PropertyMap();
83 private void OnOffsetChanged(float x, float y)
85 OnPropertyChanged?.Invoke(this);
88 private void OnScaleChanged(float widht, float height)
90 OnPropertyChanged?.Invoke(this);
94 /// The position offset value (x, y) from the top left corner.
96 [EditorBrowsable(EditorBrowsableState.Never)]
105 offset = new Vector2(OnOffsetChanged, value ?? noOffset);
106 OnPropertyChanged?.Invoke(this);
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.
114 [EditorBrowsable(EditorBrowsableState.Never)]
123 scale = new Vector2(OnScaleChanged, value ?? noScale);
124 OnPropertyChanged?.Invoke(this);
128 private PropertyValue GetTransformMap()
130 var transformMap = new PropertyMap();
132 if (!offset.Equals(noOffset))
134 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(absoluteTransformPolicy);
135 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
138 if (!scale.Equals(noScale))
140 transformMap[(int)VisualTransformPropertyType.Size] = PropertyValue.CreateWithGuard(scale);
143 return transformMap.Count() == 0 ? new PropertyValue() : new PropertyValue(transformMap);
146 abstract internal bool IsValid();
148 static internal PropertyValue ToPropertyValue(ShadowBase instance)
150 if (instance == null || !instance.IsValid())
152 return new PropertyValue();
155 instance.propertyMap[Visual.Property.Transform] = instance.GetTransformMap();
157 return new PropertyValue(instance.propertyMap);