bwidgets/src/w/caption.cpp

122 lines
3 KiB
C++
Raw Normal View History

#include <SDL2/SDL_assert.h>
#include <basic_widgets/core/math.hpp>
#include <basic_widgets/core/texture.hpp>
#include <basic_widgets/w/caption.hpp>
2021-08-06 14:22:56 +02:00
#include <basic_widgets/w/numeric_input.hpp>
using namespace bwidgets;
2021-08-08 16:00:22 +02:00
Caption::Caption(Widget* parent) noexcept : Widget(parent)
{
2021-08-06 14:22:56 +02:00
_font_color_bg = default_font_color_bg;
_font_color_fg = default_font_color_fg;
}
2021-08-06 14:22:56 +02:00
Caption::~Caption() noexcept
2021-07-19 23:50:50 +02:00
{
2021-08-06 14:22:56 +02:00
discard(_text_texture);
2021-07-19 23:50:50 +02:00
}
2021-08-08 16:00:22 +02:00
auto Caption::render_mode(Font::RenderMode m) -> Caption*
{
2021-08-06 14:22:56 +02:00
if (m != _render_mode) {
discard(_text_texture);
_render_mode = m;
}
2021-07-23 20:08:52 +02:00
return this;
}
2021-08-06 14:22:56 +02:00
auto Caption::size() const noexcept -> Size
{
2021-08-06 14:22:56 +02:00
if (_font == nullptr) return {0, 0};
2021-08-08 14:30:56 +02:00
const Size size = _font->text_size(_text);
return {size.w + 2 * margins.w, size.h + 2 * margins.h};
}
2021-08-06 14:22:56 +02:00
auto Caption::text() const noexcept -> const std::string&
{
return _text;
}
2021-08-06 14:22:56 +02:00
auto Caption::text(const std::string& t) -> Caption*
{
2021-08-06 14:22:56 +02:00
if (t != _text) {
discard(_text_texture);
_text = t;
}
2021-07-19 23:50:50 +02:00
return this;
}
2021-08-06 14:22:56 +02:00
void Caption::_handle_font_change(Font*)
{
2021-08-06 14:22:56 +02:00
discard(_text_texture);
}
2021-08-06 14:22:56 +02:00
void Caption::_handle_font_color_change(const Color& fg, const Color& bg)
{
if (fg != _font_color_fg
2021-08-06 14:22:56 +02:00
|| (bg != _font_color_bg && _font_render_mode == Font::RenderMode::SHADED))
{
2021-08-06 14:22:56 +02:00
discard(_text_texture);
_font_color_bg = bg;
}
}
2021-08-08 16:00:22 +02:00
void Caption::_handle_geometry_change(const SDL_Rect& vp)
{
if (vp.w != _viewport.w || vp.h != _viewport.h) {
2021-08-06 14:22:56 +02:00
_widget_area = rect_margin({0, 0, vp.w, vp.h}, margins);
}
}
2021-08-06 14:22:56 +02:00
void Caption::_handle_renderer_change(Renderer*)
{
2021-08-06 14:22:56 +02:00
discard(_text_texture);
}
2021-08-06 14:22:56 +02:00
void Caption::_handle_rendering()
{
if (_text_texture == nullptr) _handle_texture_update();
2021-08-06 14:22:56 +02:00
if (_render_mode == Font::RenderMode::SHADED) {
_renderer->draw_color(_font_color_bg)
->fill_rect({0, 0, _viewport.w, _viewport.h});
}
2021-08-08 14:30:56 +02:00
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:
2021-08-06 14:22:56 +02:00
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);
}
2021-08-06 14:22:56 +02:00
void Caption::_handle_texture_update()
{
2021-08-08 16:00:22 +02:00
SDL_assert_release(_font != nullptr && _text_texture == nullptr); // NOLINT
2021-08-06 14:22:56 +02:00
SDL_Surface* s;
switch (_render_mode) {
2021-08-06 14:22:56 +02:00
case Font::RenderMode::SHADED:
s = _font->render(_render_mode, _text, _font_color_fg, _font_color_bg);
break;
2021-07-23 20:08:52 +02:00
default:
2021-08-06 14:22:56 +02:00
s = _font->render(_render_mode, _text, _font_color_fg);
break;
2021-07-23 20:08:52 +02:00
}
2021-08-06 14:22:56 +02:00
_text_texture = new Texture(_renderer, s);
2021-07-27 22:15:43 +02:00
SDL_FreeSurface(s);
}