JKSV/source/ui/Frame.cpp
2025-10-30 18:11:16 -04:00

76 lines
2.7 KiB
C++

#include "ui/Frame.hpp"
#include "graphics/colors.hpp"
// ---- Construction ----
ui::Frame::Frame(int x, int y, int width, int height)
: m_x(x)
, m_y(y)
, m_width(width)
, m_height(height)
{
Frame::initialize_static_members();
}
// ---- Public functions ----
void ui::Frame::render(sdl::SharedTexture &target, bool hasFocus)
{
// This is the size of one of the "tiles" of the frame.
static constexpr int TILE = 16;
const int midWidth = m_width - (TILE * 2);
const int midHeight = m_height - (TILE * 2);
const int rightEdge = (m_x + m_width) - TILE;
const int bottomEdge = (m_y + m_height) - TILE;
const int textureCorner = TILE * 2;
// Top.
sm_frameCorners->render_part(target, m_x, m_y, 0, 0, TILE, TILE);
sm_frameCorners->render_part_stretched(target, TILE, 0, TILE, TILE, m_x + TILE, m_y, midWidth, TILE);
sm_frameCorners->render_part(target, rightEdge, m_y, 32, 0, TILE, TILE);
// Middle
sm_frameCorners->render_part_stretched(target, 0, TILE, TILE, TILE, m_x, m_y + TILE, TILE, midHeight);
sdl::render_rect_fill(target, m_x + TILE, m_y + TILE, midWidth, midHeight, colors::SLIDE_PANEL_CLEAR);
sm_frameCorners->render_part_stretched(target, textureCorner, TILE, TILE, TILE, rightEdge, m_y + TILE, TILE, midHeight);
// Bottom
sm_frameCorners->render_part(target, m_x, bottomEdge, 0, textureCorner, TILE, TILE);
sm_frameCorners->render_part_stretched(target, TILE, textureCorner, TILE, TILE, m_x + TILE, bottomEdge, midWidth, TILE);
sm_frameCorners->render_part(target, rightEdge, bottomEdge, textureCorner, textureCorner, TILE, TILE);
}
void ui::Frame::set_x(int x) noexcept { m_x = x; }
void ui::Frame::set_y(int y) noexcept { m_y = y; }
void ui::Frame::set_width(int width) noexcept { m_width = width; }
void ui::Frame::set_height(int height) noexcept { m_height = height; }
void ui::Frame::set_from_transition(const ui::Transition &transition, bool centered) noexcept
{
const int x = centered ? transition.get_centered_x() : transition.get_x();
const int y = centered ? transition.get_centered_y() : transition.get_y();
const int width = transition.get_width();
const int height = transition.get_height();
m_x = x;
m_y = y;
m_width = width;
m_height = height;
}
// ---- Private functions ----
void ui::Frame::initialize_static_members()
{
static constexpr std::string_view FRAME_NAME = "FrameCorners";
static constexpr const char *FRAME_PATH = "romfs:/Textures/Frame.png";
if (sm_frameCorners) { return; }
sm_frameCorners = sdl::TextureManager::load(FRAME_NAME, FRAME_PATH);
}