From 3828b036458e9ca6ea1c8db85cfc162cd8379e5b Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Mon, 4 Sep 2017 15:27:00 -0400 Subject: [PATCH] Implement UI scaling option There are still a few minor visual glitches, but it works pretty well --- rsrc/dialogs/preferences.xml | 6 ++++-- src/game/boe.dlgutil.cpp | 10 +++++++++- src/game/boe.graphics.cpp | 8 +++++--- src/tools/prefs.hpp | 3 +++ src/tools/prefs.mac.mm | 10 ++++++++++ src/tools/prefs.win.cpp | 11 +++++++++++ 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/rsrc/dialogs/preferences.xml b/rsrc/dialogs/preferences.xml index 4802a840..4d82eebe 100644 --- a/rsrc/dialogs/preferences.xml +++ b/rsrc/dialogs/preferences.xml @@ -39,6 +39,8 @@ Make game easier (monsters much weaker) Fewer wandering monsters Skip splash screen on startup - - + Apply UI scaling + + + diff --git a/src/game/boe.dlgutil.cpp b/src/game/boe.dlgutil.cpp index 7968ca95..e0cae6d0 100644 --- a/src/game/boe.dlgutil.cpp +++ b/src/game/boe.dlgutil.cpp @@ -1139,6 +1139,11 @@ static bool prefs_event_filter (cDialog& me, std::string id, eKeyMod) { if(dynamic_cast(me["resethelp"]).getState() == led_red) { reset_help = true; } + cLed& ui_scale = dynamic_cast(me["scaleui"]); + if(ui_scale.getState() == led_off) + set_pref("UIScale", 1.0); + else if(ui_scale.getState() == led_red) + set_pref("UIScale", 2.0); } save_prefs(reset_help); return true; @@ -1202,12 +1207,15 @@ void pick_preferences() { break; } + float ui_scale = get_float_pref("UIScale", 1.0); + dynamic_cast(prefsDlog["scaleui"]).setState(ui_scale == 1.0 ? led_off : (ui_scale == 2.0 ? led_red : led_green)); + void (*give_help)(short,short,cDialog&) = ::give_help; int store_display_mode = get_int_pref("DisplayMode"); prefsDlog.run(std::bind(give_help, 55, 0, std::ref(prefsDlog))); - if(get_int_pref("DisplayMode") != store_display_mode) + if(get_int_pref("DisplayMode") != store_display_mode || get_float_pref("UIScale") != ui_scale) changed_display_mode = true; } diff --git a/src/game/boe.graphics.cpp b/src/game/boe.graphics.cpp index f243e99d..de9d2382 100644 --- a/src/game/boe.graphics.cpp +++ b/src/game/boe.graphics.cpp @@ -131,7 +131,9 @@ void adjust_window_mode() { hideMenuBar(); int menubarHeight = getMenubarHeight(); bool firstTime = !mainPtr.isOpen(); - float width = 605, height = 430; + float ui_scale = get_float_pref("UIScale", 1.0); + if(ui_scale < 0.1) ui_scale = 1.0; + float width = 605 * ui_scale, height = 430 * ui_scale; location ul; // TODO: Make display_mode an enum @@ -165,8 +167,8 @@ void adjust_window_mode() { sf::FloatRect mainPort; mainPort.left = float(ul.x) / windRect.width(); mainPort.top = float(ul.y) / windRect.height(); - mainPort.width = width / windRect.width(); - mainPort.height = height / windRect.height(); + mainPort.width = ui_scale * width / windRect.width(); + mainPort.height = ui_scale * height / windRect.height(); mainView.setViewport(mainPort); #ifndef __APPLE__ // This overrides Dock icon on OSX, which isn't what we want at all diff --git a/src/tools/prefs.hpp b/src/tools/prefs.hpp index 30a6b837..cc2c0356 100644 --- a/src/tools/prefs.hpp +++ b/src/tools/prefs.hpp @@ -19,6 +19,9 @@ bool get_bool_pref(std::string keypath, bool fallback = false); void set_pref(std::string keypath, int value); int get_int_pref(std::string keypath, int fallback = 0); +void set_pref(std::string keypath, double value); +double get_float_pref(std::string keypath, double fallback = 0); + void append_iarray_pref(std::string keypath, int value); std::vector get_iarray_pref(std::string keypath); diff --git a/src/tools/prefs.mac.mm b/src/tools/prefs.mac.mm index 2523f93c..5334b03f 100644 --- a/src/tools/prefs.mac.mm +++ b/src/tools/prefs.mac.mm @@ -37,6 +37,16 @@ int get_int_pref(std::string keypath, int fallback) { return fallback; } +void set_pref(std::string keypath, double value) { + [[NSUserDefaults standardUserDefaults] setDouble: value forKey: convertKey(keypath)]; +} + +double get_float_pref(std::string keypath, double fallback) { + id val = [[NSUserDefaults standardUserDefaults] objectForKey: convertKey(keypath)]; + if([val isKindOfClass: [NSNumber class]]) return [val doubleValue]; + return fallback; +} + void append_iarray_pref(std::string keypath, int value) { NSString* key = convertKey(keypath); NSArray* list = [[NSUserDefaults standardUserDefaults] arrayForKey: key]; diff --git a/src/tools/prefs.win.cpp b/src/tools/prefs.win.cpp index 0d159e95..ba81cec6 100644 --- a/src/tools/prefs.win.cpp +++ b/src/tools/prefs.win.cpp @@ -35,6 +35,17 @@ int get_int_pref(std::string keypath, int fallback) { return fallback; } +void set_pref(std::string keypath, double value) { + prefsDirty = true; + prefs[keypath] = value; +} + +double get_float_pref(std::string keypath, double fallback) { + if(prefs.find(keypath) == prefs.end()) return fallback; + if(prefs[keypath].type() == typeid(double)) return boost::any_cast(prefs[keypath]); + return fallback; +} + void append_iarray_pref(std::string keypath, int value) { prefsDirty = true; if(prefs.find(keypath) == prefs.end() || prefs[keypath].type() != typeid(iarray))