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;
19 using System.Diagnostics;
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);
53 /// Create a Shadow from a property map.
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 protected ShadowBase(PropertyMap propertyMap)
58 Debug.Assert(propertyMap != null);
63 var transformProperty = propertyMap.Find(Visual.Property.Transform);
65 if (transformProperty == null)
71 var transformMap = new PropertyMap();
73 if (transformProperty.Get(transformMap))
75 SetTransformMap(transformMap);
82 [EditorBrowsable(EditorBrowsableState.Never)]
83 protected ShadowBase(ShadowBase other) : this(other.Offset, other.Extents)
88 /// The position offset value (x, y) from the top left corner.
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 public Vector2 Offset { get; set; }
94 /// The shadow will extend its size by specified amount of length.<br />
95 /// If the value is negative then the shadow will shrink.
96 /// For example, when View's size is (100, 100) and the Shadow's Extents is (5, -5),
97 /// the output shadow will have size (105, 95).
99 [EditorBrowsable(EditorBrowsableState.Never)]
100 public Vector2 Extents { get; set; }
103 /// Equality operator.
105 [EditorBrowsable(EditorBrowsableState.Never)]
106 public static bool operator ==(ShadowBase shadow1, ShadowBase shadow2)
108 return object.ReferenceEquals(shadow1, null) ? object.ReferenceEquals(shadow2, null) : shadow1.Equals(shadow2);
112 /// Inequality operator.
114 [EditorBrowsable(EditorBrowsableState.Never)]
115 public static bool operator !=(ShadowBase shadow1, ShadowBase shadow2)
117 return !(shadow1 == shadow2);
121 [EditorBrowsable(EditorBrowsableState.Never)]
122 public override bool Equals(object other)
124 if ((other == null) || !GetType().Equals(other.GetType()))
129 var otherShadow = (ShadowBase)other;
131 if (!((Offset == null) ? otherShadow.Offset == null : Offset.Equals(otherShadow.Offset)))
136 return (Extents == null) ? otherShadow.Extents == null : Extents.Equals(otherShadow.Extents);
140 [EditorBrowsable(EditorBrowsableState.Never)]
141 public override int GetHashCode()
145 int hash = Offset == null ? 0 : Offset.GetHashCode();
146 hash = (hash * 7) + (Extents == null ? 0 : Extents.GetHashCode());
151 internal abstract bool IsEmpty();
153 internal PropertyValue ToPropertyValue(BaseComponents.View attachedView)
157 return new PropertyValue();
160 var map = GetPropertyMap();
162 if (attachedView.CornerRadius > 0)
164 map[Visual.Property.CornerRadius] = new PropertyValue(attachedView.CornerRadius);
167 map[Visual.Property.Transform] = GetTransformMap();
169 return new PropertyValue(map);
173 /// Extract a property map.
175 [EditorBrowsable(EditorBrowsableState.Never)]
176 protected virtual PropertyMap GetPropertyMap()
178 PropertyMap map = new PropertyMap();
183 private PropertyValue GetTransformMap()
185 var transformMap = new PropertyMap();
187 if (!noOffset.Equals(Offset))
189 transformMap[(int)VisualTransformPropertyType.OffsetPolicy] = new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute));
190 transformMap[(int)VisualTransformPropertyType.Offset] = PropertyValue.CreateWithGuard(Offset);
193 if (!noExtents.Equals(Extents))
195 transformMap[(int)VisualTransformPropertyType.ExtraSize] = PropertyValue.CreateWithGuard(Extents);
198 transformMap[(int)VisualTransformPropertyType.Origin] = new PropertyValue((int)Visual.AlignType.Center);
199 transformMap[(int)VisualTransformPropertyType.AnchorPoint] = new PropertyValue((int)Visual.AlignType.Center);
201 return new PropertyValue(transformMap);
204 private void SetTransformMap(PropertyMap transformMap)
206 if (transformMap == null)
211 transformMap.Find((int)VisualTransformPropertyType.Offset)?.Get(Offset);
212 transformMap.Find((int)VisualTransformPropertyType.ExtraSize)?.Get(Extents);