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