The Battle for Wesnoth  1.15.2+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 "utils/functional.hpp"
31 
32 #include <sstream>
33 
34 namespace gui2
35 {
36 namespace dialogs
37 {
38 
39 REGISTER_DIALOG(end_credits)
40 
41 end_credits::end_credits(const std::string& campaign)
42  : focus_on_(campaign)
43  , backgrounds_()
44  , text_widget_(nullptr)
45  , scroll_speed_(100)
46  , last_scroll_(std::numeric_limits<uint32_t>::max())
47 {
48 }
49 
51 {
52  window.set_callback_next_draw([this]()
53  {
54  // Delay a little before beginning the scrolling
55  last_scroll_ = SDL_GetTicks() + 3000;
56  });
57 
58  connect_signal_on_draw(window, std::bind(&end_credits::timer_callback, this));
59 
61 
62  std::stringstream ss;
63  std::stringstream focus_ss;
64 
66  std::stringstream& group_stream = (group.id == focus_on_) ? focus_ss : ss;
67  group_stream << "\n";
68 
69  if(!group.header.empty()) {
70  group_stream << "<span size='xx-large'>" << group.header << "</span>" << "\n";
71  }
72 
73  for(const about::credits_group::about_group& about : group.sections) {
74  group_stream << "\n" << "<span size='x-large'>" << about.title << "</span>" << "\n";
75 
76  for(const std::string& entry : about.names) {
77  group_stream << entry << "\n";
78  }
79  }
80  }
81 
82  // If a section is focused, move it to the top
83  if(!focus_ss.str().empty()) {
84  focus_ss << ss.rdbuf();
85  }
86 
87  // Get the appropriate background images
89 
90  if(backgrounds_.empty()) {
92  }
93 
94  // TODO: implement showing all available images as the credits scroll
95  window.get_canvas(0).set_variable("background_image", wfl::variant(backgrounds_[0]));
96 
97  text_widget_ = find_widget<scroll_label>(&window, "text", false, true);
98 
101  text_widget_->set_label((focus_ss.str().empty() ? ss : focus_ss).str());
102 
103  // HACK: always hide the scrollbar, even if it's needed.
104  // This should probably be implemented as a scrollbar mode.
105  // Also, for some reason hiding the whole grid doesn't work, and the elements need to be hidden manually
106  if(grid* v_grid = dynamic_cast<grid*>(text_widget_->find("_vertical_scrollbar_grid", false))) {
107  find_widget<scrollbar_base>(v_grid, "_vertical_scrollbar", false).set_visible(widget::visibility::hidden);
108 
109  // TODO: enable again if e24336afeb7 is reverted.
110  //find_widget<repeating_button>(v_grid, "_half_page_up", false).set_visible(widget::visibility::hidden);
111  //find_widget<repeating_button>(v_grid, "_half_page_down", false).set_visible(widget::visibility::hidden);
112  }
113 }
114 
116 {
117  uint32_t now = SDL_GetTicks();
118  if(last_scroll_ > now) {
119  return;
120  }
121 
122  uint32_t missed_time = now - last_scroll_;
123 
124  unsigned int cur_pos = text_widget_->get_vertical_scrollbar_item_position();
125 
126  // Calculate how far the text should have scrolled by now
127  // The division by 1000 is to convert milliseconds to seconds.
128  unsigned int needed_dist = missed_time * scroll_speed_ / 1000;
129 
130  text_widget_->set_vertical_scrollbar_item_position(cur_pos + needed_dist);
131 
132  last_scroll_ = now;
133 }
134 
135 void end_credits::key_press_callback(const SDL_Keycode key)
136 {
137  if(key == SDLK_UP && scroll_speed_ < 400) {
138  scroll_speed_ <<= 1;
139  }
140 
141  if(key == SDLK_DOWN && scroll_speed_ > 50) {
142  scroll_speed_ >>= 1;
143  }
144 }
145 
146 } // namespace dialogs
147 } // namespace gui2
std::vector< std::string > backgrounds_
Definition: end_credits.hpp:50
virtual void set_use_markup(bool use_markup) override
See styled_widget::set_use_markup.
t_string title
The section title.
Definition: about.hpp:36
const credits_data & get_credits_data()
General getter methods for the credits config and image lists by campaign id.
Definition: about.cpp:92
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:171
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:48
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< std::string > get_background_images(const std::string &campaign)
Definition: about.cpp:97
Display credits about all contributors.
virtual void set_label(const t_string &label) override
See styled_widget::set_label.
Generic file dialog.
Definition: field-fwd.hpp:22
Base container class.
Definition: grid.hpp:30
scroll_label * text_widget_
Definition: end_credits.hpp:52
This file contains the settings handling of the widget library.
std::string game_title_background
std::vector< std::string > names
Contributor names.
Definition: about.hpp:33
void set_link_aware(bool l)
void key_press_callback(const SDL_Keycode key)
Various uncategorised dialogs.
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:228
The user sets the widget hidden, that means:
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition: end_credits.cpp:50
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:62
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:428
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:253