Migrate PopMessage to ui::Transition instead of handling logic itself.

This commit is contained in:
J-D-K 2025-10-02 21:11:00 -04:00
parent b73de43c5a
commit 1f29f954b5
9 changed files with 103 additions and 52 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include "sys/Timer.hpp"
#include "ui/DialogBox.hpp"
#include "ui/Transition.hpp"
#include <string>
@ -28,11 +29,8 @@ namespace ui
std::string_view get_message() const noexcept;
private:
// Every message begins off screen.
static inline constexpr int START_X = 624;
static inline constexpr double START_Y = 720;
static inline constexpr int START_WIDTH = 32;
static inline constexpr int PERMA_HEIGHT = 48;
/// @brief Transition for the pop up/pop out thing.
ui::Transition m_transition{};
/// @brief Ticks to start timer with.
int m_ticks{};
@ -40,18 +38,15 @@ namespace ui
/// @brief This stores the message for safe keeping.
std::string m_message{};
/// @brief The current, actual Y coord.
double m_y{};
/// @brief Currently rendering coordinate for the text.
/// @brief Current rendering coordinate for the text.
int m_textX{};
/// @brief Current width;
int m_width{};
/// @brief Whether or not the targetY coordinate was met.
bool m_yMet{};
/// @brief Whether
bool m_close{};
/// @brief Returns whether or not the message has reached the end of its life.
bool m_finished{};
@ -72,5 +67,11 @@ namespace ui
/// @brief Updates the current end offset of the text.
void update_text_offset() noexcept;
/// @brief Updates the width of the dialog.
void update_width() noexcept;
/// @brief Signals the pop message to close.
void close() noexcept;
};
}

View File

@ -27,9 +27,21 @@ namespace ui
/// @brief Updates the transition.
void update() noexcept;
/// @brief Updates only the X and Y.
void update_xy() noexcept;
/// @brief Updates only the width and height.
void update_width_height() noexcept;
/// @brief Returns whether or not the transition has been met.
bool in_place() const noexcept;
/// @brief Returns if the target X and Y coordinates were met.
bool in_place_xy() const noexcept;
/// @brief Returns if the target width and height were met.
bool in_place_width_height() const noexcept;
/// @brief Returns the X coordinate.
int get_x() const noexcept;

View File

@ -21,6 +21,7 @@ void tasks::fileoptions::copy_source_to_destination(sys::threadpool::JobData tas
FileOptionState *spawningState = castData->spawningState;
if (error::is_null(task)) { return; }
else if (error::is_null(spawningState)) { TASK_FINISH_RETURN(task); }
const bool sourceIsDir = fslib::directory_exists(source);
bool destError = false;
@ -64,6 +65,7 @@ void tasks::fileoptions::delete_target(sys::threadpool::JobData taskData)
FileOptionState *spawningState = castData->spawningState;
if (error::is_null(task)) { return; }
else if (error::is_null(task)) { TASK_FINISH_RETURN(task); }
// Gonna borrow this. No point in duplicating it.
const int popTicks = ui::PopMessageManager::DEFAULT_TICKS;

View File

@ -22,6 +22,7 @@ void tasks::mainmenu::backup_all_for_all_local(sys::threadpool::JobData taskData
backupStruct->killTask = false; // Just to be sure.
if (error::is_null(task)) { return; }
for (data::User *user : userList)
{
backupStruct->user = user;
@ -65,6 +66,7 @@ void tasks::mainmenu::backup_all_for_all_local(sys::threadpool::JobData taskData
tasks::backup::create_new_backup_local(backupStruct);
}
}
task->complete();
}
@ -122,5 +124,6 @@ void tasks::mainmenu::backup_all_for_all_remote(sys::threadpool::JobData taskDat
remote->return_to_root();
}
}
task->complete();
}

View File

@ -15,7 +15,8 @@ void tasks::savecreate::create_save_data_for(sys::threadpool::JobData taskData)
data::TitleInfo *titleInfo = castData->titleInfo;
SaveCreateState *spawningState = castData->spawningState;
if (error::is_null(task) || error::is_null(user) || error::is_null(titleInfo) || error::is_null(spawningState)) { return; }
if (error::is_null(task)) { return; }
else if (error::is_null(user) || error::is_null(titleInfo) || error::is_null(spawningState)) { TASK_FINISH_RETURN(task); }
const int popTicks = ui::PopMessageManager::DEFAULT_TICKS;
const char *statusFormat = strings::get_by_name(strings::names::USEROPTION_STATUS, 0);

View File

@ -22,7 +22,8 @@ void tasks::saveimport::import_save_backup(sys::threadpool::JobData taskData)
sys::ProgressTask *task = static_cast<sys::ProgressTask *>(castData->task);
data::User *user = castData->user;
const fslib::Path &target = castData->path;
if (error::is_null(task) || error::is_null(user)) { return; }
if (error::is_null(task)) { return; }
else if (error::is_null(user)) { TASK_FINISH_RETURN(task); }
fs::SaveMetaData metaData{};
const bool metaRead = read_save_meta(target, metaData);

View File

@ -5,42 +5,45 @@
#include "mathutil.hpp"
#include "sdl.hpp"
namespace
{
static inline constexpr int START_X = 624;
static inline constexpr double START_Y = 720;
static inline constexpr int START_WIDTH = 32;
static inline constexpr int PERMA_HEIGHT = 48;
}
ui::PopMessage::PopMessage(int ticks, std::string_view message)
: m_ticks(ticks)
: m_transition(START_X, START_Y, START_WIDTH, PERMA_HEIGHT, 0, 0, 0, 0, 4)
, m_ticks(ticks)
, m_message(message)
, m_y(PopMessage::START_Y)
, m_width(PopMessage::START_WIDTH)
, m_dialog(ui::DialogBox::create(PopMessage::START_X,
m_y - 6,
PopMessage::START_WIDTH,
PopMessage::PERMA_HEIGHT,
ui::DialogBox::Type::Light)) {};
, m_dialog(ui::DialogBox::create(START_X, START_Y - 6, START_WIDTH, PERMA_HEIGHT, ui::DialogBox::Type::Light)) {};
ui::PopMessage::PopMessage(int ticks, std::string &message)
: m_ticks(ticks)
: m_transition(START_X, START_Y, START_WIDTH, PERMA_HEIGHT, 0, 0, 0, 0, 4)
, m_ticks(ticks)
, m_message(std::move(message))
, m_y(PopMessage::START_Y)
, m_width(PopMessage::START_WIDTH)
, m_dialog(ui::DialogBox::create(PopMessage::START_X,
m_y - 6,
PopMessage::START_WIDTH,
PopMessage::PERMA_HEIGHT,
ui::DialogBox::Type::Light)) {};
, m_dialog(ui::DialogBox::create(START_X, START_Y - 6, START_WIDTH, PERMA_HEIGHT, ui::DialogBox::Type::Light)) {};
void ui::PopMessage::update(double targetY)
{
update_y(targetY);
update_text_offset();
if (m_displayTimer.is_triggered()) { m_finished = true; }
PopMessage::update_y(targetY);
PopMessage::update_text_offset();
PopMessage::update_width();
if (m_close && m_transition.in_place()) { m_finished = true; }
else if (m_displayTimer.is_triggered()) { PopMessage::close(); }
}
void ui::PopMessage::render()
{
m_dialog->render(sdl::Texture::Null, false);
if (!m_yMet) { return; }
if (!m_yMet || m_close) { return; }
// This avoids allocating and returning another std::string.
const int y = m_transition.get_y();
const std::string_view message(m_message.c_str(), m_substrOffset);
sdl::text::render(sdl::Texture::Null, m_textX, m_y + 5, 22, sdl::text::NO_WRAP, colors::BLACK, message);
sdl::text::render(sdl::Texture::Null, m_textX, y + 5, 22, sdl::text::NO_WRAP, colors::BLACK, message);
}
bool ui::PopMessage::finished() const noexcept { return m_finished; }
@ -49,20 +52,17 @@ std::string_view ui::PopMessage::get_message() const noexcept { return m_message
void ui::PopMessage::update_y(double targetY) noexcept
{
if (m_y == targetY) { return; }
if (!m_close) { m_transition.set_target_y(targetY); };
const double scaling = config::get_animation_scaling();
const double increase = (targetY - m_y) / scaling;
m_y += increase;
const int distance = math::Util<double>::absolute_distance(targetY, m_y);
if (distance <= 2)
m_transition.update_xy();
if (!m_yMet && m_transition.in_place_xy())
{
m_y = targetY;
m_yMet = true;
m_typeTimer.start(5);
}
m_dialog->set_y(m_y - 6);
const int y = m_transition.get_y();
m_dialog->set_y(y - 6);
}
void ui::PopMessage::update_text_offset() noexcept
@ -87,7 +87,28 @@ void ui::PopMessage::update_text_offset() noexcept
const int dialogX = m_textX - 16;
const int dialogWidth = stringWidth + 32;
m_dialog->set_x(dialogX);
m_dialog->set_width(dialogWidth);
m_transition.set_target_x(dialogX);
m_transition.set_target_width(dialogWidth);
if (m_substrOffset >= messageLength) { m_displayTimer.start(m_ticks); }
}
void ui::PopMessage::update_width() noexcept
{
if (!m_yMet) { return; }
m_transition.update_width_height();
const int x = m_transition.get_centered_x();
const int width = m_transition.get_width();
m_dialog->set_x(x);
m_dialog->set_width(width);
if (m_close && m_transition.in_place_width_height()) { m_transition.set_target_y(720); }
}
void ui::PopMessage::close() noexcept
{
m_close = true;
m_transition.set_target_width(32);
}

View File

@ -8,12 +8,6 @@
#include <cstdarg>
namespace
{
// Size of the buffer for va strings.
constexpr int VA_BUFFER_SIZE = 0x200;
} // namespace
void ui::PopMessageManager::update()
{
static constexpr double COORD_INIT_Y = 594.0f;

View File

@ -34,11 +34,27 @@ void ui::Transition::update() noexcept
ui::Transition::update_height();
}
void ui::Transition::update_xy() noexcept
{
ui::Transition::update_x_coord();
ui::Transition::update_y_coord();
}
void ui::Transition::update_width_height() noexcept
{
ui::Transition::update_width();
ui::Transition::update_height();
}
bool ui::Transition::in_place() const noexcept
{
return m_x == m_targetX && m_y == m_targetY && m_width == m_targetWidth && m_height == m_targetHeight;
}
bool ui::Transition::in_place_xy() const noexcept { return m_x == m_targetX && m_y == m_targetY; }
bool ui::Transition::in_place_width_height() const noexcept { return m_width == m_targetWidth && m_height == m_targetHeight; }
int ui::Transition::get_x() const noexcept { return static_cast<int>(m_x); }
int ui::Transition::get_y() const noexcept { return static_cast<int>(m_y); }