Skip to content

Commit

Permalink
Fixes KSP-KOS#1956 and changes font search order.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dunbaratu committed May 7, 2017
1 parent 00cf2a5 commit 0c70a60
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
19 changes: 17 additions & 2 deletions src/kOS/Module/AssetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -120,14 +120,29 @@ private string MakeKey(string name, int size)
/// if it's not monospaced.</param>
/// <param name="doErrorMessage">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.</param>
public Font GetSystemFontByNameAndSize(string name, int size, bool checkMono, bool doErrorMessage = true)
/// <param name="doDetectedCheck">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.</param>
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.

// Make a string key from the name plus the size:
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);
}

Expand Down
40 changes: 40 additions & 0 deletions src/kOS/Module/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"<b><size=18>Terminal Font</size></b>\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 " +
"<b><color=#FFFFFF>You can choose a font using the kOS toolbar panel " +
"that appears during play <i>when you are in flight view</color></i></b>.\n" +
"\n" +
"<b><size=16>If you liked the old look:</size></b>\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;

Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/kOS/Screen/KOSTextEditPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/kOS/Screen/TermWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ 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;

private Font font;
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;
Expand Down
2 changes: 1 addition & 1 deletion src/kOS/Suffixed/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

Expand Down

0 comments on commit 0c70a60

Please sign in to comment.