Extract load screen sheet handling into a superclass.
This commit is contained in:
@@ -22,11 +22,11 @@ namespace OpenRA.Mods.Common.LoadScreens
|
||||
public class BlankLoadScreen : ILoadScreen
|
||||
{
|
||||
public LaunchArguments Launch;
|
||||
ModData modData;
|
||||
protected ModData ModData { get; private set; }
|
||||
|
||||
public virtual void Init(ModData modData, Dictionary<string, string> info)
|
||||
{
|
||||
this.modData = modData;
|
||||
ModData = modData;
|
||||
}
|
||||
|
||||
public virtual void Display()
|
||||
@@ -110,14 +110,14 @@ namespace OpenRA.Mods.Common.LoadScreens
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public bool BeforeLoad()
|
||||
public virtual bool BeforeLoad()
|
||||
{
|
||||
// If a ModContent section is defined then we need to make sure that the
|
||||
// required content is installed or switch to the defined content installer.
|
||||
if (!modData.Manifest.Contains<ModContent>())
|
||||
if (!ModData.Manifest.Contains<ModContent>())
|
||||
return true;
|
||||
|
||||
var content = modData.Manifest.Get<ModContent>();
|
||||
var content = ModData.Manifest.Get<ModContent>();
|
||||
var contentInstalled = content.Packages
|
||||
.Where(p => p.Value.Required)
|
||||
.All(p => p.Value.TestFiles.All(f => File.Exists(Platform.ResolvePath(f))));
|
||||
@@ -125,7 +125,7 @@ namespace OpenRA.Mods.Common.LoadScreens
|
||||
if (contentInstalled)
|
||||
return true;
|
||||
|
||||
Game.InitializeMod(content.ContentInstallerMod, new Arguments(new[] { "Content.Mod=" + modData.Manifest.Id }));
|
||||
Game.InitializeMod(content.ContentInstallerMod, new Arguments(new[] { "Content.Mod=" + ModData.Manifest.Id }));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,68 +9,48 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Widgets;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.LoadScreens
|
||||
{
|
||||
public sealed class LogoStripeLoadScreen : BlankLoadScreen
|
||||
public sealed class LogoStripeLoadScreen : SheetLoadScreen
|
||||
{
|
||||
Stopwatch lastUpdate = Stopwatch.StartNew();
|
||||
Renderer r;
|
||||
|
||||
Rectangle stripeRect;
|
||||
float2 logoPos;
|
||||
Sheet sheet;
|
||||
Sprite stripe, logo;
|
||||
|
||||
Sheet lastSheet;
|
||||
Size lastResolution;
|
||||
|
||||
string[] messages = { "Loading..." };
|
||||
|
||||
public override void Init(ModData modData, Dictionary<string, string> info)
|
||||
{
|
||||
base.Init(modData, info);
|
||||
|
||||
// Avoid standard loading mechanisms so we
|
||||
// can display the loadscreen as early as possible
|
||||
r = Game.Renderer;
|
||||
if (r == null)
|
||||
return;
|
||||
|
||||
if (info.ContainsKey("Text"))
|
||||
messages = info["Text"].Split(',');
|
||||
|
||||
if (info.ContainsKey("Image"))
|
||||
{
|
||||
using (var stream = modData.DefaultFileSystem.Open(info["Image"]))
|
||||
sheet = new Sheet(SheetType.BGRA, stream);
|
||||
|
||||
logo = new Sprite(sheet, new Rectangle(0, 0, 256, 256), TextureChannel.RGBA);
|
||||
stripe = new Sprite(sheet, new Rectangle(258, 0, 253, 256), TextureChannel.RGBA);
|
||||
stripeRect = new Rectangle(0, r.Resolution.Height / 2 - 128, r.Resolution.Width, 256);
|
||||
logoPos = new float2(r.Resolution.Width / 2 - 128, r.Resolution.Height / 2 - 128);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Display()
|
||||
public override void DisplayInner(Renderer r, Sheet s)
|
||||
{
|
||||
if (r == null)
|
||||
return;
|
||||
if (s != lastSheet)
|
||||
{
|
||||
lastSheet = s;
|
||||
logo = new Sprite(s, new Rectangle(0, 0, 256, 256), TextureChannel.RGBA);
|
||||
stripe = new Sprite(s, new Rectangle(258, 0, 253, 256), TextureChannel.RGBA);
|
||||
}
|
||||
|
||||
// Update text at most every 0.5 seconds
|
||||
if (lastUpdate.Elapsed.TotalSeconds < 0.5)
|
||||
return;
|
||||
|
||||
if (r.Fonts == null)
|
||||
return;
|
||||
|
||||
lastUpdate.Restart();
|
||||
var text = messages.Random(Game.CosmeticRandom);
|
||||
var textSize = r.Fonts["Bold"].Measure(text);
|
||||
|
||||
r.BeginUI();
|
||||
if (r.Resolution != lastResolution)
|
||||
{
|
||||
lastResolution = r.Resolution;
|
||||
stripeRect = new Rectangle(0, lastResolution.Height / 2 - 128, lastResolution.Width, 256);
|
||||
logoPos = new float2(lastResolution.Width / 2 - 128, lastResolution.Height / 2 - 128);
|
||||
}
|
||||
|
||||
if (stripe != null)
|
||||
WidgetUtils.FillRectWithSprite(stripeRect, stripe);
|
||||
@@ -78,16 +58,12 @@ namespace OpenRA.Mods.Common.LoadScreens
|
||||
if (logo != null)
|
||||
r.RgbaSpriteRenderer.DrawSprite(logo, logoPos);
|
||||
|
||||
r.Fonts["Bold"].DrawText(text, new float2(r.Resolution.Width - textSize.X - 20, r.Resolution.Height - textSize.Y - 20), Color.White);
|
||||
r.EndFrame(new NullInputHandler());
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && sheet != null)
|
||||
sheet.Dispose();
|
||||
|
||||
base.Dispose(disposing);
|
||||
if (r.Fonts != null)
|
||||
{
|
||||
var text = messages.Random(Game.CosmeticRandom);
|
||||
var textSize = r.Fonts["Bold"].Measure(text);
|
||||
r.Fonts["Bold"].DrawText(text, new float2(r.Resolution.Width - textSize.X - 20, r.Resolution.Height - textSize.Y - 20), Color.White);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
@@ -20,35 +19,32 @@ using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.LoadScreens
|
||||
{
|
||||
public sealed class ModContentLoadScreen : ILoadScreen
|
||||
public sealed class ModContentLoadScreen : SheetLoadScreen
|
||||
{
|
||||
Sprite sprite;
|
||||
Rectangle bounds;
|
||||
|
||||
public void Init(ModData modData, Dictionary<string, string> info)
|
||||
{
|
||||
var res = Game.Renderer.Resolution;
|
||||
bounds = new Rectangle(0, 0, res.Width, res.Height);
|
||||
Sheet lastSheet;
|
||||
Size lastResolution;
|
||||
|
||||
using (var stream = modData.DefaultFileSystem.Open(info["Image"]))
|
||||
public override void DisplayInner(Renderer r, Sheet s)
|
||||
{
|
||||
if (s != lastSheet)
|
||||
{
|
||||
var sheet = new Sheet(SheetType.BGRA, stream);
|
||||
sprite = new Sprite(sheet, new Rectangle(0, 0, 1024, 480), TextureChannel.RGBA);
|
||||
lastSheet = s;
|
||||
sprite = new Sprite(s, new Rectangle(0, 0, 1024, 480), TextureChannel.RGBA);
|
||||
}
|
||||
}
|
||||
|
||||
public void Display()
|
||||
{
|
||||
var r = Game.Renderer;
|
||||
if (r == null)
|
||||
return;
|
||||
if (r.Resolution != lastResolution)
|
||||
{
|
||||
lastResolution = r.Resolution;
|
||||
bounds = new Rectangle(0, 0, lastResolution.Width, lastResolution.Height);
|
||||
}
|
||||
|
||||
r.BeginUI();
|
||||
WidgetUtils.FillRectWithSprite(bounds, sprite);
|
||||
r.EndFrame(new NullInputHandler());
|
||||
}
|
||||
|
||||
public void StartGame(Arguments args)
|
||||
public override void StartGame(Arguments args)
|
||||
{
|
||||
var modId = args.GetValue("Content.Mod", null);
|
||||
Manifest selectedMod;
|
||||
@@ -90,13 +86,7 @@ namespace OpenRA.Mods.Common.LoadScreens
|
||||
.All(p => p.Value.TestFiles.All(f => File.Exists(Platform.ResolvePath(f))));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (sprite != null)
|
||||
sprite.Sheet.Dispose();
|
||||
}
|
||||
|
||||
public bool BeforeLoad()
|
||||
public override bool BeforeLoad()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
62
OpenRA.Mods.Common/LoadScreens/SheetLoadScreen.cs
Normal file
62
OpenRA.Mods.Common/LoadScreens/SheetLoadScreen.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using OpenRA.Graphics;
|
||||
|
||||
namespace OpenRA.Mods.Common.LoadScreens
|
||||
{
|
||||
public abstract class SheetLoadScreen : BlankLoadScreen
|
||||
{
|
||||
Stopwatch lastUpdate;
|
||||
|
||||
protected Dictionary<string, string> Info { get; private set; }
|
||||
Sheet sheet;
|
||||
|
||||
public override void Init(ModData modData, Dictionary<string, string> info)
|
||||
{
|
||||
base.Init(modData, info);
|
||||
Info = info;
|
||||
}
|
||||
|
||||
public abstract void DisplayInner(Renderer r, Sheet s);
|
||||
|
||||
public override void Display()
|
||||
{
|
||||
// Limit load screens to at most 5 FPS
|
||||
if (Game.Renderer == null || (lastUpdate != null && lastUpdate.Elapsed.TotalSeconds < 0.2))
|
||||
return;
|
||||
|
||||
// Start the timer on the first render
|
||||
if (lastUpdate == null)
|
||||
lastUpdate = Stopwatch.StartNew();
|
||||
|
||||
if (sheet == null && Info.ContainsKey("Image"))
|
||||
using (var stream = ModData.DefaultFileSystem.Open(Info["Image"]))
|
||||
sheet = new Sheet(SheetType.BGRA, stream);
|
||||
|
||||
Game.Renderer.BeginUI();
|
||||
DisplayInner(Game.Renderer, sheet);
|
||||
Game.Renderer.EndFrame(new NullInputHandler());
|
||||
|
||||
lastUpdate.Restart();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && sheet != null)
|
||||
sheet.Dispose();
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user