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 noExtents = new Vector2(0, 0);
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 extension length
49 protected internal Vector2 extents;
52 /// The output property map
54 protected internal PropertyMap propertyMap;
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 public ShadowBase() : this(noOffset, noExtents)
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public ShadowBase(ShadowBase other) : this(other.offset, other.extents)
75 private ShadowBase(Vector2 offset, Vector2 extents)
77 propertyMap = new PropertyMap();
83 private void OnOffsetOrSizeChanged(float x, float y)
85 OnPropertyChanged?.Invoke(this);
89 /// The position offset value (x, y) from the top left corner.
91 [EditorBrowsable(EditorBrowsableState.Never)]
100 offset = new Vector2(OnOffsetOrSizeChanged, value ?? noOffset);
101 OnPropertyChanged?.Invoke(this);
106 /// The value indicates percentage of the container size. <br />
107 /// e.g. (0.5f, 1.0f) means 50% of the container's width and 100% of container's height.
109 /// <Remarks> It is not guaranteed that this would work well. It will be removed in the next version.</Remarks>
110 [EditorBrowsable(EditorBrowsableState.Never)]
115 return new Vector2();
123 /// The shadow will extend its size by specified amount of length.<br />
124 /// If the value is negative then the shadow will shrink.
125 /// For example, when View's size is (100, 100) and the Shadow's Extents is (5, -5),
126 /// the output shadow will have size (105, 95).
128 [EditorBrowsable(EditorBrowsableState.Never)]
129 public Vector2 Extents
137 extents = new Vector2(OnOffsetOrSizeChanged, value ?? noExtents);
138 OnPropertyChanged?.Invoke(this);
142 private PropertyValue GetTransformMap(BaseComponents.View attachedView)
144 var transformMap = new PropertyMap();
146 if (!offset.Equals(noOffset))
148 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(absoluteTransformPolicy);
149 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(offset);
152 if (!extents.Equals(noExtents))
154 var viewSize = new Vector2(attachedView.GetRelayoutSize(DimensionType.Width), attachedView.GetRelayoutSize(DimensionType.Height));
155 var shadowSize = viewSize + extents;
157 transformMap[(int)VisualTransformPropertyType.SizePolicy] = new PropertyValue(absoluteTransformPolicy);
158 transformMap[(int)VisualTransformPropertyType.Size] = PropertyValue.CreateWithGuard(shadowSize);
161 return transformMap.Count() == 0 ? new PropertyValue() : new PropertyValue(transformMap);
164 abstract internal bool IsValid();
166 internal bool HasValidSizeExtents()
168 return IsValid() && !extents.Equals(noExtents);
171 static internal PropertyValue ToPropertyValue(ShadowBase instance, BaseComponents.View attachedView)
173 if (instance == null || !instance.IsValid())
175 return new PropertyValue();
178 instance.propertyMap[Visual.Property.Transform] = instance.GetTransformMap(attachedView);
180 return new PropertyValue(instance.propertyMap);