예제 #1
0
        /// <summary>
        /// Updates the layout of the scrollbar.
        /// </summary>
        public override bool UpdateLayout(bool notifyParent = true)
        {
            bool defaultDisableLayoutUpdate = DisableLayoutUpdate;

            DisableLayoutUpdate = true;

            // set properties based on orientation of scrollbar
            if (AlignmentProperty.IsUndefined(this))
            {
                Alignment = Orientation == ElementOrientation.Horizontal ? ElementAlignment.Bottom : ElementAlignment.Right;
            }

            if (WidthProperty.IsUndefined(this))
            {
                OverrideWidth = Orientation == ElementOrientation.Horizontal ? Length : Breadth;
            }

            if (HeightProperty.IsUndefined(this))
            {
                OverrideHeight = Orientation == ElementOrientation.Horizontal ? Breadth : Length;
            }

            if (Orientation == ElementOrientation.Horizontal)
            {
                Handle.Alignment = ElementAlignment.Left;
            }
            else
            {
                Handle.Alignment = ElementAlignment.Top;
            }

            DisableLayoutUpdate = defaultDisableLayoutUpdate;
            return(base.UpdateLayout(notifyParent));
        }
예제 #2
0
파일: Image.cs 프로젝트: bradparks/Delight
        /// <summary>
        /// Called whenever properties affecting the image are changed.
        /// </summary>
        public virtual void ImageChanged()
        {
            if (ImageComponent == null)
            {
                return;
            }

            if (ColorProperty.IsUndefined(this))
            {
                if (ImageComponent.sprite != null || ImageComponent.overrideSprite != null)
                {
                    // use white color by default if image is set
                    ImageComponent.color = Color.white;
                }
                else
                {
                    // use clear color by default if image isn't set
                    ImageComponent.color = Color.clear;
                }
            }

            var sprite = ImageComponent.overrideSprite ?? ImageComponent.sprite;

            if (WidthProperty.IsUndefined(this) && HeightProperty.IsUndefined(this))
            {
                // if width and height is undefined, adjust size to native size of sprite
                if (sprite != null)
                {
                    ImageComponent.SetNativeSize();
                    OverrideWidth  = ElementSize.FromPixels(ImageComponent.rectTransform.sizeDelta.x);
                    OverrideHeight = ElementSize.FromPixels(ImageComponent.rectTransform.sizeDelta.y);
                    if (OverrideHeight.Pixels != 0)
                    {
                        _nativeAspectRatio = OverrideWidth.Pixels / OverrideHeight.Pixels;
                    }
                }
            }
            else
            {
                _nativeAspectRatio = -1;
            }

            bool isLoading = Sprite != null && !Sprite.IsLoaded;

            if (isLoading && sprite == null)
            {
                // always disable image while loading
                ImageComponent.enabled = false;
            }
            else
            {
                // disable raycast blocks if image is transparent
                ImageComponent.enabled = RaycastBlockMode == RaycastBlockMode.Always ? true : ImageComponent.color.a > 0;
            }
        }
예제 #3
0
        /// <summary>
        /// Called just before the view and its children are loaded.
        /// </summary>
        protected override void BeforeLoad()
        {
            if (IgnoreObject)
            {
                return;
            }
            base.BeforeLoad();

            bool autoSizeUndefined = AutoSizeProperty.IsUndefined(this);

            if (autoSizeUndefined)
            {
                // if autosize is undefined, set default value depending if width and height is set
                bool widthUndefined  = WidthProperty.IsUndefined(this);
                bool heightUndefined = HeightProperty.IsUndefined(this);

                if (widthUndefined && !heightUndefined)
                {
                    AutoSize = AutoSize.Width;
                }
                else if (!widthUndefined && heightUndefined)
                {
                    AutoSize = AutoSize.Height;
                }
                else if (widthUndefined && heightUndefined)
                {
                    AutoSize = AutoSize.WidthAndHeight;
                }
                else
                {
                    AutoSize = AutoSize.None;
                }
            }

            Label.AutoSize = AutoSize;
            if (AutoSize == AutoSize.None && WidthProperty.IsUndefined(this))
            {
                // if size isn't specified and the button doesn't adjust to label size, then set default width
                WidthProperty.SetValue(this, DefaultWidth, false);
            }

            if (LayoutParent is ToggleGroup)
            {
                // default to toggle-button if in toggle-group
                IsToggleButton = true;
                CanToggleOff   = false;
            }
        }
예제 #4
0
        /// <summary>
        /// Called whenever properties affecting the image are changed.
        /// </summary>
        public virtual void ImageChanged()
        {
            if (RawImageComponent == null)
            {
                return;
            }

            if (ColorProperty.IsUndefined(this))
            {
                if (RawImageComponent.texture != null)
                {
                    // use white color by default if image is set
                    RawImageComponent.color = Color.white;
                }
                else
                {
                    // use clear color by default if image isn't set
                    RawImageComponent.color = Color.clear;
                }
            }

            var texture = RawImageComponent.texture;

            if (WidthProperty.IsUndefined(this) && HeightProperty.IsUndefined(this))
            {
                // if width and height is undefined, adjust size to native size of sprite
                if (texture != null)
                {
                    RawImageComponent.SetNativeSize();
                    OverrideWidth  = ElementSize.FromPixels(RawImageComponent.rectTransform.sizeDelta.x);
                    OverrideHeight = ElementSize.FromPixels(RawImageComponent.rectTransform.sizeDelta.y);
                }
            }

            bool isLoading = Texture != null && !Texture.IsLoaded;

            if (isLoading && texture == null)
            {
                // always disable image while loading
                RawImageComponent.enabled = false;
            }
            else
            {
                // disable raycast blocks if image is transparent
                RawImageComponent.enabled = RaycastBlockMode == RaycastBlockMode.Always ? true : RawImageComponent.color.a > 0;
            }
        }
예제 #5
0
        /// <summary>
        /// Updates the layout of the group.
        /// </summary>
        public override bool UpdateLayout(bool notifyParent = true)
        {
            bool defaultDisableLayoutUpdate = DisableLayoutUpdate;

            DisableLayoutUpdate = true;

            bool          hasNewSize       = false;
            float         maxWidth         = 0f;
            float         maxHeight        = 0f;
            float         totalWidth       = 0f;
            float         totalHeight      = 0f;
            bool          percentageWidth  = false;
            bool          percentageHeight = false;
            bool          isHorizontal     = Orientation == ElementOrientation.Horizontal;
            List <UIView> children         = new List <UIView>();

            this.ForEach <UIView>(x =>
            {
                children.Add(x);
            }, false);

            // get size of content and set content offsets and alignment
            var spacing    = Spacing ?? ElementSize.Default;
            int childCount = children.Count;
            int childIndex = 0;

            for (int i = 0; i < childCount; ++i)
            {
                var childView = children[i];
                if (!childView.IsActive)
                {
                    // don't group inactive views
                    continue;
                }

                var childWidth  = childView.OverrideWidth ?? (childView.Width ?? ElementSize.Default);
                var childHeight = childView.OverrideHeight ?? (childView.Height ?? ElementSize.Default);

                if (childWidth.Unit == ElementSizeUnit.Percents)
                {
                    if (isHorizontal)
                    {
                        Debug.LogWarning(String.Format("#Delight# Unable to group view \"{0}\" horizontally as it doesn't specify its width in pixels.", childView.Name));
                        continue;
                    }
                    else
                    {
                        percentageWidth = true;
                    }
                }

                if (childHeight.Unit == ElementSizeUnit.Percents)
                {
                    if (!isHorizontal)
                    {
                        Debug.LogWarning(String.Format("#Delight# Unable to group view \"{0}\" vertically as it doesn't specify its height in pixels or elements.", childView.Name));
                        continue;
                    }
                    else
                    {
                        percentageHeight = true;
                    }
                }

                bool defaultDisableChildLayoutUpdate = childView.DisableLayoutUpdate;
                childView.DisableLayoutUpdate = true;

                // set offsets and alignment
                var offset = new ElementMargin(
                    new ElementSize(isHorizontal ? totalWidth + spacing.Pixels * childIndex : 0f, ElementSizeUnit.Pixels),
                    new ElementSize(!isHorizontal ? totalHeight + spacing.Pixels * childIndex : 0f, ElementSizeUnit.Pixels));

                // set desired alignment if it is valid for the orientation otherwise use defaults
                var alignment        = ElementAlignment.Center;
                var defaultAlignment = isHorizontal ? ElementAlignment.Left : ElementAlignment.Top;
                var desiredAlignment = !ContentAlignmentProperty.IsUndefined(this) ? ContentAlignment : childView.Alignment;
                if (isHorizontal && (desiredAlignment == ElementAlignment.Top || desiredAlignment == ElementAlignment.Bottom ||
                                     desiredAlignment == ElementAlignment.TopLeft || desiredAlignment == ElementAlignment.BottomLeft))
                {
                    alignment = defaultAlignment | desiredAlignment;
                }
                else if (!isHorizontal && (desiredAlignment == ElementAlignment.Left || desiredAlignment == ElementAlignment.Right ||
                                           desiredAlignment == ElementAlignment.TopLeft || desiredAlignment == ElementAlignment.TopRight))
                {
                    alignment = defaultAlignment | desiredAlignment;
                }
                else
                {
                    alignment = defaultAlignment;
                }

                // get size of content
                if (!percentageWidth)
                {
                    totalWidth += childWidth;
                    maxWidth    = childWidth.Pixels > maxWidth ? childWidth.Pixels : maxWidth;
                }

                if (!percentageHeight)
                {
                    totalHeight += childHeight;
                    maxHeight    = childHeight.Pixels > maxHeight ? childHeight.Pixels : maxHeight;
                }

                // update child layout
                if (!offset.Equals(childView.OffsetFromParent) || alignment != childView.Alignment)
                {
                    childView.OffsetFromParent = offset;
                    childView.Alignment        = alignment;

                    childView.UpdateLayout(false);
                }
                ++childIndex;
                childView.DisableLayoutUpdate = defaultDisableChildLayoutUpdate;
            }

            // set width and height
            float totalSpacing = childCount > 1 ? (childIndex - 1) * spacing.Pixels : 0f;

            // adjust width to content
            if (WidthProperty.IsUndefined(this))
            {
                if (!percentageWidth)
                {
                    // add margins
                    var margin = Margin ?? ElementMargin.Default;
                    totalWidth += isHorizontal ? totalSpacing : 0f;
                    totalWidth += margin.Left.Pixels + margin.Right.Pixels;
                    maxWidth   += margin.Left.Pixels + margin.Right.Pixels;

                    // adjust width to content
                    var newWidth = new ElementSize(isHorizontal ? totalWidth : maxWidth, ElementSizeUnit.Pixels);
                    if (!newWidth.Equals(Width))
                    {
                        OverrideWidth = newWidth;
                        hasNewSize    = true;
                    }
                }
                else
                {
                    var newWidth = new ElementSize(1, ElementSizeUnit.Percents);
                    if (!newWidth.Equals(Width))
                    {
                        OverrideWidth = newWidth;
                        hasNewSize    = true;
                    }
                }
            }
            else if (OverrideWidth != null && !OverrideWidth.Equals(Width))
            {
                // clear override
                OverrideWidth = null;
                hasNewSize    = true;
            }

            // adjust height to content
            if (HeightProperty.IsUndefined(this))
            {
                if (!percentageHeight)
                {
                    // add margins
                    var margin = Margin ?? ElementMargin.Default;
                    totalHeight += !isHorizontal ? totalSpacing : 0f;
                    totalHeight += margin.Top.Pixels + margin.Bottom.Pixels;
                    maxHeight   += margin.Top.Pixels + margin.Bottom.Pixels;

                    // adjust height to content
                    var newHeight = new ElementSize(!isHorizontal ? totalHeight : maxHeight, ElementSizeUnit.Pixels);
                    if (!newHeight.Equals(Height))
                    {
                        OverrideHeight = newHeight;
                        hasNewSize     = true;
                    }
                }
                else
                {
                    var newHeight = new ElementSize(1, ElementSizeUnit.Percents);
                    if (!newHeight.Equals(Height))
                    {
                        OverrideHeight = newHeight;
                        hasNewSize     = true;
                    }
                }
            }
            else if (OverrideHeight != null && !OverrideHeight.Equals(Height))
            {
                // clear override
                OverrideHeight = null;
                hasNewSize     = true;
            }

            DisableLayoutUpdate = defaultDisableLayoutUpdate;

            return(base.UpdateLayout(notifyParent) || hasNewSize);
        }