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 public abstract class ShadowBase
30 private static readonly Vector2 noOffset = new Vector2(0, 0);
32 private static readonly Vector2 noExtents = new Vector2(0, 0);
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 protected ShadowBase() : this(noOffset, noExtents)
45 [EditorBrowsable(EditorBrowsableState.Never)]
46 protected ShadowBase(Vector2 offset, Vector2 extents)
48 Offset = offset == null ? null : new Vector2(offset);
49 Extents = extents == null ? null : new Vector2(extents);
52 /// <summary></summary>
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 internal protected ShadowBase(PropertyMap propertyMap)
59 if (propertyMap == null)
64 SetPropertyMap(propertyMap);
70 [EditorBrowsable(EditorBrowsableState.Never)]
71 protected ShadowBase(ShadowBase other) : this(other.Offset, other.Extents)
76 /// The position offset value (x, y) from the top left corner.
78 [EditorBrowsable(EditorBrowsableState.Never)]
79 public Vector2 Offset { get; set; }
82 /// The shadow will extend its size by specified amount of length.<br />
83 /// If the value is negative then the shadow will shrink.
84 /// For example, when View's size is (100, 100) and the Shadow's Extents is (5, -5),
85 /// the output shadow will have size (105, 95).
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 public Vector2 Extents { get; set; }
90 /// <summary></summary>
91 [EditorBrowsable(EditorBrowsableState.Never)]
92 public static bool operator ==(ShadowBase shadow1, ShadowBase shadow2)
94 return object.ReferenceEquals(shadow1, null) ? object.ReferenceEquals(shadow2, null) : shadow1.Equals(shadow2);
97 /// <summary></summary>
98 [EditorBrowsable(EditorBrowsableState.Never)]
99 public static bool operator !=(ShadowBase shadow1, ShadowBase shadow2)
101 return !(shadow1 == shadow2);
105 [EditorBrowsable(EditorBrowsableState.Never)]
106 public override bool Equals(object other)
108 if ((other == null) || ! this.GetType().Equals(other.GetType()))
113 var otherShadow = (ShadowBase)other;
115 if (!((Offset == null) ? otherShadow.Offset == null : Offset.Equals(otherShadow.Offset)))
120 return ((Extents == null) ? otherShadow.Extents == null : Extents.Equals(otherShadow.Extents));
124 [EditorBrowsable(EditorBrowsableState.Never)]
125 public override int GetHashCode()
129 int hash = Offset == null ? 0 : Offset.GetHashCode();
130 hash = (hash * 7) + (Extents == null ? 0 : Extents.GetHashCode());
135 internal abstract bool IsEmpty();
137 internal PropertyValue ToPropertyValue(BaseComponents.View attachedView)
141 return new PropertyValue();
144 var map = GetPropertyMap();
146 if (attachedView.CornerRadius > 0)
148 map[Visual.Property.CornerRadius] = new PropertyValue(attachedView.CornerRadius);
151 map[Visual.Property.Transform] = GetTransformMap();
153 return new PropertyValue(map);
158 [EditorBrowsable(EditorBrowsableState.Never)]
159 protected virtual PropertyMap GetPropertyMap()
161 PropertyMap map = new PropertyMap();
168 [EditorBrowsable(EditorBrowsableState.Never)]
169 protected virtual bool SetPropertyMap(PropertyMap propertyMap)
171 if (propertyMap == null)
176 var transformProperty = propertyMap.Find(Visual.Property.Transform);
178 if (transformProperty == null)
184 var transformMap = new PropertyMap();
186 if (transformProperty.Get(transformMap))
188 SetTransformMap(transformMap);
194 private PropertyValue GetTransformMap()
196 var transformMap = new PropertyMap();
198 if (!Offset.Equals(noOffset))
200 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute));
201 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(Offset);
204 if (!Extents.Equals(noExtents))
206 transformMap[(int)VisualTransformPropertyType.ExtraSize] = PropertyValue.CreateWithGuard(Extents);
209 transformMap[(int)VisualTransformPropertyType.Origin] = new PropertyValue((int)Visual.AlignType.Center);
210 transformMap[(int)VisualTransformPropertyType.AnchorPoint] = new PropertyValue((int)Visual.AlignType.Center);
212 return new PropertyValue(transformMap);
217 [EditorBrowsable(EditorBrowsableState.Never)]
218 private void SetTransformMap(PropertyMap transformMap)
220 if (transformMap == null)
225 transformMap.Find((int)VisualTransformPropertyType.Offset)?.Get(Offset);
226 transformMap.Find((int)VisualTransformPropertyType.ExtraSize)?.Get(Extents);