From 0c70a603bd299e1aa2f2e9c2c83edd4e95830fdb Mon Sep 17 00:00:00 2001 From: Dunbaratu Date: Sat, 6 May 2017 23:50:23 -0500 Subject: [PATCH] Fixes #1956 and changes font search order. When making the font dialog, I discovered that the font search list didn't actually work properly when the first font in the list was a bogus font name like "_not_picked_". Instead of trying the next font in the list, it was just rendering everything in Arial. There are some code changes to fix that problem too in here. --- src/kOS/Module/AssetManager.cs | 19 ++++++++++++-- src/kOS/Module/Bootstrapper.cs | 40 ++++++++++++++++++++++++++++++ src/kOS/Screen/KOSTextEditPopup.cs | 5 +++- src/kOS/Screen/TermWindow.cs | 14 +++++------ src/kOS/Suffixed/Config.cs | 2 +- 5 files changed, 69 insertions(+), 11 deletions(-) diff --git a/src/kOS/Module/AssetManager.cs b/src/kOS/Module/AssetManager.cs index a4ebb976b..722abd25c 100644 --- a/src/kOS/Module/AssetManager.cs +++ b/src/kOS/Module/AssetManager.cs @@ -86,7 +86,7 @@ public void UpdateSystemFontLists() if (!FontNames.Contains(fontName)) { // Only add those fonts which pass the monospace test: - if (GetSystemFontByNameAndSize(fontName, 13, true, false) != null) + if (GetSystemFontByNameAndSize(fontName, 13, true, false, false) != null) FontNames.Add(fontName); } namesThatNoLongerExist.Remove(fontName); @@ -120,7 +120,11 @@ private string MakeKey(string name, int size) /// if it's not monospaced. /// If true, then if the checkMono check (see above) fails, a message will /// appear on screen complaining about this as it returns a null, else it will return null silently. - public Font GetSystemFontByNameAndSize(string name, int size, bool checkMono, bool doErrorMessage = true) + /// If true, then protect against trying to use un-detected font names. + /// By default, if the font name you use was not discovered when walking the list of all OS font names, this + /// method will force a null return. But if this is being called before that walk is finished, you have to + /// bypass that check to make it work at all. + public Font GetSystemFontByNameAndSize(string name, int size, bool checkMono, bool doErrorMessage = true, bool doDetectedCheck = true) { // Now that a font is asked for, now we'll lazy-load it. @@ -128,6 +132,17 @@ public Font GetSystemFontByNameAndSize(string name, int size, bool checkMono, bo string key = MakeKey(name, size); if ( (!Fonts.ContainsKey(key)) || Fonts[key] == null) { + // Font.CreateDynamicFontFromOSFont will never return null just because you + // gave it a bogus font name that doesn't exist. Instead Unity chooses + // to return the default Arial font instead when it can't find the font name. + // Because our logic requires that we try calling this method again and again + // walking through a list of fonts until we find one that works, we need this + // to return null when there's no such font. + // (Else when the first font in the list of fonts to try fails, we get a + // "success" at using Arial, instead of trying the next font.) + if (doDetectedCheck && !FontNames.Contains(name)) + return null; + Fonts[key] = Font.CreateDynamicFontFromOSFont(name, size); } diff --git a/src/kOS/Module/Bootstrapper.cs b/src/kOS/Module/Bootstrapper.cs index 28f5e937f..218b4a395 100644 --- a/src/kOS/Module/Bootstrapper.cs +++ b/src/kOS/Module/Bootstrapper.cs @@ -25,6 +25,22 @@ public class Bootstrapper : MonoBehaviour "attempt to migrate your existing scripts? See the warning regarding the boot " + "directory at http://kos.github.io/KOS_DOC/general/volumes.html#special-handling-of-files-in-the-boot-directory " + "for more details."; + private const string UNPICKED_FONT_DESC = + "Terminal Font\n" + + "\n" + + "By default, kOS will choose a font from a list of guesses, but this is less than ideal. " + + "You should pick a font choice yourself once you can during play.\n " + + "You can choose a font using the kOS toolbar panel " + + "that appears during play when you are in flight view.\n" + + "\n" + + "If you liked the old look:\n" + + "\n" + + "If you liked the old look of kOS's terminal, with its very wide boxy characters, " + + "you can have that again by downloading and installing a Commodore(tm) 64 font onto your " + + "computer and re-launching KSP. The old bitmap images were designed to mimic the " + + "character set of the old Commodore 64 computer. (We (kOS developers) considered " + + "including a Commodore 64 font in the download of kOS, but licensing terms precluded " + + "redistributing any of the fonts we found.)\n"; private bool backup = true; @@ -36,6 +52,8 @@ public void Start() BuildLogger(); CheckForLegacyArchive(); + + CheckForUnpickedFont(); var assemblies = AssemblyLoader.loadedAssemblies.Where(a => a.dllName.StartsWith("kOS.") || a.dllName.Equals("kOS") || a.dependencies.Where(d => d.name.Equals("kOS")).Any()).Select(a => a.assembly).ToArray(); AssemblyWalkAttribute.Walk(assemblies); @@ -116,6 +134,28 @@ private void CheckForLegacyBoot() ); } + private void CheckForUnpickedFont() + { + if (SafeHouse.Config.TerminalFontName.Equals("_not_chosen_yet_")) + { + PopupDialog.SpawnPopupDialog( + new MultiOptionDialog( + UNPICKED_FONT_DESC, + "kOS", + HighLogic.UISkin, + new DialogGUIButton("Okay, got it!", () => {}, true) + ), + true, + HighLogic.UISkin + ); + // Still leave the user's chosen font name as a bogus value, but + // change it to one that looks more pleasing on the button in the + // toolbar dialog. (This will also prevent this dialog box from + // ever firing off again once this value gets saved in config.xml). + SafeHouse.Config.TerminalFontName = "Choose Font"; + } + } + private void MigrateScripts() { if (backup) diff --git a/src/kOS/Screen/KOSTextEditPopup.cs b/src/kOS/Screen/KOSTextEditPopup.cs index 07e5f28b0..8edff321c 100644 --- a/src/kOS/Screen/KOSTextEditPopup.cs +++ b/src/kOS/Screen/KOSTextEditPopup.cs @@ -24,7 +24,10 @@ public class KOSTextEditPopup : KOSManagedWindow private Font font; // A list of fonts including the user's choice plus a few fallback options if the user's choice isn't working: private string[] tryFontNames = - new string[] { "_User's choice_", "Courier New Bold", "Courier Bold", "Courier New", "Courier", "Monaco", "Consolas", "Liberation Mono", "Arial" }; + new string[] { + "_User's choice_", + "Consolas Bold", "Consolas", "Monaco Bold", "Monaco", "Liberation Mono Bold", "Liberation Mono", + "Courier New Bold", "Courier Bold", "Courier New", "Courier", "Arial" }; private string prevConfigFontName = ""; private int prevConfigFontSize = 12; private Rect innerCoords; diff --git a/src/kOS/Screen/TermWindow.cs b/src/kOS/Screen/TermWindow.cs index 8c9dfb953..95c4e30fd 100644 --- a/src/kOS/Screen/TermWindow.cs +++ b/src/kOS/Screen/TermWindow.cs @@ -38,9 +38,6 @@ public class TermWindow : KOSManagedWindow , ITermWindow private bool collapseFastBeepsToOneBeep = false; // This is a setting we might want to fiddle with depending on opinion. - private KeyBinding rememberThrottleCutoffKey; - private KeyBinding rememberThrottleFullKey; - private bool allTexturesFound = true; private float cursorBlinkTime; @@ -48,13 +45,16 @@ public class TermWindow : KOSManagedWindow , ITermWindow private int fontSize; private string[] tryFontNames = { "User pick Goes Here", // overwrite this first one with the user selection - the rest are a fallback just in case - "Courier New Bold", + "Consolas Bold", // typical Windows good programming font + "Consolas", + "Monaco Bold", // typical Mac good programming font + "Monaco", + "Liberation Mono Bold", // typical Linux good programming font + "Liberation Mono", + "Courier New Bold", // The Courier ones are ugly fallbacks just in case. "Courier Bold", "Courier New", "Courier", - "Monaco", - "Consolas", - "Liberation Mono", "Arial" // very bad, proportional, but guaranteed to exist in Unity no matter what. }; private GUISkin terminalLetterSkin; diff --git a/src/kOS/Suffixed/Config.cs b/src/kOS/Suffixed/Config.cs index f42d9975e..e132a59e2 100644 --- a/src/kOS/Suffixed/Config.cs +++ b/src/kOS/Suffixed/Config.cs @@ -69,7 +69,7 @@ private void BuildValuesDictionary() AddConfigKey(PropId.TelnetPort, new ConfigKey("TelnetPort", "TPORT", "Telnet port number (must restart telnet to take effect)", 5410, 1024, 65535, typeof(int))); AddConfigKey(PropId.TelnetIPAddrString, new ConfigKey("TelnetIPAddrString", "IPADDRESS", "Telnet IP address string (must restart telnet to take effect)", "127.0.0.1", "n/a", "n/a", typeof(string))); AddConfigKey(PropId.TerminalFontDefaultSize, new ConfigKey("TerminalFontDefaultSize", "DEFAULTFONTSIZE", "Initial Terminal:CHARHEIGHT when a terminal is first opened", 12, 6, 20, typeof(int))); - AddConfigKey(PropId.TerminalFontName, new ConfigKey("TerminalFontName", "FONTNAME", "Font Name for terminal window", "Courier New Bold", "n/a", "n/a", typeof(string))); + AddConfigKey(PropId.TerminalFontName, new ConfigKey("TerminalFontName", "FONTNAME", "Font Name for terminal window", "_not_chosen_yet_", "n/a", "n/a", typeof(string))); AddConfigKey(PropId.TerminalBrightness, new ConfigKey("TerminalBrightness", "BRIGHTNESS", "Initial brightness setting for new terminals", 0.7d, 0d, 1d, typeof(double))); }