The Battle for Wesnoth  1.19.1+dev
end_credits.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "about.hpp"
20 #include "game_config.hpp"
22 #include "gui/widgets/grid.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "gettext.hpp"
27 
28 #include <functional>
29 
30 #include <sstream>
31 
32 namespace gui2::dialogs
33 {
34 
35 REGISTER_DIALOG(end_credits)
36 
37 end_credits::end_credits(const std::string& campaign)
38  : modal_dialog(window_id())
39  , focus_on_(campaign)
40  , backgrounds_()
41  , text_widget_(nullptr)
42  , scroll_speed_(100)
43  , last_scroll_(std::numeric_limits<uint32_t>::max())
44  , first_idx_(0)
45  , last_idx_(first_idx_ + sliding_size_)
46 {
47 }
48 
50 {
51  last_scroll_ = SDL_GetTicks();
52 
53  connect_signal_pre_key_press(window, std::bind(&end_credits::key_press_callback, this, std::placeholders::_5));
54 
55  std::stringstream ss;
56  std::stringstream focus_ss;
57 
59  std::stringstream& group_stream = (group.id == focus_on_) ? focus_ss : ss;
60  group_stream << "\n";
61 
62  if(!group.header.empty()) {
63  group_stream << "<span size='xx-large'>" << group.header << "</span>" << "\n";
64  }
65 
66  for(const about::credits_group::about_group& about : group.sections) {
67  group_stream << "\n" << "<span size='x-large'>" << about.title << "</span>" << "\n";
68 
69  for(const auto& entry : about.names) {
70  group_stream << entry.first << "\n";
71  }
72  }
73  }
74 
75  // If a section is focused, move it to the top
76  if(!focus_ss.str().empty()) {
77  focus_ss << ss.rdbuf();
78  }
79 
80  // Get the appropriate background images
82 
83  if(backgrounds_.empty()) {
85  }
86 
87  // TODO: implement showing all available images as the credits scroll
88  window.get_canvas(0).set_variable("background_image", wfl::variant(backgrounds_[0]));
89 
90  text_widget_ = find_widget<scroll_label>(&window, "text", false, true);
91 
94 
95  content_ = focus_ss.str().empty() ? ss.str() : focus_ss.str();
96 
97  // splits the content text by newline, leaving blanks
98  // also truncates the length of the line to 200 characters
99  // 200 characters is completely arbitrary, just prevent the possibility of ridiculously wide lines
100  // NOTE: this depends on the assumption that the <span>s added above only ever wrap a single line
101  std::vector<std::string> lines = utils::split(content_, '\n', 0);
102  int i = 0;
103  for(const std::string& line : lines) {
104  if(i % lines_per_chunk_ == 0) {
105  chunks_.emplace_back();
106  }
107  std::vector<std::string>& last_chunk = chunks_[chunks_.size()-1];
108  last_chunk.emplace_back(line.size() < 200 ? line : line.substr(0, 200));
109  i++;
110  }
111 
112  sliding_content_.clear();
113  for(std::size_t i = 0; i <= sliding_size_; i++){
114  sliding_content_ += utils::join(chunks_.at(i), "\n") + "\n";
115  }
116 
117  //concat substring strings
119  // HACK: always hide the scrollbar, even if it's needed.
120  // This should probably be implemented as a scrollbar mode.
121  // Also, for some reason hiding the whole grid doesn't work, and the elements need to be hidden manually
122  if(grid* v_grid = dynamic_cast<grid*>(text_widget_->find("_vertical_scrollbar_grid", false))) {
123  find_widget<scrollbar_base>(v_grid, "_vertical_scrollbar", false).set_visible(widget::visibility::hidden);
124 
125  // TODO: enable again if e24336afeb7 is reverted.
126  //find_widget<repeating_button>(v_grid, "_half_page_up", false).set_visible(widget::visibility::hidden);
127  //find_widget<repeating_button>(v_grid, "_half_page_down", false).set_visible(widget::visibility::hidden);
128  }
129 }
130 
132 {
133  uint32_t now = SDL_GetTicks();
134  if(last_scroll_ > now) {
135  return;
136  }
137 
138  uint32_t missed_time = now - last_scroll_;
139 
140  unsigned int cur_pos = text_widget_->get_vertical_scrollbar_item_position();
141 
142  // Calculate how far the text should have scrolled by now
143  // The division by 1000 is to convert milliseconds to seconds.
144  unsigned int needed_dist = missed_time * scroll_speed_ / 1000;
145 
146  // TODO: this doesn't allow for scrolling up again after been scrolled down
147  // only the content in the current sliding window can be scrolled up
148  if(cur_pos <= text_widget_->get_height()){
149  text_widget_->set_vertical_scrollbar_item_position(cur_pos + needed_dist);
150  } else {
151  if(first_idx_ < chunks_.size() - sliding_size_ - 1){
152  first_idx_++;
154  sliding_content_.clear();
155 
156  if(last_idx_ <= chunks_.size()){
157  for(std::size_t i = first_idx_; i <= last_idx_; i++) {
158  sliding_content_ += utils::join(chunks_[i], "\n") + "\n";
159  }
160  }
161 
162  // updates the sliding window
164  cur_pos = 0;
165  }
166  }
167 
168  last_scroll_ = now;
169 }
170 
171 void end_credits::key_press_callback(const SDL_Keycode key)
172 {
173  if(key == SDLK_UP && scroll_speed_ < 400) {
174  scroll_speed_ <<= 1;
175  }
176 
177  if(key == SDLK_DOWN && scroll_speed_ > 50) {
178  scroll_speed_ >>= 1;
179  }
180 }
181 
182 } // namespace dialogs
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:154
std::vector< std::string > backgrounds_
Definition: end_credits.hpp:50
scroll_label * text_widget_
Definition: end_credits.hpp:52
static constexpr std::size_t lines_per_chunk_
number of lines to put in each chunk of text to display the final chunk will of course probably have ...
Definition: end_credits.hpp:68
const std::string & focus_on_
Definition: end_credits.hpp:48
void key_press_callback(const SDL_Keycode key)
std::vector< std::vector< std::string > > chunks_
Definition: end_credits.hpp:73
static constexpr std::size_t sliding_size_
sliding_size_ alters how many of the sliding contents are to be run at once n-1 = 2 => 3 strings at o...
Definition: end_credits.hpp:63
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: end_credits.cpp:49
virtual void update() override
TLD override to update animations, called once per frame.
Abstract base class for all modal dialogs.
Base container class.
Definition: grid.hpp:32
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
void set_link_aware(bool l)
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
unsigned get_vertical_scrollbar_item_position() const
Returns current position of the vertical scrollbar.
void set_vertical_scrollbar_item_position(const unsigned position)
Move the vertical scrollbar to a position.
canvas & get_canvas(const unsigned index)
unsigned get_height() const
Definition: widget.cpp:331
@ hidden
The user sets the widget hidden, that means:
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
std::size_t i
Definition: function.cpp:968
This file contains the window object, this object is a top level container which has the event manage...
Display credits about all contributors.
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:94
void line(int from_x, int from_y, int to_x, int to_y)
Draw a line.
Definition: draw.cpp:180
std::string game_title_background
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_pre_key_press(dispatcher &dispatcher, const signal_keyboard &signal)
Connects the signal for 'snooping' on the keypress.
Definition: dispatcher.cpp:172
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)