/*
* Copyright(c) 2020 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;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;
using Tizen.NUI.Binding;
namespace Tizen.NUI.BaseComponents
{
///
/// View is the base class for all views.
///
/// 3
public partial class View
{
///
/// Perform an action on a visual registered to this view.
/// Visuals will have actions. This API is used to perform one of these actions with the given attributes.
///
/// The Property index of the visual.
/// The action to perform. See Visual to find the supported actions.
/// Optional attributes for the action.
/// 5
public void DoAction(int propertyIndexOfVisual, int propertyIndexOfActionId, PropertyValue attributes)
{
Interop.View.DoAction(SwigCPtr, propertyIndexOfVisual, propertyIndexOfActionId, PropertyValue.getCPtr(attributes));
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Creates an animation to animate the background color visual. If there is no
/// background visual, creates one with transparent black as it's mixColor.
///
/// 3
public Animation AnimateBackgroundColor(object destinationValue,
int startTime,
int endTime,
AlphaFunction.BuiltinFunctions? alphaFunction = null,
object initialValue = null)
{
Tizen.NUI.PropertyMap background = Background;
if (background.Empty())
{
// If there is no background yet, ensure there is a transparent
// color visual
BackgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
background = Background;
}
return AnimateColor("background", destinationValue, startTime, endTime, alphaFunction, initialValue);
}
///
/// Creates an animation to animate the mixColor of the named visual.
///
/// 3
public Animation AnimateColor(string targetVisual, object destinationColor, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialColor = null)
{
Animation animation = null;
{
PropertyMap _animator = new PropertyMap();
if (alphaFunction != null)
{
_animator.Add("alphaFunction", new PropertyValue(AlphaFunction.BuiltinToPropertyKey(alphaFunction)));
}
PropertyMap _timePeriod = new PropertyMap();
_timePeriod.Add("duration", new PropertyValue((endTime - startTime) / 1000.0f));
_timePeriod.Add("delay", new PropertyValue(startTime / 1000.0f));
_animator.Add("timePeriod", new PropertyValue(_timePeriod));
PropertyMap _transition = new PropertyMap();
_transition.Add("animator", new PropertyValue(_animator));
_transition.Add("target", new PropertyValue(targetVisual));
_transition.Add("property", new PropertyValue("mixColor"));
if (initialColor != null)
{
PropertyValue initValue = PropertyValue.CreateFromObject(initialColor);
_transition.Add("initialValue", initValue);
}
PropertyValue destValue = PropertyValue.CreateFromObject(destinationColor);
_transition.Add("targetValue", destValue);
TransitionData _transitionData = new TransitionData(_transition);
animation = new Animation(Interop.View.CreateTransition(SwigCPtr, TransitionData.getCPtr(_transitionData)), true);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
return animation;
}
// From Container Base class
///
/// Adds a child view to this view.
///
///
/// 4
public override void Add(View child)
{
bool hasLayout = (layout != null);
if (null == child)
{
Tizen.Log.Fatal("NUI", "Child is null");
return;
}
Container oldParent = child.GetParent();
if (oldParent != this)
{
// If child already has a parent then re-parent child
if (oldParent != null)
{
if (child.Layout != null)
{
child.Layout.SetReplaceFlag();
}
oldParent.Remove(child);
}
child.InternalParent = this;
Interop.Actor.Add(SwigCPtr, View.getCPtr(child));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
Children.Add(child);
if (ChildAdded != null)
{
ChildAddedEventArgs e = new ChildAddedEventArgs
{
Added = child
};
ChildAdded(this, e);
}
BindableObject.SetInheritedBindingContext(child, this?.BindingContext);
}
}
///
/// Removes a child view from this View. If the view was not a child of this view, this is a no-op.
///
///
/// 4
/// Thrown when deleting a view that is not a child of this view
public override void Remove(View child)
{
if (child == null || child.GetParent() == null) // Early out if child null.
return;
if (child.Parent != this)
{
throw new System.InvalidOperationException("You have deleted a view that is not a child of this view.");
}
bool hasLayout = (layout != null);
// If View has a layout then do a deferred child removal
// Actual child removal is performed by the layouting system so
// transitions can be completed.
if (hasLayout)
{
(layout as LayoutGroup)?.RemoveChildFromLayoutGroup(child);
}
RemoveChild(child);
}
///
/// Retrieves a child view by index.
///
///
/// 4
public override View GetChildAt(uint index)
{
if (index < Children.Count)
{
return Children[Convert.ToInt32(index)];
}
else
{
return null;
}
}
///
/// Retrieves the number of children held by the view.
///
///
/// 4
public override uint GetChildCount()
{
return Convert.ToUInt32(Children.Count);
}
///
/// Gets the views parent.
///
///
/// 4
public override Container GetParent()
{
return this.InternalParent as Container;
}
///
/// Queries whether the view has a focus.
///
/// True if this view has a focus.
/// 3
public bool HasFocus()
{
bool ret = false;
if (SwigCPtr.Handle != global::System.IntPtr.Zero)
{
ret = Interop.View.HasKeyInputFocus(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
else
{
Tizen.Log.Error("NUI", "swigCPtr of view is aleady disposed.");
}
return ret;
}
///
/// Sets the name of the style to be applied to the view.
///
/// A string matching a style described in a stylesheet.
/// 3
public void SetStyleName(string styleName)
{
Interop.View.SetStyleName(SwigCPtr, styleName);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Retrieves the name of the style to be applied to the view (if any).
///
/// A string matching a style, or an empty string.
/// 3
public string GetStyleName()
{
string ret = Interop.View.GetStyleName(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Clears the background.
///
/// 3
public void ClearBackground()
{
Interop.View.ClearBackground(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Shows the view.
///
///
/// This is an asynchronous method.
///
/// 3
public void Show()
{
SetVisible(true);
}
///
/// Hides the view.
///
///
/// This is an asynchronous method.
/// If the view is hidden, then the view and its children will not be rendered.
/// This is regardless of the individual visibility of the children, i.e., the view will only be rendered if all of its parents are shown.
///
/// 3
public void Hide()
{
SetVisible(false);
}
///
/// Raises the view above all other views.
///
///
/// Sibling order of views within the parent will be updated automatically.
/// Once a raise or lower API is used, that view will then have an exclusive sibling order independent of insertion.
///
/// 3
public void RaiseToTop()
{
var parentChildren = GetParent()?.Children;
if (parentChildren != null)
{
parentChildren.Remove(this);
parentChildren.Add(this);
LayoutGroup layout = Layout as LayoutGroup;
layout?.ChangeLayoutSiblingOrder(parentChildren.Count - 1);
Interop.NDalic.RaiseToTop(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Lowers the view to the bottom of all views.
///
///
/// The sibling order of views within the parent will be updated automatically.
/// Once a raise or lower API is used that view will then have an exclusive sibling order independent of insertion.
///
/// 3
public void LowerToBottom()
{
var parentChildren = GetParent()?.Children;
if (parentChildren != null)
{
parentChildren.Remove(this);
parentChildren.Insert(0, this);
LayoutGroup layout = Layout as LayoutGroup;
layout?.ChangeLayoutSiblingOrder(0);
Interop.NDalic.LowerToBottom(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
}
///
/// Queries if all resources required by a view are loaded and ready.
///
/// Most resources are only loaded when the control is placed on the stage.
///
/// 3
public bool IsResourceReady()
{
bool ret = Interop.View.IsResourceReady(SwigCPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Gets the parent layer of this view.If a view has no parent, this method does not do anything.
///
///
The view has been initialized.
/// The parent layer of view
/// 5
public Layer GetLayer()
{
//to fix memory leak issue, match the handle count with native side.
IntPtr cPtr = Interop.Actor.GetLayer(SwigCPtr);
Layer ret = this.GetInstanceSafely(cPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Removes a view from its parent view or layer. If a view has no parent, this method does nothing.
///
/// The (child) view has been initialized.
/// 4
public void Unparent()
{
GetParent()?.Remove(this);
}
///
/// Search through this view's hierarchy for a view with the given name.
/// The view itself is also considered in the search.
///
/// The view has been initialized.
/// The name of the view to find.
/// A handle to the view if found, or an empty handle if not.
/// 3
public View FindChildByName(string viewName)
{
//to fix memory leak issue, match the handle count with native side.
IntPtr cPtr = Interop.Actor.FindChildByName(SwigCPtr, viewName);
View ret = this.GetInstanceSafely(cPtr);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Converts screen coordinates into the view's coordinate system using the default camera.
///
/// The view has been initialized.
/// The view coordinates are relative to the top-left(0.0, 0.0, 0.5).
/// On return, the X-coordinate relative to the view.
/// On return, the Y-coordinate relative to the view.
/// The screen X-coordinate.
/// The screen Y-coordinate.
/// True if the conversion succeeded.
/// 3
public bool ScreenToLocal(out float localX, out float localY, float screenX, float screenY)
{
bool ret = Interop.Actor.ScreenToLocal(SwigCPtr, out localX, out localY, screenX, screenY);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Sets the relative to parent size factor of the view.
/// This factor is only used when ResizePolicy is set to either:
/// ResizePolicy::SIZE_RELATIVE_TO_PARENT or ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT.
/// This view's size is set to the view's size multiplied by or added to this factor, depending on ResizePolicy.
///
/// The view has been initialized.
/// A Vector3 representing the relative factor to be applied to each axis.
/// 3
public void SetSizeModeFactor(Vector3 factor)
{
Interop.Actor.SetSizeModeFactor(SwigCPtr, Vector3.getCPtr(factor));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Calculates the height of the view given a width.
/// The natural size is used for default calculation.
/// Size 0 is treated as aspect ratio 1:1.
///
/// The width to use.
/// The height based on the width.
/// 3
public float GetHeightForWidth(float width)
{
float ret = Interop.Actor.GetHeightForWidth(SwigCPtr, width);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Calculates the width of the view given a height.
/// The natural size is used for default calculation.
/// Size 0 is treated as aspect ratio 1:1.
///
/// The height to use.
/// The width based on the height.
/// 3
public float GetWidthForHeight(float height)
{
float ret = Interop.Actor.GetWidthForHeight(SwigCPtr, height);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Return the amount of size allocated for relayout.
///
/// The dimension to retrieve.
/// Return the size.
/// 3
public float GetRelayoutSize(DimensionType dimension)
{
float ret = Interop.Actor.GetRelayoutSize(SwigCPtr, (int)dimension);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
///
/// Set the padding for the view.
///
/// Padding for the view.
/// 3
public void SetPadding(PaddingType padding)
{
Interop.Actor.SetPadding(SwigCPtr, PaddingType.getCPtr(padding));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
///
/// Return the value of padding for the view.
///
/// the value of padding for the view
/// 3
public void GetPadding(PaddingType paddingOut)
{
Interop.Actor.GetPadding(SwigCPtr, PaddingType.getCPtr(paddingOut));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// 3
public uint AddRenderer(Renderer renderer)
{
uint ret = Interop.Actor.AddRenderer(SwigCPtr, Renderer.getCPtr(renderer));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
/// 3
public Renderer GetRendererAt(uint index)
{
//to fix memory leak issue, match the handle count with native side.
IntPtr cPtr = Interop.Actor.GetRendererAt(SwigCPtr, index);
HandleRef CPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
Renderer ret = Registry.GetManagedBaseHandleFromNativePtr(CPtr.Handle) as Renderer;
if (cPtr != null && ret == null)
{
ret = new Renderer(cPtr, false);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
Interop.BaseHandle.DeleteBaseHandle(CPtr);
CPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}
/// 3
public void RemoveRenderer(Renderer renderer)
{
Interop.Actor.RemoveRenderer(SwigCPtr, Renderer.getCPtr(renderer));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// 3
public void RemoveRenderer(uint index)
{
Interop.Actor.RemoveRenderer(SwigCPtr, index);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public void RotateBy(Degree angle, Vector3 axis)
{
Interop.ActorInternal.RotateByDegree(SwigCPtr, Degree.getCPtr(angle), Vector3.getCPtr(axis));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public void RotateBy(Radian angle, Vector3 axis)
{
Interop.ActorInternal.RotateByRadian(SwigCPtr, Radian.getCPtr(angle), Vector3.getCPtr(axis));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public void RotateBy(Rotation relativeRotation)
{
Interop.ActorInternal.RotateByQuaternion(SwigCPtr, Rotation.getCPtr(relativeRotation));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public void ScaleBy(Vector3 relativeScale)
{
Interop.ActorInternal.ScaleBy(SwigCPtr, Vector3.getCPtr(relativeScale));
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// This will be public opened in next tizen after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetColorMode(ColorMode colorMode)
{
Interop.ActorInternal.SetColorMode(SwigCPtr, (int)colorMode);
if (NDalicPINVOKE.SWIGPendingException.Pending)
throw NDalicPINVOKE.SWIGPendingException.Retrieve();
}
/// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public Transition GetTransition(string transitionName)
{
Transition trans = null;
transDictionary.TryGetValue(transitionName, out trans);
return trans;
}
/// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
[EditorBrowsable(EditorBrowsableState.Never)]
public void ObjectDump()
{
if (0 == Children.Count)
{
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
if (null != property && property.CanRead)
{
Tizen.Log.Fatal("NUI", $"{type.Name} {property.Name} ({property.PropertyType.Name}): {property.GetValueString(this, property.PropertyType)}");
}
}
return;
}
foreach (View view in Children)
{
view.ObjectDump();
}
}
}
}