122 lines
3.0 KiB
C++
122 lines
3.0 KiB
C++
#include <SDL2/SDL_assert.h>
|
|
|
|
#include <basic_widgets/core/math.hpp>
|
|
#include <basic_widgets/core/texture.hpp>
|
|
#include <basic_widgets/w/caption.hpp>
|
|
#include <basic_widgets/w/numeric_input.hpp>
|
|
|
|
using namespace bwidgets;
|
|
|
|
Caption::Caption(Widget* parent) noexcept : Widget(parent)
|
|
{
|
|
_font_color_bg = default_font_color_bg;
|
|
_font_color_fg = default_font_color_fg;
|
|
}
|
|
|
|
Caption::~Caption() noexcept
|
|
{
|
|
discard(_text_texture);
|
|
}
|
|
|
|
auto Caption::render_mode(Font::RenderMode m) -> Caption*
|
|
{
|
|
if (m != _render_mode) {
|
|
discard(_text_texture);
|
|
_render_mode = m;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
auto Caption::size() const noexcept -> Size
|
|
{
|
|
if (_font == nullptr) return {0, 0};
|
|
|
|
const Size size = _font->text_size(_text);
|
|
|
|
return {size.w + 2 * margins.w, size.h + 2 * margins.h};
|
|
}
|
|
|
|
auto Caption::text() const noexcept -> const std::string&
|
|
{
|
|
return _text;
|
|
}
|
|
|
|
auto Caption::text(const std::string& t) -> Caption*
|
|
{
|
|
if (t != _text) {
|
|
discard(_text_texture);
|
|
_text = t;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
void Caption::_handle_font_change(Font*)
|
|
{
|
|
discard(_text_texture);
|
|
}
|
|
|
|
void Caption::_handle_font_color_change(const Color& fg, const Color& bg)
|
|
{
|
|
if (fg != _font_color_fg
|
|
|| (bg != _font_color_bg && _font_render_mode == Font::RenderMode::SHADED))
|
|
{
|
|
discard(_text_texture);
|
|
_font_color_bg = bg;
|
|
}
|
|
}
|
|
|
|
void Caption::_handle_geometry_change(const SDL_Rect& vp)
|
|
{
|
|
if (vp.w != _viewport.w || vp.h != _viewport.h) {
|
|
_widget_area = rect_margin({0, 0, vp.w, vp.h}, margins);
|
|
}
|
|
}
|
|
|
|
void Caption::_handle_renderer_change(Renderer*)
|
|
{
|
|
discard(_text_texture);
|
|
}
|
|
|
|
void Caption::_handle_rendering()
|
|
{
|
|
if (_text_texture == nullptr) _handle_texture_update();
|
|
|
|
if (_render_mode == Font::RenderMode::SHADED) {
|
|
_renderer->draw_color(_font_color_bg)
|
|
->fill_rect({0, 0, _viewport.w, _viewport.h});
|
|
}
|
|
|
|
const Size size_dst {
|
|
(int)((float)_text_texture->attributes().w / (float)_text_texture->attributes().h
|
|
* (float)_widget_area.h),
|
|
_widget_area.h};
|
|
SDL_Rect texture_dst {margins.w, margins.h, size_dst.w, size_dst.h};
|
|
switch (alignment) {
|
|
case Alignment::CENTER:
|
|
texture_dst.x = center_line(_widget_area.w, texture_dst.w) + _widget_area.x;
|
|
break;
|
|
case Alignment::LEFT:
|
|
break;
|
|
case Alignment::RIGHT:
|
|
texture_dst.x = _widget_area.w - texture_dst.w - margins.w + _widget_area.x;
|
|
break;
|
|
}
|
|
_renderer->copy(_text_texture, nullptr, texture_dst);
|
|
}
|
|
|
|
void Caption::_handle_texture_update()
|
|
{
|
|
SDL_assert_release(_font != nullptr && _text_texture == nullptr); // NOLINT
|
|
SDL_Surface* s;
|
|
switch (_render_mode) {
|
|
case Font::RenderMode::SHADED:
|
|
s = _font->render(_render_mode, _text, _font_color_fg, _font_color_bg);
|
|
break;
|
|
default:
|
|
s = _font->render(_render_mode, _text, _font_color_fg);
|
|
break;
|
|
}
|
|
_text_texture = new Texture(_renderer, s);
|
|
SDL_FreeSurface(s);
|
|
}
|