embed plugin loader directly into the launcher

This commit is contained in:
zznty
2022-10-29 01:50:14 +07:00
parent 7204815c0c
commit 66d3dc2ead
53 changed files with 5689 additions and 10 deletions

View File

@@ -0,0 +1,138 @@
using System.Reflection;
using Sandbox.Game;
using VRage;
namespace PluginLoader.GUI;
public class SplashScreen : Form
{
private const float barWidth = 0.98f; // 98% of width
private const float barHeight = 0.06f; // 6% of height
private readonly RectangleF bar;
private readonly PictureBox gifBox;
private readonly bool invalid;
private readonly Label lbl;
private float barValue = float.NaN;
public SplashScreen()
{
Image gif;
if (Application.OpenForms.Count == 0 || !TryLoadImage(out gif))
{
invalid = true;
return;
}
var defaultSplash = Application.OpenForms[0];
Size = defaultSplash.Size;
ClientSize = defaultSplash.ClientSize;
MyVRage.Platform.Windows.HideSplashScreen();
Name = "SplashScreenPluginLoader";
TopMost = true;
FormBorderStyle = FormBorderStyle.None;
var barSize = new SizeF(Size.Width * barWidth, Size.Height * barHeight);
var padding = (1 - barWidth) * Size.Width * 0.5f;
var barStart = new PointF(padding, Size.Height - barSize.Height - padding);
bar = new(barStart, barSize);
var lblFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold);
lbl = new()
{
Name = "PluginLoaderInfo",
Font = lblFont,
BackColor = Color.Black,
ForeColor = Color.White,
MaximumSize = Size,
Size = new(Size.Width, lblFont.Height),
TextAlign = ContentAlignment.MiddleCenter,
Location = new(0, (int)(barStart.Y - lblFont.Height - 1))
};
Controls.Add(lbl);
gifBox = new()
{
Name = "PluginLoaderAnimation",
Image = gif,
Size = Size,
AutoSize = false,
SizeMode = PictureBoxSizeMode.StretchImage
};
Controls.Add(gifBox);
gifBox.Paint += OnPictureBoxDraw;
CenterToScreen();
Show();
ForceUpdate();
}
public object GameInfo { get; private set; }
private bool TryLoadImage(out Image img)
{
try
{
var myAssembly = Assembly.GetExecutingAssembly();
var myStream = myAssembly.GetManifestResourceStream("PluginLoader.splash.gif");
img = new Bitmap(myStream);
return true;
}
catch
{
img = null;
return false;
}
}
public void SetText(string msg)
{
if (invalid)
return;
lbl.Text = msg;
barValue = float.NaN;
gifBox.Invalidate();
ForceUpdate();
}
public void SetBarValue(float percent = float.NaN)
{
if (invalid)
return;
barValue = percent;
gifBox.Invalidate();
ForceUpdate();
}
private void ForceUpdate()
{
Application.DoEvents();
}
private void OnPictureBoxDraw(object sender, PaintEventArgs e)
{
if (!float.IsNaN(barValue))
{
var graphics = e.Graphics;
graphics.FillRectangle(Brushes.DarkSlateGray, bar);
graphics.FillRectangle(Brushes.White, new RectangleF(bar.Location, new(bar.Width * barValue, bar.Height)));
}
}
public void Delete()
{
if (invalid)
return;
gifBox.Paint -= OnPictureBoxDraw;
Close();
Dispose();
ForceUpdate();
MyVRage.Platform.Windows.ShowSplashScreen(MyPerGameSettings.BasicGameInfo.SplashScreenImage, new(0.7f, 0.7f));
}
}