internal static Size ComputeScaleFactor(Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { double scaleX = 1.0; double scaleY = 1.0; bool finiteWidth = !double.IsPositiveInfinity(availableSize.Width); bool finiteHeight = !double.IsPositiveInfinity(availableSize.Height); if ((stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill) && (finiteWidth || finiteHeight)) { scaleX = (MathUtil.IsZero(contentSize.Width) ? 0.0 : (availableSize.Width / contentSize.Width)); scaleY = (MathUtil.IsZero(contentSize.Height) ? 0.0 : (availableSize.Height / contentSize.Height)); if (!finiteWidth) { scaleX = scaleY; } else { if (!finiteHeight) { scaleY = scaleX; } else { ScaleByStretch(stretch, ref scaleX, ref scaleY); } } ScaleByDirectionOnly(stretchDirection, ref scaleX, ref scaleY); } return(new Size(scaleX, scaleY)); }
private static void ScaleByDirectionOnly(StretchDirection stretchDirection, ref double scaleX, ref double scaleY) { switch (stretchDirection) { case StretchDirection.UpOnly: { if (scaleX < 1.0) { scaleX = 1.0; } if (scaleY < 1.0) { scaleY = 1.0; } break; } case StretchDirection.DownOnly: { if (scaleX > 1.0) { scaleX = 1.0; } if (scaleY > 1.0) { scaleY = 1.0; } break; } } }
private static WpfSize ComputeScaleFactor (Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { bool isWidthInfinite = availableSize.IsWidthInfinite; bool isHeightInfinite = availableSize.IsHeightInfinite; if (stretch == Stretch.None || (isWidthInfinite && isHeightInfinite)) return new WpfSize(1, 1); double scaleX = contentSize.Width == 0 ? 0 : (double)availableSize.Width / contentSize.Width; double scaleY = contentSize.Height == 0 ? 0 : (double)availableSize.Height / contentSize.Height; if (isWidthInfinite) scaleX = scaleY; else if (isHeightInfinite) scaleY = scaleX; else if (stretch == Stretch.Uniform) scaleX = scaleY = Math.Min(scaleX, scaleY); else if (stretch == Stretch.UniformToFill) scaleX = scaleY = Math.Max(scaleX, scaleY); Func<double, double, double> clip = null; if (stretchDirection == StretchDirection.UpOnly) clip = Math.Max; else if (stretchDirection == StretchDirection.DownOnly) clip = Math.Min; if (clip != null) { scaleX = clip(scaleX, 1); scaleY = clip(scaleY, 1); } return new WpfSize(scaleX, scaleY); }
internal static Size CalculateScaleFactor( Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { var scaleX = 1d; var scaleY = 1d; var isConstrainedWidth = !double.IsPositiveInfinity(availableSize.Width); var isConstrainedHeight = !double.IsPositiveInfinity(availableSize.Height); if ((stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill) && (isConstrainedWidth || isConstrainedHeight)) { scaleX = Math.Abs(contentSize.Width) < double.Epsilon ? 0d : availableSize.Width / contentSize.Width; scaleY = Math.Abs(contentSize.Height) < double.Epsilon ? 0d : availableSize.Height / contentSize.Height; if (!isConstrainedWidth) { scaleX = scaleY; } else if (!isConstrainedHeight) { scaleY = scaleX; } else { switch (stretch) { case Stretch.Uniform: var minScale = Math.Min(scaleX, scaleY); scaleX = minScale; scaleY = minScale; break; case Stretch.UniformToFill: var maxScale = Math.Max(scaleX, scaleY); scaleX = maxScale; scaleY = maxScale; break; } } switch (stretchDirection) { case StretchDirection.UpOnly: scaleX = Math.Max(scaleX, 1); scaleY = Math.Max(scaleY, 1); break; case StretchDirection.DownOnly: scaleX = Math.Min(scaleX, 1); scaleY = Math.Min(scaleY, 1); break; } } return(new Size(scaleX, scaleY)); }
/// <summary> /// Calculates a scaled size based on a <see cref="Stretch"/> value. /// </summary> /// <param name="stretch">The stretch mode.</param> /// <param name="destinationSize">The size of the destination viewport.</param> /// <param name="sourceSize">The size of the source.</param> /// <param name="stretchDirection">The stretch direction.</param> /// <returns>The size of the stretched source.</returns> public static Size CalculateSize( this Stretch stretch, Size destinationSize, Size sourceSize, StretchDirection stretchDirection = StretchDirection.Both) { return(sourceSize * stretch.CalculateScaling(destinationSize, sourceSize, stretchDirection)); }
static StretchDirection Filter(StretchDirection value) { if (value < 0 || value > StretchDirection.Both) { return(DefaultRawSettings.DefaultStretchDirection); } return(value); }
private static bool ValidateStretchDirectionValue(object value) { StretchDirection sd = (StretchDirection)value; return(sd == StretchDirection.Both || sd == StretchDirection.DownOnly || sd == StretchDirection.UpOnly); }
private void CreateGifAnimation(MemoryStream memoryStream) { this.gifAnimation = new GifAnimation(); this.gifAnimation.CreateGifAnimation(memoryStream); this.gifAnimation.Stretch = this.Stretch; this.gifAnimation.StretchDirection = this.StretchDirection; this.AddChild(this.gifAnimation); }
private void CreateNonGifAnimationImage() { this.image = new Image(); this.image.ImageFailed += new EventHandler <ExceptionRoutedEventArgs>(this.image_ImageFailed); this.image.Source = (ImageSource) new ImageSourceConverter().ConvertFromString(this.Source); this.image.Stretch = this.Stretch; this.image.StretchDirection = this.StretchDirection; this.AddChild(this.image); }
private static bool IsStretchDirectionValid(object value) { StretchDirection stretchDirection = (StretchDirection)value; if (stretchDirection != StretchDirection.Both && stretchDirection != 0) { return(stretchDirection == StretchDirection.DownOnly); } return(true); }
static Size ComputeScaleFactor (Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { double widthFactor = 1.0; double heightFactor = 1.0; bool widthSet = !double.IsPositiveInfinity (availableSize.Width); bool heightSet = !double.IsPositiveInfinity (availableSize.Height); if ((stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill) && (widthSet || heightSet)) { widthFactor = (IsZero (contentSize.Width) ? 0.0 : (availableSize.Width / contentSize.Width)); heightFactor = (IsZero (contentSize.Height) ? 0.0 : (availableSize.Height / contentSize.Height)); if (!widthSet) widthFactor = heightFactor; else { if (!heightSet) heightFactor = widthFactor; else { switch (stretch) { case Stretch.Uniform: { double num3 = (widthFactor < heightFactor) ? widthFactor : heightFactor; heightFactor = (widthFactor = num3); break; } case Stretch.UniformToFill: { double num4 = (widthFactor > heightFactor) ? widthFactor : heightFactor; heightFactor = (widthFactor = num4); break; } } } } switch (stretchDirection) { case StretchDirection.UpOnly: if (widthFactor < 1.0) widthFactor = 1.0; if (heightFactor < 1.0) heightFactor = 1.0; break; case StretchDirection.DownOnly: if (widthFactor > 1.0) widthFactor = 1.0; if (heightFactor > 1.0) heightFactor = 1.0; break; } } return new Size (widthFactor, heightFactor); }
private static void OnStretchDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ImageEx ex = (ImageEx)d; StretchDirection newValue = (StretchDirection)e.NewValue; if (ex._gifAnimation != null) { ex._gifAnimation.StretchDirection = newValue; } else if (ex._image != null) { ex._image.StretchDirection = newValue; } }
private static void OnStretchDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { GifImage obj = (GifImage)d; StretchDirection s = (StretchDirection)e.NewValue; if (obj.gifAnimation != null) { obj.gifAnimation.StretchDirection = s; } else if (obj.image != null) { obj.image.StretchDirection = s; } }
public static void StretchItems(IEnumerable <DesignItem> items, StretchDirection stretchDirection) { var collection = items; var container = collection.First().Parent; if (collection.Any(x => x.Parent != container)) { return; } var placement = container.Extensions.OfType <IPlacementBehavior>().FirstOrDefault(); if (placement == null) { return; } var changeGroup = container.OpenGroup("StretchItems"); var w = GetWidth(collection.First().View); var h = GetHeight(collection.First().View); foreach (var item in collection.Skip(1)) { switch (stretchDirection) { case StretchDirection.Width: { if (!double.IsNaN(w)) { item.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(w); } } break; case StretchDirection.Height: { if (!double.IsNaN(h)) { item.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(h); } } break; } } changeGroup.Commit(); }
public override void Deserialize(XElement itemXML, double OffsetX, double OffsetY) { base.Deserialize(itemXML, OffsetX, OffsetY); try { this.Resource = new Engine.PwResourceLink(itemXML.Attribute("Resource").Value); } catch { } try { this.Stretch = (Stretch)(Enum.Parse(typeof(Stretch), itemXML.Attribute("Stretch").Value, true)); this.StretchDirection = (StretchDirection)(Enum.Parse(typeof(StretchDirection), itemXML.Attribute("StretchDirection").Value, true)); } catch { } }
private static void OnStretchDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { GifImage gifImage = (GifImage)d; StretchDirection stretchDirection = (StretchDirection)e.NewValue; if (gifImage.gifAnimation != null) { gifImage.gifAnimation.StretchDirection = stretchDirection; } else { if (gifImage.image == null) { return; } gifImage.image.StretchDirection = stretchDirection; } }
/// <summary> /// This is a helper provided to assist derived Sources when scaling their content to /// the owner size. /// </summary> /// <param name="target">The total available space.</param> /// <param name="source">The unscaled source size.</param> /// <param name="stretchMode">The <see cref="Stretch"/> mode that determines which stretching technique to use.</param> /// <param name="direction">The <see cref="StretchDirection"/> that determines when to perform scaling.</param> /// <returns>The scaled source size, which may be larger than the <paramref name="target"/> size.</returns> public SizeF StretchSource(SizeF target, SizeF source, Stretch stretchMode, StretchDirection direction) { if (direction == StretchDirection.DownOnly && source.Width <= target.Width && source.Height <= target.Height) { return(source); } if (direction == StretchDirection.UpOnly && source.Width >= target.Width && source.Height >= target.Height) { return(source); } switch (stretchMode) { case Stretch.None: // Original size break; case Stretch.Fill: // Stretch to fit source = target; break; case Stretch.Uniform: // Keep aspect ratio and show borders { float ratio = System.Math.Min(target.Width / source.Width, target.Height / source.Height); source.Width *= ratio; source.Height *= ratio; } break; case Stretch.UniformToFill: // Keep aspect ratio, zoom in to avoid borders { float ratio = System.Math.Max(target.Width / source.Width, target.Height / source.Height); source.Width *= ratio; source.Height *= ratio; } break; } return(source); }
private static void OnStretchDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { ImageAnim obj = (ImageAnim)d; StretchDirection s = (StretchDirection)e.NewValue; if (obj._gifAnimation != null) { obj._gifAnimation.StretchDirection = s; } else if (obj._image != null) { obj._image.StretchDirection = s; } } catch (Exception ex) { VMuktiAPI.VMuktiHelper.ExceptionHandler(ex, "OnSrectchDirectionChange()", "Controls\\VMuktiGrid\\ImageAnim.cs"); } }
public override void Render(RenderContext renderContext, Stretch stretchMode, StretchDirection stretchDirection) { Allocate(); Texture currentTexture = CurrentTexture; SizeF currentRawSourceSize = CurrentRawSourceSize; RectangleF currentTextureClip = CurrentTextureClip; Vector4 frameData = new Vector4(currentRawSourceSize.Width, currentRawSourceSize.Height, (float) EffectTimer, 0); if (_transitionActive) { double elapsed = (SkinContext.FrameRenderingStartTime - _transitionStart).TotalSeconds / Math.Max(TransitionDuration, 0.01); if (elapsed > 1.0) _transitionActive = false; else { Texture lastTexture = LastTexture; SizeF lastRawSourceSize = LastRawSourceSize; RectangleF lastTextureClip = LastTextureClip; Vector4 lastFrameData = new Vector4(lastRawSourceSize.Width, lastRawSourceSize.Height, (float) EffectTimer, 0); Texture start = lastTexture ?? NullTexture.Texture; Texture end = currentTexture ?? NullTexture.Texture; if (start != end) { SizeF startSize = StretchSource(_lastImageContext.RotatedFrameSize, lastRawSourceSize, stretchMode, stretchDirection); SizeF endSize = StretchSource(_imageContext.RotatedFrameSize, currentRawSourceSize, stretchMode, stretchDirection); // Render transition from last texture to current texture _lastImageContext.Update(startSize, start, lastTextureClip); if (_imageContext.StartRenderTransition(renderContext, (float) elapsed, _lastImageContext, endSize, end, currentTextureClip, BorderColor, lastFrameData, frameData)) { _primitiveBuffer.Render(0); _imageContext.EndRenderTransition(); } } return; } } if (IsAllocated) { SizeF sourceSize = StretchSource(_imageContext.RotatedFrameSize, currentRawSourceSize, stretchMode, stretchDirection); if (_imageContext.StartRender(renderContext, sourceSize, currentTexture, currentTextureClip, BorderColor, frameData)) { _primitiveBuffer.Render(0); _imageContext.EndRender(); } } }
/// <summary> /// Sets the stretch <see cref="StretchDirection"/> associated with the specified <see cref="Windows.UI.Xaml.Controls.ListViewBase"/> /// </summary> /// <param name="obj">The <see cref="Windows.UI.Xaml.Controls.ListViewBase"/> to associate the <see cref="StretchDirection"/> with</param> /// <param name="value">The <see cref="StretchDirection"/> for binding to the <see cref="Windows.UI.Xaml.Controls.ListViewBase"/></param> public static void SetStretchItemContainerDirection(Windows.UI.Xaml.Controls.ListViewBase obj, StretchDirection value) { obj.SetValue(StretchItemContainerDirectionProperty, value); }
private static Size ComputeScaleFactor(Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { var unlimitedWidth = double.IsPositiveInfinity(availableSize.Width); var unlimitedHeight = double.IsPositiveInfinity(availableSize.Height); if (stretch != Stretch.Uniform && stretch != Stretch.UniformToFill && stretch != Stretch.Fill || unlimitedWidth && unlimitedHeight) { return new Size(1d, 1d); } var scaleX = Equals(contentSize.Width, 0d) ? 0.0 : availableSize.Width / contentSize.Width; var scaleY = Equals(contentSize.Height, 0d) ? 0.0 : availableSize.Height / contentSize.Height; if (unlimitedWidth) { scaleX = scaleY; } else if (unlimitedHeight) { scaleY = scaleX; } else { switch (stretch) { case Stretch.Uniform: scaleX = scaleY = scaleX < scaleY ? scaleX : scaleY; break; case Stretch.UniformToFill: scaleX = scaleY = scaleX > scaleY ? scaleX : scaleY; break; case Stretch.Fill: break; } } switch (stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0) scaleX = 1.0; if (scaleY < 1.0) scaleY = 1.0; break; case StretchDirection.DownOnly: if (scaleX > 1.0) scaleX = 1.0; if (scaleY > 1.0) scaleY = 1.0; break; case StretchDirection.Both: break; } return new Size(scaleX, scaleY); }
public override void Render(RenderContext renderContext, Stretch stretchMode, StretchDirection stretchDirection) { if (!IsAllocated) return; SizeF rawSourceSize = RawSourceSize; SizeF modifiedSourceSize = StretchSource(_imageContext.RotatedFrameSize, rawSourceSize, stretchMode, stretchDirection); Vector4 frameData = new Vector4(rawSourceSize.Width, rawSourceSize.Height, (float) EffectTimer, 0); if (_primitiveBuffer != null && _imageContext.StartRender(renderContext, modifiedSourceSize, Texture, TextureClip, BorderColor, frameData)) { _primitiveBuffer.Render(0); _imageContext.EndRender(); } }
/// <summary> /// This is a helper function that computes scale factors depending on a target size and a content size /// </summary> /// <param name="availableSize">Size into which the content is being fitted.</param> /// <param name="contentSize">Size of the content, measured natively (unconstrained).</param> /// <param name="stretch">Value of the Stretch property on the element.</param> /// <param name="stretchDirection">Value of the StretchDirection property on the element.</param> internal static Size ComputeScaleFactor(Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { // Compute scaling factors to use for axes double scaleX = 1.0; double scaleY = 1.0; bool isConstrainedWidth = !Double.IsPositiveInfinity(availableSize.Width); bool isConstrainedHeight = !Double.IsPositiveInfinity(availableSize.Height); if ( (stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill) && (isConstrainedWidth || isConstrainedHeight) ) { // Compute scaling factors for both axes scaleX = (DoubleUtil.IsZero(contentSize.Width)) ? 0.0 : availableSize.Width / contentSize.Width; scaleY = (DoubleUtil.IsZero(contentSize.Height)) ? 0.0 : availableSize.Height / contentSize.Height; if (!isConstrainedWidth) scaleX = scaleY; else if (!isConstrainedHeight) scaleY = scaleX; else { // If not preserving aspect ratio, then just apply transform to fit switch (stretch) { case Stretch.Uniform: //Find minimum scale that we use for both axes double minscale = scaleX < scaleY ? scaleX : scaleY; scaleX = scaleY = minscale; break; case Stretch.UniformToFill: //Find maximum scale that we use for both axes double maxscale = scaleX > scaleY ? scaleX : scaleY; scaleX = scaleY = maxscale; break; case Stretch.Fill: //We already computed the fill scale factors above, so just use them break; } } //Apply stretch direction by bounding scales. //In the uniform case, scaleX=scaleY, so this sort of clamping will maintain aspect ratio //In the uniform fill case, we have the same result too. //In the fill case, note that we change aspect ratio, but that is okay switch(stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0) scaleX = 1.0; if (scaleY < 1.0) scaleY = 1.0; break; case StretchDirection.DownOnly: if (scaleX > 1.0) scaleX = 1.0; if (scaleY > 1.0) scaleY = 1.0; break; case StretchDirection.Both: break; default: break; } } //Return this as a size now return new Size(scaleX, scaleY); }
internal static Size ComputeScaleFactor(Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { // Compute scaling factors to use for axes double scaleX = 1.0; double scaleY = 1.0; bool isConstrainedWidth = !Double.IsPositiveInfinity(availableSize.Width); bool isConstrainedHeight = !Double.IsPositiveInfinity(availableSize.Height); if ((stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill) && (isConstrainedWidth || isConstrainedHeight)) { // Compute scaling factors for both axes scaleX = (DoubleUtil.IsZero(contentSize.Width)) ? 0.0 : availableSize.Width / contentSize.Width; scaleY = (DoubleUtil.IsZero(contentSize.Height)) ? 0.0 : availableSize.Height / contentSize.Height; if (!isConstrainedWidth) { scaleX = scaleY; } else if (!isConstrainedHeight) { scaleY = scaleX; } else { // If not preserving aspect ratio, then just apply transform to fit switch (stretch) { case Stretch.Uniform: //Find minimum scale that we use for both axes double minscale = scaleX < scaleY ? scaleX : scaleY; scaleX = scaleY = minscale; break; case Stretch.UniformToFill: //Find maximum scale that we use for both axes double maxscale = scaleX > scaleY ? scaleX : scaleY; scaleX = scaleY = maxscale; break; case Stretch.Fill: //We already computed the fill scale factors above, so just use them break; } } //Apply stretch direction by bounding scales. //In the uniform case, scaleX=scaleY, so this sort of clamping will maintain aspect ratio //In the uniform fill case, we have the same result too. //In the fill case, note that we change aspect ratio, but that is okay switch (stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0) { scaleX = 1.0; } if (scaleY < 1.0) { scaleY = 1.0; } break; case StretchDirection.DownOnly: if (scaleX > 1.0) { scaleX = 1.0; } if (scaleY > 1.0) { scaleY = 1.0; } break; case StretchDirection.Both: break; default: break; } } //Return this as a size now return(new Size(scaleX, scaleY)); }
private static Size ComputeScaleFactor(Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { var scaleX = 1.0; var scaleY = 1.0; var isConstrainedWidth = !double.IsPositiveInfinity(availableSize.Width); var isConstrainedHeight = !double.IsPositiveInfinity(availableSize.Height); if ((stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill) && (isConstrainedWidth || isConstrainedHeight)) { scaleX = Equals(0d, contentSize.Width) ? 0.0 : availableSize.Width / contentSize.Width; scaleY = Equals(0d, contentSize.Height) ? 0.0 : availableSize.Height / contentSize.Height; if (!isConstrainedWidth) { scaleX = scaleY; } else if (!isConstrainedHeight) { scaleY = scaleX; } else { switch (stretch) { case Stretch.Uniform: var minscale = scaleX < scaleY ? scaleX : scaleY; scaleX = scaleY = minscale; break; case Stretch.UniformToFill: var maxscale = scaleX > scaleY ? scaleX : scaleY; scaleX = scaleY = maxscale; break; case Stretch.Fill: break; } } switch (stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0) { scaleX = 1.0; } if (scaleY < 1.0) { scaleY = 1.0; } break; case StretchDirection.DownOnly: if (scaleX > 1.0) { scaleX = 1.0; } if (scaleY > 1.0) { scaleY = 1.0; } break; case StretchDirection.Both: break; } } return(new Size(scaleX, scaleY)); }
public override void Render(RenderContext renderContext, Stretch stretchMode, StretchDirection stretchDirection) { Allocate(); Texture currentTexture = CurrentTexture; SizeF currentRawSourceSize = CurrentRawSourceSize; RectangleF currentTextureClip = CurrentTextureClip; Vector4 frameData = new Vector4(currentRawSourceSize.Width, currentRawSourceSize.Height, (float)EffectTimer, 0); if (_transitionActive) { double elapsed = (SkinContext.FrameRenderingStartTime - _transitionStart).TotalSeconds / Math.Max(TransitionDuration, 0.01); if (elapsed > 1.0) { _transitionActive = false; } else { Texture lastTexture = LastTexture; SizeF lastRawSourceSize = LastRawSourceSize; RectangleF lastTextureClip = LastTextureClip; Vector4 lastFrameData = new Vector4(lastRawSourceSize.Width, lastRawSourceSize.Height, (float)EffectTimer, 0); Texture start = lastTexture ?? NullTexture.Texture; Texture end = currentTexture ?? NullTexture.Texture; if (start != end) { SizeF startSize = StretchSource(_lastImageContext.RotatedFrameSize, lastRawSourceSize, stretchMode, stretchDirection); SizeF endSize = StretchSource(_imageContext.RotatedFrameSize, currentRawSourceSize, stretchMode, stretchDirection); // Render transition from last texture to current texture _lastImageContext.Update(startSize, start, lastTextureClip); if (_imageContext.StartRenderTransition(renderContext, (float)elapsed, _lastImageContext, endSize, end, currentTextureClip, BorderColor, lastFrameData, frameData)) { _primitiveBuffer.Render(0); _imageContext.EndRenderTransition(); } } return; } } if (IsAllocated) { SizeF sourceSize = StretchSource(_imageContext.RotatedFrameSize, currentRawSourceSize, stretchMode, stretchDirection); if (_imageContext.StartRender(renderContext, sourceSize, currentTexture, currentTextureClip, BorderColor, frameData)) { _primitiveBuffer.Render(0); _imageContext.EndRender(); } } }
private static Rect GetStretchRect(Size size, Size availableSize, Stretch stretch, StretchDirection stretchDirection) { if (size.IsNullOrEmpty() || size == Size.Zero || availableSize == Size.Zero) { return(Rect.Zero); } Size stretchSize = GetStretchSize(size, availableSize, stretch); if (stretchDirection == StretchDirection.DownOnly) { stretchSize = stretchSize.Min(size); } if (stretchDirection == StretchDirection.UpOnly) { stretchSize = stretchSize.Max(size); } return(new Rect((availableSize.ToPoint() - stretchSize.ToPoint()) / 2, stretchSize)); }
/// <summary> /// Renders this image source to the client area using the passed <paramref name="renderContext"/>. /// </summary> /// <param name="renderContext">The current rendering context</param> /// <param name="stretchMode">The mode of stretching to perform when the image doesn't fit the client area.</param> /// <param name="stretchDirection">The condition required for stretching to take place.</param> public abstract void Render(RenderContext renderContext, Stretch stretchMode, StretchDirection stretchDirection);
internal static Size ComputeScaleFactor(Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { double num = 1.0; double num2 = 1.0; bool flag = !double.IsPositiveInfinity(availableSize.Width); bool flag2 = !double.IsPositiveInfinity(availableSize.Height); if ((stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill) && (flag | flag2)) { num = (DoubleUtil.IsZero(contentSize.Width) ? 0.0 : (availableSize.Width / contentSize.Width)); num2 = (DoubleUtil.IsZero(contentSize.Height) ? 0.0 : (availableSize.Height / contentSize.Height)); if (!flag) { num = num2; } else if (!flag2) { num2 = num; } else { switch (stretch) { case Stretch.Uniform: num2 = (num = ((num < num2) ? num : num2)); break; case Stretch.UniformToFill: num2 = (num = ((num > num2) ? num : num2)); break; } } switch (stretchDirection) { case StretchDirection.UpOnly: if (num < 1.0) { num = 1.0; } if (num2 < 1.0) { num2 = 1.0; } break; case StretchDirection.DownOnly: if (num > 1.0) { num = 1.0; } if (num2 > 1.0) { num2 = 1.0; } break; } } return(new Size(num, num2)); }
static StretchDirection Filter(StretchDirection value) { if (value < 0 || value > StretchDirection.Both) return DefaultRawSettings.DefaultStretchDirection; return value; }
public static T StretchDirection <T>(this T target, StretchDirection value) where T : Viewbox { target.StretchDirection = value; return(target); }
internal static Vector2 ComputeScaleFactor( Vector2 availableSize, Vector2 contentSize, Stretch stretch, StretchDirection stretchDirection) { float scaleX = 1.0f; float scaleY = 1.0f; bool isWidthContrained = !float.IsPositiveInfinity(availableSize.X); bool isHeightConstrained = !float.IsPositiveInfinity(availableSize.Y); if (stretch == Stretch.None || (!isWidthContrained && !isHeightConstrained)) { return(new Vector2(scaleX, scaleY)); } scaleX = contentSize.X.IsCloseTo(0) ? 0 : (availableSize.X / contentSize.X); scaleY = contentSize.Y.IsCloseTo(0) ? 0 : (availableSize.Y / contentSize.Y); if (!isWidthContrained) { scaleX = scaleY; } else if (!isHeightConstrained) { scaleY = scaleX; } else { switch (stretch) { case Stretch.Fill: return(new Vector2(availableSize.X / contentSize.X, availableSize.Y / contentSize.Y)); case Stretch.Uniform: scaleX = scaleY = (scaleX < scaleY) ? scaleX : scaleY; break; case Stretch.UniformToFill: scaleX = scaleY = (scaleX > scaleY) ? scaleX : scaleY; break; } } switch (stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0f) { scaleX = 1.0f; } if (scaleY < 1.0f) { scaleY = 1.0f; } break; case StretchDirection.DownOnly: if (scaleX > 1.0f) { scaleX = 1.0f; } if (scaleY > 1.0f) { scaleY = 1.0f; } break; } return(new Vector2(scaleX, scaleY)); }
/// <summary> /// This is a helper provided to assist derived Sources when scaling their content to /// the owner size. /// </summary> /// <param name="target">The total available space.</param> /// <param name="source">The unscaled source size.</param> /// <param name="stretchMode">The <see cref="Stretch"/> mode that determines which stretching technique to use.</param> /// <param name="direction">The <see cref="StretchDirection"/> that determines when to perform scaling.</param> /// <returns>The scaled source size, which may be larger than the <paramref name="target"/> size.</returns> public SizeF StretchSource(SizeF target, SizeF source, Stretch stretchMode, StretchDirection direction) { if (direction == StretchDirection.DownOnly && source.Width <= target.Width && source.Height <= target.Height) return source; if (direction == StretchDirection.UpOnly && source.Width >= target.Width && source.Height >= target.Height) return source; switch (stretchMode) { case Stretch.None: // Original size break; case Stretch.Fill: // Stretch to fit source = target; break; case Stretch.Uniform: // Keep aspect ratio and show borders { float ratio = System.Math.Min(target.Width / source.Width, target.Height / source.Height); source.Width *= ratio; source.Height *= ratio; } break; case Stretch.UniformToFill: // Keep aspect ratio, zoom in to avoid borders { float ratio = System.Math.Max(target.Width / source.Width, target.Height / source.Height); source.Width *= ratio; source.Height *= ratio; } break; } return source; }
internal static Vector ComputeScaleFactor( Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { double scaleX = 1.0; double scaleY = 1.0; bool isWidthContrained = !double.IsPositiveInfinity(availableSize.Width); bool isHeightConstrained = !double.IsPositiveInfinity(availableSize.Height); if (stretch == Stretch.None || (!isWidthContrained && !isHeightConstrained)) { return new Vector(scaleX, scaleY); } scaleX = contentSize.Width.IsCloseTo(0) ? 0 : (availableSize.Width / contentSize.Width); scaleY = contentSize.Height.IsCloseTo(0) ? 0 : (availableSize.Height / contentSize.Height); if (!isWidthContrained) { scaleX = scaleY; } else if (!isHeightConstrained) { scaleY = scaleX; } else { switch (stretch) { case Stretch.Uniform: scaleX = scaleY = (scaleX < scaleY) ? scaleX : scaleY; break; case Stretch.UniformToFill: scaleX = scaleY = (scaleX > scaleY) ? scaleX : scaleY; break; } } switch (stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0) { scaleX = 1.0; } if (scaleY < 1.0) { scaleY = 1.0; } break; case StretchDirection.DownOnly: if (scaleX > 1.0) { scaleX = 1.0; } if (scaleY > 1.0) { scaleY = 1.0; } break; } return new Vector(scaleX, scaleY); }
/// <summary> /// Calculates the actual image size from the size that is available. /// </summary> /// <param name="sprite"></param> /// <param name="availableSizeWithoutMargins"></param> /// <param name="stretchType"></param> /// <param name="stretchDirection"></param> /// <param name="isMeasuring"></param> /// <returns></returns> public static Vector3 CalculateImageSizeFromAvailable(Sprite sprite, Vector3 availableSizeWithoutMargins, StretchType stretchType, StretchDirection stretchDirection, bool isMeasuring) { if (sprite == null) // no associated image -> no region needed return Vector3.Zero; var idealSize = sprite.SizeInPixels; if (idealSize.X <= 0 || idealSize.Y <= 0) // image size null or invalid -> no region needed return Vector3.Zero; if (float.IsInfinity(availableSizeWithoutMargins.X) && float.IsInfinity(availableSizeWithoutMargins.Y)) // unconstrained available size -> take the best size for the image: the image size return new Vector3(idealSize, 0); // initialize the desired size with maximum available size var desiredSize = availableSizeWithoutMargins; // compute the desired image ratios var desiredScale = new Vector2(desiredSize.X / idealSize.X, desiredSize.Y / idealSize.Y); // when the size along a given axis is free take the same ratio as the constrained axis. if (float.IsInfinity(desiredScale.X)) desiredScale.X = desiredScale.Y; if (float.IsInfinity(desiredScale.Y)) desiredScale.Y = desiredScale.X; // adjust the scales depending on the type of stretch to apply switch (stretchType) { case StretchType.None: desiredScale = Vector2.One; break; case StretchType.Uniform: desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y); break; case StretchType.UniformToFill: desiredScale.X = desiredScale.Y = Math.Max(desiredScale.X, desiredScale.Y); break; case StretchType.FillOnStretch: // if we are only measuring we prefer keeping the image resolution than using all the available space. if (isMeasuring) desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y); break; case StretchType.Fill: break; default: throw new ArgumentOutOfRangeException(nameof(stretchType)); } // adjust the scales depending on the stretch directions switch (stretchDirection) { case StretchDirection.Both: break; case StretchDirection.DownOnly: desiredScale.X = Math.Min(desiredScale.X, 1); desiredScale.Y = Math.Min(desiredScale.Y, 1); break; case StretchDirection.UpOnly: desiredScale.X = Math.Max(1, desiredScale.X); desiredScale.Y = Math.Max(1, desiredScale.Y); break; default: throw new ArgumentOutOfRangeException(nameof(stretchDirection)); } // update the desired size based on the desired scales desiredSize = new Vector3(idealSize.X * desiredScale.X, idealSize.Y * desiredScale.Y, 0f); if (!isMeasuring || !sprite.HasBorders) return desiredSize; // consider sprite borders var borderSum = new Vector2(sprite.BordersInternal.X + sprite.BordersInternal.Z, sprite.BordersInternal.Y + sprite.BordersInternal.W); if (sprite.Orientation == ImageOrientation.Rotated90) Utilities.Swap(ref borderSum.X, ref borderSum.Y); return new Vector3(Math.Max(desiredSize.X, borderSum.X), Math.Max(desiredSize.Y, borderSum.Y), desiredSize.Z); }
/// <summary> /// Calculates the actual image size from the size that is available. /// </summary> /// <param name="sprite"></param> /// <param name="availableSizeWithoutMargins"></param> /// <param name="stretchType"></param> /// <param name="stretchDirection"></param> /// <param name="isMeasuring"></param> /// <returns></returns> public static Vector3 CalculateImageSizeFromAvailable(Sprite sprite, Vector3 availableSizeWithoutMargins, StretchType stretchType, StretchDirection stretchDirection, bool isMeasuring) { if (sprite == null) // no associated image -> no region needed { return(Vector3.Zero); } var idealSize = sprite.SizeInPixels; if (idealSize.X <= 0 || idealSize.Y <= 0) // image size null or invalid -> no region needed { return(Vector3.Zero); } if (float.IsInfinity(availableSizeWithoutMargins.X) && float.IsInfinity(availableSizeWithoutMargins.Y)) // unconstrained available size -> take the best size for the image: the image size { return(new Vector3(idealSize, 0)); } // initialize the desired size with maximum available size var desiredSize = availableSizeWithoutMargins; // compute the desired image ratios var desiredScale = new Vector2(desiredSize.X / idealSize.X, desiredSize.Y / idealSize.Y); // when the size along a given axis is free take the same ratio as the constrained axis. if (float.IsInfinity(desiredScale.X)) { desiredScale.X = desiredScale.Y; } if (float.IsInfinity(desiredScale.Y)) { desiredScale.Y = desiredScale.X; } // adjust the scales depending on the type of stretch to apply switch (stretchType) { case StretchType.None: desiredScale = Vector2.One; break; case StretchType.Uniform: desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y); break; case StretchType.UniformToFill: desiredScale.X = desiredScale.Y = Math.Max(desiredScale.X, desiredScale.Y); break; case StretchType.FillOnStretch: // if we are only measuring we prefer keeping the image resolution than using all the available space. if (isMeasuring) { desiredScale.X = desiredScale.Y = Math.Min(desiredScale.X, desiredScale.Y); } break; case StretchType.Fill: break; default: throw new ArgumentOutOfRangeException(nameof(stretchType)); } // adjust the scales depending on the stretch directions switch (stretchDirection) { case StretchDirection.Both: break; case StretchDirection.DownOnly: desiredScale.X = Math.Min(desiredScale.X, 1); desiredScale.Y = Math.Min(desiredScale.Y, 1); break; case StretchDirection.UpOnly: desiredScale.X = Math.Max(1, desiredScale.X); desiredScale.Y = Math.Max(1, desiredScale.Y); break; default: throw new ArgumentOutOfRangeException(nameof(stretchDirection)); } // update the desired size based on the desired scales desiredSize = new Vector3(idealSize.X * desiredScale.X, idealSize.Y * desiredScale.Y, 0f); if (!isMeasuring || !sprite.HasBorders) { return(desiredSize); } // consider sprite borders var borderSum = new Vector2(sprite.BordersInternal.X + sprite.BordersInternal.Z, sprite.BordersInternal.Y + sprite.BordersInternal.W); if (sprite.Orientation == ImageOrientation.Rotated90) { Utilities.Swap(ref borderSum.X, ref borderSum.Y); } return(new Vector3(Math.Max(desiredSize.X, borderSum.X), Math.Max(desiredSize.Y, borderSum.Y), desiredSize.Z)); }
private static Rect GetStretchRect(Size size, Size availableSize, Stretch stretch, StretchDirection stretchDirection) { if (size.IsNullOrEmpty() || size == Size.Zero || availableSize == Size.Zero) { return Rect.Zero; } Size stretchSize = GetStretchSize(size, availableSize, stretch); if (stretchDirection == StretchDirection.DownOnly) { stretchSize = stretchSize.Min(size); } if (stretchDirection == StretchDirection.UpOnly) { stretchSize = stretchSize.Max(size); } return new Rect((availableSize.ToPoint() - stretchSize.ToPoint()) / 2, stretchSize); }
internal static Vector ComputeScaleFactor( Size availableSize, Size contentSize, Stretch stretch, StretchDirection stretchDirection) { double scaleX = 1.0; double scaleY = 1.0; bool isWidthContrained = !double.IsPositiveInfinity(availableSize.Width); bool isHeightConstrained = !double.IsPositiveInfinity(availableSize.Height); if (stretch == Stretch.None || (!isWidthContrained && !isHeightConstrained)) { return(new Vector(scaleX, scaleY)); } scaleX = contentSize.Width.IsCloseTo(0) ? 0 : (availableSize.Width / contentSize.Width); scaleY = contentSize.Height.IsCloseTo(0) ? 0 : (availableSize.Height / contentSize.Height); if (!isWidthContrained) { scaleX = scaleY; } else if (!isHeightConstrained) { scaleY = scaleX; } else { switch (stretch) { case Stretch.Uniform: scaleX = scaleY = (scaleX < scaleY) ? scaleX : scaleY; break; case Stretch.UniformToFill: scaleX = scaleY = (scaleX > scaleY) ? scaleX : scaleY; break; } } switch (stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0) { scaleX = 1.0; } if (scaleY < 1.0) { scaleY = 1.0; } break; case StretchDirection.DownOnly: if (scaleX > 1.0) { scaleX = 1.0; } if (scaleY > 1.0) { scaleY = 1.0; } break; } return(new Vector(scaleX, scaleY)); }
public override void Render(RenderContext renderContext, Stretch stretchMode, StretchDirection stretchDirection) { if (!IsAllocated) { return; } SizeF rawSourceSize = RawSourceSize; SizeF modifiedSourceSize = StretchSource(_imageContext.RotatedFrameSize, rawSourceSize, stretchMode, stretchDirection); Vector4 frameData = new Vector4(rawSourceSize.Width, rawSourceSize.Height, (float)EffectTimer, 0); if (_primitiveBuffer != null && _imageContext.StartRender(renderContext, modifiedSourceSize, Texture, TextureClip, BorderColor, frameData)) { _primitiveBuffer.Render(0); _imageContext.EndRender(); } }
/// /// This is a helper function that computes scale factors depending on a target size and a content size /// /// Size into which the content is being fitted. /// Size of the content, measured natively (unconstrained). /// Value of the Stretch property on the element. /// Value of the StretchDirection property on the element. public static SizeF ComputeScaleFactor(SizeF availableSize, SizeF contentSize, StretchDirection stretchDirection) { // Compute scaling factors to use for axes double scaleX = 1.0; double scaleY = 1.0; bool isConstrainedWidth = availableSize.Width != -1; bool isConstrainedHeight = availableSize.Height != -1; if ((isConstrainedWidth || isConstrainedHeight)) { // Compute scaling factors for both axes scaleX = (int)contentSize.Width == 0 ? 0.0 : availableSize.Width / contentSize.Width; scaleY = (int)contentSize.Height == 0 ? 0.0 : availableSize.Height / contentSize.Height; if (!isConstrainedWidth) { scaleX = scaleY; } else if (!isConstrainedHeight) { scaleY = scaleX; } else { // If not preserving aspect ratio, then just apply transform to fit double maxscale = scaleX > scaleY ? scaleX : scaleY; scaleX = scaleY = maxscale; } //Apply stretch direction by bounding scales. //In the uniform case, scaleX=scaleY, so this sort of clamping will maintain aspect ratio //In the uniform fill case, we have the same result too. //In the fill case, note that we change aspect ratio, but that is okay switch (stretchDirection) { case StretchDirection.UpOnly: if (scaleX < 1.0) { scaleX = 1.0; } if (scaleY < 1.0) { scaleY = 1.0; } break; case StretchDirection.DownOnly: if (scaleX > 1.0) { scaleX = 1.0; } if (scaleY > 1.0) { scaleY = 1.0; } break; case StretchDirection.Both: break; default: break; } } //Return this as a size now return(new SizeF((float)scaleX, (float)scaleY)); }
/// <summary> /// Check whether the passed in object value is a valid StretchDirection enum value. /// </summary> /// <param name="o">The object typed value to be checked.</param> /// <returns>True if o is a valid StretchDirection enum value, false o/w.</returns> private static bool IsValidStretchDirectionValue(object o) { StretchDirection sd = (StretchDirection)o; return(sd == StretchDirection.UpOnly || sd == StretchDirection.DownOnly || sd == StretchDirection.Both); }
public static void StretchItems(IEnumerable<DesignItem> items, StretchDirection stretchDirection) { var collection = items; var container = collection.First().Parent; if (collection.Any(x => x.Parent != container)) return; var placement = container.Extensions.OfType<IPlacementBehavior>().FirstOrDefault(); if (placement == null) return; var changeGroup = container.OpenGroup("StretchItems"); var w = GetWidth(collection.First().View); var h = GetHeight(collection.First().View); foreach (var item in collection.Skip(1)) { switch (stretchDirection) { case StretchDirection.Width: { if (!double.IsNaN(w)) item.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(w); } break; case StretchDirection.Height: { if (!double.IsNaN(h)) item.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(h); } break; } } changeGroup.Commit(); }