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