Bunch of code cleanup relating to scaling options

This commit is contained in:
Charles Dang 2017-03-14 10:43:43 +11:00
parent 06a5443851
commit fac813d24f
5 changed files with 71 additions and 71 deletions

View file

@ -16,18 +16,12 @@
#include "gui/dialogs/advanced_graphics_options.hpp"
#include "desktop/notifications.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/toggle_button.hpp"
#include "gui/widgets/window.hpp"
#include "image.hpp"
#include "preferences.hpp"
#include "formula/string_utils.hpp"
#include "utils/functional.hpp"
@ -40,65 +34,71 @@ namespace dialogs
REGISTER_DIALOG(advanced_graphics_options)
const std::vector<std::string> advanced_graphics_options::scale_cases = {"zoom", "hex"};
advanced_graphics_options::SCALING_ALGORITHM advanced_graphics_options::get_scale_pref(const std::string& pref_id)
{
SCALING_ALGORITHM algo = SCALING_ALGORITHM::LINEAR;
try {
algo = SCALING_ALGORITHM::string_to_enum(preferences::get(pref_id));
} catch (bad_enum_cast &) {
preferences::set(pref_id, algo.to_string());
}
// algo is now synced with preference, and default value of linear if something went wrong
return algo;
}
void advanced_graphics_options::setup_scale_case(const std::string & case_id, window & window)
{
std::string pref_id = "scale_" + case_id;
group<SCALING_ALGORITHM>& group = groups_[case_id];
for (size_t x = 0; x < SCALING_ALGORITHM::count; ++x) {
SCALING_ALGORITHM scale = SCALING_ALGORITHM::from_int(x);
toggle_button* button = &find_widget<toggle_button>(&window, pref_id + "_" + scale.to_string(), false);
group.add_member(button, scale);
}
group.set_member_states(get_scale_pref(pref_id));
}
void advanced_graphics_options::update_scale_case(const std::string & case_id)
{
std::string pref_id = "scale_" + case_id;
SCALING_ALGORITHM new_val = groups_[case_id].get_active_member_value();
if(new_val != get_scale_pref(pref_id)) {
image::flush_cache();
}
preferences::set(pref_id, new_val.to_string());
}
const std::vector<std::string> advanced_graphics_options::scale_cases {"zoom", "hex"};
advanced_graphics_options::advanced_graphics_options()
: groups_()
{
}
void advanced_graphics_options::pre_show(window& window)
{
for(const std::string & i : scale_cases) {
for(const std::string& i : scale_cases) {
setup_scale_case(i, window);
}
}
/*
button * defaults;
defaults = &find_widget<button>(&window,"revert_to_defaults", false);
connect_signal_mouse_left_click(*defaults, std::bind(&revert_to_default_pref_values, std::ref(window)));
*/
advanced_graphics_options::SCALING_ALGORITHM advanced_graphics_options::get_scale_pref(const std::string& pref_id)
{
SCALING_ALGORITHM algo = preferences::default_scaling_algorithm;
try {
algo = SCALING_ALGORITHM::string_to_enum(preferences::get(pref_id));
} catch(bad_enum_cast&) {
preferences::set(pref_id, algo.to_string());
}
// algo is now synced with preference, and default value set if something went wrong
return algo;
}
void advanced_graphics_options::setup_scale_case(const std::string& case_id, window& window)
{
const std::string pref_id = "scale_" + case_id;
group<SCALING_ALGORITHM>& group = groups_[case_id];
for(size_t x = 0; x < SCALING_ALGORITHM::count; ++x) {
SCALING_ALGORITHM scale = SCALING_ALGORITHM::from_int(x);
// The widget ids in advanced_graphics_options.cfg must match the enum string values for this to work.
toggle_button* button = find_widget<toggle_button>(&window, pref_id + "_" + scale.to_string(), false, true);
VALIDATE(button, _("No matching widget found for scaling option") + " " + scale.to_string());
group.add_member(button, scale);
}
group.set_member_states(get_scale_pref(pref_id));
}
void advanced_graphics_options::update_scale_case(const std::string& case_id)
{
const std::string pref_id = "scale_" + case_id;
SCALING_ALGORITHM new_val = groups_[case_id].get_active_member_value();
if(new_val != get_scale_pref(pref_id)) {
image::flush_cache();
}
preferences::set(pref_id, new_val.to_string());
}
void advanced_graphics_options::post_show(window& /*window*/)
{
if(get_retval() == window::OK) {
for(const std::string & i : scale_cases) {
for(const std::string& i : scale_cases) {
update_scale_case(i);
}
image::update_from_preferences();
}
}

View file

@ -17,12 +17,10 @@
#include "gui/dialogs/modal_dialog.hpp"
#include "gui/widgets/group.hpp"
#include "utils/make_enum.hpp"
#include "preferences.hpp"
namespace gui2
{
class label;
class toggle_button;
namespace dialogs
{
@ -45,14 +43,6 @@ public:
// These names must match the infixes of the widget ids in advanced_graphics_options.cfg
static const std::vector<std::string> scale_cases;
// These names must match the suffixes of the widget ids in advanced_graphics_options.cfg
MAKE_ENUM(SCALING_ALGORITHM,
(LINEAR, "linear")
(NEAREST_NEIGHBOR, "nn")
(XBRZ_LIN, "xbrzlin")
(XBRZ_NN, "xbrznn")
)
private:
/** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
virtual const std::string& window_id() const;
@ -63,11 +53,14 @@ private:
/** Inherited from modal_dialog. */
void post_show(window& window);
void setup_scale_case(const std::string &, window &);
void update_scale_case(const std::string &);
void setup_scale_case(const std::string&, window&);
void update_scale_case(const std::string&);
using SCALING_ALGORITHM = preferences::SCALING_ALGORITHM;
SCALING_ALGORITHM get_scale_pref(const std::string& pref_id);
std::map<std::string,group<SCALING_ALGORITHM> > groups_;
std::map<std::string, group<SCALING_ALGORITHM>> groups_;
};
} // namespace dialogs

View file

@ -26,7 +26,6 @@
#include "image_modifications.hpp"
#include "log.hpp"
#include "gettext.hpp"
#include "gui/dialogs/advanced_graphics_options.hpp"
#include "preferences.hpp"
#include "sdl/rect.hpp"
#include "utils/general.hpp"
@ -754,7 +753,7 @@ static surface scale_xbrz_helper(const surface & res, int w, int h)
return F(scale_surface_xbrz(res, legal_zoom), w, h);
}
using SCALING_ALGORITHM = gui2::dialogs::advanced_graphics_options::SCALING_ALGORITHM;
using SCALING_ALGORITHM = preferences::SCALING_ALGORITHM;
static scaling_function select_algorithm(SCALING_ALGORITHM algo)
{
@ -1164,14 +1163,14 @@ bool save_image(const surface & surf, const std::string & filename)
bool update_from_preferences()
{
SCALING_ALGORITHM algo = SCALING_ALGORITHM::LINEAR;
SCALING_ALGORITHM algo = preferences::default_scaling_algorithm;
try {
algo = SCALING_ALGORITHM::string_to_enum(preferences::get("scale_hex"));
} catch (bad_enum_cast &) {}
scale_to_hex_func = select_algorithm(algo);
algo = SCALING_ALGORITHM::LINEAR;
algo = preferences::default_scaling_algorithm;
try {
algo = SCALING_ALGORITHM::string_to_enum(preferences::get("scale_zoom"));
} catch (bad_enum_cast &) {}

View file

@ -57,7 +57,7 @@ namespace preferences {
/*
* Stores all the static, default values for certain game preferences. The values
* are kept here for easy modification without a lnegthy rebuild.
* are kept here for easy modification without a lengthy rebuild.
*
* Add any variables of similar type here.
*/
@ -70,6 +70,7 @@ const int def_window_height = 768;
const int min_font_scaling = 80;
const int max_font_scaling = 150;
const SCALING_ALGORITHM default_scaling_algorithm = SCALING_ALGORITHM::LINEAR;
class prefs_event_handler : public events::sdl_handler {
public:

View file

@ -21,8 +21,7 @@ class display;
#include "config.hpp"
#include "terrain/translation.hpp"
#include <SDL.h>
#include "utils/make_enum.hpp"
#include <utility>
@ -30,7 +29,6 @@ namespace hotkey {
class hotkey_item;
}
namespace preferences {
struct base_manager
@ -48,6 +46,15 @@ namespace preferences {
extern const int min_font_scaling;
extern const int max_font_scaling;
MAKE_ENUM(SCALING_ALGORITHM,
(LINEAR, "linear")
(NEAREST_NEIGHBOR, "nn")
(XBRZ_LIN, "xbrzlin")
(XBRZ_NN, "xbrznn")
)
extern const SCALING_ALGORITHM default_scaling_algorithm;
void write_preferences();
void set(const std::string& key, const std::string &value);