Exemple #1
0
        /// <summary>
        /// Converts object to bindable collection.
        /// </summary>
        public static BindableCollection ToBindableCollection(this object obj, LayoutRoot layoutRoot)
        {
            if (obj == null)
            {
                return(null);
            }

            Type objType = obj.GetType();

            // converts object to an bindable collection
            if (BindableCollectionType.IsAssignableFrom(objType))
            {
                return(obj as BindableCollection);
            }
            else if (ObservedCollection.CanBeObserved(obj))
            {
                var observedCollection = new ObservedCollection(obj, layoutRoot);
                return(observedCollection);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Called before the view is loaded.
        /// </summary>
        protected override void BeforeLoad()
        {
            // if adjusting size to parent EnableScriptEvents needs to be true before scene object is loaded
            AdjustToParentChanged();

            // load scene object
            base.BeforeLoad();

            // initialize root canvas
            if (LayoutRoot == null)
            {
                var parentUIView = this.FindParent <UIView>(x => !x.IgnoreObject);
                if (parentUIView == null)
                {
                    // we are the root view
                    if (typeof(LayoutRoot).IsAssignableFrom(GetType()))
                    {
                        // we are the layout root
                        LayoutRoot = this as LayoutRoot;
                    }
                    else
                    {
                        // create new layout root
                        var layoutRoot = new LayoutRoot();
                        layoutRoot.Load();
                        LayoutRoot   = layoutRoot;
                        LayoutParent = LayoutRoot;

                        if (!IgnoreObject)
                        {
                            GameObject.transform.SetParent(layoutRoot.GameObject.transform, false);
                        }
                    }
                }
                else
                {
                    // use the same layout root as parent UIView
                    LayoutRoot = parentUIView.LayoutRoot;
                }
            }

            if (IgnoreObject)
            {
                return;
            }

            // add rect transform
            var rectTransform = GameObject.GetComponent <RectTransform>();

            if (rectTransform == null)
            {
                rectTransform = GameObject.AddComponent <RectTransform>();
            }
            RectTransform = rectTransform;

            // if hidden while loading set canvas alpha to zero
            if (LoadMode.HasFlag(LoadMode.HiddenWhileLoading))
            {
                // to change alpha we need a canvas group attached
                if (CanvasGroup == null)
                {
                    var canvasGroup = GameObject.GetComponent <CanvasGroup>();
                    CanvasGroup = canvasGroup == null?GameObject.AddComponent <CanvasGroup>() : canvasGroup;
                }

                CanvasGroup.alpha = 0;
            }
        }
Exemple #3
0
 /// <summary>
 /// Starts the animation.
 /// </summary>
 public Task Animate(LayoutRoot layoutRoot, float initialDelay)
 {
     return(Task.WhenAll(this.Select(x => x.Animate(layoutRoot, initialDelay))));
 }
Exemple #4
0
        /// <summary>
        /// Sets the state of the view.
        /// </summary>
        public virtual void SetState(string newState)
        {
            if (newState.IEquals(_previousState))
            {
                return;
            }

            // handle state animations
            List <DependencyProperty> animatedProperties = null;

            if (_stateAnimations != null)
            {
                // complete any previous state animators if active
                foreach (var stateAnimator in _stateAnimations)
                {
                    stateAnimator.StopAnimation();
                    stateAnimator.IsCompleted = true;
                }

                // start any state animators applicable to current state transition
                foreach (var stateAnimator in _stateAnimations)
                {
                    if ((stateAnimator.FromAny || stateAnimator.FromState == _previousState) &&
                        (stateAnimator.ToAny || stateAnimator.ToState == newState))
                    {
                        foreach (var animator in stateAnimator)
                        {
                            animator.StartAnimation();
                            LayoutRoot.RegisterAnimator(animator);

                            if (animatedProperties == null)
                            {
                                animatedProperties = new List <DependencyProperty>();
                            }
                            animatedProperties.Add(animator.Property);
                        }
                    }
                }
            }

            // load/unload state changing properties that aren't animated
            var stateChangingProperties = GetStateChangingProperties(newState);

            if (stateChangingProperties != null)
            {
                // unload all state changing dependency properties
                for (int i = 0; i < stateChangingProperties.Count; ++i)
                {
                    var stateChangingProperty = stateChangingProperties[i];
                    if (animatedProperties != null && animatedProperties.Contains(stateChangingProperty))
                    {
                        continue;
                    }

                    stateChangingProperties[i].Unload(this);
                }

                // set state
                _state         = newState;
                _previousState = newState;

                // load all state changing dependency properties
                for (int i = 0; i < stateChangingProperties.Count; ++i)
                {
                    var stateChangingProperty = stateChangingProperties[i];
                    if (animatedProperties != null && animatedProperties.Contains(stateChangingProperty))
                    {
                        continue;
                    }

                    stateChangingProperties[i].Load(this);
                }
            }
            else
            {
                // set state
                _state         = newState;
                _previousState = newState;
            }
        }
Exemple #5
0
        /// <summary>
        /// Updates the binding and propagates it.
        /// </summary>
        public void UpdateBinding(bool propagateValues = true, bool propagateTargetToSource = false, LayoutRoot layoutRoot = null)
        {
            if (layoutRoot != null)
            {
                LayoutRoot = layoutRoot;
            }

            if (IsSimple)
            {
                PropagateSourceToTarget();
                return;
            }

            // detach any existing property changed listeners
            foreach (var source in Sources)
            {
                foreach (var sourceObject in source.Objects)
                {
                    RemoveChangeListener(source, sourceObject);
                }
            }

            if (IsTwoWay)
            {
                foreach (var targetObject in Target.Objects)
                {
                    RemoveChangeListener(Target, targetObject);
                }
            }

            // get source and target objects and attach property changed listeners
            SourcesReachable = true;
            foreach (var source in Sources)
            {
                source.Objects.Clear();
                source.IsReachable = true;
                foreach (var sourceObjectGetter in source.ObjectGetters)
                {
                    var sourceObject = sourceObjectGetter();
                    if (sourceObject == null)
                    {
                        source.IsReachable = false;
                        SourcesReachable   = false;
                        break;
                    }

                    source.Objects.Add(sourceObject);
                    AddChangeListener(source, sourceObject);
                }
            }

            Target.Objects.Clear();
            Target.IsReachable = true;
            foreach (var targetObjectGetter in Target.ObjectGetters)
            {
                var targetObject = targetObjectGetter();
                if (targetObject == null)
                {
                    Target.IsReachable = false;
                    break;
                }

                Target.Objects.Add(targetObject);

                if (IsTwoWay)
                {
                    AddChangeListener(Target, targetObject);
                }
            }

            // propagate value if sources and target can be reached
            if (!Target.IsReachable || !SourcesReachable)
            {
                return;
            }

            //LogPropagation(propagateTargetToSource);
            if (propagateValues)
            {
                if (!propagateTargetToSource)
                {
                    PropagateSourceToTarget();
                }
                else
                {
                    if (IsTwoWay)
                    {
                        PropagateTargetToSource();
                    }
                }
            }
        }