Skip to content

Commit

Permalink
"Defaults" buttons to reset the settings of a specific MP component.
Browse files Browse the repository at this point in the history
  • Loading branch information
lipk committed Jun 6, 2015
1 parent 8b08513 commit 10632ff
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 51 deletions.
95 changes: 63 additions & 32 deletions src/game_initialization/mp_options.cpp
Expand Up @@ -107,6 +107,20 @@ void manager::init_widgets()
widgets_ordered_.back()->set_value(get_stored_value(id));
widgets_[id] = widgets_ordered_.back();
}
widgets_ordered_.push_back(new reset_display(display_.video(), comp.cfg["id"], *this));
}
}

void manager::restore_defaults(const std::string &component)
{
BOOST_FOREACH (const config::any_child& i, get_component_cfg(component).all_children_range()) {
if (!is_valid_option(i.key, i.cfg)) {
continue;
}

const std::string id = i.cfg["id"].str();

widgets_[id]->set_value(get_default_value(id));
}
}

Expand Down Expand Up @@ -199,10 +213,9 @@ void manager::layout_widgets(int startx, int starty)

void manager::process_event()
{
for (std::map<std::string, option_display*>::iterator i = widgets_.begin();
i != widgets_.end(); ++i)
for (size_t i = 0; i<widgets_ordered_.size(); ++i)
{
i->second->process_event();
widgets_ordered_[i]->process_event();
}
}

Expand Down Expand Up @@ -295,6 +308,24 @@ const config& manager::get_option_info_cfg(const std::string& id) const
return empty;
}

const config& manager::get_component_cfg(const std::string &id) const
{
static const config empty;
const config &m = options_info_.find_child("modification", "id", id);
if (m) {
return m;
}
const config &s = options_info_.find_child("scenario", "id", id);
if (s) {
return s;
}
const config &e = options_info_.find_child("era", "id", id);
if (e) {
return e;
}
return empty;
}


config::attribute_value manager::get_stored_value(const std::string& id) const
{
Expand Down Expand Up @@ -345,35 +376,6 @@ bool manager::is_valid_option(const std::string& key, const config& option)
(!option["id"].empty());
}

void manager::restore_defaults(manager* m)
{
const config& era = m->options_info_.find_child("era", "id", m->era_);
restore_defaults_for_component(era, m);

const config& scen = m->options_info_.find_child("multiplayer", "id",
m->scenario_);
restore_defaults_for_component(scen, m);

BOOST_FOREACH (const std::string& id, m->modifications_) {
const config& mod = m->options_info_.find_child("modification", "id",
id);
restore_defaults_for_component(mod, m);
}
}

void manager::restore_defaults_for_component(const config& c, manager* m)
{
BOOST_FOREACH (const config::any_child& i, c.all_children_range()) {
if (!is_valid_option(i.key, i.cfg)) {
continue;
}

const std::string id = i.cfg["id"].str();

m->widgets_[id]->set_value(m->get_default_value(id));
}
}

bool manager::is_active(const std::string &id) const
{
return (era_ == id) || (scenario_ == id) ||
Expand Down Expand Up @@ -591,6 +593,35 @@ void combo_display::hide_children(bool hide)
combo_->hide(hide);
}

reset_display::reset_display(CVideo &video, const std::string &comp, manager &m)
: manager_(m)
, component_(comp)
, button_(new gui::button(video, _("Defaults")))
{}

reset_display::~reset_display()
{
delete button_;
}

void reset_display::layout(int &xpos, int &ypos, int border_size, gui::scrollpane *pane)
{
pane->add_widget(button_, xpos, ypos);
ypos += button_->height() + border_size;
}

void reset_display::hide_children(bool hide)
{
button_->hide(hide);
}

void reset_display::process_event()
{
if (button_->pressed()) {
manager_.restore_defaults(component_);
}
}

} // namespace options

} // namespace mp
Expand Down
43 changes: 24 additions & 19 deletions src/game_initialization/mp_options.hpp
Expand Up @@ -140,6 +140,26 @@ class title_display : public option_display
gui::label* title_;
};

class manager;

class reset_display : public option_display
{
public:
reset_display(CVideo& video, const std::string& comp, manager &m);
~reset_display();

void layout(int &xpos, int &ypos, int border_size, gui::scrollpane *pane);
void set_value(const config::attribute_value &/*val*/) {}
config::attribute_value get_value() const { return config::attribute_value(); }
void hide_children(bool hide);
void process_event();

private:
manager &manager_;
std::string component_;
gui::button* button_;
};

class manager
{
public:
Expand Down Expand Up @@ -217,6 +237,8 @@ class manager

void init_widgets();

void restore_defaults(const std::string &component);

private:

/** Stores needed info about each element and their configuration options */
Expand Down Expand Up @@ -286,6 +308,8 @@ class manager
*/
const config& get_option_info_cfg(const std::string& id) const;

const config& get_component_cfg(const std::string& id) const;

/**
* Finds the parent node of an options.
*
Expand Down Expand Up @@ -343,25 +367,6 @@ class manager
*/
static bool is_valid_option(const std::string& key, const config& option);

/**
* Restores every widget's value to its default for a window.
*
* @param m A pointer to the manager which generated
* the window.
*/
static void restore_defaults(manager* m);

/**
* Finds the widgets representing the options of a certain component in a
* window (era, scenario or modification) and sets their value to their
* defaults.
*
* @param comp The config of the component.
* @param m A pointer to the manager which generated
* the window.
*/
static void restore_defaults_for_component(const config& comp, manager* m);

bool is_active(const std::string& id) const;
};

Expand Down

0 comments on commit 10632ff

Please sign in to comment.