The Battle for Wesnoth  1.15.7+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 credits_data::const_iterator get_campaign_credits(const std::string& campaign)
100 {
101  return std::find_if(parsed_credits_data.begin(), parsed_credits_data.end(),
102  [&campaign](const credits_group& group) { return group.id == campaign; });
103 }
104 
105 std::vector<std::string> get_background_images(const std::string& campaign)
106 {
107  if(!campaign.empty() && !images_campaigns[campaign].empty()) {
108  return images_campaigns[campaign];
109  }
110 
111  return images_general;
112 }
113 
114 void set_about(const game_config_view& cfg)
115 {
116  parsed_credits_data.clear();
117 
118  // TODO: should we reserve space in parsed_credits_data here?
119 
120  images_campaigns.clear();
121  images_general.clear();
122 
123  //
124  // Parse all [credits_group] tags
125  //
126  for(const config& group : cfg.child_range("credits_group")) {
127  if(group.has_child("about")) {
128  parsed_credits_data.emplace_back(group, false);
129 
130  // Not in the credits_group since we don't want to inadvertently
131  // pick up images from campaigns.
132  gather_images(group, images_general);
133  }
134  }
135 
136  //
137  // Parse all toplevel [about] tags.
138  //
139  config misc;
140  for(const config& about : cfg.child_range("about")) {
141  misc.add_child("about", about);
142  }
143 
144  if(!misc.empty()) {
145  parsed_credits_data.emplace_back(misc, false);
146  }
147 
148  //
149  // Parse all campaign [about] tags.
150  //
151  for(const config& campaign : cfg.child_range("campaign")) {
152  if(campaign.has_child("about")) {
153  parsed_credits_data.emplace_back(campaign, true);
154  }
155  }
156 }
157 
158 } // end namespace about
t_string title
The section title.
Definition: about.hpp:37
credits_data::const_iterator get_campaign_credits(const std::string &campaign)
Gets credits for a given campaign.
Definition: about.cpp:99
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:457
unsigned child_count(config_key_type key) const
Definition: config.cpp:390
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:362
std::string str
Definition: statement.cpp:110
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< about_group > sections
The group&#39;s sub-groups.
Definition: about.hpp:45
std::vector< std::string > get_background_images(const std::string &campaign)
Gets credit background images for a given campaaign.
Definition: about.cpp:105
Display credits about all contributors.
std::vector< credits_group > credits_data
Definition: about.hpp:54
std::vector< std::pair< std::string, std::string > > names
Contributor names.
Definition: about.hpp:34
void set_about(const game_config_view &cfg)
Regenerates the credits data.
Definition: about.cpp:114
credits_group(const config &cfg, bool is_campaign_credits)
Definition: about.cpp:47
about_group(const config &cfg)
Definition: about.cpp:78
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:62
static int sort(lua_State *L)
Definition: ltablib.cpp:411
config & add_child(config_key_type key)
Definition: config.cpp:476
t_string header
Optional group tite.
Definition: about.hpp:51
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
const std::string & str() const
Definition: tstring.hpp:186
bool empty() const
Definition: config.cpp:884
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...