/// <summary> /// Initializes a new instance of the class. /// </summary> public ElementMargin(ElementSize margin) { Left = new ElementSize(margin); Top = new ElementSize(margin); Right = new ElementSize(margin); Bottom = new ElementSize(margin); }
/// <summary> /// Initializes a new instance of the class. /// </summary> public ElementMargin() { Left = new ElementSize(); Top = new ElementSize(); Right = new ElementSize(); Bottom = new ElementSize(); }
/// <summary> /// Called when label text changes. /// </summary> public virtual void TextChanged() { try { if (AutoSize != AutoSize.None && EnableWordWrappingProperty.IsUndefined(this)) { // when autosizing disable wordwrapping so PreferredWidth is calculated correctly EnableWordWrapping = false; } // adjust label size to text if (AutoSize == AutoSize.Width) { Width = new ElementSize(PreferredWidth); } else if (AutoSize == AutoSize.Height) { Height = new ElementSize(PreferredHeight); } else if (AutoSize == AutoSize.WidthAndHeight || AutoSize == AutoSize.Default) { Width = new ElementSize(PreferredWidth); Height = new ElementSize(PreferredHeight); } } catch { // bugfix of older version of TextMeshPro bug where PreferredWidth / PreferredHeight throws exceptions } }
/// <summary> /// Converts value from string. /// </summary> public override ElementMargin Convert(string stringValue) { string[] valueList; valueList = stringValue.Split(',').ToArray(); if (valueList.Length == 1) { return(new ElementMargin(ElementSize.Parse(valueList[0]))); } else if (valueList.Length == 2) { return(new ElementMargin( ElementSize.Parse(valueList[0]), ElementSize.Parse(valueList[1]))); } else if (valueList.Length == 3) { return(new ElementMargin( ElementSize.Parse(valueList[0]), ElementSize.Parse(valueList[1]), ElementSize.Parse(valueList[2]))); } else if (valueList.Length == 4) { return(new ElementMargin( ElementSize.Parse(valueList[0]), ElementSize.Parse(valueList[1]), ElementSize.Parse(valueList[2]), ElementSize.Parse(valueList[3]))); } throw new Exception(String.Format("Improperly formatted string.")); }
/// <summary> /// Initializes a new instance of the class. /// </summary> public ElementMargin(ElementSize left, ElementSize top) { Left = new ElementSize(left); Top = new ElementSize(top); Right = new ElementSize(); Bottom = new ElementSize(); }
/// <summary> /// Called when label text changes. /// </summary> public virtual void TextChanged() { try { // adjust button size to text if (AutoSize == AutoSize.Width || AutoSize == AutoSize.Default) { Width = new ElementSize(Label.PreferredWidth + TextPadding.Left.Pixels + TextPadding.Right.Pixels); } else if (AutoSize == AutoSize.Height) { Height = new ElementSize(Label.PreferredHeight + TextPadding.Top.Pixels + TextPadding.Bottom.Pixels); } else if (AutoSize == AutoSize.WidthAndHeight) { Width = new ElementSize(Label.PreferredWidth + TextPadding.Left.Pixels + TextPadding.Right.Pixels); Height = new ElementSize(Label.PreferredHeight + TextPadding.Top.Pixels + TextPadding.Bottom.Pixels); } } catch { // bugfix of older version of TextMeshPro bug where PreferredWidth / PreferredHeight throws exceptions } // disable label if no text is shown Label.IsActive = !String.IsNullOrEmpty(Label.Text); }
/// <summary> /// Parses string into element size. /// </summary> public static ElementSize Parse(string value) { ElementSize elementSize = new ElementSize(); string trimmedValue = value.Trim(); if (trimmedValue.EndsWith("*")) { int lastIndex = trimmedValue.LastIndexOf("*", StringComparison.OrdinalIgnoreCase); var proportion = lastIndex > 0 ? System.Convert.ToSingle(trimmedValue.Substring(0, lastIndex), CultureInfo.InvariantCulture) : 1.0f; elementSize.Value = proportion; elementSize.Unit = ElementSizeUnit.Proportional; } else if (trimmedValue.EndsWith("%")) { int lastIndex = trimmedValue.LastIndexOf("%", StringComparison.OrdinalIgnoreCase); elementSize.Value = System.Convert.ToSingle(trimmedValue.Substring(0, lastIndex), CultureInfo.InvariantCulture) / 100.0f; elementSize.Unit = ElementSizeUnit.Percents; } else if (trimmedValue.EndsWith("px")) { int lastIndex = trimmedValue.LastIndexOf("px", StringComparison.OrdinalIgnoreCase); elementSize.Value = System.Convert.ToSingle(trimmedValue.Substring(0, lastIndex), CultureInfo.InvariantCulture); elementSize.Unit = ElementSizeUnit.Pixels; } else { elementSize.Value = System.Convert.ToSingle(trimmedValue, CultureInfo.InvariantCulture); elementSize.Unit = ElementSizeUnit.Pixels; } return(elementSize); }
/// <summary> /// Initializes a new instance of the class. /// </summary> public ElementMargin(ElementSize left, ElementSize top, ElementSize right, ElementSize bottom) { Left = new ElementSize(left); Top = new ElementSize(top); Right = new ElementSize(right); Bottom = new ElementSize(bottom); }
/// <summary> /// Updates layout. /// </summary> public override bool UpdateLayout(bool notifyParent = true) { bool defaultDisableLayoutUpdate = DisableLayoutUpdate; DisableLayoutUpdate = true; bool hasNewSize = false; if (AutoSizeToContent && !_sizeSet) { hasNewSize = AdjustSizeToContent(); } else { ElementSize newWidth = Width; ElementSize newHeight = Height; // adjust width and height to ParentList if (ParentList == null || ParentList.Orientation == ElementOrientation.Horizontal) { newWidth = Width != null && Width.Unit != ElementSizeUnit.Percents ? Width : new ElementSize(Length); if (Height == null) { newHeight = Breadth != null ? new ElementSize(Breadth) : ElementSize.FromPercents(1); } } else { // if neither width nor length is set, use 100% width if (Width == null) { newWidth = Length != null ? new ElementSize(Length) : ElementSize.FromPercents(1); } newHeight = Height != null && Height.Unit != ElementSizeUnit.Percents ? Height : new ElementSize(Breadth); } // adjust size to content unless it has been set if (!newWidth.Equals(Width)) { Width = newWidth; hasNewSize = true; } if (!newHeight.Equals(Height)) { Height = newHeight; hasNewSize = true; } } DisableLayoutUpdate = defaultDisableLayoutUpdate; return(base.UpdateLayout(notifyParent) || hasNewSize); }
/// <summary> /// Called whenever properties affecting the image are changed. /// </summary> public virtual void ImageChanged() { if (IgnoreObject) { return; } if (ImageComponent == null) { // add image component if background color is defined if (BackgroundColorProperty.IsUndefined(this, true)) { return; } ImageComponent = GameObject.AddComponent <UnityEngine.UI.Image>(); } if (BackgroundColorProperty.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 (Width == null && Height == null) { // 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); } } bool isLoading = BackgroundSprite != null && !BackgroundSprite.IsLoaded; if (isLoading && sprite == null) { // always disable image while loading if current sprite isn't set ImageComponent.enabled = false; } else { // disable raycast blocks if image is transparent ImageComponent.enabled = RaycastBlockMode == RaycastBlockMode.Always ? true : ImageComponent.color.a > 0; } }
/// <summary> /// Adjusts the size of the list item to its content. /// </summary> private bool AdjustSizeToContent() { bool hasNewSize = false; // the default behavior of the list-item is to adjust its height and width to its content float maxWidth = 0f; float maxHeight = 0f; int childCount = LayoutChildren.Count; // get size of content and set content offsets and alignment for (int i = 0; i < childCount; ++i) { var childView = LayoutChildren[i] as UIView; if (childView == null) { continue; } var childWidth = childView.OverrideWidth ?? (childView.Width ?? ElementSize.Default); var childHeight = childView.OverrideHeight ?? (childView.Height ?? ElementSize.Default); // get size of content if (childWidth.Unit != ElementSizeUnit.Percents) { maxWidth = childWidth.Pixels > maxWidth ? childWidth.Pixels : maxWidth; } if (childHeight.Unit != ElementSizeUnit.Percents) { maxHeight = childHeight.Pixels > maxHeight ? childHeight.Pixels : maxHeight; } } // add margins var margin = Margin ?? ElementMargin.Default; maxWidth += margin.Left.Pixels + margin.Right.Pixels; maxHeight += margin.Top.Pixels + margin.Bottom.Pixels; // adjust size to content unless it has been set var newWidth = new ElementSize(maxWidth); if (!newWidth.Equals(Width)) { Width = newWidth; hasNewSize = true; } var newHeight = new ElementSize(maxHeight); if (!newHeight.Equals(Height)) { Height = newHeight; hasNewSize = true; } return(hasNewSize); }
/// <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; } }
/// <summary> /// Called when label text changes. /// </summary> public virtual void TextChanged() { try { if (AutoSize != AutoSize.None && EnableWordWrappingProperty.IsUndefined(this)) { // when autosizing disable wordwrapping so PreferredWidth is calculated correctly EnableWordWrapping = false; } var margin = Margin ?? new ElementMargin(); // adjust label size to text if (AutoSize == AutoSize.Width) { Width = new ElementSize(PreferredWidth + margin.Left + margin.Right); } else if (AutoSize == AutoSize.Height) { Height = new ElementSize(PreferredHeight + margin.Top + margin.Bottom); } else if (AutoSize == AutoSize.WidthAndHeight || AutoSize == AutoSize.Default) { if (MaxWidth != null) { // if MaxWidth is set we want the label to expand and then wrap when reaching the max width float actualPreferredWidth = PreferredWidth + margin.Left + margin.Right; if (actualPreferredWidth > MaxWidth) { EnableWordWrapping = true; Width = new ElementSize(MaxWidth); } else { EnableWordWrapping = false; Width = new ElementSize(actualPreferredWidth); } UpdateLayout(false); Height = new ElementSize(PreferredHeight + margin.Top + margin.Bottom); } else { Width = new ElementSize(PreferredWidth + margin.Left + margin.Right); Height = new ElementSize(PreferredHeight + margin.Top + margin.Bottom); } } } catch { // bugfix of older version of TextMeshPro bug where PreferredWidth / PreferredHeight throws exceptions } }
/// <summary> /// Initializes a new instance of the class. /// </summary> public ElementSize(ElementSize elementSize) { if (elementSize == null) { _value = 0.0f; _unit = ElementSizeUnit.Pixels; } else { _value = elementSize.Value; _unit = elementSize.Unit; } }
/// <summary> /// Updates layout. /// </summary> public override bool UpdateLayout(bool notifyParent = true) { bool hasNewSize = false; var newWidth = new ElementSize(HeaderGroup.ActualWidth, ElementSizeUnit.Pixels); if (!newWidth.Equals(Width)) { WidthProperty.SetValue(this, newWidth, false); hasNewSize = true; } return(base.UpdateLayout(notifyParent) || hasNewSize); }
/// <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; } }
/// <summary> /// Called when label text changes. /// </summary> public virtual void TextChanged() { // adjust label size to text if (AutoSize == AutoSize.Width) { Width = new ElementSize(PreferredWidth); } else if (AutoSize == AutoSize.Height) { Height = new ElementSize(PreferredHeight); } else if (AutoSize == AutoSize.WidthAndHeight || AutoSize == AutoSize.Default) { Width = new ElementSize(PreferredWidth); Height = new ElementSize(PreferredHeight); } }
/// <summary> /// Called when label text changes. /// </summary> public virtual void TextChanged() { // adjust button size to text if (AutoSize == AutoSize.Width || AutoSize == AutoSize.Default) { Width = new ElementSize(Label.PreferredWidth + TextPadding.Left.Pixels + TextPadding.Right.Pixels); } else if (AutoSize == AutoSize.Height) { Height = new ElementSize(Label.PreferredHeight + TextPadding.Top.Pixels + TextPadding.Bottom.Pixels); } else if (AutoSize == AutoSize.WidthAndHeight) { Width = new ElementSize(Label.PreferredWidth + TextPadding.Left.Pixels + TextPadding.Right.Pixels); Height = new ElementSize(Label.PreferredHeight + TextPadding.Top.Pixels + TextPadding.Bottom.Pixels); } // disable label if no text is shown Label.IsActive = !String.IsNullOrEmpty(Label.Text); }
/// <summary> /// Updates based on expanded content size. /// </summary> public override bool UpdateLayout(bool notifyParent = true) { bool defaultDisableLayoutUpdate = DisableLayoutUpdate; DisableLayoutUpdate = true; // adjust width to content unless it has been explicitly set bool hasNewSize = false; if (Width == null) { float headerWidth = GetPixelWidth(_expanderHeader); float contentWidth = GetPixelWidth(_expanderContent); float totalWidth = Mathf.Max(headerWidth, contentWidth); ElementSize newWidth = totalWidth > 0 ? new ElementSize(totalWidth) : ElementSize.DefaultLayout; if (!newWidth.Equals(OverrideWidth)) { OverrideWidth = newWidth; hasNewSize = true; } } // adjust height to content unless it has been explicitly set if (Height == null) { float headerHeight = GetPixelHeight(_expanderHeader); float contentHeight = GetPixelHeight(_expanderContent); float totalHeight = headerHeight + contentHeight; ElementSize newHeight = totalHeight > 0 ? new ElementSize(totalHeight) : 40; if (!newHeight.Equals(OverrideHeight)) { OverrideHeight = newHeight; hasNewSize = true; } } DisableLayoutUpdate = defaultDisableLayoutUpdate; return(base.UpdateLayout(notifyParent) || hasNewSize); }
/// <summary> /// Sets size of view in pixels. /// </summary> public void SetSize(float width, float height) { if (Width == null) { Width = new ElementSize(); } if (Height == null) { Height = new ElementSize(); } if (Width.Pixels != width) { Width = new ElementSize(width); } if (Height.Pixels != height) { Height = new ElementSize(height); } }
/// <summary> /// Scales view content. /// </summary> public void SetScale(Vector3 scale) { if (_displayedView == null) { ContentRegionCanvas.Scale = Vector3.one; return; } ContentRegionCanvas.Scale = scale; // adjust size of view region based on size of view and viewport var viewportWidth = ScrollableContentRegion.ActualWidth; var viewportHeight = ScrollableContentRegion.ActualHeight; var width = _displayedView.OverrideWidth ?? _displayedView.Width ?? ElementSize.FromPercents(1); var height = _displayedView.OverrideHeight ?? _displayedView.Height ?? ElementSize.FromPercents(1); float adjustedWidth = viewportWidth * Mathf.Max(scale.x, 1.0f); float adjustedHeight = viewportHeight * Mathf.Max(scale.y, 1.0f); if (width.Unit != ElementSizeUnit.Percents) { float viewWidth = width.Pixels * Mathf.Max(scale.x, 1.0f); adjustedWidth = Mathf.Max(viewWidth, adjustedWidth); } if (height.Unit != ElementSizeUnit.Percents) { float viewHeight = height.Pixels * Mathf.Max(scale.y, 1.0f); adjustedHeight = Mathf.Max(viewHeight, adjustedHeight); } // adjust content regions to size ContentRegionCanvas.SetSize(adjustedWidth * 2, adjustedHeight * 2); ViewContentRegion.SetSize(adjustedWidth, adjustedHeight); }
/// <summary> /// Gets right margin from right size. /// </summary> public static ElementMargin FromRight(ElementSize right) { return(new ElementMargin(null, null, right, null)); }
/// <summary> /// Gets bottom margin from bottom size. /// </summary> public static ElementMargin FromBottom(ElementSize bottom) { return(new ElementMargin(null, null, null, bottom)); }
/// <summary> /// Gets left margin from left size. /// </summary> public static ElementMargin FromLeft(ElementSize left) { return(new ElementMargin(left, null, null, null)); }
/// <summary> /// Gets top margin from top size. /// </summary> public static ElementMargin FromTop(ElementSize top) { return(new ElementMargin(null, top, null, null)); }
public RowDefinition(ElementSize height, float minHeight, float maxHeight) { Height = height; MinHeight = minHeight; MaxHeight = maxHeight; }
public ColumnDefinition(ColumnDefinition columnDefinition) { Width = columnDefinition.Width; ActualWidth = columnDefinition.ActualWidth; ActualOffset = columnDefinition.ActualOffset; }
public ColumnDefinition(ElementSize width, float minWidth, float maxWidth) { Width = width; MinWidth = minWidth; MaxWidth = maxWidth; }
public ColumnDefinition(ElementSize width) { Width = width; MaxWidth = float.MaxValue; }
public RowDefinition(RowDefinition rowDefinition) { Height = rowDefinition.Height; ActualHeight = rowDefinition.ActualHeight; ActualOffset = rowDefinition.ActualOffset; }