The Battle for Wesnoth  1.15.1+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 "gettext.hpp"
20 
21 #include <map>
22 
23 /**
24  * @namespace about
25  * Display credits %about all contributors.
26  */
27 namespace about
28 {
29 namespace
30 {
31 credits_data parsed_credits_data;
32 std::map<std::string, std::vector<std::string>> images_campaigns;
33 std::vector<std::string> images_general;
34 
35 void gather_images(const config& from, std::vector<std::string>& to)
36 {
37  const auto& im = utils::parenthetical_split(from["images"], ',');
38  if(!im.empty()) {
39  to.insert(to.end(), im.begin(), im.end());
40  }
41 }
42 
43 } // end anon namespace
44 
45 credits_group::credits_group(const config& cfg, bool is_campaign_credits)
46  : sections()
47  , id()
48  , header()
49 {
50  if(is_campaign_credits) {
51  id = cfg["id"].str();
52  header = cfg["name"].t_str();
53  }
54 
55  sections.reserve(cfg.child_count("about"));
56 
57  for(const config& about : cfg.child_range("about")) {
58  if(!about.has_child("entry")) {
59  continue;
60  }
61 
62  sections.emplace_back(about);
63 
64  if(is_campaign_credits) {
65  gather_images(about, images_campaigns[id]);
66  } else {
67  gather_images(about, images_general);
68  }
69  }
70 
71  if(cfg["sort"].to_bool(false)) {
72  std::sort(sections.begin(), sections.end());
73  }
74 }
75 
77  : names()
78  , title(cfg["title"].t_str())
79 {
80  names.reserve(cfg.child_count("entry"));
81 
82  for(const config& entry : cfg.child_range("entry")) {
83  names.push_back(entry["name"].str());
84  }
85 }
86 
88 {
89  return translation::compare(title.str(), o.title.str()) < 0;
90 }
91 
93 {
94  return parsed_credits_data;
95 }
96 
97 std::vector<std::string> get_background_images(const std::string& campaign)
98 {
99  if(!campaign.empty() && !images_campaigns[campaign].empty()){
100  return images_campaigns[campaign];
101  }
102 
103  return images_general;
104 }
105 
106 void set_about(const config& cfg)
107 {
108  parsed_credits_data.clear();
109 
110  // TODO: should we reserve space in parsed_credits_data here?
111 
112  images_campaigns.clear();
113  images_general.clear();
114 
115  //
116  // Parse all [credits_group] tags
117  //
118  for(const config& group : cfg.child_range("credits_group")) {
119  if(group.has_child("about")) {
120  parsed_credits_data.emplace_back(group, false);
121 
122  // Not in the credits_group since we don't want to inadvertently
123  // pick up images from campaigns.
124  gather_images(group, images_general);
125  }
126  }
127 
128  //
129  // Parse all toplevel [about] tags.
130  //
131  config misc;
132  for(const config& about : cfg.child_range("about")) {
133  misc.add_child("about", about);
134  }
135 
136  if(!misc.empty()) {
137  parsed_credits_data.emplace_back(misc, false);
138  }
139 
140  //
141  // Parse all campaign [about] tags.
142  //
143  for(const config& campaign : cfg.child_range("campaign")) {
144  if(campaign.has_child("about")) {
145  parsed_credits_data.emplace_back(campaign, true);
146  }
147  }
148 }
149 
150 } // end namespace about
t_string title
The section title.
Definition: about.hpp:36
int compare(const std::string &s1, const std::string &s2)
Case-sensitive lexicographical comparison.
Definition: gettext.cpp:458
unsigned child_count(config_key_type key) const
Definition: config.cpp:390
const credits_data & get_credits_data()
General getter methods for the credits config and image lists by campaign id.
Definition: about.cpp:92
child_itors child_range(config_key_type key)
Definition: config.cpp:362
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< about_group > sections
The group&#39;s sub-groups.
Definition: about.hpp:44
std::vector< std::string > get_background_images(const std::string &campaign)
Definition: about.cpp:97
Display credits about all contributors.
std::vector< credits_group > credits_data
Definition: about.hpp:53
credits_group(const config &cfg, bool is_campaign_credits)
Definition: about.cpp:45
std::vector< std::string > names
Contributor names.
Definition: about.hpp:33
about_group(const config &cfg)
Definition: about.cpp:76
bool operator<(const about_group &o) const
Definition: about.cpp:87
std::vector< std::string > names
Definition: build_info.cpp:56
static int sort(lua_State *L)
Definition: ltablib.cpp:411
config & add_child(config_key_type key)
Definition: config.cpp:476
void set_about(const config &cfg)
Regenerates the credits config.
Definition: about.cpp:106
t_string header
Optional group tite.
Definition: about.hpp:50
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
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...