Files
2026-10-11 23:23:20 +00:00

200 lines
9.8 KiB
C#

using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace UCVL.VPNPlus;
// Decoration only. Text, official logos, controls, counters and plots are never
// baked into this asset, and none of the theme methods access network state.
internal static class ScenicTheme
{
internal const int HeaderHeight = 108;
private static readonly Lazy<Image?> Landscape = new(() =>
{
// Embedded in the existing EXE payload: an older updater cannot omit
// this decoration by retaining an old external-assets allowlist.
using var stream = typeof(ScenicTheme).Assembly.GetManifestResourceStream("UCVL.VPNPlus.assets.landscape-header-v1.png");
if (stream is null) return null;
using var source = Image.FromStream(stream);
return new Bitmap(source);
});
internal static bool ArtworkAvailable => Landscape.Value is not null;
private static readonly Lazy<Image?> LoginScene = new(() =>
{
using var stream = typeof(ScenicTheme).Assembly.GetManifestResourceStream("UCVL.VPNPlus.assets.login-cloud-scene-v1.jpg");
if (stream is null) return null;
using var source = Image.FromStream(stream);
return new Bitmap(source);
});
internal static bool LoginSceneAvailable => LoginScene.Value is not null;
internal static (Rectangle Destination, Rectangle Source) LoginSceneLayout(Size size)
{
// The supplied original stays byte-for-byte intact. Exclude only its
// embedded side letterboxes while drawing; do not stretch the people.
var source = new Rectangle(352, 0, 1918, 1080);
if (size.Width <= 0 || size.Height <= 0) return (Rectangle.Empty, source);
var height = Math.Max(1, Math.Min(size.Height * 2 / 3,
(int)Math.Round(size.Width * source.Height / (double)source.Width)));
source.Height = Math.Max(1, Math.Min(source.Height,
(int)Math.Round(source.Width * height / (double)size.Width)));
return (new Rectangle(0, size.Height - height, size.Width, height), source);
}
internal static void Art(Graphics g, Rectangle bounds, float opacity = 1)
{
if (bounds.Width <= 0 || bounds.Height <= 0 || Landscape.Value is not { } image) return;
using var attributes = new ImageAttributes();
attributes.SetColorMatrix(new ColorMatrix { Matrix33 = opacity });
var interpolation = g.InterpolationMode;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, bounds, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
g.InterpolationMode = interpolation;
}
internal static void Backdrop(Graphics g, Size size, int dpi, bool header = false, bool footer = false, bool login = false)
{
if (size.Width <= 0 || size.Height <= 0) return;
float scale = dpi / 96f;
int S(int value) => (int)Math.Round(value * scale);
g.Clear(Brand.Surface);
if (footer)
{
Art(g, new Rectangle(0, -S(32), size.Width, size.Height + S(72)), .25f);
return;
}
if (login && Landscape.Value is { } landscape)
{
// Only the login canvas gets this centered landscape. Crop out
// the sky/sun so the title artwork is not repeated. It is painted
// in parent coordinates, including beneath the translucent card.
// Center the artwork behind the whole login card, not below its
// upper fields. The card and scene share parent coordinates.
var height = Math.Min(size.Height, Math.Max(S(440), size.Width / 3));
var area = new Rectangle(0, (size.Height - height) / 2, size.Width, height);
using var attributes = new ImageAttributes();
attributes.SetColorMatrix(new ColorMatrix { Matrix33 = .74f });
g.DrawImage(landscape, area, 0, landscape.Height / 3, landscape.Width,
landscape.Height * 2 / 3, GraphicsUnit.Pixel, attributes);
var veil = new Rectangle(area.X, area.Y, area.Width, Math.Min(S(48), height));
using var blend = new LinearGradientBrush(veil, Brand.Surface, Color.FromArgb(0, Brand.Surface), 90f)
{ WrapMode = WrapMode.TileFlipXY };
g.FillRectangle(blend, veil);
var tail = new Rectangle(area.X, area.Bottom - Math.Min(S(64), height), area.Width, Math.Min(S(64), height));
using var lowerMist = new LinearGradientBrush(tail, Color.FromArgb(0, Brand.Surface), Brand.Surface, 90f)
{ WrapMode = WrapMode.TileFlipXY };
g.FillRectangle(lowerMist, tail);
}
if (login && LoginScene.Value is { } scene)
{
// Decoration belongs only to the lower login canvas. The actual
// form fields remain separate, opaque controls on the light card.
var (area, source) = LoginSceneLayout(size);
using var attributes = new ImageAttributes();
var interpolation = g.InterpolationMode;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
void Segment(int y, int height, float alpha)
{
attributes.SetColorMatrix(new ColorMatrix { Matrix33 = alpha });
g.DrawImage(scene, new Rectangle(area.X, area.Y + y, area.Width, height), source.X,
source.Y + y * source.Height / (float)area.Height, source.Width,
height * source.Height / (float)area.Height, GraphicsUnit.Pixel, attributes);
}
// Fade the illustration itself, preserving the mountains beneath
// it. An opaque blue veil would leave a hard horizontal seam.
var upper = Math.Min(S(96), area.Height / 2);
var lower = Math.Min(S(32), area.Height / 2);
var step = Math.Max(1, S(2));
for (var y = 0; y < upper; y += step)
{
var height = Math.Min(step, upper - y);
Segment(y, height, .72f * (y + height / 2f) / upper);
}
if (area.Height > upper + lower) Segment(upper, area.Height - upper - lower, .72f);
for (var y = area.Height - lower; y < area.Height; y += step)
{
var height = Math.Min(step, area.Height - y);
Segment(y, height, .72f * (area.Height - y - height / 2f) / lower);
}
g.InterpolationMode = interpolation;
}
// Same image coordinate space above and below the client title area.
var offset = header ? 0 : S(HeaderHeight);
var artHeight = S(HeaderHeight + 72);
Art(g, new Rectangle(0, -offset, size.Width, artHeight));
var fade = new Rectangle(0, artHeight - offset - S(40), size.Width, S(40) + 1);
using var mist = new LinearGradientBrush(fade, Color.FromArgb(0, Brand.Surface), Brand.Surface, 90)
{ WrapMode = WrapMode.TileFlipXY };
g.FillRectangle(mist, fade);
if (!header) Clouds(g, new Rectangle(size.Width - S(240), S(8), S(105), S(46)), .3f);
}
internal static void Clouds(Graphics g, Rectangle area, float opacity)
{
if (area.Width <= 0 || area.Height <= 0) return;
var state = g.Save();
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TranslateTransform(area.X, area.Y);
g.ScaleTransform(area.Width / 140f, area.Height / 60f);
using var gold = new Pen(Color.FromArgb((int)(255 * opacity), 206, 164, 86), .9f);
g.DrawBezier(gold, 0, 44, 38, 66, 66, 21, 97, 40);
g.DrawBezier(gold, 20, 47, 45, 60, 62, 32, 83, 32);
g.DrawArc(gold, 72, 10, 37, 28, 35, 300);
g.DrawArc(gold, 80, 15, 23, 18, 25, 285);
g.DrawBezier(gold, 97, 40, 111, 4, 132, 17, 140, 32);
g.Restore(state);
}
}
internal sealed class ScenicPanel : Panel
{
private Bitmap? _loginBackdrop;
private int _loginBackdropDpi;
private bool _loginBackdropFooter;
// Only the account-content subtree is composed. Do not compose the entire
// top-level window or delay native caption/resize/input handling.
internal bool ComposeChildren { get; set; }
internal bool FooterArtwork { get; set; }
internal bool LoginArtwork { get; set; }
protected override CreateParams CreateParams
{
get
{
var parameters = base.CreateParams;
if (ComposeChildren) parameters.ExStyle |= 0x02000000; // WS_EX_COMPOSITED
return parameters;
}
}
internal ScenicPanel() { DoubleBuffered = true; ResizeRedraw = true; BackColor = Brand.Surface; }
protected override void OnPaintBackground(PaintEventArgs e)
{
if (!LoginArtwork)
{
ScenicTheme.Backdrop(e.Graphics, ClientSize, DeviceDpi, footer: FooterArtwork);
return;
}
if (Width <= 0 || Height <= 0) return;
// Transparent labels ask their ancestors to repaint. Cache decoration
// only (never child text/account data), so each label does not resample
// a full-window landscape during the first login frame.
if (_loginBackdrop is null || _loginBackdrop.Size != ClientSize
|| _loginBackdropDpi != DeviceDpi || _loginBackdropFooter != FooterArtwork)
{
using var next = new Bitmap(Width, Height);
using (var graphics = Graphics.FromImage(next))
ScenicTheme.Backdrop(graphics, ClientSize, DeviceDpi, footer: FooterArtwork, login: true);
var previous = _loginBackdrop;
_loginBackdrop = new Bitmap(next);
_loginBackdropDpi = DeviceDpi;
_loginBackdropFooter = FooterArtwork;
previous?.Dispose();
}
e.Graphics.DrawImageUnscaled(_loginBackdrop, Point.Empty);
}
protected override void Dispose(bool disposing)
{
if (disposing) { _loginBackdrop?.Dispose(); _loginBackdrop = null; }
base.Dispose(disposing);
}
}