Make ui::TitleTile use ui::Transition.

This commit is contained in:
J-D-K 2025-10-04 21:15:54 -04:00
parent fd655de1d0
commit ecdc6a198d
2 changed files with 40 additions and 25 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include "sdl.hpp"
#include "ui/Transition.hpp"
namespace ui
{
@ -34,15 +35,13 @@ namespace ui
int get_height() const noexcept;
private:
/// @brief Width in pixels to render icon at.
int m_renderWidth = 128;
/// @brief Height in pixels to render icon at.
int m_renderHeight = 128;
/// @brief Transition for the tile select/deselect.
ui::Transition m_transition{};
/// @brief Whether or not the title is a favorite.
bool m_isFavorite{};
int m_index{};
/// @brief Title's icon texture.

View File

@ -3,49 +3,65 @@
#include "graphics/colors.hpp"
#include "logging/logger.hpp"
namespace
{
constexpr int UNSELECTED_WIDTH_HEIGHT = 128;
constexpr int SELECTED_WIDTH_HEIGHT = 176;
}
ui::TitleTile::TitleTile(bool isFavorite, int index, sdl::SharedTexture icon)
: m_isFavorite(isFavorite)
: m_transition(0,
0,
UNSELECTED_WIDTH_HEIGHT,
UNSELECTED_WIDTH_HEIGHT,
0,
0,
UNSELECTED_WIDTH_HEIGHT,
UNSELECTED_WIDTH_HEIGHT,
m_transition.DEFAULT_THRESHOLD)
, m_isFavorite(isFavorite)
, m_index(index)
, m_icon(icon) {};
void ui::TitleTile::update(int selected)
{
static constexpr int BASE_WIDTH = 128;
static constexpr int EXPAND_WIDTH = 176;
static constexpr int INCREASE = 16;
static constexpr int DECREASE = 8;
const bool isSelected = selected == m_index;
const bool isSelected = m_index == selected;
if (isSelected && m_renderWidth != EXPAND_WIDTH)
if (isSelected)
{
// I think it's safe to assume both are too small.
m_renderWidth += INCREASE;
m_renderHeight += INCREASE;
m_transition.set_target_width(SELECTED_WIDTH_HEIGHT);
m_transition.set_target_height(SELECTED_WIDTH_HEIGHT);
}
else if (!isSelected && m_renderWidth != BASE_WIDTH)
else
{
m_renderWidth -= DECREASE;
m_renderHeight -= DECREASE;
m_transition.set_target_width(UNSELECTED_WIDTH_HEIGHT);
m_transition.set_target_height(UNSELECTED_WIDTH_HEIGHT);
}
m_transition.update_width_height();
}
void ui::TitleTile::render(sdl::SharedTexture &target, int x, int y)
{
static constexpr std::string_view HEART_CHAR = "\uE017";
const int renderX = x - ((m_renderWidth - 128) / 2);
const int renderY = y - ((m_renderHeight - 128) / 2);
const int width = m_transition.get_width();
const int height = m_transition.get_height();
const int renderX = x - ((width - 128) / 2);
const int renderY = y - ((width - 128) / 2);
m_icon->render_stretched(target, renderX, renderY, m_renderWidth, m_renderHeight);
m_icon->render_stretched(target, renderX, renderY, width, height);
if (m_isFavorite) { sdl::text::render(target, renderX + 2, renderY + 2, 28, sdl::text::NO_WRAP, colors::PINK, HEART_CHAR); }
}
void ui::TitleTile::reset() noexcept
{
m_renderWidth = 128;
m_renderHeight = 128;
m_transition.set_width(UNSELECTED_WIDTH_HEIGHT);
m_transition.set_height(UNSELECTED_WIDTH_HEIGHT);
m_transition.set_target_width(UNSELECTED_WIDTH_HEIGHT);
m_transition.set_target_height(UNSELECTED_WIDTH_HEIGHT);
}
int ui::TitleTile::get_width() const noexcept { return m_renderWidth; }
int ui::TitleTile::get_width() const noexcept { return m_transition.get_width(); }
int ui::TitleTile::get_height() const noexcept { return m_renderHeight; }
int ui::TitleTile::get_height() const noexcept { return m_transition.get_height(); }