(41c5679ec) Resize fonts according to resolution (TODO: test on resolutions larger than 1080p)

This commit is contained in:
Joonas Rikkonen
2019-04-17 12:43:29 +03:00
parent 8c8f03ae25
commit cfd4df925a
2 changed files with 85 additions and 22 deletions
@@ -7,7 +7,7 @@ using System.Xml.Linq;
namespace Barotrauma namespace Barotrauma
{ {
public class ScalableFont public class ScalableFont : IDisposable
{ {
private static List<ScalableFont> FontList = new List<ScalableFont>(); private static List<ScalableFont> FontList = new List<ScalableFont>();
private static Library Lib = null; private static Library Lib = null;
@@ -30,7 +30,7 @@ namespace Barotrauma
set set
{ {
size = value; size = value;
if (graphicsDevice!=null) RenderAtlas(graphicsDevice, charRanges, texDims, baseChar); if (graphicsDevice != null) RenderAtlas(graphicsDevice, charRanges, texDims, baseChar);
} }
} }
@@ -302,5 +302,15 @@ namespace Barotrauma
} }
return retVal; return retVal;
} }
public void Dispose()
{
FontList.Remove(this);
foreach (Texture2D texture in textures)
{
texture.Dispose();
}
textures.Clear();
}
} }
} }
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Xml.Linq; using System.Xml.Linq;
@@ -9,6 +10,8 @@ namespace Barotrauma
{ {
private Dictionary<string, GUIComponentStyle> componentStyles; private Dictionary<string, GUIComponentStyle> componentStyles;
private XElement configElement;
public ScalableFont Font { get; private set; } public ScalableFont Font { get; private set; }
public ScalableFont SmallFont { get; private set; } public ScalableFont SmallFont { get; private set; }
public ScalableFont LargeFont { get; private set; } public ScalableFont LargeFont { get; private set; }
@@ -26,6 +29,8 @@ namespace Barotrauma
{ {
componentStyles = new Dictionary<string, GUIComponentStyle>(); componentStyles = new Dictionary<string, GUIComponentStyle>();
GameMain.Instance.OnResolutionChanged += () => { RescaleFonts(); };
XDocument doc; XDocument doc;
try try
{ {
@@ -37,29 +42,11 @@ namespace Barotrauma
DebugConsole.ThrowError("Loading style \"" + file + "\" failed", e); DebugConsole.ThrowError("Loading style \"" + file + "\" failed", e);
return; return;
} }
configElement = doc.Root;
foreach (XElement subElement in doc.Root.Elements()) foreach (XElement subElement in doc.Root.Elements())
{ {
switch (subElement.Name.ToString().ToLowerInvariant()) switch (subElement.Name.ToString().ToLowerInvariant())
{ {
case "font":
Font = new ScalableFont(subElement, graphicsDevice);
break;
case "smallfont":
SmallFont = new ScalableFont(subElement, graphicsDevice);
break;
case "largefont":
LargeFont = new ScalableFont(subElement, graphicsDevice);
break;
case "objectivetitle":
ObjectiveTitleFont = new ScalableFont(subElement, graphicsDevice);
break;
case "objectivename":
ObjectiveNameFont = new ScalableFont(subElement, graphicsDevice);
break;
case "videotitle":
VideoTitleFont = new ScalableFont(subElement, graphicsDevice);
break;
case "cursor": case "cursor":
CursorSprite = new Sprite(subElement); CursorSprite = new Sprite(subElement);
break; break;
@@ -69,6 +56,24 @@ namespace Barotrauma
case "focusindicator": case "focusindicator":
FocusIndicator = new SpriteSheet(subElement); FocusIndicator = new SpriteSheet(subElement);
break; break;
case "font":
Font = LoadFont(subElement, graphicsDevice);
break;
case "smallfont":
SmallFont = LoadFont(subElement, graphicsDevice);
break;
case "largefont":
LargeFont = LoadFont(subElement, graphicsDevice);
break;
case "objectivetitle":
ObjectiveTitleFont = LoadFont(subElement, graphicsDevice);
break;
case "objectivename":
ObjectiveNameFont = LoadFont(subElement, graphicsDevice);
break;
case "videotitle":
VideoTitleFont = LoadFont(subElement, graphicsDevice);
break;
default: default:
GUIComponentStyle componentStyle = new GUIComponentStyle(subElement); GUIComponentStyle componentStyle = new GUIComponentStyle(subElement);
componentStyles.Add(subElement.Name.ToString().ToLowerInvariant(), componentStyle); componentStyles.Add(subElement.Name.ToString().ToLowerInvariant(), componentStyle);
@@ -77,6 +82,54 @@ namespace Barotrauma
} }
} }
private void RescaleFonts()
{
foreach (XElement subElement in configElement.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "font":
Font.Size = GetFontSize(subElement);
break;
case "smallfont":
SmallFont.Size = GetFontSize(subElement);
break;
case "largefont":
LargeFont.Size = GetFontSize(subElement);
break;
case "objectivetitle":
ObjectiveTitleFont.Size = GetFontSize(subElement);
break;
case "objectivename":
ObjectiveNameFont.Size = GetFontSize(subElement);
break;
case "videotitle":
VideoTitleFont.Size = GetFontSize(subElement);
break;
}
}
}
private ScalableFont LoadFont(XElement element, GraphicsDevice graphicsDevice)
{
string file = element.GetAttributeString("file", "");
uint size = GetFontSize(element);
return new ScalableFont(file, size, graphicsDevice);
}
private uint GetFontSize(XElement element)
{
foreach (XElement subElement in element.Elements())
{
Point maxResolution = subElement.GetAttributePoint("maxresolution", new Point(int.MaxValue, int.MaxValue));
if (GameMain.GraphicsWidth <= maxResolution.X && GameMain.GraphicsHeight <= maxResolution.Y)
{
return (uint)subElement.GetAttributeInt("size", 14);
}
}
return 14;
}
public GUIComponentStyle GetComponentStyle(string name) public GUIComponentStyle GetComponentStyle(string name)
{ {
componentStyles.TryGetValue(name.ToLowerInvariant(), out GUIComponentStyle style); componentStyles.TryGetValue(name.ToLowerInvariant(), out GUIComponentStyle style);