The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
about.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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#include "utils/general.hpp"
24
25#include <map>
26
27/**
28 * @namespace about
29 * Display credits %about all contributors.
30 */
31namespace about
32{
33namespace
34{
35
36credits_data parsed_credits_data;
37std::map<std::string, std::vector<std::string>> images_campaigns;
38std::vector<std::string> images_general;
39
40void gather_images(const config& from, std::vector<std::string>& to)
41{
42 const auto& im = utils::parenthetical_split(from["images"].str(), ',');
43 to.insert(to.end(), im.begin(), im.end());
44}
45
46} // namespace
47
48credits_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
100utils::optional<credits_data::const_iterator> get_campaign_credits(const std::string& campaign)
101{
102 const auto res = utils::ranges::find(get_credits_data(), campaign, &credits_group::id);
103 return res != parsed_credits_data.end() ? utils::make_optional(res) : utils::nullopt;
104}
105
106std::vector<std::string> get_background_images(const std::string& campaign)
107{
108 if(campaign.empty()) {
109 return images_general;
110 }
111
112 if(const auto it = images_campaigns.find(campaign); it != images_campaigns.cend()) {
113 return it->second;
114 }
115
116 return images_general;
117}
118
120{
121 parsed_credits_data.clear();
122
123 // TODO: should we reserve space in parsed_credits_data here?
124
125 images_campaigns.clear();
126 images_general.clear();
127
128 //
129 // Parse all [credits_group] tags
130 //
131 for(const config& group : cfg.child_range("credits_group")) {
132 if(group.has_child("about")) {
133 parsed_credits_data.emplace_back(group, false);
134
135 // Not in the credits_group since we don't want to inadvertently
136 // pick up images from campaigns.
137 gather_images(group, images_general);
138 }
139 }
140
141 //
142 // Parse all toplevel [about] tags.
143 //
144 config misc;
145 for(const config& about : cfg.child_range("about")) {
146 misc.add_child("about", about);
147 }
148
149 if(!misc.empty()) {
150 parsed_credits_data.emplace_back(misc, false);
151 }
152
153 //
154 // Parse all campaign [about] tags.
155 //
156 for(const config& campaign : cfg.child_range("campaign")) {
157 if(campaign.has_child("about")) {
158 parsed_credits_data.emplace_back(campaign, true);
159 }
160 }
161}
162
163} // end namespace about
std::vector< std::string > names
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
config & add_child(std::string_view key)
Definition config.cpp:436
child_itors child_range(std::string_view key)
Definition config.cpp:268
std::size_t child_count(std::string_view key) const
Definition config.cpp:292
bool empty() const
Definition config.cpp:823
A class grating read only view to a vector of config objects, viewed as one config with all children ...
const std::string & str() const
Definition tstring.hpp:204
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
std::string id
Text to match against addon_info.tags()
Definition manager.cpp:199
Display credits about all contributors.
std::vector< credits_group > credits_data
Definition about.hpp:57
utils::optional< credits_data::const_iterator > get_campaign_credits(const std::string &campaign)
Gets credits for a given campaign.
Definition about.cpp:100
void set_about(const game_config_view &cfg)
Regenerates the credits data.
Definition about.cpp:119
std::vector< std::string > get_background_images(const std::string &campaign)
Gets credit background images for a given campaign.
Definition about.cpp:106
const credits_data & get_credits_data()
Gets all credits data.
Definition about.cpp:95
std::string escape_text(std::string_view text)
Escapes the pango markup characters in a text.
Definition escape.hpp:33
int compare(const std::string &s1, const std::string &s2)
Case-sensitive lexicographical comparison.
Definition gettext.cpp:502
auto find(Container &container, const Value &value, const Projection &projection={})
Definition general.hpp:213
std::vector< std::string > parenthetical_split(std::string_view val, const char separator, std::string_view left, std::string_view right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...
about_group(const config &cfg)
Definition about.cpp:79
bool operator<(const about_group &o) const
Definition about.cpp:90
std::vector< std::pair< std::string, std::string > > names
Contributor names.
Definition about.hpp:37
t_string title
The section title.
Definition about.hpp:40
t_string header
Optional group title.
Definition about.hpp:54
std::string id
Optional group ID.
Definition about.hpp:51
credits_group(const config &cfg, bool is_campaign_credits)
Definition about.cpp:48
std::vector< about_group > sections
The group's sub-groups.
Definition about.hpp:48