The Battle for Wesnoth  1.17.0-dev
end_credits.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2021
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 "config.hpp"
21 #include "game_config.hpp"
23 #include "gui/widgets/grid.hpp"
27 #include "gui/widgets/settings.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "gettext.hpp"
30 
31 #include <functional>
32 
33 #include <sstream>
34 
35 namespace gui2::dialogs
36 {
37 
38 REGISTER_DIALOG(end_credits)
39 
40 end_credits::end_credits(const std::string& campaign)
41  : focus_on_(campaign)
42  , backgrounds_()
43  , text_widget_(nullptr)
44  , scroll_speed_(100)
45  , last_scroll_(std::numeric_limits<uint32_t>::max())
46 {
47 }
48 
50 {
51  window.set_callback_next_draw([this]()
52  {
53  // Delay a little before beginning the scrolling
54  last_scroll_ = SDL_GetTicks() + 3000;
55  });
56 
57  connect_signal_on_draw(window, std::bind(&end_credits::timer_callback, this));
58 
59  connect_signal_pre_key_press(window, std::bind(&end_credits::key_press_callback, this, std::placeholders::_5));
60 
61  std::stringstream ss;
62  std::stringstream focus_ss;
63 
65  std::stringstream& group_stream = (group.id == focus_on_) ? focus_ss : ss;
66  group_stream << "\n";
67 
68  if(!group.header.empty()) {
69  group_stream << "<span size='xx-large'>" << group.header << "</span>" << "\n";
70  }
71 
72  for(const about::credits_group::about_group& about : group.sections) {
73  group_stream << "\n" << "<span size='x-large'>" << about.title << "</span>" << "\n";
74 
75  for(const auto& entry : about.names) {
76  group_stream << entry.first << "\n";
77  }
78  }
79  }
80 
81  // If a section is focused, move it to the top
82  if(!focus_ss.str().empty()) {
83  focus_ss << ss.rdbuf();
84  }
85 
86  // Get the appropriate background images
88 
89  if(backgrounds_.empty()) {
91  }
92 
93  // TODO: implement showing all available images as the credits scroll
94  window.get_canvas(0).set_variable("background_image", wfl::variant(backgrounds_[0]));
95 
96  text_widget_ = find_widget<scroll_label>(&window, "text", false, true);
97 
100  text_widget_->set_label((focus_ss.str().empty() ? ss : focus_ss).str());
101 
102  // HACK: always hide the scrollbar, even if it's needed.
103  // This should probably be implemented as a scrollbar mode.
104  // Also, for some reason hiding the whole grid doesn't work, and the elements need to be hidden manually
105  if(grid* v_grid = dynamic_cast<grid*>(text_widget_->find("_vertical_scrollbar_grid", false))) {
106  find_widget<scrollbar_base>(v_grid, "_vertical_scrollbar", false).set_visible(widget::visibility::hidden);
107 
108  // TODO: enable again if e24336afeb7 is reverted.
109  //find_widget<repeating_button>(v_grid, "_half_page_up", false).set_visible(widget::visibility::hidden);
110  //find_widget<repeating_button>(v_grid, "_half_page_down", false).set_visible(widget::visibility::hidden);
111  }
112 }
113 
115 {
116  uint32_t now = SDL_GetTicks();
117  if(last_scroll_ > now) {
118  return;
119  }
120 
121  uint32_t missed_time = now - last_scroll_;
122 
123  unsigned int cur_pos = text_widget_->get_vertical_scrollbar_item_position();
124 
125  // Calculate how far the text should have scrolled by now
126  // The division by 1000 is to convert milliseconds to seconds.
127  unsigned int needed_dist = missed_time * scroll_speed_ / 1000;
128 
129  text_widget_->set_vertical_scrollbar_item_position(cur_pos + needed_dist);
130 
131  last_scroll_ = now;
132 }
133 
134 void end_credits::key_press_callback(const SDL_Keycode key)
135 {
136  if(key == SDLK_UP && scroll_speed_ < 400) {
137  scroll_speed_ <<= 1;
138  }
139 
140  if(key == SDLK_DOWN && scroll_speed_ > 50) {
141  scroll_speed_ >>= 1;
142  }
143 }
144 
145 } // namespace dialogs
std::vector< std::string > backgrounds_
Definition: end_credits.hpp:49
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
t_string title
The section title.
Definition: about.hpp:39
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:168
const credits_data & get_credits_data()
Gets all credits data.
Definition: about.cpp:95
This file contains the window object, this object is a top level container which has the event manage...
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
STL namespace.
const std::string & focus_on_
Definition: end_credits.hpp:47
Definitions for the interface to Wesnoth Markup Language (WML).
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.
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
std::vector< std::pair< std::string, std::string > > names
Contributor names.
Definition: about.hpp:36
Base container class.
Definition: grid.hpp:31
scroll_label * text_widget_
Definition: end_credits.hpp:51
This file contains the settings handling of the widget library.
std::string game_title_background
void set_link_aware(bool l)
void key_press_callback(const SDL_Keycode key)
void set_vertical_scrollbar_item_position(const unsigned position)
Move the vertical scrollbar to a position.
void connect_signal_pre_key_press(dispatcher &dispatcher, const signal_keyboard_function &signal)
Connects the signal for &#39;snooping&#39; on the keypress.
Definition: dispatcher.cpp:167
The user sets the widget hidden, that means:
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: end_credits.cpp:49
unsigned get_vertical_scrollbar_item_position() const
Returns current position of the vertical scrollbar.
canvas & get_canvas(const unsigned index)
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
void set_callback_next_draw(std::function< void()> func)
Sets a callback that will be called after the window is drawn next time.
Definition: window.hpp:423
void connect_signal_on_draw(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a callback when the widget is drawn.
Definition: dispatcher.cpp:192