/* * Copyright(c) 2019 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using System.ComponentModel; using Tizen.NUI.BaseComponents; namespace Tizen.NUI { /// /// A class encapsulating the transform map of the visual. /// /// 3 public class VisualMap { /// /// outputVisualMap. /// /// 3 protected PropertyMap _outputVisualMap = null; /// /// The shader of the visual. /// /// 3 protected PropertyMap _shader = null; //private PropertyMap _transform = null; /// /// The premultipliedAlpha of the visual. /// /// 3 protected bool? _premultipliedAlpha = null; /// /// The mixColor of the Visual. /// /// 3 protected Color _mixColor = null; /// /// The opacity of the visual. /// /// 3 protected float? _opacity = null; /// /// The FittingMode of the visual. /// /// 5 protected VisualFittingModeType? _visualFittingMode = null; /// /// The corner radius value of the visual. /// [EditorBrowsable(EditorBrowsableState.Never)] protected float? _cornerRadius = null; /// /// The map for visual. /// /// 3 protected PropertyMap _commonlyUsedMap = null; private Vector2 _visualSize = null; private Vector2 _visualOffset = null; private Vector2 _visualOffsetPolicy = null; private Vector2 _visualSizePolicy = null; private Visual.AlignType? _visualOrigin = null; private Visual.AlignType? _visualAnchorPoint = null; private PropertyMap _visualTransformMap = null; private int? _depthIndex = null; /// /// Constructor. /// /// 3 public VisualMap() { } /// /// Gets or sets the size of the visual.
/// It can be either relative (percentage of the parent) /// or absolute (in world units).
/// Optional. ///
/// 3 public Size2D Size { get { return _visualSize ?? (new Size2D(1, 1)); } set { _visualSize = value; if (_visualSizePolicy == null) { _visualSizePolicy = new Vector2(1.0f, 1.0f); } UpdateVisual(); } } /// /// Gets or sets the offset of the visual.
/// It can be either relative (percentage of the parent) /// or absolute (in world units).
/// Optional. ///
/// 3 public Vector2 Position { get { return _visualOffset ?? (new Vector2(0.0f, 0.0f)); } set { _visualOffset = value; if (_visualOffsetPolicy == null) { _visualOffsetPolicy = new Vector2(1.0f, 1.0f); } UpdateVisual(); } } /// /// Gets or sets the relative size of the visual
/// (percentage [0.0f to 1.0f] of the control).
/// Optional. ///
/// 3 public RelativeVector2 RelativeSize { get { return _visualSize ?? (new RelativeVector2(1.0f, 1.0f)); } set { _visualSize = value; _visualSizePolicy = new Vector2(0.0f, 0.0f); UpdateVisual(); } } /// /// Gets or sets the relative offset of the visual
/// (percentage [0.0f to 1.0f] of the control).
/// Optional. ///
/// 3 public RelativeVector2 RelativePosition { get { return _visualOffset ?? (new RelativeVector2(0.0f, 0.0f)); } set { _visualOffset = value; _visualOffsetPolicy = new Vector2(0.0f, 0.0f); UpdateVisual(); } } /// /// Gets or sets whether the x and y offset values are relative
/// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).
/// By default, both the x and the y offset are relative.
/// Optional. ///
/// 3 public VisualTransformPolicyType PositionPolicy { get { if (_visualOffsetPolicy != null && _visualOffsetPolicy.X == 1.0f && _visualOffsetPolicy.Y == 1.0f) { return VisualTransformPolicyType.Absolute; } return VisualTransformPolicyType.Relative; } set { switch (value) { case VisualTransformPolicyType.Relative: _visualOffsetPolicy = new Vector2(0.0f, 0.0f); break; case VisualTransformPolicyType.Absolute: _visualOffsetPolicy = new Vector2(1.0f, 1.0f); break; default: _visualOffsetPolicy = new Vector2(0.0f, 0.0f); break; } UpdateVisual(); } } /// /// Gets or sets whether the x offset values are relative
/// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).
/// By default, the x offset is relative.
/// Optional. ///
/// 3 public VisualTransformPolicyType PositionPolicyX { get { if (_visualOffsetPolicy != null && _visualOffsetPolicy.X == 1.0f) { return VisualTransformPolicyType.Absolute; } return VisualTransformPolicyType.Relative; } set { float x = 0.0f; switch (value) { case VisualTransformPolicyType.Relative: x = 0.0f; break; case VisualTransformPolicyType.Absolute: x = 1.0f; break; default: x = 0.0f; break; } _visualOffsetPolicy = new Vector2(x, _visualOffsetPolicy?.Y ?? 0); UpdateVisual(); } } /// /// Gets or sets whether the y offset values are relative
/// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).
/// By default, the y offset is relative.
/// Optional. ///
/// 3 public VisualTransformPolicyType PositionPolicyY { get { if (_visualOffsetPolicy != null && _visualOffsetPolicy.Y == 1.0f) { return VisualTransformPolicyType.Absolute; } return VisualTransformPolicyType.Relative; } set { float y = 0.0f; switch (value) { case VisualTransformPolicyType.Relative: y = 0.0f; break; case VisualTransformPolicyType.Absolute: y = 1.0f; break; default: y = 0.0f; break; } _visualOffsetPolicy = new Vector2(_visualOffsetPolicy?.X ?? 0, y); UpdateVisual(); } } /// /// Gets or sets whether the size values of the width or the height are relative
/// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).
/// By default, offsets of both the width and the height are relative to the control's size.
/// Optional. ///
/// 3 public VisualTransformPolicyType SizePolicy { get { if (_visualSizePolicy != null && _visualSizePolicy.X == 1.0f && _visualSizePolicy.Y == 1.0f) { return VisualTransformPolicyType.Absolute; } return VisualTransformPolicyType.Relative; } set { switch (value) { case VisualTransformPolicyType.Relative: _visualSizePolicy = new Vector2(0.0f, 0.0f); break; case VisualTransformPolicyType.Absolute: _visualSizePolicy = new Vector2(1.0f, 1.0f); break; default: _visualSizePolicy = new Vector2(0.0f, 0.0f); break; } UpdateVisual(); } } /// /// Gets or sets whether size values of the width are relative.
/// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).
/// By default, the value of the width is relative to the control's width.
/// Optional. ///
/// 3 public VisualTransformPolicyType SizePolicyWidth { get { if (_visualSizePolicy != null && _visualSizePolicy.Width == 1.0f) { return VisualTransformPolicyType.Absolute; } return VisualTransformPolicyType.Relative; } set { float width = 0.0f; switch (value) { case VisualTransformPolicyType.Relative: width = 0.0f; break; case VisualTransformPolicyType.Absolute: width = 1.0f; break; default: width = 0.0f; break; } _visualSizePolicy = new Vector2(width, _visualSizePolicy?.Height ?? 0); UpdateVisual(); } } /// /// Gets or sets whether size values of the height are relative
/// (percentage [0.0f to 1.0f] of the control) or absolute (in world units).
/// By default, the height value is relative to the control's height.
/// Optional. ///
/// 3 public VisualTransformPolicyType SizePolicyHeight { get { if (_visualSizePolicy != null && _visualSizePolicy.Height == 1.0f) { return VisualTransformPolicyType.Absolute; } return VisualTransformPolicyType.Relative; } set { float height = 0.0f; switch (value) { case VisualTransformPolicyType.Relative: height = 0.0f; break; case VisualTransformPolicyType.Absolute: height = 1.0f; break; default: height = 0.0f; break; } _visualSizePolicy = new Vector2(_visualSizePolicy?.Width ?? 0, height); UpdateVisual(); } } /// /// Gets or sets the origin of the visual within its control area.
/// By default, the origin is center.
/// Optional. ///
/// 3 public Visual.AlignType Origin { get { return _visualOrigin ?? (Visual.AlignType.Center); } set { _visualOrigin = value; UpdateVisual(); } } /// /// Gets or sets the anchor point of the visual.
/// By default, the anchor point is center.
/// Optional. ///
/// 3 public Visual.AlignType AnchorPoint { get { return _visualAnchorPoint ?? (Visual.AlignType.Center); } set { _visualAnchorPoint = value; UpdateVisual(); } } /// /// Gets or sets the depth index of the visual.
/// By default, the depth index is 0.
/// Optional. ///
/// 3 public int DepthIndex { get { return _depthIndex ?? (0); } set { _depthIndex = value; } } /// /// Gets the transform map used by the visual. /// /// 3 public PropertyMap OutputTransformMap { get { ComposingTransformMap(); return _visualTransformMap; } } /// /// Gets the property map to create the visual. /// /// 3 public PropertyMap OutputVisualMap { get { ComposingPropertyMap(); return _outputVisualMap; } } /// /// The shader to use in the visual. /// /// 3 public PropertyMap Shader { get { return _shader; } set { _shader = value; UpdateVisual(); } } /// /// Enables or disables the premultiplied alpha.
/// The premultiplied alpha is false by default unless this behavior is modified by the derived visual type. ///
/// 3 public bool PremultipliedAlpha { get { return _premultipliedAlpha ?? (false); } set { _premultipliedAlpha = value; UpdateVisual(); } } /// /// Mix color is a blend color for any visual. /// /// 3 public Color MixColor { get { return _mixColor; } set { _mixColor = value; UpdateVisual(); } } /// /// Opacity is the alpha component of the mix color discussed above. /// /// 3 public float Opacity { get { return _opacity ?? (1.0f); } set { _opacity = value; UpdateVisual(); } } /// /// The fitting mode of the visual. /// The default is defined by the type of visual (if it is suitable to be stretched or not). /// /// 5 public VisualFittingModeType VisualFittingMode { get { if (_visualFittingMode == null) { if (this is AnimatedImageVisual || this is MeshVisual || this is PrimitiveVisual || this is TextVisual) { return VisualFittingModeType.FitKeepAspectRatio; } else { return VisualFittingModeType.Fill; } } else { return (VisualFittingModeType)_visualFittingMode; } } set { _visualFittingMode = value; UpdateVisual(); } } /// /// The corner radius of the visual. /// [EditorBrowsable(EditorBrowsableState.Never)] public float CornerRadius { get { return _cornerRadius ?? (0.0f); } set { _cornerRadius = value; UpdateVisual(); } } internal string Name { set; get; } internal int VisualIndex { set; get; } internal VisualView Parent { set; get; } /// /// Suppress UpdateVisual() to update properties to Parent. /// If it is set to true, UpdateVisual() is ignored unless it is called with force. /// internal bool SuppressUpdateVisual { get; set; } = false; internal void UpdateVisual(bool force = false) { if (VisualIndex > 0 && (!SuppressUpdateVisual || force)) { NUILog.Debug("UpdateVisual()! VisualIndex=" + VisualIndex); Parent.UpdateVisual(VisualIndex, Name, this); } else { NUILog.Debug("VisualIndex was not set"); } } /// /// Compose the out visual map. /// /// 3 protected virtual void ComposingPropertyMap() { if (null == _outputVisualMap) { _outputVisualMap = new PropertyMap(); } if (_shader != null) { _outputVisualMap.Add(Visual.Property.Shader, new PropertyValue(_shader)); } if (_premultipliedAlpha != null) { _outputVisualMap.Add(Visual.Property.PremultipliedAlpha, new PropertyValue((bool)_premultipliedAlpha)); } if (_mixColor != null) { _outputVisualMap.Add(Visual.Property.MixColor, new PropertyValue(_mixColor)); } if (_opacity != null) { _outputVisualMap.Add(Visual.Property.Opacity, new PropertyValue((float)_opacity)); } if (_visualFittingMode != null) { _outputVisualMap.Add(Visual.Property.VisualFittingMode, new PropertyValue((int)_visualFittingMode)); } if (_cornerRadius != null) { _outputVisualMap.Add(Visual.Property.CornerRadius, new PropertyValue((int)_cornerRadius)); } } private void ComposingTransformMap() { _visualTransformMap = new PropertyMap(); if (_visualSize != null) { _visualTransformMap.Add((int)VisualTransformPropertyType.Size, new PropertyValue(_visualSize)); } if (_visualOffset != null) { _visualTransformMap.Add((int)VisualTransformPropertyType.Offset, new PropertyValue(_visualOffset)); } if (_visualOffsetPolicy != null) { _visualTransformMap.Add((int)VisualTransformPropertyType.OffsetPolicy, new PropertyValue(_visualOffsetPolicy)); } if (_visualSizePolicy != null) { _visualTransformMap.Add((int)VisualTransformPropertyType.SizePolicy, new PropertyValue(_visualSizePolicy)); } if (_visualOrigin != null) { _visualTransformMap.Add((int)VisualTransformPropertyType.Origin, new PropertyValue((int)_visualOrigin)); } if (_visualAnchorPoint != null) { _visualTransformMap.Add((int)VisualTransformPropertyType.AnchorPoint, new PropertyValue((int)_visualAnchorPoint)); } } } }