The Battle for Wesnoth  1.15.12+dev
end_credits.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #define GETTEXT_DOMAIN "wesnoth-lib"
15 
17 
18 #include "about.hpp"
19 #include "config.hpp"
20 #include "game_config.hpp"
22 #include "gui/widgets/grid.hpp"
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/window.hpp"
28 #include "gettext.hpp"
29 
30 #include <functional>
31 
32 #include <sstream>
33 
34 namespace gui2::dialogs
35 {
36 
37 REGISTER_DIALOG(end_credits)
38 
39 end_credits::end_credits(const std::string& campaign)
40  : focus_on_(campaign)
41  , backgrounds_()
42  , text_widget_(nullptr)
43  , scroll_speed_(100)
44  , last_scroll_(std::numeric_limits<uint32_t>::max())
45 {
46 }
47 
49 {
50  window.set_callback_next_draw([this]()
51  {
52  // Delay a little before beginning the scrolling
53  last_scroll_ = SDL_GetTicks() + 3000;
54  });
55 
56  connect_signal_on_draw(window, std::bind(&end_credits::timer_callback, this));
57 
58  connect_signal_pre_key_press(window, std::bind(&end_credits::key_press_callback, this, std::placeholders::_5));
59 
60  std::stringstream ss;
61  std::stringstream focus_ss;
62 
64  std::stringstream& group_stream = (group.id == focus_on_) ? focus_ss : ss;
65  group_stream << "\n";
66 
67  if(!group.header.empty()) {
68  group_stream << "<span size='xx-large'>" << group.header << "</span>" << "\n";
69  }
70 
71  for(const about::credits_group::about_group& about : group.sections) {
72  group_stream << "\n" << "<span size='x-large'>" << about.title << "</span>" << "\n";
73 
74  for(const auto& entry : about.names) {
75  group_stream << entry.first << "\n";
76  }
77  }
78  }
79 
80  // If a section is focused, move it to the top
81  if(!focus_ss.str().empty()) {
82  focus_ss << ss.rdbuf();
83  }
84 
85  // Get the appropriate background images
87 
88  if(backgrounds_.empty()) {
90  }
91 
92  // TODO: implement showing all available images as the credits scroll
93  window.get_canvas(0).set_variable("background_image", wfl::variant(backgrounds_[0]));
94 
95  text_widget_ = find_widget<scroll_label>(&window, "text", false, true);
96 
99  text_widget_->set_label((focus_ss.str().empty() ? ss : focus_ss).str());
100 
101  // HACK: always hide the scrollbar, even if it's needed.
102  // This should probably be implemented as a scrollbar mode.
103  // Also, for some reason hiding the whole grid doesn't work, and the elements need to be hidden manually
104  if(grid* v_grid = dynamic_cast<grid*>(text_widget_->find("_vertical_scrollbar_grid", false))) {
105  find_widget<scrollbar_base>(v_grid, "_vertical_scrollbar", false).set_visible(widget::visibility::hidden);
106 
107  // TODO: enable again if e24336afeb7 is reverted.
108  //find_widget<repeating_button>(v_grid, "_half_page_up", false).set_visible(widget::visibility::hidden);
109  //find_widget<repeating_button>(v_grid, "_half_page_down", false).set_visible(widget::visibility::hidden);
110  }
111 }
112 
114 {
115  uint32_t now = SDL_GetTicks();
116  if(last_scroll_ > now) {
117  return;
118  }
119 
120  uint32_t missed_time = now - last_scroll_;
121 
122  unsigned int cur_pos = text_widget_->get_vertical_scrollbar_item_position();
123 
124  // Calculate how far the text should have scrolled by now
125  // The division by 1000 is to convert milliseconds to seconds.
126  unsigned int needed_dist = missed_time * scroll_speed_ / 1000;
127 
128  text_widget_->set_vertical_scrollbar_item_position(cur_pos + needed_dist);
129 
130  last_scroll_ = now;
131 }
132 
133 void end_credits::key_press_callback(const SDL_Keycode key)
134 {
135  if(key == SDLK_UP && scroll_speed_ < 400) {
136  scroll_speed_ <<= 1;
137  }
138 
139  if(key == SDLK_DOWN && scroll_speed_ > 50) {
140  scroll_speed_ >>= 1;
141  }
142 }
143 
144 } // namespace dialogs
std::vector< std::string > backgrounds_
Definition: end_credits.hpp:48
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
t_string title
The section title.
Definition: about.hpp:37
const credits_data & get_credits_data()
Gets all credits data.
Definition: about.cpp:94
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:167
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:46
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:105
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:34
Base container class.
Definition: grid.hpp:30
scroll_label * text_widget_
Definition: end_credits.hpp:50
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:166
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:48
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:64
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:422
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:191