GUI2/Loading Screen: improved animation

This commit is contained in:
Charles Dang 2021-01-25 09:21:01 +11:00
parent 673937d573
commit 454bed95ce
3 changed files with 48 additions and 29 deletions

View file

@ -78,13 +78,14 @@
[image]
definition = "logo"
label = "misc/logo-bg.png"
#label = "wesnoth-logo-256.png"
[/image]
[/column]
[/row]
{GUI_FILLER_ROW HEIGHT=30}
{GUI_FILLER_ROW HEIGHT=25}
[row]
grow_factor = 0
@ -114,15 +115,44 @@
grow_factor = 1
border = "all"
border_size = 5
horizontal_grow = true
horizontal_alignment = "center"
vertical_alignment = "center"
[label]
text_alignment = "center"
definition = "default_large"
id = "test_animation"
label = " "
[/label]
[drawing]
definition = "default"
id = "animation"
width = 250
height = 30
#{_WIDGET_DEBUG_BORDER}
[draw]
[text]
#
# Uses a bezier in-out easing to calculate the position of the dot each frame.
#
# First, it gets a normalized [0, 1] value based on the value of `tick` in the range [0, steps], then uses
# this value multiplied by the width of the drawing canvas (sans the width of the text, since there still
# needs to be enough horizontal space to render the dot on the last step of the loop) to get this frame's
# x-coordinate. Note that `steps` *must* be a decimal or the formula won't work.
#
# For the record, the original linear formula I came up with was:
# "(floor(jump * (tick % steps)) where jump = (width / steps) where steps = 40)"
#
x = "(round(w * (t * t * (3.0 - 2.0 * t))) where w = (width - text_width) where t = ((tick % steps) / steps) where steps = 40.0)"
y = 0
w = "(text_width)"
h = "(height)"
text = "•"
font_size = {GUI_FONT_SIZE_DEFAULT}
color = {GUI__FONT_COLOR_ENABLED__DEFAULT}
[/text]
[/draw]
[/drawing]
[/column]

View file

@ -24,16 +24,17 @@
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/timer.hpp"
#include "gui/widgets/drawing.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "preferences/general.hpp"
#include <functional>
#include "video.hpp"
#include <chrono>
#include <cstdlib>
#include <functional>
static lg::log_domain log_loadscreen("loadscreen");
#define ERR_LS LOG_STREAM(err, log_loadscreen)
@ -79,22 +80,13 @@ loading_screen::loading_screen(std::function<void()> f)
, worker_result_()
, cursor_setter_()
, progress_stage_label_(nullptr)
, animation_label_(nullptr)
, animation_(nullptr)
, current_stage_(loading_stage::none)
, visible_stages_()
, animation_stages_()
, current_visible_stage_()
{
for(const auto& pair : stage_names) {
visible_stages_[pair.first] = t_string(pair.second, "wesnoth-lib") + "...";
}
animation_stages_.reserve(20);
for(int i = 0; i != 20; ++i) {
std::string s(20, ' ');
s[i] = '.';
animation_stages_.push_back(std::move(s));
for(const auto& [stage, description] : stage_names) {
visible_stages_[stage] = t_string(description, "wesnoth-lib") + "...";
}
current_visible_stage_ = visible_stages_.end();
@ -119,7 +111,7 @@ void loading_screen::pre_show(window& window)
}
progress_stage_label_ = find_widget<label>(&window, "status", false, true);
animation_label_ = find_widget<label>(&window, "test_animation", false, true);
animation_ = find_widget<drawing>(&window, "animation", false, true);
// Add a draw callback to handle the animation, et al.
window.connect_signal<event::DRAW>(
@ -168,10 +160,8 @@ void loading_screen::draw_callback()
progress_stage_label_->set_label(iter->second);
}
++animation_counter_;
if(animation_counter_ % 2 == 0) {
animation_label_->set_label(animation_stages_[(animation_counter_ / 2) % animation_stages_.size()]);
}
animation_->get_drawing_canvas().set_variable("tick", wfl::variant(animation_counter_++));
animation_->set_is_dirty(true);
}
loading_screen::~loading_screen()

View file

@ -63,6 +63,7 @@ enum class loading_stage
namespace gui2
{
class drawing;
class label;
class window;
@ -104,14 +105,12 @@ private:
std::unique_ptr<cursor::setter> cursor_setter_;
label* progress_stage_label_;
label* animation_label_;
drawing* animation_;
std::atomic<loading_stage> current_stage_;
using stage_map = std::map<loading_stage, t_string>;
stage_map visible_stages_;
std::vector<t_string> animation_stages_;
stage_map::const_iterator current_visible_stage_;
};