Credits Crash Bug fix (#7931)

Co-authored-by: pentarctagon <pentarctagon@tutamail.com>
This commit is contained in:
irregularBismuth 2023-11-03 18:10:59 +01:00 committed by GitHub
parent 6cc717042d
commit b8adf0cd5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 5 deletions

View file

@ -44,13 +44,14 @@ end_credits::end_credits(const std::string& campaign)
, text_widget_(nullptr)
, scroll_speed_(100)
, last_scroll_(std::numeric_limits<uint32_t>::max())
, first_idx_(0)
, last_idx_(first_idx_ + sliding_size_)
{
}
void end_credits::pre_show(window& window)
{
// Delay a little before beginning the scrolling
last_scroll_ = SDL_GetTicks() + 3000;
last_scroll_ = SDL_GetTicks();
connect_signal_pre_key_press(window, std::bind(&end_credits::key_press_callback, this, std::placeholders::_5));
@ -93,8 +94,31 @@ void end_credits::pre_show(window& window)
text_widget_->set_use_markup(true);
text_widget_->set_link_aware(false);
text_widget_->set_label((focus_ss.str().empty() ? ss : focus_ss).str());
content_ = focus_ss.str().empty() ? ss.str() : focus_ss.str();
// splits the content text by newline, leaving blanks
// also truncates the length of the line to 200 characters
// 200 characters is completely arbitrary, just prevent the possibility of ridiculously wide lines
// NOTE: this depends on the assumption that the <span>s added above only ever wrap a single line
std::vector<std::string> lines = utils::split(content_, '\n', 0);
int i = 0;
for(const std::string& line : lines) {
if(i % lines_per_chunk_ == 0) {
chunks_.emplace_back();
}
std::vector<std::string>& last_chunk = chunks_[chunks_.size()-1];
last_chunk.emplace_back(line.size() < 200 ? line : line.substr(0, 200));
i++;
}
sliding_content_.clear();
for(std::size_t i = 0; i <= sliding_size_; i++){
sliding_content_ += utils::join(chunks_.at(i), "\n") + "\n";
}
//concat substring strings
text_widget_->set_label(sliding_content_);
// HACK: always hide the scrollbar, even if it's needed.
// This should probably be implemented as a scrollbar mode.
// Also, for some reason hiding the whole grid doesn't work, and the elements need to be hidden manually
@ -122,7 +146,27 @@ void end_credits::update()
// The division by 1000 is to convert milliseconds to seconds.
unsigned int needed_dist = missed_time * scroll_speed_ / 1000;
text_widget_->set_vertical_scrollbar_item_position(cur_pos + needed_dist);
// TODO: this doesn't allow for scrolling up again after been scrolled down
// only the content in the current sliding window can be scrolled up
if(cur_pos <= text_widget_->get_height()){
text_widget_->set_vertical_scrollbar_item_position(cur_pos + needed_dist);
} else {
if(first_idx_ < chunks_.size() - sliding_size_ - 1){
first_idx_++;
last_idx_ = first_idx_ + sliding_size_;
sliding_content_.clear();
if(last_idx_ <= chunks_.size()){
for(std::size_t i = first_idx_; i <= last_idx_; i++) {
sliding_content_ += utils::join(chunks_[i], "\n") + "\n";
}
}
// updates the sliding window
text_widget_->set_label(sliding_content_);
cur_pos = 0;
}
}
last_scroll_ = now;
}

View file

@ -44,7 +44,6 @@ private:
virtual void pre_show(window& window) override;
void timer_callback();
void key_press_callback(const SDL_Keycode key);
const std::string& focus_on_;
@ -57,6 +56,22 @@ private:
int scroll_speed_;
uint32_t last_scroll_;
/**
* sliding_size_ alters how many of the sliding contents are to be run at once
* n-1 = 2 => 3 strings at once concatinated
*/
static constexpr std::size_t sliding_size_ = 2;
/**
* number of lines to put in each chunk of text to display
* the final chunk will of course probably have fewer lines
*/
static constexpr std::size_t lines_per_chunk_ = 50;
std::size_t first_idx_;
std::size_t last_idx_;
std::string content_;
std::string sliding_content_;
std::vector<std::vector<std::string>> chunks_;
};
} // namespace dialogs