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 
16 #include "gui/dialogs/modal_dialog.hpp"
17 #include "gui/dialogs/multiplayer/plugin_executor.hpp"
18 #include "gui/dialogs/multiplayer/mp_options_helper.hpp"
19 
20 #include "game_initialization/create_engine.hpp"
21 #include "game_initialization/configure_engine.hpp"
22 
23 namespace gui2
24 {
25 namespace dialogs
26 {
27 
28 class sp_options_configure : public modal_dialog, private plugin_executor
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 	 */
execute(ng::create_engine & create_engine,ng::configure_engine & config_engine)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 
82 	ng::create_engine& create_engine_;
83 	ng::configure_engine& config_engine_;
84 
85 	std::unique_ptr<mp_options_helper> options_manager_;
86 };
87 
88 } // namespace dialogs
89 } // namespace gui2
90