The Battle for Wesnoth  1.17.0-dev
about.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "about.hpp"
16 
17 #include "config.hpp"
18 #include "font/pango/escape.hpp"
19 #include "game_config_view.hpp"
20 #include "gettext.hpp"
22 
23 #include <map>
24 
25 /**
26  * @namespace about
27  * Display credits %about all contributors.
28  */
29 namespace about
30 {
31 namespace
32 {
33 credits_data parsed_credits_data;
34 std::map<std::string, std::vector<std::string>> images_campaigns;
35 std::vector<std::string> images_general;
36 
37 void gather_images(const config& from, std::vector<std::string>& to)
38 {
39  const auto& im = utils::parenthetical_split(from["images"], ',');
40  if(!im.empty()) {
41  to.insert(to.end(), im.begin(), im.end());
42  }
43 }
44 
45 } // namespace
46 
47 credits_group::credits_group(const config& cfg, bool is_campaign_credits)
48  : sections()
49  , id()
50  , header()
51 {
52  if(is_campaign_credits) {
53  id = cfg["id"].str();
54  header = cfg["name"].t_str();
55  }
56 
57  sections.reserve(cfg.child_count("about"));
58 
59  for(const config& about : cfg.child_range("about")) {
60  if(!about.has_child("entry")) {
61  continue;
62  }
63 
64  sections.emplace_back(about);
65 
66  if(is_campaign_credits) {
67  gather_images(about, images_campaigns[id]);
68  } else {
69  gather_images(about, images_general);
70  }
71  }
72 
73  if(cfg["sort"].to_bool(false)) {
74  std::sort(sections.begin(), sections.end());
75  }
76 }
77 
79  : names()
80  , title(cfg["title"].t_str())
81 {
82  names.reserve(cfg.child_count("entry"));
83 
84  for(const config& entry : cfg.child_range("entry")) {
85  names.emplace_back(font::escape_text(entry["name"].str()), font::escape_text(entry["comment"].str()));
86  }
87 }
88 
90 {
91  return translation::compare(title.str(), o.title.str()) < 0;
92 }
93 
95 {
96  return parsed_credits_data;
97 }
98 
99 std::optional<credits_data::const_iterator> get_campaign_credits(const std::string& campaign)
100 {
101  const auto res = std::find_if(parsed_credits_data.begin(), parsed_credits_data.end(),
102  [&campaign](const credits_group& group) { return group.id == campaign; });
103  return res != parsed_credits_data.end() ? std::make_optional(res) : std::nullopt;
104 }
105 
106 std::vector<std::string> get_background_images(const std::string& campaign)
107 {
108  if(!campaign.empty() && !images_campaigns[campaign].empty()) {
109  return images_campaigns[campaign];
110  }
111 
112  return images_general;
113 }
114 
115 void set_about(const game_config_view& cfg)
116 {
117  parsed_credits_data.clear();
118 
119  // TODO: should we reserve space in parsed_credits_data here?
120 
121  images_campaigns.clear();
122  images_general.clear();
123 
124  //
125  // Parse all [credits_group] tags
126  //
127  for(const config& group : cfg.child_range("credits_group")) {
128  if(group.has_child("about")) {
129  parsed_credits_data.emplace_back(group, false);
130 
131  // Not in the credits_group since we don't want to inadvertently
132  // pick up images from campaigns.
133  gather_images(group, images_general);
134  }
135  }
136 
137  //
138  // Parse all toplevel [about] tags.
139  //
140  config misc;
141  for(const config& about : cfg.child_range("about")) {
142  misc.add_child("about", about);
143  }
144 
145  if(!misc.empty()) {
146  parsed_credits_data.emplace_back(misc, false);
147  }
148 
149  //
150  // Parse all campaign [about] tags.
151  //
152  for(const config& campaign : cfg.child_range("campaign")) {
153  if(campaign.has_child("about")) {
154  parsed_credits_data.emplace_back(campaign, true);
155  }
156  }
157 }
158 
159 } // end namespace about
t_string title
The section title.
Definition: about.hpp:38
config_array_view child_range(config_key_type key) const
int compare(const std::string &s1, const std::string &s2)
Case-sensitive lexicographical comparison.
Definition: gettext.cpp:455
unsigned child_count(config_key_type key) const
Definition: config.cpp:384
const credits_data & get_credits_data()
Gets all credits data.
Definition: about.cpp:94
child_itors child_range(config_key_type key)
Definition: config.cpp:356
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< about_group > sections
The group&#39;s sub-groups.
Definition: about.hpp:46
std::vector< std::string > get_background_images(const std::string &campaign)
Gets credit background images for a given campaaign.
Definition: about.cpp:106
Display credits about all contributors.
std::vector< credits_group > credits_data
Definition: about.hpp:55
std::vector< std::pair< std::string, std::string > > names
Contributor names.
Definition: about.hpp:35
void set_about(const game_config_view &cfg)
Regenerates the credits data.
Definition: about.cpp:115
credits_group(const config &cfg, bool is_campaign_credits)
Definition: about.cpp:47
about_group(const config &cfg)
Definition: about.cpp:78
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:214
std::string escape_text(const std::string &text)
Escapes the pango markup characters in a text.
Definition: escape.hpp:32
bool operator<(const about_group &o) const
Definition: about.cpp:89
std::vector< std::string > names
Definition: build_info.cpp:65
static int sort(lua_State *L)
Definition: ltablib.cpp:397
config & add_child(config_key_type key)
Definition: config.cpp:500
std::optional< credits_data::const_iterator > get_campaign_credits(const std::string &campaign)
Gets credits for a given campaign.
Definition: about.cpp:99
t_string header
Optional group tite.
Definition: about.hpp:52
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
const std::string & str() const
Definition: tstring.hpp:190
bool empty() const
Definition: config.cpp:916
std::vector< std::string > parenthetical_split(const std::string &val, const char separator, const std::string &left, const std::string &right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...