The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
end_credits.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 - 2025
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"
21#include "gui/widgets/grid.hpp"
25#include "gettext.hpp"
27
28#include <SDL3/SDL_timer.h>
29
30#include <functional>
31#include <sstream>
32
33namespace gui2::dialogs
34{
35
36REGISTER_DIALOG(end_credits)
37
38end_credits::end_credits(const std::string& campaign)
39 : modal_dialog(window_id())
40 , focus_on_(campaign)
41 , backgrounds_()
42 , text_widget_(nullptr)
43 , scroll_speed_(100)
44 , last_scroll_(std::numeric_limits<uint64_t>::max())
45 , scroll_remainder_(0)
46{
47}
48
50{
51 last_scroll_ = SDL_GetTicks();
52
53 connect_signal_pre_key_press(*this, 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 << markup::span_size("xx-large", group.header) << "\n";
64 }
65
66 for(const about::credits_group::about_group& about : group.sections) {
67 group_stream << "\n" << markup::span_size("x-large", about.title) << "\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 get_canvas(0).set_variable("background_image", wfl::variant(backgrounds_[0]));
89
90 text_widget_ = find_widget<scroll_label>("text", false, true);
91
94
95 // Text shapes only render their visible part, so the whole text can go in
96 // one label without exceeding the maximum surface or texture size.
97 text_widget_->set_label(focus_ss.str().empty() ? ss.str() : focus_ss.str());
98
99 // HACK: always hide the scrollbar, even if it's needed.
100 // This should probably be implemented as a scrollbar mode.
101 // Also, for some reason hiding the whole grid doesn't work, and the elements need to be hidden manually
102 if(grid* v_grid = dynamic_cast<grid*>(text_widget_->find("_vertical_scrollbar_grid", false))) {
103 v_grid->find_widget<scrollbar_base>("_vertical_scrollbar").set_visible(widget::visibility::hidden);
104
105 // TODO: enable again if e24336afeb7 is reverted.
106 //v_grid.find_widget<repeating_button>("_half_page_up").set_visible(widget::visibility::hidden);
107 //v_grid.find_widget<repeating_button>("_half_page_down").set_visible(widget::visibility::hidden);
108 }
109}
110
112{
113 const uint64_t now = SDL_GetTicks();
114 if(last_scroll_ > now) {
115 return;
116 }
117
118 // Accumulate the distance due in pixel-milliseconds. Only whole pixels can be
119 // scrolled, so divide by 1000 (rounding down) and carry the remainder over to
120 // the next frame. Otherwise high frame rates or low speeds could scroll slower
121 // than intended, or not at all.
123 last_scroll_ = now;
124
125 const uint64_t needed_dist = scroll_remainder_ / 1000;
126 scroll_remainder_ %= 1000;
127
128 if(needed_dist > 0) {
130 text_widget_->get_vertical_scrollbar_item_position() + static_cast<unsigned>(needed_dist));
131 }
132}
133
134void 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
void set_variable(const std::string &key, wfl::variant &&value)
Definition canvas.hpp:165
virtual void pre_show() override
Actions to be taken before showing the window.
std::vector< std::string > backgrounds_
const std::string & focus_on_
void key_press_callback(const SDL_Keycode key)
virtual void update() override
TLD override to update animations, called once per frame.
uint64_t scroll_remainder_
Scrolling that is due but less than a whole pixel, in pixel-milliseconds.
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)
Base class for a scroll bar.
Definition scrollbar.hpp:39
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.
widget * find(const std::string_view id, const bool must_be_active) override
See widget::find.
canvas & get_canvas(const unsigned index)
void set_visible(const visibility visible)
Definition widget.cpp:479
@ hidden
The user sets the widget hidden, that means:
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
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:95
std::string game_title_background
std::string span_size(std::string_view size, Args &&... data)
Applies Pango markup to the input specifying its display size.
Definition markup.hpp:146
STL namespace.