diff --git a/Vocaluxe/Base/CDraw.cs b/Vocaluxe/Base/CDraw.cs index 2640084a..4a9d50e6 100644 --- a/Vocaluxe/Base/CDraw.cs +++ b/Vocaluxe/Base/CDraw.cs @@ -381,8 +381,7 @@ namespace Vocaluxe.Base if (textureRef == null) return; - SRectF rect; - CHelper.SetRect(bounds, out rect, textureRef.OrigAspect, aspect); + SRectF rect = CHelper.FitInBounds(bounds, textureRef.OrigAspect, aspect); DrawTexture(textureRef, rect, color); } diff --git a/VocaluxeLib/CHelper.cs b/VocaluxeLib/CHelper.cs index e14f363b..7a1042a8 100644 --- a/VocaluxeLib/CHelper.cs +++ b/VocaluxeLib/CHelper.cs @@ -78,51 +78,58 @@ namespace VocaluxeLib return result; } - public static void SetRect(SRectF bounds, out SRectF rect, float aspectRatio, EAspect aspect) + /// + /// Places a rect within the bounds maintaining the aspectRatio and using the given aspect + /// + /// Bounds to fit the rect in + /// The original aspectRatio of the rect/image/... + /// + /// Crop: Fit fully in bounds cutting off overhanging parts (same on both sides)
+ /// LetterBox: Fit the long side possibly leaving some space on the other (rect will be centered)
+ /// StretcH: Just fit the rect in the bounds (rect=bounds) + /// + public static SRectF FitInBounds(SRectF bounds, float aspectRatio, EAspect aspect) { - float boundsW = bounds.W; - float boundsH = bounds.H; - float boundsAspectRatio = boundsW / boundsH; + if (aspect == EAspect.Stretch) + return bounds; - float scaledWidth; - float scaledHeight; + float boundsAspectRatio = bounds.W / bounds.H; + + float scaledWidth, scaledHeight; switch (aspect) { case EAspect.Crop: if (boundsAspectRatio >= aspectRatio) { - scaledWidth = boundsW; - scaledHeight = boundsW / aspectRatio; + scaledWidth = bounds.W; + scaledHeight = bounds.W / aspectRatio; } else { - scaledHeight = boundsH; - scaledWidth = boundsH * aspectRatio; + scaledHeight = bounds.H; + scaledWidth = bounds.H * aspectRatio; } break; case EAspect.LetterBox: if (boundsAspectRatio <= aspectRatio) { - scaledWidth = boundsW; - scaledHeight = boundsW / aspectRatio; + scaledWidth = bounds.W; + scaledHeight = bounds.W / aspectRatio; } else { - scaledHeight = boundsH; - scaledWidth = boundsH * aspectRatio; + scaledHeight = bounds.H; + scaledWidth = bounds.H * aspectRatio; } break; default: - scaledWidth = boundsW; - scaledHeight = boundsH; - break; + return bounds; } + float left = (bounds.W - scaledWidth) / 2 + bounds.X; + float top = (bounds.H - scaledHeight) / 2 + bounds.Y; - float left = (boundsW - scaledWidth) / 2 + bounds.X; - float upper = (boundsH - scaledHeight) / 2 + bounds.Y; - - rect = new SRectF(left, upper, scaledWidth, scaledHeight, bounds.Z); + return new SRectF(left, top, scaledWidth, scaledHeight, bounds.Z); } /// diff --git a/VocaluxeLib/Menu/CStatic.cs b/VocaluxeLib/Menu/CStatic.cs index 9745933d..1c52b081 100644 --- a/VocaluxeLib/Menu/CStatic.cs +++ b/VocaluxeLib/Menu/CStatic.cs @@ -196,7 +196,7 @@ namespace VocaluxeLib.Menu SRectF rect = Rect.Scale(scale); rect.Z += zModify; if (texture != null) - CHelper.SetRect(rect, out rect, texture.OrigAspect, aspect); + rect = CHelper.FitInBounds(rect, texture.OrigAspect, aspect); var color = new SColorF(Color.R, Color.G, Color.B, Color.A * Alpha); if (Visible || forceDraw || (CBase.Settings.GetProgramState() == EProgramState.EditTheme)) diff --git a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs index 2fc8ce76..9f77bb6b 100644 --- a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs +++ b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs @@ -17,7 +17,6 @@ using System; using System.Collections.Generic; -using System.Drawing; using System.Linq; using System.Windows.Forms; using VocaluxeLib.Draw; @@ -473,8 +472,7 @@ namespace VocaluxeLib.Menu.SongMenu { if (vidtex.Color.A < 1) _CoverBig.Draw(EAspect.Crop); - SRectF rect; - CHelper.SetRect(_CoverBig.Rect, out rect, vidtex.OrigAspect, EAspect.Crop); + SRectF rect = CHelper.FitInBounds(_CoverBig.Rect, vidtex.OrigAspect, EAspect.Crop); CBase.Drawing.DrawTexture(vidtex, rect); CBase.Drawing.DrawTextureReflection(vidtex, rect, vidtex.Color, _CoverBig.ReflectionSpace, _CoverBig.ReflectionHeight);