The Battle for Wesnoth  1.15.2+dev
sp_options_configure.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
19 
22 
23 namespace gui2
24 {
25 namespace dialogs
26 {
27 
29 {
30 public:
31  sp_options_configure(ng::create_engine& create_engine, ng::configure_engine& config_engine);
32 
33  /**
34  * Execute function. We only want to show the dialog if the campaign has options or if
35  * there are active mods and at least one of those mods has custom options.
36  */
37  static bool execute(ng::create_engine& create_engine, ng::configure_engine& config_engine)
38  {
39  // Check campaign options.
40  const auto& campaign_mods = create_engine.current_level().data().child_range("options");
41 
42  const bool have_campaign_options = std::any_of(campaign_mods.begin(), campaign_mods.end(), [](config& mod) {
43  return !mod.empty();
44  });
45 
46  // Check active mod options.
47  bool have_mod_options = false;
48 
49  for(const auto& mod : create_engine.active_mods_data()) {
50  if(!(*mod->cfg).has_child("options")) {
51  continue;
52  }
53 
54  const auto& opt_range = (*mod->cfg).child_range("options");
55 
56  if(std::any_of(opt_range.begin(), opt_range.end(), [](const config& options) {
57  return !options.empty();
58  })) {
59  have_mod_options = true;
60  break;
61  }
62  }
63 
64  // If we have no valid options whatsoever, just bypass this dialog.
65  if(!have_campaign_options && !have_mod_options) {
66  return true;
67  }
68 
69  return sp_options_configure(create_engine, config_engine).show();
70  }
71 
72 private:
73  /** Inherited from modal_dialog, implemented by REGISTER_DIALOG. */
74  virtual const std::string& window_id() const override;
75 
76  /** Inherited from modal_dialog. */
77  virtual void pre_show(window& window) override;
78 
79  /** Inherited from modal_dialog. */
80  virtual void post_show(window& window) override;
81 
84 
85  std::unique_ptr<mp_options_helper> options_manager_;
86 };
87 
88 } // namespace dialogs
89 } // namespace gui2
configure_engine
sp_options_configure(ng::create_engine &create_engine, ng::configure_engine &config_engine)
child_itors child_range(config_key_type key)
Definition: config.cpp:362
Generic file dialog.
Definition: field-fwd.hpp:22
const config & data() const
std::vector< extras_metadata_ptr > active_mods_data()
const config & options()
Definition: game.cpp:593
std::unique_ptr< mp_options_helper > options_manager_
Various uncategorised dialogs.
static bool execute(ng::create_engine &create_engine, ng::configure_engine &config_engine)
Execute function.
virtual void post_show(window &window) override
Inherited from modal_dialog.
level & current_level() const
virtual void pre_show(window &window) override
Inherited from modal_dialog.
virtual const std::string & window_id() const override
Inherited from modal_dialog, implemented by REGISTER_DIALOG.
Abstract base class for all modal dialogs.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
bool empty() const
Definition: config.cpp:884