Extract load screen sheet handling into a superclass.

This commit is contained in:
Paul Chote
2020-01-26 16:11:37 +00:00
committed by abcdefg30
parent 57a8cf7a59
commit 518450cd8a
5 changed files with 172 additions and 161 deletions

View File

@@ -10,108 +10,101 @@
#endregion
using System.Collections.Generic;
using System.Diagnostics;
using OpenRA.Graphics;
using OpenRA.Mods.Common.LoadScreens;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc
{
public sealed class CncLoadScreen : BlankLoadScreen
public sealed class CncLoadScreen : SheetLoadScreen
{
readonly NullInputHandler nih = new NullInputHandler();
Dictionary<string, string> loadInfo;
Stopwatch loadTimer = Stopwatch.StartNew();
Sheet sheet;
Sprite[] border;
int loadTick;
float2 nodPos, gdiPos, evaPos;
Sprite nodLogo, gdiLogo, evaLogo, brightBlock, dimBlock;
Sprite[] border;
float2 nodPos, gdiPos, evaPos;
Rectangle bounds;
Renderer r;
SpriteFont loadingFont, versionFont;
string loadingText, versionText;
float2 loadingPos, versionPos;
Sheet lastSheet;
Size lastResolution;
IReadOnlyDictionary<string, SpriteFont> lastFonts;
public override void Init(ModData modData, Dictionary<string, string> info)
{
base.Init(modData, info);
loadInfo = info;
// Avoid standard loading mechanisms so we
// can display loadscreen as early as possible
r = Game.Renderer;
if (r == null) return;
using (var stream = modData.DefaultFileSystem.Open(info["Image"]))
sheet = new Sheet(SheetType.BGRA, stream);
var res = r.Resolution;
bounds = new Rectangle(0, 0, res.Width, res.Height);
border = new[]
{
new Sprite(sheet, new Rectangle(129, 129, 32, 32), TextureChannel.RGBA),
new Sprite(sheet, new Rectangle(161, 129, 62, 32), TextureChannel.RGBA),
new Sprite(sheet, new Rectangle(223, 129, 32, 32), TextureChannel.RGBA),
new Sprite(sheet, new Rectangle(129, 161, 32, 62), TextureChannel.RGBA),
null,
new Sprite(sheet, new Rectangle(223, 161, 32, 62), TextureChannel.RGBA),
new Sprite(sheet, new Rectangle(129, 223, 32, 32), TextureChannel.RGBA),
new Sprite(sheet, new Rectangle(161, 223, 62, 32), TextureChannel.RGBA),
new Sprite(sheet, new Rectangle(223, 223, 32, 32), TextureChannel.RGBA)
};
nodLogo = new Sprite(sheet, new Rectangle(0, 256, 256, 256), TextureChannel.RGBA);
gdiLogo = new Sprite(sheet, new Rectangle(256, 256, 256, 256), TextureChannel.RGBA);
evaLogo = new Sprite(sheet, new Rectangle(769, 320, 128, 64), TextureChannel.RGBA);
nodPos = new float2(bounds.Width / 2 - 384, bounds.Height / 2 - 128);
gdiPos = new float2(bounds.Width / 2 + 128, bounds.Height / 2 - 128);
evaPos = new float2(bounds.Width - 43 - 128, 43);
brightBlock = new Sprite(sheet, new Rectangle(777, 385, 16, 35), TextureChannel.RGBA);
dimBlock = new Sprite(sheet, new Rectangle(794, 385, 16, 35), TextureChannel.RGBA);
versionText = modData.Manifest.Metadata.Version;
}
object rendererFonts;
SpriteFont loadingFont, versionFont;
string loadingText, versionText;
float2 loadingPos, versionPos;
public override void Display()
public override void DisplayInner(Renderer r, Sheet s)
{
if (r == null || loadTimer.Elapsed.TotalSeconds < 0.25)
return;
if (s != lastSheet)
{
lastSheet = s;
loadTimer.Restart();
border = new[]
{
new Sprite(s, new Rectangle(129, 129, 32, 32), TextureChannel.RGBA),
new Sprite(s, new Rectangle(161, 129, 62, 32), TextureChannel.RGBA),
new Sprite(s, new Rectangle(223, 129, 32, 32), TextureChannel.RGBA),
new Sprite(s, new Rectangle(129, 161, 32, 62), TextureChannel.RGBA),
null,
new Sprite(s, new Rectangle(223, 161, 32, 62), TextureChannel.RGBA),
new Sprite(s, new Rectangle(129, 223, 32, 32), TextureChannel.RGBA),
new Sprite(s, new Rectangle(161, 223, 62, 32), TextureChannel.RGBA),
new Sprite(s, new Rectangle(223, 223, 32, 32), TextureChannel.RGBA)
};
nodLogo = new Sprite(s, new Rectangle(0, 256, 256, 256), TextureChannel.RGBA);
gdiLogo = new Sprite(s, new Rectangle(256, 256, 256, 256), TextureChannel.RGBA);
evaLogo = new Sprite(s, new Rectangle(769, 320, 128, 64), TextureChannel.RGBA);
brightBlock = new Sprite(s, new Rectangle(777, 385, 16, 35), TextureChannel.RGBA);
dimBlock = new Sprite(s, new Rectangle(794, 385, 16, 35), TextureChannel.RGBA);
}
if (r.Resolution != lastResolution)
{
lastResolution = r.Resolution;
bounds = new Rectangle(0, 0, lastResolution.Width, lastResolution.Height);
nodPos = new float2(bounds.Width / 2 - 384, bounds.Height / 2 - 128);
gdiPos = new float2(bounds.Width / 2 + 128, bounds.Height / 2 - 128);
evaPos = new float2(bounds.Width - 43 - 128, 43);
}
var barY = bounds.Height - 78;
// The fonts dictionary may change when switching between the mod and content installer
if (r.Fonts != lastFonts)
{
lastFonts = r.Fonts;
loadingFont = lastFonts["BigBold"];
loadingText = Info["Text"];
loadingPos = new float2((bounds.Width - loadingFont.Measure(loadingText).X) / 2, barY);
versionFont = lastFonts["Regular"];
var versionSize = versionFont.Measure(versionText);
versionPos = new float2(bounds.Width - 107 - versionSize.X / 2, 115 - versionSize.Y / 2);
}
loadTick = ++loadTick % 8;
r.BeginUI();
r.RgbaSpriteRenderer.DrawSprite(gdiLogo, gdiPos);
r.RgbaSpriteRenderer.DrawSprite(nodLogo, nodPos);
r.RgbaSpriteRenderer.DrawSprite(evaLogo, evaPos);
WidgetUtils.DrawPanel(bounds, border);
var barY = bounds.Height - 78;
// The fonts dictionary may change when switching between the mod and content installer
if (r.Fonts != rendererFonts)
{
rendererFonts = r.Fonts;
loadingFont = r.Fonts["BigBold"];
loadingText = loadInfo["Text"];
loadingPos = new float2((bounds.Width - loadingFont.Measure(loadingText).X) / 2, barY);
versionFont = r.Fonts["Regular"];
var versionSize = versionFont.Measure(versionText);
versionPos = new float2(bounds.Width - 107 - versionSize.X / 2, 115 - versionSize.Y / 2);
}
if (loadingFont != null)
loadingFont.DrawText(loadingText, loadingPos, Color.Gray);
if (versionFont != null)
versionFont.DrawTextWithContrast(versionText, versionPos, Color.White, Color.Black, 2);
@@ -123,16 +116,6 @@ namespace OpenRA.Mods.Cnc
r.RgbaSpriteRenderer.DrawSprite(block,
new float2(bounds.Width / 2 + 114 + i * 32 - 16, barY));
}
r.EndFrame(nih);
}
protected override void Dispose(bool disposing)
{
if (disposing && sheet != null)
sheet.Dispose();
base.Dispose(disposing);
}
}
}